Skip to content

Commit 21f0132

Browse files
committed
feat: totaly removed lodash 🎉
1 parent 87c4f78 commit 21f0132

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

‎src/morphism.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { set } from 'lodash';
2-
31
const aggregator = (paths: any, object: any) => {
42
return paths.reduce((delta: any, path: any) => {
53
return set(delta, path, get(object, path));
@@ -49,7 +47,20 @@ function isString(value: any): value is string {
4947
function isFunction(value: any): value is (...args: any[]) => any {
5048
return typeof value === 'function';
5149
}
50+
function set(object: object, path: string, value: any) {
51+
path = path.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
52+
path = path.replace(/^\./, ''); // strip a leading dot
53+
const paths = path.split('.');
54+
const lastProperty = paths.pop();
55+
const finalValue = paths.reduceRight(
56+
(finalObject, prop) => {
57+
return { [prop]: finalObject };
58+
},
59+
{ [lastProperty]: value }
60+
);
5261

62+
return { ...object, ...finalValue };
63+
}
5364
function get(object: object, path: string) {
5465
path = path.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
5566
path = path.replace(/^\./, ''); // strip a leading dot
@@ -70,13 +81,13 @@ function zipObject(props: string[], values: any[]) {
7081
return { ...prev, [prop]: values[i] };
7182
}, {});
7283
}
73-
84+
export type Many<T> = T | T[];
7485
export interface Schema {
7586
[targetProperty: string]:
7687
| string
77-
| ((iteratee: object | object[], source: object | object[], target: any) => any)
88+
| ((iteratee: Many<object>, source: Many<object>, target: any) => any)
7889
| string[]
79-
| { path: string | string[]; fn: (fieldValue: object | object[], items: object | object[]) => any };
90+
| { path: string | string[]; fn: (fieldValue: Many<object>, items: Many<object>) => any };
8091
}
8192

8293
/**

0 commit comments

Comments
 (0)