Skip to content

Commit

Permalink
types: fixed tryit function argument types (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
DuCanhGH authored Jun 27, 2023
1 parent 6f2a16e commit c3c9dac
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 39 deletions.
3 changes: 0 additions & 3 deletions .npmignore

This file was deleted.

7 changes: 1 addition & 6 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,7 @@ const memoize = (cache, func, keyFunc, ttl) => {
};
};
const memo = (func, options = {}) => {
return memoize(
{},
func,
options.key ?? null,
options.ttl ?? null
);
return memoize({}, func, options.key ?? null, options.ttl ?? null);
};
const debounce = ({ delay }, func) => {
let timer = void 0;
Expand Down
7 changes: 1 addition & 6 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,7 @@ var radash = (function (exports) {
};
};
const memo = (func, options = {}) => {
return memoize(
{},
func,
options.key ?? null,
options.ttl ?? null
);
return memoize({}, func, options.key ?? null, options.ttl ?? null);
};
const debounce = ({ delay }, func) => {
let timer = void 0;
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"sideEffects": false,
"files": [
"dist/**/*"
"dist"
],
"repository": {
"url": "https://github.com/rayepps/radash"
Expand Down
19 changes: 7 additions & 12 deletions src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,24 +254,19 @@ export const sleep = (milliseconds: number) => {
return new Promise(res => setTimeout(res, milliseconds))
}

type ArgumentsType<T> = T extends (...args: infer U) => any ? U : never
type UnwrapPromisify<T> = T extends Promise<infer U> ? U : T

/**
* A helper to try an async function without forking
* the control flow. Returns an error first callback _like_
* array response as [Error, result]
*/
export const tryit = <TFunction extends (...args: any) => any>(
func: TFunction
export const tryit = <Args extends any[], Return>(
func: (...args: Args) => Return
) => {
return async (
...args: ArgumentsType<TFunction>
): Promise<
[Error, undefined] | [undefined, UnwrapPromisify<ReturnType<TFunction>>]
> => {
...args: Args
): Promise<[Error, undefined] | [undefined, Awaited<Return>]> => {
try {
return [undefined, await func(...(args as any))]
return [undefined, await func(...args)]
} catch (err) {
return [err as any, undefined]
}
Expand All @@ -288,13 +283,13 @@ export const guard = <TFunction extends () => any>(
func: TFunction,
shouldGuard?: (err: any) => boolean
): ReturnType<TFunction> extends Promise<any>
? Promise<UnwrapPromisify<ReturnType<TFunction>> | undefined>
? Promise<Awaited<ReturnType<TFunction>> | undefined>
: ReturnType<TFunction> | undefined => {
const _guard = (err: any) => {
if (shouldGuard && !shouldGuard(err)) throw err
return undefined as any
}
const isPromise = (result: any): result is ReturnType<TFunction> =>
const isPromise = (result: any): result is Promise<any> =>
result instanceof Promise
try {
const result = func()
Expand Down
11 changes: 3 additions & 8 deletions src/curry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,14 @@ const memoize = <T>(
* is given previously computed values will be checked
* for expiration before being returned.
*/
export const memo = <TFunc extends Function>(
export const memo = <TFunc extends (...args: any) => any>(
func: TFunc,
options: {
key?: Func<any, string>
ttl?: number
} = {}
) => {
return memoize(
{},
func as any,
options.key ?? null,
options.ttl ?? null
) as any as TFunc
return memoize({}, func, options.key ?? null, options.ttl ?? null) as TFunc
}

export type DebounceFunction<TArgs extends any[]> = {
Expand Down Expand Up @@ -204,7 +199,7 @@ export const throttle = <TArgs extends any[]>(
export const callable = <
TValue,
TObj extends Record<string | number | symbol, TValue>,
TFunc extends Function
TFunc extends (...args: any) => any
>(
obj: TObj,
fn: (self: TObj) => TFunc
Expand Down
2 changes: 1 addition & 1 deletion src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export const clone = <T>(obj: T): T => {

// Access the constructor and create a new object.
// This method can create an array as well.
const newObj = new ((obj as Object).constructor as { new (): T })()
const newObj = new ((obj as object).constructor as { new (): T })()

// Assign the props.
Object.getOwnPropertyNames(obj).forEach(prop => {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/curry.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from 'chai'
import * as _ from '..'
import { DebounceFunction } from '../curry'
import type { DebounceFunction } from '../curry'

describe('curry module', () => {
describe('compose function', () => {
Expand Down

1 comment on commit c3c9dac

@vercel
Copy link

@vercel vercel bot commented on c3c9dac Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.