Skip to content

Commit

Permalink
perf(common.utils.ts): implement internal versions of get isempty isdate
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Nov 3, 2019
1 parent 43f9a0d commit d945122
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 84 deletions.
6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@
"@commitlint/cli": "8.2.0",
"@commitlint/config-conventional": "8.2.0",
"@types/jest": "24.0.21",
"@types/lodash.get": "4.4.6",
"@types/lodash.isdate": "4.0.6",
"@types/lodash.isempty": "4.4.6",
"@types/lodash.set": "4.3.6",
"commitizen": "4.0.3",
"coveralls": "3.0.7",
Expand All @@ -104,9 +101,6 @@
},
"dependencies": {
"class-transformer": "0.2.3",
"lodash.get": "4.4.2",
"lodash.isdate": "4.0.1",
"lodash.isempty": "4.4.0",
"lodash.set": "4.3.2"
}
}
16 changes: 8 additions & 8 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { plainToClass } from 'class-transformer';
import get from 'lodash.get';
import isDate from 'lodash.isdate';
import isEmpty from 'lodash.isempty';
import set from 'lodash.set';
import { defaultMapActionOptions } from './constants';
import {
Expand All @@ -22,10 +19,13 @@ import {
} from './types';
import {
_assertMappingErrors,
_get,
_getMappingKey,
_getSourcePropertyKey,
_initializeReversedMappingProperties,
_isClass,
_isDate,
_isEmpty,
_isObjectLike,
_isResolver,
_setMappingPropertyForMapFromMember,
Expand Down Expand Up @@ -240,7 +240,7 @@ export abstract class AutoMapperBase {
(mapWith as MapWithTransformOptions).destination
);
const _source = (mapWith as MapWithTransformOptions).fromValue(sourceObj);
if (isEmpty(_source)) {
if (_isEmpty(_source)) {
console.warn(
`${propSourceMemberPath} does not exist on ${_mapping.source}`
);
Expand All @@ -262,7 +262,7 @@ export abstract class AutoMapperBase {
set(
destinationObj,
destinationMemberPath,
isEmpty(_source[0])
_isEmpty(_source[0])
? []
: (this._mapArray(_source, _mapping as Mapping) as any)
);
Expand All @@ -284,7 +284,7 @@ export abstract class AutoMapperBase {
set(
destinationObj,
destinationMemberPath,
get(sourceObj, propSourceMemberPath)
_get(sourceObj, propSourceMemberPath)
);
return;
}
Expand Down Expand Up @@ -325,14 +325,14 @@ export abstract class AutoMapperBase {
// }

if (_isObjectLike(sourceVal)) {
if (isDate(sourceVal)) {
if (_isDate(sourceVal)) {
set(destinationObj, destinationMemberPath, new Date(sourceVal));
return;
}

if (Array.isArray(sourceVal)) {
const _first = sourceVal[0];
if (isEmpty(_first)) {
if (_isEmpty(_first)) {
set(destinationObj, destinationMemberPath, []);
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/utils/automapper.utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { plainToClass } from 'class-transformer';
import get from 'lodash.get';
import {
ConditionPredicate,
ConvertUsingTransformOptions,
Expand All @@ -17,6 +16,7 @@ import {
ValueSelector,
} from '../types';
import {
_get,
_getMemberPath,
_getPathRecursive,
_getSourcePropertyKey,
Expand Down Expand Up @@ -66,7 +66,7 @@ export function _initializeMappingProperties<
preCondition: null,
type: TransformationType.MapInitialize,
},
mapFrom: s => get(s, _paths.join('.')),
mapFrom: s => _get(s, _paths.join('.')),
},
destinationMemberPath: path,
destinationMemberSelector: d => (d as any)[path],
Expand All @@ -84,7 +84,7 @@ export function _initializeMappingProperties<
preCondition: null,
type: TransformationType.MapInitialize,
},
mapFrom: s => get(s, sourcePath),
mapFrom: s => _get(s, sourcePath),
},
destinationMemberSelector: d => (d as any)[path],
destinationMemberPath: path,
Expand Down Expand Up @@ -144,7 +144,7 @@ export function _initializeReversedMappingProperties<
preCondition: null,
type: TransformationType.MapInitialize,
},
mapFrom: d => get(d, prop.destinationMemberPath),
mapFrom: d => _get(d, prop.destinationMemberPath),
},
})
);
Expand Down
44 changes: 44 additions & 0 deletions src/utils/common.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,47 @@ export function _assertMappingErrors<T extends Dict<T> = any>(
`);
}
}

export enum ObjectTag {
Undefined = '[object Undefined]',
Null = '[object Null]',
Object = '[object Object]',
Array = '[object Array]',
Date = '[object Date]',
Map = '[object Map]',
}

function _getTag(value: any): string {
if (value == null) {
return value === undefined ? ObjectTag.Undefined : ObjectTag.Null;
}

return Object.prototype.toString.call(value) as ObjectTag;
}

export function _get<T>(object: T, path: string, defaultVal: any = null): any {
const _path = path.split('.').filter(Boolean);
const _val = _path.reduce((obj: any, key) => obj && obj[key], object);
return _val == undefined ? defaultVal : _val;
}

export function _isEmpty(value: any): boolean {
const tag = _getTag(value);
if (tag === ObjectTag.Map) {
return !value.size;
}

if (tag === ObjectTag.Object) {
return !Object.keys(value).length;
}

if (tag === ObjectTag.Array) {
return !value.length;
}

return !value;
}

export function _isDate(value: any): boolean {
return _getTag(value) === ObjectTag.Date;
}
69 changes: 3 additions & 66 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1228,27 +1228,6 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636"
integrity sha512-Il2DtDVRGDcqjDtE+rF8iqg1CArehSK84HZJCT7AMITlyXRBpuPhqGLDQMowraqqu1coEaimg4ZOqggt6L6L+A==

"@types/[email protected]":
version "4.4.6"
resolved "https://registry.yarnpkg.com/@types/lodash.get/-/lodash.get-4.4.6.tgz#0c7ac56243dae0f9f09ab6f75b29471e2e777240"
integrity sha512-E6zzjR3GtNig8UJG/yodBeJeIOtgPkMgsLjDU3CbgCAPC++vJ0eCMnJhVpRZb/ENqEFlov1+3K9TKtY4UdWKtQ==
dependencies:
"@types/lodash" "*"

"@types/[email protected]":
version "4.0.6"
resolved "https://registry.yarnpkg.com/@types/lodash.isdate/-/lodash.isdate-4.0.6.tgz#22a0998f080744e8ce7e52f3ce0c4a9d217e52c2"
integrity sha512-s1wl7+oFYgUVn9COL9meCzNgfG7+dsFRZZut91WwbbHnjeA+FD8w/jgMEMNwxjb8//OmIyM1blxJRYaIDpR3Mg==
dependencies:
"@types/lodash" "*"

"@types/[email protected]":
version "4.4.6"
resolved "https://registry.yarnpkg.com/@types/lodash.isempty/-/lodash.isempty-4.4.6.tgz#48a5576985727d9b85d59a60199d6b11ac756a3e"
integrity sha512-AauKrFlA4z3Usog5HLGDupKzkCP7h5KXGlfAcRGUfvTmL7guVuEzDSNI6lYJ7syO7J2RE2F47179pSLr26UHIw==
dependencies:
"@types/lodash" "*"

"@types/[email protected]":
version "4.3.6"
resolved "https://registry.yarnpkg.com/@types/lodash.set/-/lodash.set-4.3.6.tgz#33e635c2323f855359225df6a5c8c6f1f1908264"
Expand Down Expand Up @@ -2786,7 +2765,7 @@ debug@^4.0.0, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -4313,7 +4292,7 @@ import-local@^2.0.0:
pkg-dir "^3.0.0"
resolve-cwd "^2.0.0"

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -5677,11 +5656,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -5690,33 +5664,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._reinterpolate@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
Expand All @@ -5742,21 +5694,11 @@ lodash.escaperegexp@^4.1.2:
resolved "https://registry.yarnpkg.com/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz#64762c48618082518ac3df4ccf5d5886dae20347"
integrity sha1-ZHYsSGGAglGKw99Mz11YhtriA0c=

lodash.get@4.4.2, lodash.get@^4.4.2:
lodash.get@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=

[email protected]:
version "4.0.1"
resolved "https://registry.yarnpkg.com/lodash.isdate/-/lodash.isdate-4.0.1.tgz#35a543673b9d76110de4114b32cc577048a7f366"
integrity sha1-NaVDZzuddhEN5BFLMsxXcEin82Y=

[email protected]:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.isempty/-/lodash.isempty-4.4.0.tgz#6f86cbedd8be4ec987be9aaf33c9684db1b31e7e"
integrity sha1-b4bL7di+TsmHvpqvM8loTbGzHn4=

lodash.ismatch@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.ismatch/-/lodash.ismatch-4.4.0.tgz#756cb5150ca3ba6f11085a78849645f188f85f37"
Expand Down Expand Up @@ -5787,11 +5729,6 @@ lodash.merge@^4.6.2:
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

[email protected], lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down

0 comments on commit d945122

Please sign in to comment.