Compare commits
No commits in common. "master" and "v1.0.3" have entirely different histories.
@ -1,66 +0,0 @@
|
||||
name: Default (not tags)
|
||||
|
||||
on:
|
||||
push:
|
||||
tags-ignore:
|
||||
- '**'
|
||||
|
||||
env:
|
||||
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
|
||||
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||
|
||||
jobs:
|
||||
security:
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: true
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Install pnpm and npmci
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
|
||||
- name: Run npm prepare
|
||||
run: npmci npm prepare
|
||||
|
||||
- name: Audit production dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --prod
|
||||
continue-on-error: true
|
||||
|
||||
- name: Audit development dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --dev
|
||||
continue-on-error: true
|
||||
|
||||
test:
|
||||
if: ${{ always() }}
|
||||
needs: security
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Test stable
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm test
|
||||
|
||||
- name: Test build
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm build
|
@ -1,124 +0,0 @@
|
||||
name: Default (tags)
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- '*'
|
||||
|
||||
env:
|
||||
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
|
||||
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||
|
||||
jobs:
|
||||
security:
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: true
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Audit production dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --prod
|
||||
continue-on-error: true
|
||||
|
||||
- name: Audit development dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --dev
|
||||
continue-on-error: true
|
||||
|
||||
test:
|
||||
if: ${{ always() }}
|
||||
needs: security
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Test stable
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm test
|
||||
|
||||
- name: Test build
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm build
|
||||
|
||||
release:
|
||||
needs: test
|
||||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Release
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm publish
|
||||
|
||||
metadata:
|
||||
needs: test
|
||||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
continue-on-error: true
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Code quality
|
||||
run: |
|
||||
npmci command npm install -g typescript
|
||||
npmci npm install
|
||||
|
||||
- name: Trigger
|
||||
run: npmci trigger
|
||||
|
||||
- name: Build docs and upload artifacts
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
pnpm install -g @gitzone/tsdoc
|
||||
npmci command tsdoc
|
||||
continue-on-error: true
|
@ -19,35 +19,23 @@ mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
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:
|
||||
audit:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=dev
|
||||
- npmci command npm audit --audit-level=high
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
allow_failure: true
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
@ -62,7 +50,9 @@ testStable:
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
@ -73,7 +63,9 @@ testBuild:
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
@ -93,8 +85,6 @@ release:
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm prepare
|
||||
|
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@ -2,10 +2,28 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"command": "npm test",
|
||||
"name": "Run npm test",
|
||||
"name": "current file",
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"type": "node-terminal"
|
||||
"args": [
|
||||
"${relativeFile}"
|
||||
],
|
||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"protocol": "inspector",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
},
|
||||
{
|
||||
"name": "test.ts",
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"args": [
|
||||
"test/test.ts"
|
||||
],
|
||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"protocol": "inspector",
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -15,7 +15,7 @@
|
||||
"properties": {
|
||||
"projectType": {
|
||||
"type": "string",
|
||||
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||
"enum": ["website", "element", "service", "npm"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,29 +2,17 @@
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "push.rocks",
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
"gitrepo": "smartobject",
|
||||
"description": "A library to simplify and enhance object manipulation and traversal.",
|
||||
"npmPackagename": "@push.rocks/smartobject",
|
||||
"shortDescription": "work with objects",
|
||||
"npmPackagename": "@pushrocks/smartobject",
|
||||
"license": "MIT",
|
||||
"projectDomain": "push.rocks",
|
||||
"keywords": [
|
||||
"object manipulation",
|
||||
"deep traversal",
|
||||
"typescript",
|
||||
"object comparison",
|
||||
"property matching",
|
||||
"dynamic property access",
|
||||
"object flattening"
|
||||
]
|
||||
"projectDomain": "push.rocks"
|
||||
}
|
||||
},
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"tsdoc": {
|
||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||
}
|
||||
}
|
10869
package-lock.json
generated
Normal file
10869
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
44
package.json
44
package.json
@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "@push.rocks/smartobject",
|
||||
"version": "1.0.12",
|
||||
"name": "@pushrocks/smartobject",
|
||||
"version": "1.0.3",
|
||||
"private": false,
|
||||
"description": "A library to simplify and enhance object manipulation and traversal.",
|
||||
"description": "work with objects",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"author": "Lossless GmbH",
|
||||
@ -10,20 +10,17 @@
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild --web)",
|
||||
"format": "(gitzone format)",
|
||||
"buildDocs": "tsdoc"
|
||||
"format": "(gitzone format)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.66",
|
||||
"@gitzone/tsrun": "^1.2.42",
|
||||
"@gitzone/tstest": "^1.0.74",
|
||||
"@push.rocks/tapbundle": "^5.0.8",
|
||||
"@types/node": "^20.4.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"fast-deep-equal": "^3.1.3",
|
||||
"minimatch": "^9.0.3"
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tstest": "^1.0.15",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^10.11.7",
|
||||
"tslint": "^5.11.0",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
},
|
||||
"dependencies": {},
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
],
|
||||
@ -38,20 +35,5 @@
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"type": "module",
|
||||
"keywords": [
|
||||
"object manipulation",
|
||||
"deep traversal",
|
||||
"typescript",
|
||||
"object comparison",
|
||||
"property matching",
|
||||
"dynamic property access",
|
||||
"object flattening"
|
||||
],
|
||||
"homepage": "https://code.foss.global/push.rocks/smartobject",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://code.foss.global/push.rocks/smartobject.git"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
4727
pnpm-lock.yaml
generated
4727
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -1 +0,0 @@
|
||||
|
145
readme.md
145
readme.md
@ -1,130 +1,39 @@
|
||||
# @push.rocks/smartobject
|
||||
# @pushrocks/smartobject
|
||||
work with objects
|
||||
|
||||
## Install
|
||||
To install `@push.rocks/smartobject`, use the following npm command. It's recommended to install this package in a TypeScript project to make full use of its functionalities and type definitions.
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartobject)
|
||||
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartobject)
|
||||
* [github.com (source mirror)](https://github.com/pushrocks/smartobject)
|
||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartobject/)
|
||||
|
||||
```
|
||||
npm install @push.rocks/smartobject --save
|
||||
```
|
||||
## Status for master
|
||||
|
||||
This command will add `@push.rocks/smartobject` to your project's `package.json` file and download the package to the `node_modules` directory.
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||
|
||||
## Usage
|
||||
This module provides a convenient way to work with JavaScript objects, offering utilities for deep object manipulation, comparison, and more. Below are detailed examples of how to utilize `@push.rocks/smartobject` in a variety of use cases. Always use ESM syntax in combination with TypeScript for the best developer experience.
|
||||
|
||||
### Importing the Module
|
||||
use TypeScript for best in class intellisense
|
||||
|
||||
Before you can use the functionalities provided by `smartobject`, you need to import the required classes or functions into your TypeScript file.
|
||||
## Contribution
|
||||
|
||||
```typescript
|
||||
import {
|
||||
SmartObject,
|
||||
fastDeepEqual,
|
||||
smartGet,
|
||||
smartAdd,
|
||||
compareObjects,
|
||||
forEachMinimatch
|
||||
} from '@push.rocks/smartobject';
|
||||
```
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
|
||||
### Working with SmartObject
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
The `SmartObject` class is a wrapper around a standard JavaScript object that provides additional capabilities for manipulating and querying the object.
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
|
||||
#### Creating a SmartObject
|
||||
|
||||
```typescript
|
||||
// Original JavaScript object
|
||||
const originalObject = {
|
||||
level1: {
|
||||
level2: 'value',
|
||||
},
|
||||
};
|
||||
|
||||
// Creating a SmartObject
|
||||
const smartObj = new SmartObject(originalObject);
|
||||
```
|
||||
|
||||
#### Adding and Retrieving Values
|
||||
|
||||
```typescript
|
||||
// Adding a new value
|
||||
smartObj.addValueAtFlatPathString('level1.newKey', 'newValue');
|
||||
|
||||
// Retrieving a value
|
||||
const value = smartObj.getValueAtFlatPathString('level1.level2');
|
||||
console.log(value); // Output: 'value'
|
||||
```
|
||||
|
||||
#### Converting to a Flat Object
|
||||
|
||||
```typescript
|
||||
const flatObject = smartObj.toFlatObject();
|
||||
console.log(flatObject); // Output: { 'level1.level2': 'value', 'level1.newKey': 'newValue' }
|
||||
```
|
||||
|
||||
### Deep Object Comparison
|
||||
|
||||
`compareObjects` allows you to compare two objects and get detailed information about their differences.
|
||||
|
||||
```typescript
|
||||
const obj1 = {name: 'John', age: 30};
|
||||
const obj2 = {name: 'John', age: 31};
|
||||
|
||||
const comparisonResult = compareObjects(obj1, obj2);
|
||||
console.log(comparisonResult);
|
||||
```
|
||||
|
||||
### Deep Object Manipulation
|
||||
|
||||
`smartAdd` and `smartGet` enable deep manipulation and querying of objects using dot notation paths.
|
||||
|
||||
```typescript
|
||||
// Using smartAdd to add a deep-nested property
|
||||
const obj = {};
|
||||
smartAdd(obj, 'user.info.name', 'Jane Doe');
|
||||
|
||||
// Using smartGet to retrieve a deep-nested property
|
||||
const name = smartGet(obj, 'user.info.name');
|
||||
console.log(name); // Output: Jane Doe
|
||||
```
|
||||
|
||||
### Iterating over Object Properties with Minimatch Patterns
|
||||
|
||||
`forEachMinimatch` provides a way to execute a function for each object property that matches a given minimatch pattern.
|
||||
|
||||
```typescript
|
||||
const userObj = {
|
||||
'user.name': 'John Doe',
|
||||
'user.age': 30,
|
||||
'config.theme': 'dark',
|
||||
};
|
||||
|
||||
forEachMinimatch(userObj, 'user.*', (value, key) => {
|
||||
console.log(key, value);
|
||||
});
|
||||
```
|
||||
|
||||
This module leverages the power of TypeScript to offer robust typing and intelligent auto-completion, making it easier to work with complex objects and avoid common pitfalls. Whether you are manipulating deeply nested properties, comparing object differences, or iterating over specific object keys, `@push.rocks/smartobject` provides a versatile toolkit to streamline your object handling logic.
|
||||
|
||||
For a complete list of features and functionalities, including additional utility functions not covered in these examples, please refer to the module's documentation and type definitions.
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
|
||||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||
|
||||
### Trademarks
|
||||
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
|
||||
### Company Information
|
||||
|
||||
Task Venture Capital GmbH
|
||||
Registered at District court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||
[](https://maintainedby.lossless.com)
|
||||
|
@ -1,69 +0,0 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import * as smartobject from '../ts/index.js';
|
||||
|
||||
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();
|
@ -1,24 +0,0 @@
|
||||
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
|
||||
import * as smartobject from '../ts/index.js';
|
||||
|
||||
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();
|
12
test/test.ts
Normal file
12
test/test.ts
Normal file
@ -0,0 +1,12 @@
|
||||
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();
|
@ -1,8 +0,0 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartobject',
|
||||
version: '1.0.12',
|
||||
description: 'work with objects'
|
||||
}
|
73
ts/index.ts
73
ts/index.ts
@ -1,6 +1,69 @@
|
||||
import * as plugins from './smartobject.plugins.js';
|
||||
const fastDeepEqual = plugins.fastDeepEqual;
|
||||
export { fastDeepEqual };
|
||||
import * as plugins from './smartobject.plugins';
|
||||
|
||||
export * from './smartobject.classes.smartobject.js';
|
||||
export * from './tools/index.js';
|
||||
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 propertySet = new Set(returnComparisonObject[currentProperty]);
|
||||
const uniqueArray = [...propertySet];
|
||||
returnComparisonObject[currentProperty] = uniqueArray;
|
||||
}
|
||||
|
||||
return returnComparisonObject;
|
||||
};
|
||||
|
@ -1,24 +0,0 @@
|
||||
import * as tools from './tools/index.js';
|
||||
|
||||
/**
|
||||
* 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,6 +1,2 @@
|
||||
// thirdparty scope
|
||||
// tslint:disable-next-line: no-submodule-imports
|
||||
import fastDeepEqual from 'fast-deep-equal/es6/index.js';
|
||||
import * as minimatch from 'minimatch';
|
||||
|
||||
export { fastDeepEqual, minimatch };
|
||||
const removeme = {};
|
||||
export { removeme };
|
||||
|
@ -1,85 +0,0 @@
|
||||
import * as plugins from '../smartobject.plugins.js';
|
||||
|
||||
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;
|
||||
};
|
@ -1,14 +0,0 @@
|
||||
import * as plugins from '../smartobject.plugins.js';
|
||||
|
||||
/**
|
||||
* 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;
|
||||
};
|
@ -1,21 +0,0 @@
|
||||
import * as plugins from '../smartobject.plugins.js';
|
||||
|
||||
/**
|
||||
* 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.minimatch(propertyNameArg, wildcardArg);
|
||||
});
|
||||
for (let propertyNameArg of propertyNamesMatched) {
|
||||
await callbackArg(parentObjectArg[propertyNameArg]);
|
||||
}
|
||||
};
|
@ -1,5 +0,0 @@
|
||||
export * from './compareobjects.js';
|
||||
export * from './exists.js';
|
||||
export * from './foreachminimatch.js';
|
||||
export * from './smart_add_get.js';
|
||||
export * from './toFlatObject.js';
|
@ -1,92 +0,0 @@
|
||||
import * as plugins from '../smartobject.plugins.js';
|
||||
|
||||
/**
|
||||
* 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;
|
||||
};
|
@ -1,29 +0,0 @@
|
||||
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;
|
||||
};
|
@ -1,14 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
}
|
17
tslint.json
Normal file
17
tslint.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user