-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
156 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { uniq } from "./uniq"; | ||
import { uniq } from './uniq'; | ||
|
||
export function classNames(...args: unknown[]) { | ||
const cnames = args | ||
.filter(Boolean) | ||
.join(" ") | ||
.map(String) | ||
.join(' ') | ||
.split(/\r\n|\n|\r|\s+/) | ||
.filter(Boolean); | ||
|
||
return uniq(cnames).join(" "); | ||
return uniq(cnames).join(' '); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export function cleanCRLF(value: string) { | ||
return value.replace(/\r\n|\n|\r|\s+/g, " "); | ||
export function cleanCRLF(value: string): string { | ||
return value.replace(/\r\n|\n|\r|\s+/g, ' '); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export function copy(src: any): any { | ||
return (src as any).pop ? [...src] : { ...src }; | ||
import { isArr } from './isArr'; | ||
|
||
export function copy<T extends object>(src: T): T { | ||
return isArr(src) ? ([...src] as T) : { ...src }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
import { curry } from './curry'; | ||
import { Fn } from './types'; | ||
|
||
interface DebounceFn { | ||
(a: number): <T extends unknown[], R extends unknown>(f: Fn<T, R>) => Fn<T, R>; | ||
<T extends unknown[], R extends unknown>(a: number, f: Fn<T, R>): Fn<T, R>; | ||
} | ||
|
||
/** | ||
* TODO: adicionar documentação | ||
* | ||
* https://davidwalsh.name/function-debounce | ||
*/ | ||
export const debounce = curry(function <T extends any[], R>( | ||
wait: number, | ||
func: Fn<T, R>, | ||
): Fn<T, R> { | ||
let timeout: NodeJS.Timeout; | ||
export const debounce = curry((wait: number, fn: Function) => { | ||
let timeout: any; | ||
|
||
return function (...args: T) { | ||
// @ts-ignore | ||
// eslint-disable-next-line consistent-this | ||
return function (this: any, ...args: any[]) { | ||
const self = this; | ||
|
||
const later = function () { | ||
// @ts-ignore | ||
timeout = undefined; | ||
func.apply(self, args); | ||
fn.apply(self, args); | ||
}; | ||
|
||
clearTimeout(timeout); | ||
timeout = setTimeout(later, wait); | ||
|
||
if (!timeout) { | ||
func.apply(self, args); | ||
fn.apply(self, args); | ||
} | ||
} as Fn<T, R>; | ||
}); | ||
}; | ||
}) as DebounceFn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
export function diff<T = unknown>(arr1: T[], arr2: T[]): T[] { | ||
return [arr1, arr2].reduce((a, b) => a.filter((c) => !b.includes(c))); | ||
import { curry } from './curry'; | ||
|
||
interface DiffFn { | ||
<A extends unknown[]>(a: A): <B extends unknown[]>(b: B) => [...A, ...B]; | ||
<A extends unknown[], B extends unknown[]>(a: A, b: B): [...A, ...B]; | ||
} | ||
|
||
export const diff = curry((a: any[], b: any[]) => { | ||
return [a, b].reduce((c, d) => c.filter((e) => !d.includes(e))); | ||
}) as DiffFn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
import { curry } from './curry'; | ||
import { Obj } from './types'; | ||
import { isStr } from './isStr'; | ||
|
||
interface GetFn { | ||
(a: string | string[]): <R extends unknown>(b: object) => R; | ||
<R extends unknown>(a: string | string[], b: object): R; | ||
} | ||
|
||
/** | ||
* https://github.com/developit/dlv | ||
* | ||
* TODO: adicionar documentação | ||
*/ | ||
export const get = curry(<T extends Obj>(path: string | string[], src: T) => { | ||
(path as string).split && (path = (path as string).split('.')); | ||
export const get = curry((path: string | string[], src: object): unknown => { | ||
isStr(path) && (path = path.split('.')); | ||
|
||
for (let i = 0, l = path.length; i < l; i++) { | ||
src = src ? src[path[i]] : undefined; | ||
src = src ? (src as any)[path[i]] : undefined; | ||
} | ||
|
||
return src; | ||
}); | ||
}) as GetFn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function isArr(a: unknown): a is unknown[] { | ||
return Array.isArray(a); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import { isObject } from './isObject'; | ||
|
||
/** | ||
* TODO: adicionar documentação | ||
*/ | ||
export function isEmpty(data: unknown): boolean { | ||
if (typeof data === 'object' && data !== null) { | ||
return !Object.keys(data || {}).length; | ||
export function isEmpty(val: unknown): boolean { | ||
if (isObject(val)) { | ||
return !Object.keys(val).length; | ||
} | ||
|
||
return !data; | ||
return !val; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export const isFn = (a: unknown) => typeof a === 'function'; | ||
export function isFn(a: unknown): a is (...b: unknown[]) => unknown { | ||
return typeof a === 'function'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export function isObj(a: unknown): a is object { | ||
return a !== null && typeof a === 'object'; | ||
import { isArr } from './isArr'; | ||
import { isObject } from './isObject'; | ||
|
||
export function isObj(a: unknown): a is Record<string, unknown> { | ||
return isObject(a) && !isArr(a); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function isObject(a: unknown): a is object { | ||
return typeof a === 'object' && a !== null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export const isStr = (a: unknown) => typeof a === 'string'; | ||
export function isStr(a: unknown): a is string { | ||
return typeof a === 'string'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
import { isFn } from './isFn'; | ||
import { Fn } from './types'; | ||
|
||
interface NotFn { | ||
<T extends unknown[], R extends unknown>(a: Fn<T, R>): Fn<T, boolean>; | ||
(a: unknown): boolean; | ||
} | ||
|
||
/** | ||
* TODO: adicionar documentação | ||
*/ | ||
export function not<T extends any[], R>(value: unknown | Fn<T, R>): boolean | Fn<T, boolean> { | ||
return typeof value === 'function' ? (...args: T) => !value(...args) : !value; | ||
} | ||
export const not = ((val: any) => { | ||
return isFn(val) ? (...a: unknown[]) => !val(...a) : !val; | ||
}) as NotFn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import { curry } from './curry'; | ||
import { pick } from './pick'; | ||
import { Obj } from './types'; | ||
import { diff } from './diff'; | ||
import { pick, PickFn } from './pick'; | ||
|
||
interface OmitFn extends PickFn {} | ||
|
||
/** | ||
* TODO: adicionar documentação | ||
*/ | ||
export const omit = curry(function <T extends Obj>(path: string | string[], obj: T): T { | ||
export const omit = curry((path: string | string[], obj: any) => { | ||
const arr = ([] as string[]).concat(path); | ||
const keys = Object.keys(obj).filter((k) => !arr.includes(k)); | ||
|
||
return pick(keys, obj); | ||
}); | ||
return pick(diff(Object.keys(obj), arr), obj); | ||
}) as OmitFn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,21 @@ | ||
import { curry } from './curry'; | ||
import { Obj } from './types'; | ||
|
||
export interface PickFn { | ||
(a: string | string[]): <B extends object>(b: B) => Partial<B>; | ||
<B extends object>(a: string | string[], b: B): Partial<B>; | ||
} | ||
|
||
/** | ||
* TODO: adicionar documentação | ||
*/ | ||
export const pick = curry(function <T extends Obj>(path: string | string[], obj: T): T { | ||
export const pick = curry((path: string | string[], obj: any) => { | ||
const keys = ([] as string[]).concat(path); | ||
|
||
return keys.reduce(function (res, key) { | ||
if (obj.hasOwnProperty(key)) { | ||
(res as Obj)[key] = obj[key]; | ||
res[key] = obj[key]; | ||
} | ||
|
||
return res; | ||
}, {} as T); | ||
}); | ||
}, {} as any); | ||
}) as PickFn; |
Oops, something went wrong.