6 Commits

Author SHA1 Message Date
2c1440082e 1.0.7 2022-03-06 11:09:54 +01:00
a7c12795fb fix(core): update 2022-03-06 11:09:53 +01:00
1ccc67215c 1.0.6 2021-03-22 02:33:23 +00:00
6c9b19cb9f fix(core): update 2021-03-22 02:33:23 +00:00
5944310af9 1.0.5 2021-03-22 02:33:02 +00:00
5732f0e2cd fix(core): update 2021-03-22 02:33:01 +00:00
8 changed files with 20451 additions and 3768 deletions

View File

@@ -19,23 +19,35 @@ mirror:
stage: security stage: security
script: script:
- npmci git mirror - npmci git mirror
only:
- tags
tags: tags:
- lossless - lossless
- docker - docker
- notpriv - notpriv
audit: auditProductionDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --production --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=prod --production
tags:
- docker
auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security stage: security
script: script:
- npmci npm prepare - npmci npm prepare
- npmci command npm install --ignore-scripts - npmci command npm install --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org - npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high - npmci command npm audit --audit-level=high --only=dev
tags: tags:
- lossless
- docker - docker
- notpriv allow_failure: true
# ==================== # ====================
# test stage # test stage
@@ -50,9 +62,7 @@ testStable:
- npmci npm test - npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/ coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- lossless
- docker - docker
- priv
testBuild: testBuild:
stage: test stage: test
@@ -63,9 +73,7 @@ testBuild:
- npmci command npm run build - npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/ coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- lossless
- docker - docker
- notpriv
release: release:
stage: release stage: release
@@ -85,6 +93,8 @@ release:
codequality: codequality:
stage: metadata stage: metadata
allow_failure: true allow_failure: true
only:
- tags
script: script:
- npmci command npm install -g tslint typescript - npmci command npm install -g tslint typescript
- npmci npm prepare - npmci npm prepare

View File

@@ -15,7 +15,7 @@
"properties": { "properties": {
"projectType": { "projectType": {
"type": "string", "type": "string",
"enum": ["website", "element", "service", "npm"] "enum": ["website", "element", "service", "npm", "wcc"]
} }
} }
} }

23922
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartobject", "name": "@pushrocks/smartobject",
"version": "1.0.4", "version": "1.0.7",
"private": false, "private": false,
"description": "work with objects", "description": "work with objects",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@@ -13,14 +13,17 @@
"format": "(gitzone format)" "format": "(gitzone format)"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.0.22", "@gitzone/tsbuild": "^2.1.29",
"@gitzone/tstest": "^1.0.15", "@gitzone/tstest": "^1.0.64",
"@pushrocks/tapbundle": "^3.0.7", "@pushrocks/tapbundle": "^4.0.8",
"@types/node": "^10.11.7", "@types/node": "^17.0.21",
"tslint": "^5.11.0", "tslint": "^6.1.3",
"tslint-config-prettier": "^1.15.0" "tslint-config-prettier": "^1.15.0"
}, },
"dependencies": {}, "dependencies": {
"fast-deep-equal": "^3.1.3",
"minimatch": "^5.0.1"
},
"browserslist": [ "browserslist": [
"last 1 chrome versions" "last 1 chrome versions"
], ],

69
test/test.both.ts Normal file
View File

