Compare commits
43 Commits
Author | SHA1 | Date | |
---|---|---|---|
cbc7e4ebce | |||
097f54a74c | |||
807624d157 | |||
9d7e2e03d9 | |||
9686caff18 | |||
9260d653a1 | |||
28cd849d0d | |||
89a399bf42 | |||
16048fe96d | |||
04d60333a9 | |||
c30f1f4ab9 | |||
fc91dd3171 | |||
55f8951732 | |||
92f212e9d3 | |||
1a22db26da | |||
0d3bcab40f | |||
e11ace6a23 | |||
f29b632bd2 | |||
25f80b6b59 | |||
176ac34504 | |||
2707542234 | |||
ee025d094c | |||
9bc1c4bf93 | |||
5176a34575 | |||
fe9f9299fc | |||
62cf2fd262 | |||
65e581d48b | |||
4ff97294bb | |||
2d5bd6a2f0 | |||
a2649b7854 | |||
b901f8604b | |||
d70310ce8b | |||
e0e9ceed31 | |||
e3add99efe | |||
91d57c84f4 | |||
37b32d5d5d | |||
f6199740aa | |||
215cf77205 | |||
6e2c86dbb4 | |||
a5b9bbd376 | |||
f6a9810e71 | |||
32423f5475 | |||
5e945ddad6 |
18
.gitignore
vendored
18
.gitignore
vendored
@ -1,4 +1,20 @@
|
|||||||
node_modules/
|
.nogit/
|
||||||
|
|
||||||
|
# artifacts
|
||||||
coverage/
|
coverage/
|
||||||
public/
|
public/
|
||||||
pages/
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_*/
|
||||||
|
|
||||||
|
# custom
|
151
.gitlab-ci.yml
151
.gitlab-ci.yml
@ -1,16 +1,19 @@
|
|||||||
# gitzone standard
|
# gitzone ci_default
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
- .npmci_cache/
|
- .npmci_cache/
|
||||||
key: "$CI_BUILD_STAGE"
|
key: '$CI_BUILD_STAGE'
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- security
|
- security
|
||||||
- test
|
- test
|
||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- npm install -g @shipzone/npmci
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
@ -18,109 +21,113 @@ stages:
|
|||||||
mirror:
|
mirror:
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci git mirror
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
snyk:
|
auditProductionDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g snyk
|
- npmci npm prepare
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --production --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=prod --production
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
allow_failure: true
|
||||||
|
|
||||||
|
auditDevDependencies:
|
||||||
|
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
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
testLEGACY:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci node install legacy
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
testLTS:
|
testStable:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci node install lts
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci node install stable
|
||||||
- npmci npm test
|
- npmci npm install
|
||||||
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
testBuild:
|
||||||
testSTABLE:
|
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci node install stable
|
||||||
- npmci npm test
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# metadata stage
|
# metadata stage
|
||||||
# ====================
|
# ====================
|
||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
image: docker:stable
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
services:
|
only:
|
||||||
- docker:stable-dind
|
- tags
|
||||||
script:
|
script:
|
||||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
- npmci command npm install -g typescript
|
||||||
- docker run
|
- npmci npm prepare
|
||||||
--env SOURCE_CODE="$PWD"
|
- npmci npm install
|
||||||
--volume "$PWD":/code
|
|
||||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
|
||||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
|
||||||
artifacts:
|
|
||||||
paths: [codeclimate.json]
|
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- priv
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci trigger
|
- npmci trigger
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g typedoc typescript
|
- npmci node install stable
|
||||||
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
- npmci command npm run buildDocs
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
only:
|
only:
|
||||||
@ -128,15 +135,5 @@ pages:
|
|||||||
artifacts:
|
artifacts:
|
||||||
expire_in: 1 week
|
expire_in: 1 week
|
||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
windowsCompatibility:
|
|
||||||
image: stefanscherer/node-windows:10-build-tools
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npm install & npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- windows
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
4
.snyk
Normal file
4
.snyk
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
|
||||||
|
version: v1.13.3
|
||||||
|
ignore: {}
|
||||||
|
patch: {}
|
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"command": "npm test",
|
||||||
|
"name": "Run npm test",
|
||||||
|
"request": "launch",
|
||||||
|
"type": "node-terminal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"json.schemas": [
|
||||||
|
{
|
||||||
|
"fileMatch": ["/npmextra.json"],
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"npmci": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for npmci"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for gitzone",
|
||||||
|
"properties": {
|
||||||
|
"projectType": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
56
README.md
56
README.md
@ -1,56 +0,0 @@
|
|||||||
# smartjson
|
|
||||||
|
|
||||||
typed json handlers
|
|
||||||
|
|
||||||
## Availabililty
|
|
||||||
|
|
||||||
[](https://www.npmjs.com/package/smartjson)
|
|
||||||
[](https://GitLab.com/pushrocks/smartjson)
|
|
||||||
[](https://github.com/pushrocks/smartjson)
|
|
||||||
[](https://pushrocks.gitlab.io/smartjson/)
|
|
||||||
|
|
||||||
## Status for master
|
|
||||||
|
|
||||||
[](https://GitLab.com/pushrocks/smartjson/commits/master)
|
|
||||||
[](https://GitLab.com/pushrocks/smartjson/commits/master)
|
|
||||||
[](https://www.npmjs.com/package/smartjson)
|
|
||||||
[](https://david-dm.org/pushrocks/smartjson)
|
|
||||||
[](https://www.bithound.io/github/pushrocks/smartjson/master/dependencies/npm)
|
|
||||||
[](https://www.bithound.io/github/pushrocks/smartjson)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](http://standardjs.com/)
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
|
||||||
|
|
||||||
### Classes
|
|
||||||
|
|
||||||
SmartJson makes it easy to fold and enfold classes into and from JSON
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
import { Foldable, foldDec } from 'smartjson';
|
|
||||||
|
|
||||||
class AwesomeClass extends Foldable {
|
|
||||||
computedValue: string;
|
|
||||||
|
|
||||||
@foldDec() // mark anotherValueToStore as foldable
|
|
||||||
anotherValueToStore: string = null;
|
|
||||||
constructor() {
|
|
||||||
super(); // this is important
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let myAwesomeInstance = new AwesomeClass();
|
|
||||||
let foldedObject = myAwesomeInstance.foldToObject(); // will return {anotherValueToStore: null}
|
|
||||||
myAwesomeInstance.enfoldFromObject({ anotherValueToStore: 'hi' });
|
|
||||||
foldedObject = myAwesomeInstance.foldToObject(); // will return {anotherValueToStore: 'hi'}
|
|
||||||
```
|
|
||||||
|
|
||||||
For further information read the linked docs at the top of this README.
|
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
|
||||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
|
||||||
|
|
||||||
[](https://push.rocks)
|
|
@ -1,5 +1,16 @@
|
|||||||
{
|
{
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "pushrocks",
|
||||||
|
"gitrepo": "smartjson",
|
||||||
|
"description": "typed json handlers",
|
||||||
|
"npmPackagename": "@pushrocks/smartjson",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
10758
package-lock.json
generated
10758
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
47
package.json
47
package.json
@ -1,13 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartjson",
|
"name": "@pushrocks/smartjson",
|
||||||
"version": "2.0.2",
|
"version": "5.0.0",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "typed json handlers",
|
"description": "typed json handlers",
|
||||||
"main": "dist/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/)",
|
"test": "(tstest test/ --web)",
|
||||||
"build": "(tsbuild)"
|
"build": "(tsbuild --web --allowimplicitany)",
|
||||||
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -20,13 +21,35 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartjson#README",
|
"homepage": "https://gitlab.com/pushrocks/smartjson#README",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.0.22",
|
"@gitzone/tsbuild": "^2.1.63",
|
||||||
"@gitzone/tsrun": "^1.1.12",
|
"@gitzone/tsrun": "^1.2.35",
|
||||||
"@gitzone/tstest": "^1.0.15",
|
"@gitzone/tstest": "^1.0.71",
|
||||||
"@pushrocks/tapbundle": "^3.0.1",
|
"@pushrocks/tapbundle": "^5.0.3",
|
||||||
"@types/node": "^10.9.4"
|
"@types/node": "^17.0.41",
|
||||||
|
"tslint": "^6.1.3",
|
||||||
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"lodash": "^4.17.10"
|
"@types/buffer-json": "^2.0.1",
|
||||||
}
|
"@types/fast-json-stable-stringify": "^2.1.0",
|
||||||
|
"buffer-json": "^2.0.0",
|
||||||
|
"fast-json-stable-stringify": "^2.1.0",
|
||||||
|
"lodash.clonedeep": "^4.5.0"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"ts/**/*",
|
||||||
|
"ts_web/**/*",
|
||||||
|
"dist/**/*",
|
||||||
|
"dist_*/**/*",
|
||||||
|
"dist_ts/**/*",
|
||||||
|
"dist_ts_web/**/*",
|
||||||
|
"assets/**/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
],
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
|
],
|
||||||
|
"type": "module"
|
||||||
}
|
}
|
||||||
|
66
readme.md
Normal file
66
readme.md
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
# @pushrocks/smartjson
|
||||||
|
typed json handlers
|
||||||
|
|
||||||
|
## Availabililty and Links
|
||||||
|
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartjson)
|
||||||
|
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartjson)
|
||||||
|
* [github.com (source mirror)](https://github.com/pushrocks/smartjson)
|
||||||
|
* [docs (typedoc)](https://pushrocks.gitlab.io/smartjson/)
|
||||||
|
|
||||||
|
## Status for master
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
|
### Classes
|
||||||
|
|
||||||
|
SmartJson makes it easy to fold and enfold classes into and from JSON
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import { Smartjson, foldDec } from 'smartjson';
|
||||||
|
|
||||||
|
class AwesomeClass extends Smartjson {
|
||||||
|
static stringify;
|
||||||
|
|
||||||
|
computedValue: string;
|
||||||
|
|
||||||
|
@foldDec() // mark anotherValueToStore as foldable
|
||||||
|
anotherValueToStore: string = null;
|
||||||
|
constructor() {
|
||||||
|
super(); // this is important
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let myAwesomeInstance = new AwesomeClass();
|
||||||
|
let foldedObject = myAwesomeInstance.foldToObject(); // will return {anotherValueToStore: null}
|
||||||
|
// You can also fold to Json, with support for buffers
|
||||||
|
// const jsonString = myAwesomeInstance.foldToJson();
|
||||||
|
myAwesomeInstance = AwesomeClass.enfoldFromObject({ anotherValueToStore: 'hi' });
|
||||||
|
foldedObject = myAwesomeInstance.foldToObject(); // will return {anotherValueToStore: 'hi'}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contribution
|
||||||
|
|
||||||
|
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). :)
|
||||||
|
|
||||||
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||||
|
|
||||||
|
[](https://maintainedby.lossless.com)
|
43
test/test.both.ts
Normal file
43
test/test.both.ts
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
|
import * as smartjson from '../ts/index.js';
|
||||||
|
|
||||||
|
class SomeClass extends smartjson.Smartjson {
|
||||||
|
@smartjson.foldDec() thisis: string = 'test';
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
console.log(this.saveableProperties);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mySomeClass: SomeClass;
|
||||||
|
|
||||||
|
tap.test('should create a Foldable extended instance', async () => {
|
||||||
|
mySomeClass = new SomeClass();
|
||||||
|
expect(mySomeClass).toBeInstanceOf(SomeClass);
|
||||||
|
expect(mySomeClass).toBeInstanceOf(smartjson.Smartjson);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should create a folded object', async () => {
|
||||||
|
let foldedObject = mySomeClass.foldToObject();
|
||||||
|
expect(foldedObject).property('thisis').toEqual('test');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should enfold from object', async () => {
|
||||||
|
const mySomeClass2 = SomeClass.enfoldFromObject({ thisis: 'test2' });
|
||||||
|
expect(mySomeClass2).property('thisis').toEqual('test2');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should products stable jsons', async () => {
|
||||||
|
const jsonString = smartjson.stringify({
|
||||||
|
a: 1,
|
||||||
|
f: 6,
|
||||||
|
b: 3,
|
||||||
|
c: 3,
|
||||||
|
e: 5,
|
||||||
|
d: 4,
|
||||||
|
});
|
||||||
|
console.log(jsonString);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
35
test/test.ts
35
test/test.ts
@ -1,35 +0,0 @@
|
|||||||
import { tap, expect } from "@pushrocks/tapbundle";
|
|
||||||
|
|
||||||
import { Foldable, foldDec } from "../ts/index";
|
|
||||||
|
|
||||||
class SomeClass extends Foldable {
|
|
||||||
@foldDec() thisis: string = "test";
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
console.log(this.saveableProperties);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mySomeClass: SomeClass;
|
|
||||||
|
|
||||||
tap.test("should create a Foldable extended instance", async () => {
|
|
||||||
mySomeClass = new SomeClass();
|
|
||||||
expect(mySomeClass).to.be.instanceof(SomeClass);
|
|
||||||
expect(mySomeClass).to.be.instanceof(Foldable);
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test("should create a folded object", async () => {
|
|
||||||
let foldedObject = mySomeClass.foldToObject();
|
|
||||||
expect(foldedObject)
|
|
||||||
.property("thisis")
|
|
||||||
.to.equal("test");
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test("should enfold from object", async () => {
|
|
||||||
mySomeClass.enfoldFromObject({ thisis: "test2" });
|
|
||||||
expect(mySomeClass)
|
|
||||||
.property("thisis")
|
|
||||||
.to.equal("test2");
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.start()
|
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@pushrocks/smartjson',
|
||||||
|
version: '5.0.0',
|
||||||
|
description: 'typed json handlers'
|
||||||
|
}
|
88
ts/index.ts
88
ts/index.ts
@ -1,35 +1,87 @@
|
|||||||
let lodash = require('lodash');
|
import * as plugins from './smartjson.plugins.js';
|
||||||
|
|
||||||
export class Foldable {
|
/**
|
||||||
saveableProperties: string[];
|
* allows you to parse a json
|
||||||
|
*/
|
||||||
|
export const parse = plugins.bufferJson.parse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param objArg
|
||||||
|
* @param optionsArg
|
||||||
|
*/
|
||||||
|
export const stringify = (
|
||||||
|
objArg: any,
|
||||||
|
simpleOrderArray?: string[],
|
||||||
|
optionsArg: plugins.IStableJsonTypes['Options'] = {}
|
||||||
|
): string => {
|
||||||
|
const bufferedJson = plugins.bufferJson.stringify(objArg);
|
||||||
|
objArg = JSON.parse(bufferedJson);
|
||||||
|
let returnJson = plugins.stableJson(objArg, optionsArg);
|
||||||
|
return returnJson;
|
||||||
|
};
|
||||||
|
|
||||||
|
export class Smartjson {
|
||||||
|
/**
|
||||||
|
* enfolds data from an object
|
||||||
|
*/
|
||||||
|
public static enfoldFromObject(objectArg) {
|
||||||
|
const newInstance = new this();
|
||||||
|
for (const keyName in objectArg) {
|
||||||
|
if (newInstance.saveableProperties.indexOf(keyName) !== -1) {
|
||||||
|
newInstance[keyName] = objectArg[keyName];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* enfold from json
|
||||||
|
*/
|
||||||
|
public static enfoldFromJson(jsonArg: string) {
|
||||||
|
const objectFromJson = parse(jsonArg);
|
||||||
|
return this.enfoldFromObject(objectFromJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========
|
||||||
|
// INSTANCE
|
||||||
|
// ========
|
||||||
|
|
||||||
|
public saveableProperties: string[];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* folds a class into an object
|
* folds a class into an object
|
||||||
*/
|
*/
|
||||||
foldToObject() {
|
public foldToObject() {
|
||||||
let newFoldedObject = {};
|
const newFoldedObject: { [key: string]: any } = {};
|
||||||
for (let keyName of this.saveableProperties) {
|
const trackMap = [];
|
||||||
newFoldedObject[keyName] = lodash.cloneDeep(this[keyName]);
|
for (const keyName of this.saveableProperties) {
|
||||||
|
let value = this[keyName];
|
||||||
|
if (value instanceof Smartjson) {
|
||||||
|
if (trackMap.includes(value)) {
|
||||||
|
throw new Error('cycle detected');
|
||||||
|
}
|
||||||
|
trackMap.push(value);
|
||||||
|
value = value.foldToObject();
|
||||||
|
}
|
||||||
|
newFoldedObject[keyName] = plugins.lodashCloneDeep(value);
|
||||||
}
|
}
|
||||||
return newFoldedObject;
|
return newFoldedObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* enfolds data from an object
|
* folds a class into an object
|
||||||
*/
|
*/
|
||||||
enfoldFromObject(objectArg) {
|
public foldToJson() {
|
||||||
for (let keyName in objectArg) {
|
const foldedObject = this.foldToObject();
|
||||||
if (this.saveableProperties.indexOf(keyName) !== -1) {
|
return stringify(foldedObject);
|
||||||
this[keyName] = objectArg[keyName];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decorator that marks a property as foldable
|
* Decorator that marks a property as foldable
|
||||||
*/
|
*/
|
||||||
export let foldDec = () => {
|
export const foldDec = () => {
|
||||||
return (target: any, key: string) => {
|
return (target: any, key: string) => {
|
||||||
if (!target.saveableProperties) {
|
if (!target.saveableProperties) {
|
||||||
target.saveableProperties = [];
|
target.saveableProperties = [];
|
||||||
@ -37,3 +89,9 @@ export let foldDec = () => {
|
|||||||
target.saveableProperties.push(key);
|
target.saveableProperties.push(key);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const deepEqualObjects = (object1: any, object2: any): boolean => {
|
||||||
|
const object1String = stringify(object1);
|
||||||
|
const object2String = stringify(object2);
|
||||||
|
return object1String === object2String;
|
||||||
|
};
|
||||||
|
26
ts/smartjson.plugins.ts
Normal file
26
ts/smartjson.plugins.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// third party scope
|
||||||
|
import lodashCloneDeep from 'lodash.clonedeep';
|
||||||
|
import stableJson2 from 'fast-json-stable-stringify';
|
||||||
|
import bufferJson from 'buffer-json';
|
||||||
|
|
||||||
|
const stableJson = stableJson2 as any;
|
||||||
|
|
||||||
|
export { bufferJson, lodashCloneDeep, stableJson };
|
||||||
|
|
||||||
|
export interface IStableJsonTypes {
|
||||||
|
Comparator: (
|
||||||
|
a: IStableJsonTypes['CompareDescriptor'],
|
||||||
|
b: IStableJsonTypes['CompareDescriptor']
|
||||||
|
) => number;
|
||||||
|
CompareDescriptor: {
|
||||||
|
key: string;
|
||||||
|
value: any;
|
||||||
|
};
|
||||||
|
Options: {
|
||||||
|
cmp?: (
|
||||||
|
a: IStableJsonTypes['CompareDescriptor'],
|
||||||
|
b: IStableJsonTypes['CompareDescriptor']
|
||||||
|
) => number;
|
||||||
|
cycles?: boolean;
|
||||||
|
};
|
||||||
|
}
|
@ -1,7 +1,9 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"target": "es2016",
|
"useDefineForClassFields": false,
|
||||||
"module": "commonjs"
|
"target": "ES2022",
|
||||||
}
|
"module": "ES2022",
|
||||||
|
"moduleResolution": "nodenext"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
16
tslint.json
16
tslint.json
@ -1,3 +1,17 @@
|
|||||||
{
|
{
|
||||||
"extends": "tslint-config-standard"
|
"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"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user