-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Functions for working with cyclic objects
- Loading branch information
Showing
23 changed files
with
3,667 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
dist | ||
.github | ||
**/*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"es2021": true | ||
}, | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"ecmaVersion": "latest", | ||
"sourceType": "module" | ||
}, | ||
"plugins": [ | ||
"@typescript-eslint" | ||
], | ||
"rules": { | ||
"semi": ["error", "always"], | ||
"@typescript-eslint/semi": ["error", "always"], | ||
"no-console": ["error"], | ||
"no-control-regex": "off", | ||
"no-useless-escape": "off" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: CI | ||
on: | ||
pull_request: | ||
paths: | ||
- 'src/**' | ||
- 'package.json' | ||
jobs: | ||
ci: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node-version: ['18.x'] | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
|
||
- name: Install dependencies | ||
run: yarn install | ||
|
||
- name: Lint | ||
run: yarn lint | ||
|
||
- name: Build | ||
run: yarn build | ||
|
||
- name: Run tests | ||
run: yarn test:ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
name: Publish Package to npmjs | ||
on: | ||
push: | ||
branches: | ||
- 'main' | ||
paths: | ||
- 'package.json' | ||
jobs: | ||
tag: | ||
runs-on: ubuntu-latest | ||
env: | ||
CI_COMMIT_EMAIL: username@users.noreply.github.com | ||
CI_COMMIT_AUTHOR: CI Bot | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Get tag | ||
id: get_tag | ||
run: | | ||
echo "version=v$(npm pkg get version | tr -d '\"')" >> $GITHUB_OUTPUT | ||
- name: Tag the commit | ||
run: | | ||
git config --global user.name "${{ env.CI_COMMIT_AUTHOR }}" | ||
git config --global user.email "${{ env.CI_COMMIT_EMAIL }}" | ||
next_version=${{ steps.get_tag.outputs.version }} | ||
git tag -a "$next_version" -m "Version $next_version" | ||
git push origin "$next_version" | ||
publish: | ||
runs-on: ubuntu-latest | ||
needs: tag | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: '18.x' | ||
- name: Install dependencies | ||
run: yarn install | ||
- run: yarn lint | ||
- run: yarn build | ||
- run: yarn test:ci | ||
- run: yarn pub | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN} | ||
@tsxper:registry=https://registry.npmjs.org/ | ||
always-auth=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
OUT_BUILD := ./dist/ | ||
|
||
.PHONY: build | ||
build: clean mkdist | ||
npx tsc --declaration --emitDeclarationOnly --declarationDir ./dist/types | ||
npx tsc --project tsconfig.esm.json | ||
npx tsc --project tsconfig.cjs.json | ||
sh ./esm_fix.sh | ||
|
||
.PHONY: mkdist | ||
mkdist: | ||
mkdir -p ${OUT_BUILD} | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -rf ./dist | ||
|
||
.PHONY: prepub | ||
prepub: | ||
cp ./package.json ./dist | ||
cp ./LICENSE ./dist | ||
cp ./README.md ./dist | ||
cp ./.npmrc.tpl ./dist/.npmrc | ||
cp ./package.module.json ./dist/esm/package.json | ||
|
||
.PHONY: pub | ||
pub: prepub | ||
cd ./dist && npm publish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,57 @@ | ||
# cyclic-object | ||
Convert cyclic object to JSON | ||
# @tsxper/cyclic-object | ||
Check if given object has cyclic refs to a given depth. | ||
Replace circular references and convert cyclic objects to JSON. | ||
|
||
[![NPM Version](https://img.shields.io/npm/v/@tsxper/cyclic-object.svg?style=flat-square)](https://www.npmjs.com/package/@tsxper/cyclic-object) | ||
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](LICENSE) | ||
![npm type definitions](https://img.shields.io/npm/types/@tsxper/cyclic-object) | ||
[![NPM Downloads](https://img.shields.io/npm/dt/@tsxper/cyclic-object.svg?style=flat-square)](https://www.npmjs.com/package/@tsxper/cyclic-object) | ||
|
||
## Usage | ||
|
||
```bash | ||
npm i @tsxper/cyclic-object | ||
``` | ||
|
||
Configure object with cyclic ref. | ||
```TypeScript | ||
class A { | ||
constructor(public a: A[]) { } | ||
} | ||
const a: A[] = []; | ||
const obj = new A(a); | ||
a.push(obj); | ||
``` | ||
|
||
Convert to JSON. | ||
```TypeScript | ||
import { toJSON } from '@tsxper/cyclic-object'; | ||
const jsonStr = toJSON(a); // '[{"a":"[Circular]"}]' | ||
``` | ||
|
||
Replace cyclic | ||
```TypeScript | ||
import { replaceCyclicRefs } from '@tsxper/cyclic-object'; | ||
const newObj = replaceCyclicRefs(a); // [{"a":"[Circular]"}] | ||
``` | ||
|
||
Detect cyclic | ||
```TypeScript | ||
import { hasCyclicRefs } from '@tsxper/cyclic-object'; | ||
const isCyclic = hasCyclicRefs(a); // true | ||
``` | ||
|
||
## Interfaces | ||
```TypeScript | ||
hasCyclicRefs: (obj: unknown, maxDepth?: number) => boolean; | ||
|
||
replaceCyclicRefs: (obj: unknown, maxDepth?: number, repl?: Partial<Replacements>) => unknown; | ||
|
||
toJSON: (obj: unknown, maxDepth?: number, cyclicMarkers?: string[], replacements?: Partial<Replacements>) => string; | ||
``` | ||
|
||
Where: | ||
- obj [required]: input. | ||
- maxDepth [optional]: starting from 0, -1 is disabled (default). | ||
- cyclicMarkers [optional]: list of strings that detects JSON.stringify TypeError is related to cyclic refs. By default calling *toJSON()* will try to convert input to json string. In case there is an error related to cyclic references than object will be normalized through calling *replaceCyclicRefs()*. | ||
- replacements [optional]: object, set custom replacements for 'array', 'object' and 'circular' when calling *replaceCyclicRefs()*. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/sh | ||
|
||
if [[ "$OSTYPE" == "darwin"* ]]; then | ||
find ./dist/esm -name "*.js" -exec sed -i '' -E "s#export (.*) from '\.(.*)';#export \1 from '.\2\.js';#g" {} +; | ||
find ./dist/esm -name "*.js" -exec sed -i '' -E "s#import (.*) from '\.(.*)';#import \1 from '.\2\.js';#g" {} +; | ||
else | ||
find ./dist/esm -name "*.js" -exec sed -i -E "s#export (.*) from '\.(.*)';#export \1 from '.\2\.js';#g" {} +; | ||
find ./dist/esm -name "*.js" -exec sed -i -E "s#import (.*) from '\.(.*)';#import \1 from '.\2\.js';#g" {} +; | ||
fi |
Oops, something went wrong.