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

crystallize

v0.3.1

Published

Turn a flat object into a nested tree-like one, based on matching prefixes amongst its keys.

Downloads

30

Readme

crystallize() turns a flat data object into a nested, tree-like object. Keys get automatically grouped together if they share matching prefixes.

crystallize() provides 1 way to store JSON-like data in a flat memory structure (e.g. relational tables, CSV). Upon reading from memory, crystallize() deserializes your flat record into a structured object.

Status

This library is approximately 90% complete. The following features remain on the roadmap:

  • The reverse (serializing) operation to crystallize() (probably crystallize.smash())
  • How to not over-reverse when reversing, for values that are objects.

However, a good serializing substitute for now is flat.

Strikethroughed lines below describe features that are planned but not yet implemented. Everything else works.

Usage

var crystallize = require('crystallize');

// The data to crystallize.
var flatBob = {
  id: 1,
  name: 'bob',
  content_description: 'bob is a traveling salesman',
  content_tagline: 'the handsomest salesman in the world',
  vehicles_air: 'diy quadrocopter',
  vehicles_land_bike: 'fixie',
  vehicles_land_car: 'jalopy'
};

var nestedBob = crystallize(flatBob);

/*
nestedBob:
{
  id: 1,
  name: 'bob',
  content: {
    description: 'bob is a traveling salesman',
    tagline: 'the handsomest salesman in the world'
  },
  vehicles: {
    air: 'diy quadrocopter',
    land: {
      bike: 'fixie',
      car: 'jalopy' 
    }
  }
}
 */

API

crystallize(flatObject, [opts])

Returns the nested version of flatObject. Pass in an optional options object.

Note: Object key order will not be preserved. Rather, keys will end up being lexicographically ordered. File an issue describing your use case if you need object key order preserved.

Options

  • delimiter
    String value that specifies the delimiter between words. '_' and '.' are common. ~~Multi-character delimiters like '__' (double underscore) are allowed as well.~~ To delimit by PascalCase/camelCase, supply either 'pascalcase' or 'camelcase'. (default: '_')

  • excludes
    Array of prefix words excluded from crystallizing. One common example is 'is'. See example below. For excluded phrases, either supply the phrase in the appropriate delimitation standard (e.g. 'stateOf' when delimiter is 'camelCase'), or supply the phrase an array of words (e.g. ['state', 'of'], has the advantage of being delimiter-agnostic). (default: [])

Example of Exclusion

var data = {
  id: 1,
  name: 'bob',
  content_description: 'bob is a traveling salesman',
  content_tagline: 'the handsomest salesman in the world',
  is_live: true,
  is_public: true,
  hair_is_black: true,
  hair_is_silky: true
}

// Exclusion example.
var result = crystallize(data, {excludes: ['is']})

/*
Result:
{
  id: 1,
  name: 'bob',
  content: {
    description: 'bob is a traveling salesman',
    tagline: 'the handsomest salesman in the world'
  },
  is_live: true, // Excluded, starts with 'is'.
  is_public: true,
  hair_is: { // Not excluded, 'is' isn't first word.
    black: true,
    silky: true
  }
}
*/

// Exclusion of phrases.
crystallize(data, {exclude: ['is', 'has', 'state_of']}); // Assuming '_' delimiter.
crystallize(data, {exclude: ['is', 'has', 'stateOf']}); // Assuming camelCase delimiter.
crystallize(data, {exclude: ['is', 'has', ['state', 'of']]});

Library Edge Cases

These situations usually shouldn't be happening. But for the sake of completeness...

  • When I use the camelCase or PascalCase delimiter, what happens if my source object has a mix of both PascalCase (capitalized first character) and camelCase (uncapitalized first character) keys?
    First characters remain unmodified, so the first level of the resulting object will still contain a mix of PascalCase and camelCase keys. However, the keys of any nested branches will be capitalized (or uncapitalized) to follow the style you dictated for your delimiter (this is already happening in a normal situation).