diff --git a/packages/types/package.json b/packages/types/package.json index 8e0916537..7726e2ba2 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -35,16 +35,19 @@ "dependencies": {}, "devDependencies": { "@types/jest": "^24.0.11", - "@types/node": "^11.11.3", "@types/lodash": "^4.14.123", + "@types/node": "^11.11.3", "@types/parsimmon": "^1.10.0", + "@types/underscore": "^1.8.13", "dts-generator": "^3.0.0", + "dtslint": "^0.5.5", "flowgen": "^1.7.0", + "lodash": "^4.17.11", "replace-in-file": "^3.4.4", "ts-loader": "^5.3.3", "ts-node": "^8.0.3", "ts-to-flow": "^0.0.3", "typescript": "3.3.3333", - "dtslint": "^0.5.5" + "underscore": "^1.9.1" } } diff --git a/packages/types/src/main/IUtilEach.ts b/packages/types/src/main/IUtilEach.ts new file mode 100644 index 000000000..f45973a81 --- /dev/null +++ b/packages/types/src/main/IUtilEach.ts @@ -0,0 +1,8 @@ +export type TUtilEachCollection = any[] | object + +export type TUtilEachHandler = (value: any, key?: string | number, collection?: TUtilEachCollection) => void + +export type TUtilEach = ( + collection: TUtilEachCollection, + handler: TUtilEachHandler +) => TUtilEachCollection diff --git a/packages/types/src/main/IUtilMap.ts b/packages/types/src/main/IUtilMap.ts new file mode 100644 index 000000000..6e4fcac55 --- /dev/null +++ b/packages/types/src/main/IUtilMap.ts @@ -0,0 +1,8 @@ +export type TUtilMapCollection = any[] | object + +export type TUtilMapHandler = (value: any, key?: string | number, collection?: TUtilMapCollection) => any + +export type TUtilMap = ( + collection: TUtilMapCollection, + handler: TUtilMapHandler +) => any[] diff --git a/packages/types/src/main/IUtilSet.ts b/packages/types/src/main/IUtilSet.ts new file mode 100644 index 000000000..9de7989d2 --- /dev/null +++ b/packages/types/src/main/IUtilSet.ts @@ -0,0 +1,11 @@ +export type TUtilSetPath = Array | string + +export type TUtilSetObject = object + +export type TUtilSetValue = any + +export type TUtilSet = ( + obj: TUtilSetObject, + path: TUtilSetPath, + value: TUtilSetValue +) => void diff --git a/packages/types/src/main/index.ts b/packages/types/src/main/index.ts index 8c8d52671..453329b48 100644 --- a/packages/types/src/main/index.ts +++ b/packages/types/src/main/index.ts @@ -3,4 +3,7 @@ export { IAnyMap } from './IAnyMap' export { IEventEmitter } from './IEventEmitter' export { ILogger, TLogLevel, ILoggerMethod } from './ILogger' export { TUtilGet } from './IUtilGet' +export { TUtilSet } from './IUtilSet' +export { TUtilEach } from './IUtilEach' +export { TUtilMap } from './IUtilMap' export { ICloneable } from './ICloneable' diff --git a/packages/types/src/test/index.ts b/packages/types/src/test/index.ts index 871f8fc6e..26167043e 100644 --- a/packages/types/src/test/index.ts +++ b/packages/types/src/test/index.ts @@ -1,3 +1,6 @@ +import { forEach as lodashEach, map as lodashMap, set as lodashSet, get as lodashGet } from 'lodash'; +import { each as underscoreEach, map as underscoreMap } from 'underscore'; + import { IStringMap, IAnyMap, @@ -6,6 +9,9 @@ import { ILogger, TLogLevel, TUtilGet, + TUtilSet, + TUtilEach, + TUtilMap, ICloneable } from '../main'; @@ -76,8 +82,35 @@ const logger: ILogger = { // TUtilGet -const brokenUtilGet: TUtilGet = (obj: object, path: any[], defaultValue?: any) => undefined; // $ExpectError -const utilGet: TUtilGet = (obj: object, path: Array | string, defaultValue?: any) => obj || path || defaultValue; +const brokenUtilGet: TUtilGet = (obj: object, path: any[], defaultValue?: any) => { obj || path || defaultValue; }; // $ExpectError +const utilGet: TUtilGet = (obj: object, path: Array | string, defaultValue?: any) => { + obj || path; + return defaultValue; +}; +const lodashUtilGet: TUtilGet = lodashGet; + +// TUtilSet + +const brokenUtilSet: TUtilSet = (obj: object, path: any[], value: boolean) => { obj || path || value; }; // $ExpectError +const utilSet: TUtilSet = (obj: object, path: Array | string, value: boolean) => { obj || path || value; }; +const lodashUtilSet: TUtilSet = lodashSet; + +// TUtilEach + +const brokenUtilEach: TUtilEach = (collection: object, handler: (value: string, key: boolean, collection: object) => void) => { handler || collection; }; // $ExpectError +const utilEach: TUtilEach = (collection: object, handler: (value: string, key: number, collection: object) => void): object => { + handler(JSON.stringify(collection), 1, collection); + return collection; +}; +const lodashUtilEach: TUtilEach = lodashEach; +const underscoreUtilEach: TUtilEach = underscoreEach; + +// TUtilMap + +const brokenUtilMap: TUtilMap = (collection: object, handler: (value: string, key: boolean, collection: object) => object): any[] => [handler(JSON.stringify(collection), false, collection)]; // $ExpectError +const utilMap: TUtilMap = (collection: object, handler: (value: string, key: string, collection: object) => object): any[] => [handler(JSON.stringify(collection), 'key', collection)]; +const lodashUtilMap: TUtilMap = lodashMap; +const underscoreUtilMap: TUtilMap = underscoreMap; // ICloneable const cloneable: ICloneable = { diff --git a/yarn.lock b/yarn.lock index 29391a6e2..5b633e196 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1178,6 +1178,11 @@ resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e" integrity sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw== +"@types/underscore@^1.8.13": + version "1.8.13" + resolved "https://registry.yarnpkg.com/@types/underscore/-/underscore-1.8.13.tgz#e1bf89141fbccb71dcb3bf12d86a923c007698e3" + integrity sha512-2YEdDHTYAv3YmSoQofzFnlWYDAAtMtVcQ0lOHoURuCt1aT9OgxhhjRRU1WVAiLchNWM7B3a8/iomxenfr9TwKA== + "@types/yargs@^12.0.2", "@types/yargs@^12.0.9": version "12.0.9" resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-12.0.9.tgz#693e76a52f61a2f1e7fb48c0eef167b95ea4ffd0" @@ -7118,6 +7123,11 @@ umask@^1.1.0, umask@~1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/umask/-/umask-1.1.0.tgz#f29cebf01df517912bb58ff9c4e50fde8e33320d" +underscore@^1.9.1: + version "1.9.1" + resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.9.1.tgz#06dce34a0e68a7babc29b365b8e74b8925203961" + integrity sha512-5/4etnCkd9c8gwgowi5/om/mYO5ajCaOgdzj/oW+0eQV9WxKBDZw5+ycmKmeaTXjInS/W0BzpGLo2xR2aBwZdg== + union-value@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.0.tgz#5c71c34cb5bad5dcebe3ea0cd08207ba5aa1aea4"