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

dot-object-array

v3.1.2

Published

Implements array-like methods for Object with support for dot notation keys

Downloads

8

Readme

NPM

GitHub release Gzip size Build Status Coverage Status semantic-release Documentation

Known Vulnerabilities

DotObjectArray, a.k.a. DOA a.k.a. ObjectArray

Why DOA ?

For three reasons :

  • No support for associative arrays in vanilla JS
  • Creating deep levels keys in a vanilla JS Object can programmatically be a pain if none of parent keys exists
  • Bored of always using the same snippets everywhere and wants to have a less then 10KB NPM dependency ready to import

Features

DOA is an object with a set of methods to :

  • Check, push, get and store data with ease regardless of its level
  • Bring some array-like behaviours for convenience (you know forEach for arrays, eh ?)
  • Provide data serializers and parsers
  • Work as well on a whole dataset or a key-based sub-selection of the dataset
  • Ease management of options default values

Quick examples

// Import data at creation
var doa = new ObjectArray({
	item1: 3,
 item2: 12,
 item3: 5
});

// Add data
doa.push('item4', 4); // Single item or dataset
doa.import({
  item5: 5,
  item6: 6
});

// Add data with dotted notations
doa.push('dat.long.darn.key','isntIt?'); // Will automatically create each keys

// Define default values
// Will keep value if existent or create keys with value if not
doa.define('options.section1.item14', true);

//Iterate on keys at root level or in sub dataset
doa.forEach(function(value, key, index) {
 [...]
});
doa.forEach(function(value, key, index) {
 [...]
}, 'dat.path.to.data');

// Sub dataset import
doa.import({
 subitem1: 1,
 subitem2: 'astring',
 subitem3: {obj: really}
}, 'dat.long.and.far.away.key');

// sub dataset access
doa.dataset('dat.long.and.far.away.key');
// or
doa.pull('dat.long.and.far.away.key');

// And many more !

Installation

Module

The ObjectArray class is provided as a UMD module.

npm install dot-object-array

or

yarn add dot-object-array

Then simply require/import it :

import ObjectArray from 'dot-object-array';
const ObjectArray = require('dot-object-array').default;

ObjectArray have been coded with ECMA6 class standard.

Browser

DOA is available as CDN external link or can easily be installed locally.

Bundle

<script type="text/javascript" src="https://bundle.run/dot-object-array@latest"></script>

JsDelivr

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/dot-object-array@latest"></script>

Unpkg

<script type="text/javascript" src="https://unpkg.com/dot-object-array@latest"></script>

Local install

For browser install, you can simply fetch file dist/objectarray.min.js in this repo or clone it and load it :

<script type="text/javascript" src="myJsFolder/objectarray.min.js"></script>

An ObjectArray constructor will be added to global (window) scope.

Configuring ObjectArray behaviour when a required key doesn't exist

In version 1.x, a non-existent key yields to an undefined returned value.

In version 2.x, a non-existent key data request raises an exception except for some methods that can leverage a throwable trigger.

Since version 3.x, the behaviour can be configure per method call or override globally. Each data request have a predefined behaviour given its goal. For instance, the empty method will throw an exception by default and a dataset call will return undefined by default.

Please check API reference for details.

Playground

If you want to go further and try a bit, you can go to the playground.

Documentation

A full documentation (manual and API reference) set is available :

JSON support

You can easily use ObjectArrays to manipulate JSON data. Just rely on JSON native object to import your JSON structure, manipulate it with ObjectArray ease and get it back at the end :wink:

var jstring = '{"dat": {"long": {"path": "foo", "dream": "baz"}}}';
var doa = new ObjectArray(JSON.parse(jstring));

// Let's say we want to move all dat.long stuff to a short thing
doa.push('short', doa.dataset('dat.long')).remove('dat');

console.log(JSON.stringify(doa.data)); // outputs {"short":{"path":"foo","dream":"baz"}}

Bugs and features requests

ObjectArray is test-driven though it did not prevent all issues. Please report here any trouble or features request.

Want to help ?

There is many more to do to implements othe features. Don't mind fork DOA, tweak it and submit a pull request :wink: