npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-origami

v0.6.1

Published

Transform and reshape your JSON objects with the artistry of origami.

Downloads

706

Readme

json-origami

Transform and reshape your JSON objects with the artistry of origami, all without any external dependencies.

Description

json-origami is a lightweight utility library crafted with the finesse of origami techniques. Dive into the world of JSON manipulation without the overhead of additional dependencies. Whether you're looking to flatten a nested structure or revert a flat map into its multi-dimensional glory, json-origami makes the process seamless.

Key Features

  • Lightweight & Standalone: Operates without any external dependencies, ensuring quick installations and reduced bundle sizes.
  • Simple & Intuitive: Designed with a simple API that is easy to learn and use.

Installation

npm install json-origami

Usage

fold

Flatten a multi-layered JSON object into a single-tiered representation.

import { fold } from 'json-origami'

const obj = {
  a: 1,
  b: {
    c: 2,
    d: [3, { e: 4 }]
  }
}
const result = fold(obj)
console.log(result)
/// { a: 1, 'b.c': 2, 'b.d[0]': 3, 'b.d[1].e': 4 }

unfold

Transform a single-layer JSON map back to its original nested structure.

import { unfold } from 'json-origami'

const obj = {
  a: 1,
  'b.c': 2,
  'b.d[0]': 3,
  'b.d[1].e': 4
}

const result = unfold(obj)
console.log(result)
/// { a: 1, b: { c: 2, d: [3, { e: 4 }] } }

twist

Reshape and reorganize the keys of your JSON structure, mirroring the intricate adjustments made in origami.

import { twist } from 'json-origami'

const obj = {
  a: 1,
  b: {
    c: 2,
    d: [3, { e: 4 }]
  }
}

const reshapingMap = {
  a: 'foo',
  'b.c': 'bar',
  'b.d': 'baz'
}

const result = twist(obj, reshapingMap)
console.log(result)
/// { foo: 1, bar: 2, baz: [3, { e: 4 }] }

pick

Select only the specified keys from a JSON object.

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.

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

Type: 'dot' | 'bracket' Default: 'bracket'

Determines the formatting of array indexes when keys are compressed. Choose between dot notation (.0) or bracket notation ([0]).

import { fold } from 'json-origami'

const obj = {
  a: 1,
  b: {
    c: 2,
    d: [3, { e: 4 }]
  }
}

const result = fold(obj, { arrayIndex: 'dot' })
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.

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'

Type: string Default: '' (empty string)

Adds a specified prefix to the keys in the output.

License

MIT

Contributing

see CONTRIBUTING.md