Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
djalmajr committed Feb 8, 2022
1 parent 46c19f5 commit e206450
Show file tree
Hide file tree
Showing 31 changed files with 156 additions and 90 deletions.
6 changes: 3 additions & 3 deletions src/arr2obj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Obj } from './types';
/**
* TODO: adicionar documentação
*/
export function arr2obj<T = unknown>(
export function arr2obj<T extends unknown>(
key: keyof T,
items?: T[],
formatter?: (value: string) => string,
formatKey?: (value: string) => string,
): Obj<T> {
const fn = formatter || ((s) => s);
const fn = formatKey || ((s) => s);

return (items || []).reduce((r, d) => ({ ...r, [fn(d[key] as any)]: d }), {});
}
4 changes: 3 additions & 1 deletion src/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { clone } from './clone';
import { curry } from './curry';
import { merge, MergeFn } from './merge';

interface AssignFn extends MergeFn {}

export const assign = curry((target: any, source: any) => {
return merge(clone(target), source);
}) as MergeFn;
}) as AssignFn;
7 changes: 4 additions & 3 deletions src/classNames.ts
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(' ');
}
4 changes: 2 additions & 2 deletions src/cleanCRLF.ts
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, ' ');
}
4 changes: 2 additions & 2 deletions src/clone.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { isObj } from './isObj';
import { isObject } from './isObject';

/**
* http://documentcloud.github.io/underscore-contrib/#snapshot
*
* TODO: adicionar documentação
*/
export function clone<T extends object>(data: T): T {
if (!isObj(data)) {
if (!isObject(data)) {
return data;
}

Expand Down
6 changes: 4 additions & 2 deletions src/copy.ts
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 };
}
2 changes: 1 addition & 1 deletion src/curry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type SameLength<T extends any[]> = Extract<{ [K in keyof T]: any }, any[]
* const toSquare = pow(2);
* console.log(toSquare(9)) // logs 81
*/
export function curry<A extends any[], R>(fn: Fn<A, R>): Curried<A, R> {
export function curry<A extends unknown[], R>(fn: Fn<A, R>): Curried<A, R> {
return function (...args: any[]): any {
return args.length < fn.length ? curry((fn as any).bind(null, ...args)) : fn(...(args as any));
};
Expand Down
25 changes: 12 additions & 13 deletions src/debounce.ts
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;
11 changes: 9 additions & 2 deletions src/diff.ts
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;
15 changes: 10 additions & 5 deletions src/get.ts
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;
9 changes: 7 additions & 2 deletions src/groupByEveryN.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { curry } from './curry';

interface GroupByEveryNFn {
(a: number): <B extends unknown[]>(b: B) => B[];
<B extends unknown[]>(a: number, b: B): B[];
}

/**
* TODO: adicionar documentação
*/
export const groupByEveryN = curry(<T>(num: number, items: T[]): T[][] => {
export const groupByEveryN = curry((num: number, items: unknown[]): unknown[][] => {
const res = [];
const arr = [...items];

Expand All @@ -12,4 +17,4 @@ export const groupByEveryN = curry(<T>(num: number, items: T[]): T[][] => {
}

return res;
});
}) as GroupByEveryNFn;
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ export * from './arr2obj';
export * from './assign';
export * from './camelCase';
export * from './classNames';
export * from './cleanCRLF';
export * from './clone';
export * from './contains';
export * from './copy';
export * from './curry';
export * from './debounce';
export * from './diff';
export * from './firstUpper';
export * from './flow';
export * from './get';
export * from './groupByEveryN';
export * from './isArr';
export * from './isEmpty';
export * from './isEqual';
export * from './isFn';
export * from './isObj';
export * from './isObject';
export * from './isStr';
export * from './isURL';
export * from './kebabCase';
Expand Down
3 changes: 3 additions & 0 deletions src/isArr.ts
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);
}
10 changes: 6 additions & 4 deletions src/isEmpty.ts
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;
}
13 changes: 6 additions & 7 deletions src/isEqual.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { curry } from './curry';
import { isObj } from './isObj';
import { isObject } from './isObject';
import { size } from './size';
import { Obj } from './types';

const { keys } = Object;

export const isEqual = curry(<T = unknown>(source: T, target: T): boolean => {
if (!isObj(source) || !isObj(target)) {
export const isEqual = curry((source: object, target: object): boolean => {
if (!isObject(source) || !isObject(target)) {
return source === target;
}

if (size(source) !== size(target)) {
return false;
}

const src = source as Obj;
const tgt = target as Obj;
const src = source as any;
const tgt = target as any;

return keys(src).every((key: string): boolean => {
if (isObj(src[key]) && isObj(tgt[key])) {
if (isObject(src[key]) && isObject(tgt[key])) {
return isEqual(src[key], tgt[key]);
}

Expand Down
4 changes: 3 additions & 1 deletion src/isFn.ts
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';
}
7 changes: 5 additions & 2 deletions src/isObj.ts
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);
}
3 changes: 3 additions & 0 deletions src/isObject.ts
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;
}
4 changes: 3 additions & 1 deletion src/isStr.ts
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';
}
4 changes: 2 additions & 2 deletions src/merge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { curry } from './curry';
import { isObj } from './isObj';
import { isObject } from './isObject';
import { Spread } from './types';

const getCtor = (v: any) => v.constructor;
Expand All @@ -11,7 +11,7 @@ export interface MergeFn {

export const merge = curry((target: any, source: any) => {
for (const [key, val] of Object.entries(source)) {
if (isObj(val)) {
if (isObject(val)) {
if (target[key] === void 0 || getCtor(target[key]) !== getCtor(val)) {
target[key] = new (val as any).constructor();
}
Expand Down
12 changes: 9 additions & 3 deletions src/not.ts
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;
2 changes: 1 addition & 1 deletion src/obj2arr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import { Obj } from './types';
* TODO: adicionar documentação
*/
export function obj2arr<T extends Obj>(obj: T): [keyof T, T][] {
return Object.entries(obj || {}).map(([k, v]) => [k, v]);
return Object.entries(obj).map(([k, v]) => [k, v]);
}
13 changes: 7 additions & 6 deletions src/omit.ts
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;
14 changes: 9 additions & 5 deletions src/pick.ts
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;
Loading

0 comments on commit e206450

Please sign in to comment.