Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a0dc540a7c | |||
| b4f780216f | |||
| 1ae72169b2 | |||
| e240c71c83 | |||
| e382922398 | |||
| 0f2003d0bc | |||
| 2c1440082e | |||
| a7c12795fb |
22363
package-lock.json
generated
22363
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
13
package.json
13
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartobject",
|
||||
"version": "1.0.6",
|
||||
"version": "1.0.10",
|
||||
"private": false,
|
||||
"description": "work with objects",
|
||||
"main": "dist_ts/index.js",
|
||||
@@ -13,15 +13,16 @@
|
||||
"format": "(gitzone format)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tstest": "^1.0.52",
|
||||
"@pushrocks/tapbundle": "^3.2.14",
|
||||
"@types/node": "^14.14.35",
|
||||
"@gitzone/tsbuild": "^2.1.29",
|
||||
"@gitzone/tstest": "^1.0.64",
|
||||
"@pushrocks/tapbundle": "^4.0.8",
|
||||
"@types/node": "^17.0.21",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"fast-deep-equal": "^3.1.3"
|
||||
"fast-deep-equal": "^3.1.3",
|
||||
"minimatch": "^5.0.1"
|
||||
},
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
|
||||
@@ -10,8 +10,60 @@ tap.test('first test', async () => {
|
||||
});
|
||||
|
||||
tap.test('should fast deep equal objects', async () => {
|
||||
expect(smartobject.fastDeepEqual({ hello: 'yes' }, { hello: 'yes' })).to.be.true;
|
||||
expect(smartobject.fastDeepEqual({ hello: 'yes' }, { hello: 3 })).to.be.false;
|
||||
expect(smartobject.fastDeepEqual({ hello: 'yes' }, { hello: 'yes' })).toBeTrue();
|
||||
expect(smartobject.fastDeepEqual({ hello: 'yes' }, { hello: 3 })).toBeFalse();
|
||||
});
|
||||
|
||||
let parentObject: any = {};
|
||||
let childObject: any = {};
|
||||
|
||||
tap.test('childObject should not yet be in parentObject', async () => {
|
||||
expect(smartobject.exists(parentObject, 'childObject')).toBeFalse();
|
||||
parentObject.childObject = childObject;
|
||||
});
|
||||
|
||||
tap.test('childObject should now be in parentObject', async () => {
|
||||
expect(smartobject.exists(parentObject, 'childObject')).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('should be able to deepAdd an childParam', async () => {
|
||||
const parentObject = {
|
||||
hello: 'there'
|
||||
};
|
||||
const parentObject2 = smartobject.smartAdd(parentObject, 'wow.za', 'yes');
|
||||
console.log(parentObject2);
|
||||
expect(smartobject.exists(parentObject2.wow, 'za')).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('should be able to deep get an item', async () => {
|
||||
const testObject = {
|
||||
hey: {
|
||||
there: {
|
||||
so: 'cool'
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const item = smartobject.smartGet(testObject, 'hey.there.so');
|
||||
expect(item).toEqual('cool');
|
||||
});
|
||||
|
||||
tap.test('should call properties for minimatched properties', async () => {
|
||||
let testObject = {
|
||||
matchedOne: 'Hey!',
|
||||
matchedTwo: 'this works!',
|
||||
notmatched: 'NOT!'
|
||||
};
|
||||
|
||||
const matchedStrings: string[] = [];
|
||||
await smartobject.forEachMinimatch(testObject, 'matched*', matchedProperty => {
|
||||
matchedStrings.push(matchedProperty);
|
||||
console.log(matchedProperty);
|
||||
});
|
||||
|
||||
expect(matchedStrings).toContain('Hey!');
|
||||
expect(matchedStrings).toContain('this works!');
|
||||
expect(matchedStrings).not.toContain('NOT!');
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
||||
24
test/test.smartobject.ts
Normal file
24
test/test.smartobject.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { expect, expectAsync, tap} from '@pushrocks/tapbundle';
|
||||
import * as smartobject from '../ts';
|
||||
|
||||
let testSmartobject: smartobject.SmartObject;
|
||||
|
||||
tap.test('should create a smartobject', async () => {
|
||||
const originalObject = {
|
||||
test: 'hey',
|
||||
deep: {
|
||||
yeah: 'so deep',
|
||||
evendeeper: {
|
||||
sodeep: 2,
|
||||
deepArray: ['one array', 'two array']
|
||||
}
|
||||
}
|
||||
}
|
||||
testSmartobject = new smartobject.SmartObject(originalObject);
|
||||
testSmartobject.addValueAtFlatPathString('deep.nice', 'yeah that has been added');
|
||||
console.log(testSmartobject.originalObject);
|
||||
console.log(testSmartobject.toFlatObject())
|
||||
expect(testSmartobject.getValueAtFlatPathString('deep.yeah')).toEqual('so deep');
|
||||
});
|
||||
|
||||
tap.start();
|
||||
81
ts/index.ts
81
ts/index.ts
@@ -1,83 +1,6 @@
|
||||
import * as plugins from './smartobject.plugins';
|
||||
|
||||
const fastDeepEqual = plugins.fastDeepEqual;
|
||||
export { fastDeepEqual };
|
||||
|
||||
export interface IObjectCompareResult {
|
||||
presentInBothProperties: string[];
|
||||
missingProperties: string[];
|
||||
additionalProperties: string[];
|
||||
nulledProperties: string[];
|
||||
undefinedProperties: string[];
|
||||
divergingProperties: string[];
|
||||
equalProperties: string[];
|
||||
}
|
||||
|
||||
export const compareObjects = (
|
||||
referenceObjectArg: any,
|
||||
comparisonObjectArg: any
|
||||
): IObjectCompareResult => {
|
||||
const returnComparisonObject = {
|
||||
missingProperties: [],
|
||||
additionalProperties: [],
|
||||
presentInBothProperties: [],
|
||||
nulledProperties: [],
|
||||
undefinedProperties: [],
|
||||
divergingProperties: [],
|
||||
equalProperties: [],
|
||||
};
|
||||
|
||||
const allProperties = Object.keys(referenceObjectArg).concat(Object.keys(comparisonObjectArg));
|
||||
for (const currentProperty of allProperties) {
|
||||
// lets find presentInBothProperties
|
||||
if (referenceObjectArg[currentProperty] && comparisonObjectArg[currentProperty]) {
|
||||
returnComparisonObject.presentInBothProperties.push(currentProperty);
|
||||
}
|
||||
|
||||
// lets find missingProperties
|
||||
if (referenceObjectArg[currentProperty] && !comparisonObjectArg[currentProperty]) {
|
||||
returnComparisonObject.missingProperties.push(currentProperty);
|
||||
}
|
||||
|
||||
// lets find additionalProperties
|
||||
if (!referenceObjectArg[currentProperty] && comparisonObjectArg[currentProperty]) {
|
||||
returnComparisonObject.additionalProperties.push(currentProperty);
|
||||
}
|
||||
|
||||
// lets find nulledProperties
|
||||
if (comparisonObjectArg[currentProperty] === null) {
|
||||
returnComparisonObject.nulledProperties.push(currentProperty);
|
||||
}
|
||||
|
||||
// lets find undefinedProperties
|
||||
if (comparisonObjectArg[currentProperty] === undefined) {
|
||||
returnComparisonObject.undefinedProperties.push(currentProperty);
|
||||
}
|
||||
|
||||
// lets find divergingProperties
|
||||
if (
|
||||
JSON.stringify(referenceObjectArg[currentProperty]) !==
|
||||
JSON.stringify(comparisonObjectArg[currentProperty])
|
||||
) {
|
||||
returnComparisonObject.divergingProperties.push(currentProperty);
|
||||
}
|
||||
|
||||
// lets find equalProperties
|
||||
if (
|
||||
JSON.stringify(referenceObjectArg[currentProperty]) ===
|
||||
JSON.stringify(comparisonObjectArg[currentProperty])
|
||||
) {
|
||||
returnComparisonObject.equalProperties.push(currentProperty);
|
||||
}
|
||||
}
|
||||
|
||||
for (const currentProperty of Object.keys(returnComparisonObject)) {
|
||||
const onlyUnique = (value, index, self) => {
|
||||
return self.indexOf(value) === index;
|
||||
};
|
||||
const uniqueArray = returnComparisonObject[currentProperty].filter(onlyUnique);
|
||||
returnComparisonObject[currentProperty] = uniqueArray;
|
||||
}
|
||||
|
||||
return returnComparisonObject;
|
||||
};
|
||||
export * from './smartobject.classes.smartobject';
|
||||
export * from './tools';
|
||||
|
||||
25
ts/smartobject.classes.smartobject.ts
Normal file
25
ts/smartobject.classes.smartobject.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import * as tools from './tools';
|
||||
|
||||
/**
|
||||
* a smartobject that simplifies accessing objects
|
||||
*/
|
||||
export class SmartObject {
|
||||
|
||||
// instance
|
||||
public originalObject: object;
|
||||
constructor(originalObjectArg: object) {
|
||||
this.originalObject = originalObjectArg;
|
||||
}
|
||||
|
||||
public getValueAtFlatPathString(pathArg: string) {
|
||||
return tools.smartGet(this.originalObject, pathArg);
|
||||
}
|
||||
|
||||
public addValueAtFlatPathString(pathArg: string, valueArg: any) {
|
||||
return tools.smartAdd(this.originalObject, pathArg, valueArg);
|
||||
}
|
||||
|
||||
public toFlatObject() {
|
||||
return tools.toFlatObject(this.originalObject);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
// thirdparty scope
|
||||
// tslint:disable-next-line: no-submodule-imports
|
||||
import fastDeepEqual from 'fast-deep-equal/es6';
|
||||
import minimatch from 'minimatch';
|
||||
|
||||
export { fastDeepEqual };
|
||||
export { fastDeepEqual, minimatch };
|
||||
|
||||
79
ts/tools/compareobjects.ts
Normal file
79
ts/tools/compareobjects.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import * as plugins from '../smartobject.plugins';
|
||||
|
||||
export interface IObjectCompareResult {
|
||||
presentInBothProperties: string[];
|
||||
missingProperties: string[];
|
||||
additionalProperties: string[];
|
||||
nulledProperties: string[];
|
||||
undefinedProperties: string[];
|
||||
divergingProperties: string[];
|
||||
equalProperties: string[];
|
||||
}
|
||||
|
||||
export const compareObjects = (
|
||||
referenceObjectArg: any,
|
||||
comparisonObjectArg: any
|
||||
): IObjectCompareResult => {
|
||||
const returnComparisonObject = {
|
||||
missingProperties: [] as string[],
|
||||
additionalProperties: [] as string[],
|
||||
presentInBothProperties: [] as string[],
|
||||
nulledProperties: [] as string[],
|
||||
undefinedProperties: [] as string[],
|
||||
divergingProperties: [] as string[],
|
||||
equalProperties: [] as string[],
|
||||
};
|
||||
|
||||
const allProperties = Object.keys(referenceObjectArg).concat(Object.keys(comparisonObjectArg));
|
||||
for (const currentProperty of allProperties) {
|
||||
// lets find presentInBothProperties
|
||||
if (referenceObjectArg[ currentProperty ] && comparisonObjectArg[ currentProperty ]) {
|
||||
returnComparisonObject.presentInBothProperties.push(currentProperty);
|
||||
}
|
||||
|
||||
// lets find missingProperties
|
||||
if (referenceObjectArg[ currentProperty ] && !comparisonObjectArg[ currentProperty ]) {
|
||||
returnComparisonObject.missingProperties.push(currentProperty);
|
||||
}
|
||||
|
||||
// lets find additionalProperties
|
||||
if (!referenceObjectArg[ currentProperty ] && comparisonObjectArg[ currentProperty ]) {
|
||||
returnComparisonObject.additionalProperties.push(currentProperty);
|
||||
}
|
||||
|
||||
// lets find nulledProperties
|
||||
if (comparisonObjectArg[ currentProperty ] === null) {
|
||||
returnComparisonObject.nulledProperties.push(currentProperty);
|
||||
}
|
||||
|
||||
// lets find undefinedProperties
|
||||
if (comparisonObjectArg[ currentProperty ] === undefined) {
|
||||
returnComparisonObject.undefinedProperties.push(currentProperty);
|
||||
}
|
||||
|
||||
// lets find divergingProperties
|
||||
if (
|
||||
JSON.stringify(referenceObjectArg[ currentProperty ]) !==
|
||||
JSON.stringify(comparisonObjectArg[ currentProperty ])
|
||||
) {
|
||||
returnComparisonObject.divergingProperties.push(currentProperty);
|
||||
}
|
||||
|
||||
// lets find equalProperties
|
||||
if (
|
||||
plugins.fastDeepEqual(referenceObjectArg[ currentProperty ], comparisonObjectArg[ currentProperty ])
|
||||
) {
|
||||
returnComparisonObject.equalProperties.push(currentProperty);
|
||||
}
|
||||
}
|
||||
|
||||
for (const currentProperty of Object.keys(returnComparisonObject)) {
|
||||
const onlyUnique = (value: any, index: number, self: Array<any>) => {
|
||||
return self.indexOf(value) === index;
|
||||
};
|
||||
const uniqueArray = returnComparisonObject[ currentProperty as keyof (typeof returnComparisonObject) ].filter(onlyUnique);
|
||||
returnComparisonObject[ currentProperty as keyof (typeof returnComparisonObject) ] = uniqueArray;
|
||||
}
|
||||
|
||||
return returnComparisonObject;
|
||||
};
|
||||
14
ts/tools/exists.ts
Normal file
14
ts/tools/exists.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import * as plugins from '../smartobject.plugins';
|
||||
|
||||
/**
|
||||
* checks if an object has a parameter with a given key name, returns true if yes.
|
||||
* @param parentObject
|
||||
* @param childParam
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export let exists = (parentObject: object, childParam: string): boolean => {
|
||||
if (parentObject.hasOwnProperty(childParam)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
17
ts/tools/foreachminimatch.ts
Normal file
17
ts/tools/foreachminimatch.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import * as plugins from '../smartobject.plugins';
|
||||
|
||||
/**
|
||||
* runs a function for all properties of an object whose key matches a regex expression
|
||||
* @param parentObjectArg the parent object
|
||||
* @param wildcardArg the rege expression to match the property keys against
|
||||
* @param callbackArg the function to run with those properties
|
||||
*/
|
||||
export let forEachMinimatch = async (parentObjectArg: any, wildcardArg: string, callbackArg: (matchedArg: string) => void) => {
|
||||
let propertyNames = Object.getOwnPropertyNames(parentObjectArg);
|
||||
let propertyNamesMatched = propertyNames.filter(propertyNameArg => {
|
||||
return plugins.minimatch(propertyNameArg, wildcardArg);
|
||||
});
|
||||
for (let propertyNameArg of propertyNamesMatched) {
|
||||
await callbackArg(parentObjectArg[propertyNameArg]);
|
||||
}
|
||||
};
|
||||
5
ts/tools/index.ts
Normal file
5
ts/tools/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export * from './compareobjects';
|
||||
export * from './exists';
|
||||
export * from './foreachminimatch';
|
||||
export * from './smart_add_get';
|
||||
export * from './toFlatObject';
|
||||
92
ts/tools/smart_add_get.ts
Normal file
92
ts/tools/smart_add_get.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import * as plugins from '../smartobject.plugins';
|
||||
|
||||
/**
|
||||
* adds an object to the parent object if it doesn't exists
|
||||
* @param parentObject
|
||||
* @param childParam
|
||||
* @param logBool
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export const smartAdd = (
|
||||
parentObject: object,
|
||||
childParam: string,
|
||||
valueArg: any = {},
|
||||
optionsArg?: {
|
||||
interpretDotsAsLevel: boolean;
|
||||
}
|
||||
): typeof parentObject & any => {
|
||||
optionsArg = {
|
||||
interpretDotsAsLevel: true,
|
||||
...optionsArg
|
||||
};
|
||||
|
||||
let paramLevels: string[];
|
||||
let referencePointer: any = parentObject;
|
||||
if (optionsArg.interpretDotsAsLevel) {
|
||||
paramLevels = childParam.split('.');
|
||||
} else {
|
||||
paramLevels = [childParam];
|
||||
}
|
||||
|
||||
for (let i = 0; i !== paramLevels.length; i++) {
|
||||
const varName: string = paramLevels[i];
|
||||
|
||||
// is there a next variable ?
|
||||
const varNameNext: string = (() => {
|
||||
if (paramLevels[i + 1]) {
|
||||
return paramLevels[i + 1];
|
||||
}
|
||||
return null;
|
||||
})();
|
||||
|
||||
// build the tree in parentObject
|
||||
if (!referencePointer[varName] && !varNameNext) {
|
||||
referencePointer[varName] = valueArg;
|
||||
referencePointer = null;
|
||||
} else if (!referencePointer[varName] && varNameNext) {
|
||||
referencePointer[varName] = {};
|
||||
referencePointer = referencePointer[varName];
|
||||
} else if (referencePointer[varName] && varNameNext) {
|
||||
referencePointer = referencePointer[varName];
|
||||
} else {
|
||||
throw new Error('Something is strange!');
|
||||
}
|
||||
}
|
||||
return parentObject;
|
||||
};
|
||||
|
||||
/**
|
||||
* gets an object from the parent object using dots as levels by default
|
||||
* @param parentObject
|
||||
* @param childParam
|
||||
* @param optionsArg
|
||||
*/
|
||||
export const smartGet = <T>(
|
||||
parentObject: object,
|
||||
childParam: string,
|
||||
optionsArg?: {
|
||||
interpretDotsAsLevel: boolean;
|
||||
}
|
||||
): T => {
|
||||
optionsArg = {
|
||||
interpretDotsAsLevel: true,
|
||||
...optionsArg
|
||||
};
|
||||
|
||||
let paramLevels: string[];
|
||||
if (optionsArg.interpretDotsAsLevel) {
|
||||
paramLevels = childParam.split('.');
|
||||
} else {
|
||||
paramLevels = [childParam];
|
||||
}
|
||||
|
||||
let referencePointer: any = parentObject;
|
||||
for (const level of paramLevels) {
|
||||
if (referencePointer[level as any]) {
|
||||
referencePointer = referencePointer[level as any];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return referencePointer as T;
|
||||
};
|
||||
21
ts/tools/toFlatObject.ts
Normal file
21
ts/tools/toFlatObject.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
export const toFlatObject = (objectArg: object) => {
|
||||
const returnObject: {[key: string]: any} = {};
|
||||
const extractLayer = (subObject: {[key: string]: any}, pathArg: string, loopProtection: object[]) => {
|
||||
if (loopProtection.indexOf(subObject) > -1) {
|
||||
return;
|
||||
}
|
||||
if (subObject)
|
||||
for (const key of Object.keys(subObject)) {
|
||||
let localPathArg = pathArg;
|
||||
if (typeof subObject[ key ] === 'object' && !(subObject[ key ] instanceof Array)) {
|
||||
const newLoopbackArray = loopProtection.slice();
|
||||
newLoopbackArray.push(subObject);
|
||||
extractLayer(subObject[ key ], localPathArg ? localPathArg += `.${key}` : key, newLoopbackArray);
|
||||
} else {
|
||||
returnObject[localPathArg ? localPathArg += `.${key}` : key] = subObject[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
extractLayer(objectArg, '', []);
|
||||
return returnObject;
|
||||
}
|
||||
Reference in New Issue
Block a user