Skip to content

Commit

Permalink
feat(types): add TUtilEach, TUtilMap, TUtilSet types
Browse files Browse the repository at this point in the history
feat(types): add TUtilEach, TUtilMap, TUtilSet types
  • Loading branch information
oljekechoro authored and antongolub committed Mar 26, 2019
1 parent 60da7d2 commit deab615
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 4 deletions.
7 changes: 5 additions & 2 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
8 changes: 8 additions & 0 deletions packages/types/src/main/IUtilEach.ts
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions packages/types/src/main/IUtilMap.ts
Original file line number Diff line number Diff line change
@@ -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[]
11 changes: 11 additions & 0 deletions packages/types/src/main/IUtilSet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type TUtilSetPath = Array<string | number> | string

export type TUtilSetObject = object

export type TUtilSetValue = any

export type TUtilSet = (
obj: TUtilSetObject,
path: TUtilSetPath,
value: TUtilSetValue
) => void
3 changes: 3 additions & 0 deletions packages/types/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
37 changes: 35 additions & 2 deletions packages/types/src/test/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -6,6 +9,9 @@ import {
ILogger,
TLogLevel,
TUtilGet,
TUtilSet,
TUtilEach,
TUtilMap,
ICloneable
} from '../main';

Expand Down Expand Up @@ -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 | number> | 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 | number> | 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 | number> | 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<string> = {
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit deab615

Please sign in to comment.