Skip to content

Commit

Permalink
init: first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rubeniskov committed Nov 23, 2020
0 parents commit 287310a
Show file tree
Hide file tree
Showing 12 changed files with 836 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# EditorConfig is awesome: https://editorconfig.org/

# top-most EditorConfig file
root = true

[*.md]
trim_trailing_whitespace = false

[*.js]
trim_trailing_whitespace = true

# Unix-style newlines with a newline ending every file
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
insert_final_newline = true
max_line_length = 100
16 changes: 16 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"env": {
"browser": true,
"es6": true,
"node": true
},
"parserOptions": {
"ecmaVersion": 2018
},
"extends": "eslint:recommended",
"rules": {
"semi": [2, "always"],
"comma-dangle": ["error", "always-multiline"],
"no-multiple-empty-lines": "off"
}
}
17 changes: 17 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: npm-publish
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '10.x'
registry-url: 'https://registry.npmjs.org'
- run: npm install
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
node_modules
package-lock.json
.nyc_output
*.tgz
coverage
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.js
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
dist: xenial
language: node_js
cache: npm
os:
- linux
env:
global:
node_js:
- '10'
script:
- npm test
- npm run coverage
11 changes: 11 additions & 0 deletions HEADER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# traverse-json

[![Build Status](https://travis-ci.org/rubeniskov/traverse-json.svg?branch=master)](https://travis-ci.org/rubeniskov/traverse-json)
![npm-publish](https://github.com/rubeniskov/traverse-json/workflows/npm-publish/badge.svg)
[![Downloads](https://img.shields.io/npm/dw/traverse-json)](https://www.npmjs.com/package/traverse-json)

A complete traverse json function with `iterable` interface.

## Motivation

Many time I've encontered with the difficult task of mutate a object with an with nested properties by filtering properties using a single function, so a `traverse-json` solves this using multiple options for traversing.
210 changes: 210 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
# traverse-json

[![Build Status](https://travis-ci.org/rubeniskov/traverse-json.svg?branch=master)](https://travis-ci.org/rubeniskov/traverse-json)
![npm-publish](https://github.com/rubeniskov/traverse-json/workflows/npm-publish/badge.svg)
[![Downloads](https://img.shields.io/npm/dw/traverse-json)](https://www.npmjs.com/package/traverse-json)

A complete traverse json function with `iterable` interface.

## Motivation

Many time I've encontered with the difficult task of mutate a object with an with nested properties by filtering properties using a single function, so a `traverse-json` solves this using multiple options for traversing.
## Functions

<dl>
<dt><a href="#traverseJson">traverseJson(obj, [opts])</a> ⇒ <code><a href="#TraverseIterator">TraverseIterator</a></code></dt>
<dd><p>Create a function which traverses an object by its keys and values recursively</p>
</dd>
<dt><a href="#createIterator">createIterator(obj, [opts])</a> ⇒ <code>Iterable</code></dt>
<dd><p>Returns a traverseJson iterable, usefull for use it in a for loop.</p>
</dd>
</dl>

## Typedefs

<dl>
<dt><a href="#TraverseJsonOptions">TraverseJsonOptions</a> : <code>Object</code></dt>
<dd></dd>
<dt><a href="#TraverseIteratorResult">TraverseIteratorResult</a> : <code>Object</code></dt>
<dd></dd>
<dt><a href="#TraverseIterator">TraverseIterator</a> ⇒ <code><a href="#TraverseIteratorResult">TraverseIteratorResult</a></code></dt>
<dd></dd>
</dl>

<a name="traverseJson"></a>

## traverseJson(obj, [opts]) ⇒ [<code>TraverseIterator</code>](#TraverseIterator)
Create a function which traverses an object by its keys and values recursively

**Kind**: global function

| Param | Type |
| --- | --- |
| obj | <code>Object</code> |
| [opts] | [<code>TraverseJsonOptions</code>](#TraverseJsonOptions) |

**Example**
```javascript
const traverseJson = require('.');

const options = {...};

const iterator = traverseJson({
foo: 0,
nested: {
depth: 1,
nested: {
depth: 2,
nested: {
depth: 3,
nested: {
depth: 4,
},
},
},
},
bar: 1,
}, options);

for (;;) {
const { done, value } = iterator();
if (done)
break;
console.log(value);
}
```
### Outputs
`{}`
```
[ '/foo', 0 ]
[ '/nested/depth', 1 ]
[ '/nested/nested/depth', 2 ]
[ '/nested/nested/nested/depth', 3 ]
[ '/nested/nested/nested/nested/depth', 4 ]
[ '/bar', 1 ]
```
`{ nested: true }`
```
[ '/foo', 0 ]
[ '/nested',
{ depth: 1, nested: { depth: 2, nested: [Object] } } ]
[ '/nested/depth', 1 ]
[ '/nested/nested',
{ depth: 2, nested: { depth: 3, nested: [Object] } } ]
[ '/nested/nested/depth', 2 ]
[ '/nested/nested/nested', { depth: 3, nested: { depth: 4 } } ]
[ '/nested/nested/nested/depth', 3 ]
[ '/nested/nested/nested/nested', { depth: 4 } ]
[ '/nested/nested/nested/nested/depth', 4 ]
[ '/bar', 1 ]
```
`{ recursive: false }`
```
[ '/foo', 0 ]
[ '/nested',
{ depth: 1, nested: { depth: 2, nested: [Object] } } ]
[ '/bar', 1 ]
```
`{ step: 2 }`
```
[ '/foo', 0 ]
[ '/bar', 1 ]
```
`{ test: /depth$/ }`
```
[ '/nested/depth', 1 ]
[ '/nested/nested/depth', 2 ]
[ '/nested/nested/nested/depth', 3 ]
[ '/nested/nested/nested/nested/depth', 4 ]
```
`{ test: /nested$/, nested: true }`
```
[ '/nested',
{ depth: 1, nested: { depth: 2, nested: [Object] } } ]
[ '/nested/nested',
{ depth: 2, nested: { depth: 3, nested: [Object] } } ]
[ '/nested/nested/nested', { depth: 3, nested: { depth: 4 } } ]
[ '/nested/nested/nested/nested', { depth: 4 } ]
```
`{ test: "**\/{depth,foo}" }`
```
[ '/foo', 0 ]
[ '/nested/depth', 1 ]
[ '/nested/nested/depth', 2 ]
[ '/nested/nested/nested/depth', 3 ]
[ '/nested/nested/nested/nested/depth', 4 ]
```
<a name="createIterator"></a>

## createIterator(obj, [opts]) ⇒ <code>Iterable</code>
Returns a traverseJson iterable, usefull for use it in a for loop.

**Kind**: global function

| Param | Type |
| --- | --- |
| obj | <code>Object</code> |
| [opts] | [<code>TraverseJsonOptions</code>](#TraverseJsonOptions) |

**Example**
```javascript
const { createIterator } = require('traverse-json');
const options =
const ientries = createIterator({
foo: 0,
nested: {
depth: 1,
nested: {
depth: 2,
nested: {
depth: 3,
nested: {
depth: 4,
},
},
},
},
bar: 1,
}, {});

for (let [k, v] of ientries) {
console.log(k, v);
}
````
### Output
```
/foo 0
/nested/depth 1
/nested/nested/depth 2
/nested/nested/nested/depth 3
/nested/nested/nested/nested/depth 4
/bar 1
```
<a name="TraverseJsonOptions"></a>
## TraverseJsonOptions : <code>Object</code>
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| [opts.recursive] | <code>Boolean</code> | enable/disable nested arrays and objects recursion |
| [opts.nested] | <code>Boolean</code> | also emit nested array or objects |
| [opts.step] | <code>Boolean</code> | the step to increment, default 1 |
| [opts.test] | <code>Boolean</code> | regexp, string minimatch or function to filter properties |
<a name="TraverseIteratorResult"></a>
## TraverseIteratorResult : <code>Object</code>
**Kind**: global typedef
**Properties**
| Name | Type |
| --- | --- |
| value | <code>Array.&lt;String, any&gt;</code> |
| done | <code>Boolean</code> |
<a name="TraverseIterator"></a>
## TraverseIterator ⇒ [<code>TraverseIteratorResult</code>](#TraverseIteratorResult)
**Kind**: global typedef
Loading

0 comments on commit 287310a

Please sign in to comment.