Skip to content

Commit

Permalink
Functions for working with cyclic objects
Browse files Browse the repository at this point in the history
  • Loading branch information
vbabak committed Feb 4, 2024
1 parent 31b0dac commit 4b7684f
Show file tree
Hide file tree
Showing 23 changed files with 3,667 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
.github
**/*.js
25 changes: 25 additions & 0 deletions .eslintrc.json
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"
}
}
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
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
43 changes: 43 additions & 0 deletions .github/workflows/publish.yml
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 }}
3 changes: 3 additions & 0 deletions .npmrc.tpl
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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 TSXPER
Copyright (c) 2024 Vladyslav Babak

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
28 changes: 28 additions & 0 deletions Makefile
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
59 changes: 57 additions & 2 deletions README.md
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()*.
9 changes: 9 additions & 0 deletions esm_fix.sh
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

0 comments on commit 4b7684f

Please sign in to comment.