From a4fe7f57cd8a647567e19b09a29728ad0b99fbae Mon Sep 17 00:00:00 2001 From: uyamanaka Date: Thu, 5 Oct 2023 16:25:34 +0900 Subject: [PATCH] chore: update docs --- README.md | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d48e2f0..eb2a1df 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,7 @@ Whether you're looking to flatten a nested structure or revert a flat map into i ## Key Features - **Lightweight & Standalone**: Operates without any external dependencies, ensuring quick installations and reduced bundle sizes. -- **fold**: Flatten a multi-layered JSON object into a single-tiered representation. -- **unfold**: Transform a single-layer JSON map back to its original nested structure. -- **twist**: Reshape and reorganize the keys of your JSON structure, mirroring the intricate adjustments made in origami. +- **Simple & Intuitive**: Designed with a simple API that is easy to learn and use. ## Installation @@ -25,6 +23,8 @@ npm install json-origami ### fold +Flatten a multi-layered JSON object into a single-tiered representation. + ```javascript import { fold } from 'json-origami' @@ -42,6 +42,8 @@ console.log(result) ### unfold +Transform a single-layer JSON map back to its original nested structure. + ```javascript import { unfold } from 'json-origami' @@ -59,6 +61,8 @@ console.log(result) ### twist +Reshape and reorganize the keys of your JSON structure, mirroring the intricate adjustments made in origami. + ```javascript import { twist } from 'json-origami' @@ -81,6 +85,46 @@ console.log(result) /// { foo: 1, bar: 2, baz: [3, { e: 4 }] } ``` +### pick + +Select only the specified keys from a JSON object. + +```javascript +import { pick } from 'json-origami' + +const obj = { + a: 1, + b: { + c: 2, + d: [3, { e: 4 }] + } +} + +const result = pick(obj, ['a', 'b.c']) +console.log(result) +/// { a: 1, b: { c: 2 } } +``` + +### omit + +Remove the specified keys from a JSON object. + +```javascript +import { omit } from 'json-origami' + +const obj = { + a: 1, + b: { + c: 2, + d: [3, { e: 4 }] + } +} + +const result = omit(obj, ['b.c', 'b.d[1]']) +console.log(result) +/// { a: 1, b: { d: [3] } } +``` + ## Options ### arrayIndex @@ -106,6 +150,35 @@ conole.log(result) /// { a: 1, 'b.c': 2, 'b.d.0': 3, 'b.d.1.e': 4 } ``` +### pruneArray + +Options for 'unfold', 'twist', 'pick', 'omit' + +**Type**: `boolean` +**Default**: `true` + +Specifies whether to remove the specified array elements or not. + +```javascript +import { twist } from 'json-origami' + +const obj = { + a: 1, + b: { + c: 2, + d: [3, 4, 5, 6] + } +} + +const result1 = twist(obj, { 'b.d[1]': 'D' }, { pruneArray: true }) +console.log(result1) +/// { a: 1, b: { c: 2, d: [3, 5, 6] }, D: 4 } + +const result2 = twist(obj, { 'b.d[1]': 'D' }, { pruneArray: false }) +console.log(result2) +/// { a: 1, b: { c: 2, d: [3, undefined, 5, 6] }, D: 4 } +``` + ### keyPrefix Options for 'fold'