@@ -0,0 +1,69 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as smartobject from '../ts/index';
tap.test('first test', async () => {
const result = smartobject.compareObjects(
{ thisIsEq: 'wow', thisIsDeepEq: { deeper: 'sodeep' } },
{ thisIsEq: 'wow', thisIsDeepEq: { deeper: 'sodeep' }, hey: 'there' }
);
console.log(result);
});
tap.test('should fast deep equal objects', async () => {
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();

View File

@@ -1,12 +0,0 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as smartobject from '../ts/index';
tap.test('first test', async () => {
const result = smartobject.compareObjects(
{ thisIsEq: 'wow', thisIsDeepEq: { deeper: 'sodeep' } },
{ thisIsEq: 'wow', thisIsDeepEq: { deeper: 'sodeep' }, hey: 'there' }
);
console.log(result);
});
tap.start();

View File

@@ -1,5 +1,8 @@
import * as plugins from './smartobject.plugins'; import * as plugins from './smartobject.plugins';
const fastDeepEqual = plugins.fastDeepEqual;
export { fastDeepEqual };
export interface IObjectCompareResult { export interface IObjectCompareResult {
presentInBothProperties: string[]; presentInBothProperties: string[];
missingProperties: string[]; missingProperties: string[];
@@ -10,17 +13,20 @@ export interface IObjectCompareResult {
equalProperties: string[]; equalProperties: string[];
} }
export const compareObjects = (referenceObjectArg: any, comparisonObjectArg: any): IObjectCompareResult => { export const compareObjects = (
referenceObjectArg: any,
comparisonObjectArg: any
): IObjectCompareResult => {
const returnComparisonObject = { const returnComparisonObject = {
missingProperties: [], missingProperties: [] as string[],
additionalProperties: [], additionalProperties: [] as string[],
presentInBothProperties: [], presentInBothProperties: [] as string[],
nulledProperties: [], nulledProperties: [] as string[],
undefinedProperties: [], undefinedProperties: [] as string[],
divergingProperties: [], divergingProperties: [] as string[],
equalProperties: [], equalProperties: [] as string[],
}; };
const allProperties = Object.keys(referenceObjectArg).concat(Object.keys(comparisonObjectArg)); const allProperties = Object.keys(referenceObjectArg).concat(Object.keys(comparisonObjectArg));
for (const currentProperty of allProperties) { for (const currentProperty of allProperties) {
// lets find presentInBothProperties // lets find presentInBothProperties
@@ -49,21 +55,149 @@ export const compareObjects = (referenceObjectArg: any, comparisonObjectArg: any
} }
// lets find divergingProperties // lets find divergingProperties
if (JSON.stringify(referenceObjectArg[currentProperty]) !== JSON.stringify(comparisonObjectArg[currentProperty])) { if (
JSON.stringify(referenceObjectArg[currentProperty]) !==
JSON.stringify(comparisonObjectArg[currentProperty])
) {
returnComparisonObject.divergingProperties.push(currentProperty); returnComparisonObject.divergingProperties.push(currentProperty);
} }
// lets find equalProperties // lets find equalProperties
if (JSON.stringify(referenceObjectArg[currentProperty]) === JSON.stringify(comparisonObjectArg[currentProperty])) { if (
JSON.stringify(referenceObjectArg[currentProperty]) ===
JSON.stringify(comparisonObjectArg[currentProperty])
) {
returnComparisonObject.equalProperties.push(currentProperty); returnComparisonObject.equalProperties.push(currentProperty);
} }
} }
for (const currentProperty of Object.keys(returnComparisonObject)) { for (const currentProperty of Object.keys(returnComparisonObject)) {
const propertySet = new Set(returnComparisonObject[currentProperty]); const onlyUnique = (value: any, index: number, self: Array<any>) => {
const uniqueArray = [...propertySet]; return self.indexOf(value) === index;
returnComparisonObject[currentProperty] = uniqueArray; };
const uniqueArray = returnComparisonObject[currentProperty as keyof(typeof returnComparisonObject)].filter(onlyUnique);
returnComparisonObject[currentProperty as keyof(typeof returnComparisonObject)] = uniqueArray;
} }
return returnComparisonObject; return returnComparisonObject;
}; };
/**
* 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;
};
/**
* 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;
};
/**
* 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]);
}
};

View File

@@ -1,2 +1,7 @@
const removeme = {};
export { removeme }; // thirdparty scope
// tslint:disable-next-line: no-submodule-imports
import fastDeepEqual from 'fast-deep-equal/es6';
import minimatch from 'minimatch';
export { fastDeepEqual, minimatch };