Skip to content

Commit

Permalink
Initial code drop
Browse files Browse the repository at this point in the history
  • Loading branch information
b41ex committed Dec 28, 2024
1 parent 3873fd8 commit 874c6d9
Show file tree
Hide file tree
Showing 25 changed files with 5,541 additions and 1 deletion.
22 changes: 22 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
*.js text eol=lf
*.ts text eol=lf
*.md text eol=lf
*.yaml text eol=lf
*.html text eol=lf
*.json text eol=lf
*.sh text eol=lf
*.svg text eol=lf
*.cjs text eol=lf
.browserslistrc text eol=lf
*.mp4 binary
*.png binary
*.jpg binary
*.gif binary
*.vsdx binary
*.ttf binary
*.eot binary
*.woff binary
*.woff2 binary
.gitignore text eol=lf
.npmignore text eol=lf
.gitattributes text eol=lf
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules/
.env
.vscode/
dist/
.vuepress
temp
browser
coverage
.DS_store/
*.iml
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!dist/**/*
73 changes: 73 additions & 0 deletions CODE-OF-CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Code of Conduct

This repository is governed by following code of conduct guidelines.

We put collaboration, trust, respect and transparency as core values for our community.
Our community welcomes participants from all over the world with different experience,
opinion and ideas to share.

We have adopted this code of conduct and require all contributors to agree with that to build a healthy,
safe and productive community for all.

The guideline is aimed to support a community where all people should feel safe to participate,
introduce new ideas and inspire others, regardless of:

* Age
* Gender
* Gender identity or expression
* Family status
* Marital status
* Ability
* Ethnicity
* Race
* Sex characteristics
* Sexual identity and orientation
* Education
* Native language
* Background
* Caste
* Religion
* Geographic location
* Socioeconomic status
* Personal appearance
* Any other dimension of diversity

## Our Standards

We are welcoming the following behavior:

* Be respectful for different ideas, opinions and points of view
* Be constructive and professional
* Use inclusive language
* Be collaborative and show the empathy
* Focus on the best results for the community

The following behavior is unacceptable:

* Violence, threats of violence, or inciting others to commit self-harm
* Personal attacks, trolling, intentionally spreading misinformation, insulting/derogatory comments
* Public or private harassment
* Publishing others' private information, such as a physical or electronic address, without explicit permission
* Derogatory language
* Encouraging unacceptable behavior
* Other conduct which could reasonably be considered inappropriate in a professional community

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of the Code of Conduct
and are expected to take appropriate actions in response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or reject comments,
commits, code, wiki edits, issues, and other contributions that are not aligned
to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors
that they deem inappropriate, threatening, offensive, or harmful.

## Reporting

If you believe you’re experiencing unacceptable behavior that will not be tolerated as outlined above,
please report to `[email protected]`. All complaints will be reviewed and investigated and will result in a response
that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality
with regard to the reporter of an incident.

Please also report if you observe a potentially dangerous situation, someone in distress, or violations of these guidelines,
even if the situation is not happening to you.
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Contribution Guide

We'd love to accept patches and contributions to this project.
Please, follow these guidelines to make the contribution process easy and effective for everyone involved.

## Contributor License Agreement

