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

@xenoarea/struct-writer

v0.0.0

Published

Function to write complex structures (obj, array) from keypath

Downloads

2

Readme

struct-writer

Function to write complex structures (obj, array) from keypath

Installation

npm install --save @xenoarea/struct-writer

Philosophy

I was originally developing another package in which I needed to sequentially rewrite a state object, with nested keys and arrays. In order to do this I created that function that I then decided to isolate in its own package.

Usage

import write from '@xenoarea/struct-writer'

const struct0 = write('Hello world !')
console.log(struct0)
// output: Hello world !

const struct1 = write('Hello there !', ['field1', 'field2', 'field3'])
console.log(struct1)
// output: { field1: { field2: { field3: 'Hello there !' } } }

const struct2 = write('Hi', ['field1', 'field2', 'field4'], struct1)
console.log(struct2)
// output: { field1: { field2: { field3: 'Hello there !', field4: 'Hi' } } }

const struct3 = write('Cheers', ['field1', { key: 'field6', array: true }], struct2)
console.log(struct3)
// output: { field1: { field2: { field3: 'Hello there !', field4: 'Hi' }, field6: ['Cheers'] } }

const struct4 = write('Hoy', ['field1', { key: 'field6', array: true, index: 0 }], struct3)
console.log(struct4)
// output: { field1: { field2: { field3: 'Hello there !', field4: 'Hi' }, field6: ['Hoy', 'Cheers'] } }

const struct5 = write('Allo', ['field1', { key: 'field6', array: true, update: true, index: 0 }], struct4)
console.log(struct5)
// output: { field1: { field2: { field3: 'Hello there !', field4: 'Hi' }, field6: ['Allo', 'Cheers'] } }

const struct6 = write('Salut', ['field1', { key: 'field6', array: true }, 'field7'], struct5)
console.log(struct6)
// output: { field1: { field2: { field3: 'Hello there !', field4: 'Hi' }, field6: ['Allo', 'Cheers', { field7: 'Salut' }] } }

const struct7 = write('Good morning', ['field1', { key: 'field6', array: true, update: true }, 'field8'], struct6)
console.log(struct7)
// output: { field1: { field2: { field3: 'Hello there !', field4: 'Hi' }, field6: ['Allo', 'Cheers', { field7: 'Salut', fiels8: 'Good morning' }] } }

Keypath

The keypath provided as second parameter to the write function is an array of key segments. A key segment can be a string, in that case it will be used as a key for the structure being created or an object defining some options to create and manipulate array in the structure being created.

Key segment

{
  key: string           // key of the entry in the structure being created.

  array: boolean        // set to true to make the entry an array in the structure being created.

  update: boolean       // set to true to update an item of the array entry in the structure being created.
                        // If no index given, the last item will be updated.
                        // set to false (or do not define) to perform an insertion in the array entry.
                        // If no index given, the new item will be added at the end of the array.

  index: integer        // if update is set to true, the index will determine which item will be updated.
                        // if update is set to false (or not defined), the index will determine at which position
                        // the item will be inserted in the array entry.
}

Find me on