You must sign the [Contributor License Agreement](https://pages.netcracker.com/cla-main.html) in order to contribute.

## Code of Conduct

Please make sure to read and follow the [Code of Conduct](CODE-OF-CONDUCT.md).

1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Expand Down
7 changes: 7 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
qubership-apihub-json-crawl
Copyright 2024-2025 NetCracker Technology Corporation

This product includes software developed by https://github.com/udamir
https://github.com/udamir/json-crawl
MIT License
Copyright (c) 2022 Damir Yusipov
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
# qubership-apihub-json-crawl
# qubership-apihub-json-crawl

This package provides utility functions for crawling/cloning json objects like a tree

## Modifications
Modified version of [json-crawl](https://github.com/udamir/json-crawl)

- responsibility of dealing with cycled JSO moved back from json-crawl to client code
- introduced afterHooks hook
- small fixes in typings

## Purpose

The purpose of this package is to simplify the traversal and manipulation of complex JSON objects in a tree-like
structure. It provides functions that allow you to iterate over each node of a JSON object, perform custom operations,
and clone objects deeply while maintaining independence between the original and cloned objects.
You can use `crawl`/`syncCrawl` to traverse a JSON object and perform custom operations, such as logging, data
transformation, or validation, at each node. The hooks allow you to customize the behavior according to your specific
requirements.

## Features

- **Crawling**: The `crawl`/`syncCrawl` function allows you to traverse a JSON object, performing custom operations at
each node using hooks.
- **Cloning**: The `clone`/`syncClone` function creates a deep copy of a JSON object, ensuring that nested objects and
arrays are also cloned.
- **Customizable Hooks**: Both `crawl` and `clone` functions accept hooks, allowing you to provide custom logic for each
node during crawling or cloning. Hooks can modify values, state, rules or perform any desired operations.
- **Support for Async Hooks**: The `crawl` and `clone` functions supports asynchronous hooks, allowing you to perform
asynchronous operations during crawling.

## Contributing

When contributing, keep in mind that it is an objective of `json-crawl` to have no package dependencies. This may change
in the future, but for now, no-dependencies.

Please run the unit tests before submitting your PR: `npm test`. Hopefully your PR includes additional unit tests to
illustrate your change/modification!
16 changes: 16 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Security Reporting Process

Please, report any security issue to `[email protected]` where the issue will be triaged appropriately.

If you know of a publicly disclosed security vulnerability please IMMEDIATELY email `[email protected]`
to inform the team about the vulnerability, so we may start the patch, release, and communication process.

# Security Release Process

If the vulnerability is found in the latest stable release, then it would be fixed in patch version for that release.
E.g., issue is found in 2.5.0 release, then 2.5.1 version with a fix will be released.
By default, older versions will not have security releases.

If the issue doesn't affect any existing public releases, the fix for medium and high issues is performed
in a main branch before releasing a new version. For low priority issues the fix can be planned for future releases.

77 changes: 77 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"name": "@netcracker/qubership-apihub-json-crawl",
"version": "1.0.2",
"description": "Async and sync crawler for json object",
"module": "./dist/index.es.js",
"main": "./dist/index.cjs.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"exports": {
".": {
"import": "./dist/index.es.js",
"require": "./dist/index.cjs.js",
"types": "./dist/index.d.ts"
}
},
"scripts": {
"prebuild": "rimraf ./dist",
"build": "vite build",
"development:link": "yarn link",
"development:unlink": "yarn unlink",
"test": "jest --verbose",
"test:coverage": "jest --verbose --coverage",
"feature-start": "feature-start --featureName",
"feature-finish": "feature-finish",
"release-start": "release-start",
"release-finish": "release-finish"
},
"keywords": [
"json",
"crawler",
"crawl",
"deepClone",
"clone",
"hooks"
],
"license": "MIT",
"devDependencies": {
"@netcracker/qubership-apihub-npm-gitflow": "1.0.13",
"@types/jest": "^29.5.2",
"@types/node": "^20.8.10",
"jest": "^29.5.2",
"rimraf": "^5.0.5",
"rollup": "^2.79.1",
"rollup-plugin-filesize": "^9.1.0",
"rollup-plugin-progress": "^1.1.2",
"ts-jest": "^29.1.0",
"ts-node": "^10.7.0",
"tslint": "^6.1.2",
"vite": "4.5.0",
"vite-plugin-dts": "3.6.4",
"typescript": "^5.1.3"
},
"engines": {
"node": ">=14.0.0"
},
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/test/.*(\\.|/)(test|spec))\\.(ts?|tsx?|js?|jsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"modulePathIgnorePatterns": [
"<rootDir>/dist/"
],
"collectCoverage": true
},
"packageManager": "[email protected]"
}
50 changes: 50 additions & 0 deletions src/clone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { CloneHook, CloneState, CrawlParams, SyncCloneHook } from './types'
import { JSON_ROOT_KEY } from './types'
import { crawl, syncCrawl } from './crawl'
import { isObject } from './utils'

const createCloneHook = <T extends {}, R extends {} = {}>(): SyncCloneHook<T, R> => {
//do not inline cause need to separate hooks in debug
const cloneHook: SyncCloneHook<T, R> = ({ value, path, key, state }) => {
key = path.length ? key : JSON_ROOT_KEY
state.node[key] = isObject(value) ? (Array.isArray(value) ? [] : {}) : value
return { value, state: { ...state, node: state.node[key] } }
}
return cloneHook
}

export const clone = async <T extends {}, R extends {} = {}>(
data: unknown,
hooks: CloneHook<T, R> | CloneHook<T, R>[] | SyncCloneHook<T, R> | SyncCloneHook<T, R>[] = [],
params: CrawlParams<T, R> = {},
): Promise<unknown> => {
hooks = Array.isArray(hooks) ? hooks : [hooks]
const root: any = {}

const _params: CrawlParams<CloneState<T>, R> = {
state: { ...params.state ?? {} as T, root, node: root },
...params.rules ? { rules: params.rules } : {},
}

await crawl<CloneState<T>, R>(data, [...hooks, createCloneHook<T, R>()], _params)

return root[JSON_ROOT_KEY]
}

export const syncClone = <T extends {}, R extends {} = {}>(
data: unknown,
hooks: SyncCloneHook<T, R> | SyncCloneHook<T, R>[] = [],
params: CrawlParams<T, R> = {},
): unknown => {
hooks = Array.isArray(hooks) ? hooks : [hooks]
const root = { [JSON_ROOT_KEY]: undefined }

const _params: CrawlParams<CloneState<T>, R> = {
state: { ...params.state ?? {} as T, root, node: root },
...params.rules ? { rules: params.rules } : {},
}

syncCrawl<CloneState<T>, R>(data, [...hooks, createCloneHook<T, R>()], _params)

return root[JSON_ROOT_KEY]
}
Loading

0 comments on commit 874c6d9

Please sign in to comment.