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

doot-utils

v0.2.0

Published

JS Object manipulation with dot path and some magical helpers

Downloads

26

Readme

package version npm Open Source Love PRs Welcome

forthebadge

Installation

With npm: npm i doot-utils

With yarn: yarn add doot-utils

In browser: <script src="https://unpkg.com/doot-utils"></script>

Note: The dist/ folder contains:

  • UMD build: doot-utils.js
  • UMD build (core only): doot-utils.core.js
  • CommonJS build: doot-utils.common.js
  • CommonJS build (core only): doot-utils.core.common.js

Usage example

Helper functions on their own:

import { get, set, del, move, copy } from 'doot-utils'
// import { get, set, del } from 'doot-utils/dist/doot-utils.core' // if you only need core features

// Then use the helper function
get(obj, path, defaultValue)
set(obj, path, value)
copy(obj, path, targetObj, targetPath, modifierFn, doMove)
// etc...

With Doot wrapper class:

import { dootify/* , Doot */ } from 'doot-utils'
const obj = {
  a: {
    b: {
      c: 12,
      toto: {
        foo: 'bar'
      }
    },
    d: {
      e: 'foo'
    }
  },
  f: {
    g: false
  }
}

const d = dootify(obj) // same as `new Doot(obj)`
d.get('a.b')
d.value // { c: 12, toto: { ... } }

d.get('a.undefined.prop').value // undefined

d.base() // internal value (chainValue) back to `obj`
  .set('toto.foo', 'baz')
  .value // = 'baz'

const target = {}
d.base()
  .copy('f.g', target, 'a.location', v => !v)
  .value // { a: { location: true } }

See API for more details.

Features

Doot-utils is a set of helper functions that let you use dot paths to access and transform deeply nestd properties of js object with ease. All that without getting any TypeError because of a non existing property.

This package is organized in two sets of helpers:

  • Core features: basic functions are in the core bundle.
    • get, set or a nested property
  • Plugin features: other helpers that extand the core feautres.
    • copy or move a nested property in object to a nested destination in another object

What does new Doot(obj) does ?

I wrote a class called Doot that wraps does helper functions. When creating a new instance of Doot with an object passed to its constructor, Doot will store this object and allow you to call any of the helpers and chain them to apply transformations to the object.

Example:

new Doot(obj)
  .get('nested.prop')
  .set('deeper.prop', value) 
  .copy(...)
  // etc...

All helpers are attached to the Doot instance as methods. There are two properties of Doot you need to know:

  • new Doot(obj).value will contain the result of a previously called method. You can access this property when you are done manipulating the object and you want to collect the final result.
  • new Doot(obj).chainValue will always contain the object that need to be passed to the next method call. Its value may be the same as .value or may not.

The methods of the Doot instance behave like proxies to the helper functions. They all receive as first argument the value of chainValue so you just have to pass the rest of the parameters.

Writing a plugin

The Doot class has a static method called use that takes a function and calls it with the Doot class as sole argument.

Here is a useless example of a plugin that gets a nested property and adds 1 to it:

import { Doot, get } from 'doot-utils'

Doot.use(DootClass => {
  DootClass.prototype.aPlugin = function(arg) {
    this.value = get(this.chainValue, arg) + 1
    // you can change the value of `this.chainValue` for the next method in the chain

    // Do not forget to return `this` for the chain !
    return this
  }
})

// Then later
new Doot(obj).aPlugin('nested.prop')

If you want to author a plugin for Doot, please call it doot-plugin-<pluginname> so that others can find it easily.

API

When used as methods, all helper functions will be passed this.chainValue as first parameter so you just have to pass the remaining parameters.

Doot#base([obj])

Sets this.value & this.chainValue to the object provided when creating the instance (new Doot({...})) or to the argument obj if defined.

copy(obj, path, targetObject [, targetPath [, modifierFn [, isMove] ] ])

Copies a nested property from a source object to a destination object.

Return value: The copied value, or undefined if the path does not exist in source object.

path & targetPath can be either a dot path string or an array of keys. targetPath is an optional parameter, it will be the same as path if not given.

modifierFn is a function that will be applied if you wish to transform the value found at obj[path] before copying it to the target. It will be given the following arguments: modifierFn(value, sourcePath, targetPath). This parameter is optionnal and will default to v => v.

isMove is a boolean that defaults to false. If true the copied property value will be removed from source after the copy.

NOTE: When used as as Doot method, it will set this.value to the copied value and this.chainValue to the target object.

Examples: TODO

del(obj, path) (core)

Deletes a nested property from an object.

Return value: true if the property was deleted, false otherwise.

path is either a dot path string or an array of keys.

NOTE: When used as as Doot method, it will set this.value to true/false and this.chainValue will remain the same but altered object.

Examples: TODO

get(obj, path [, ...args]) (core)

Gets a nested property of an object.

Return value: the nested property value or undefined.

path is either a dot path string or an array of keys.

The last element of args is returned as the default value if the nested property does not exist. If the last key of path ends with '()', it will be interpreted as a function call and the function will be passed ...args as parameters. :warning: If you are making a function call, then keep in mind that the last argument you give is the default value if the path does not exist.

NOTE: When used as as Doot method, it will set both this.value and this.chainValue to the nested property value.

Examples: TODO

move(obj, path, targetObject [, targetPath [, modifierFn] ])

It is just an alias to copy with the isMove argument set to true

See copy above for more details.

set(obj, path, value) (core)

Sets a nested property of an object to the given value.

Return value: the nested property value or undefined.

path is either a dot path string or an array of keys.

NOTE: When used as as Doot method, it will set this.value to the value you are setting and this.chainValue will remain the same but altered object.

Examples: TODO

Doot#use(pluginFn)

Use a new plugin that will extend the Doot prototype with new feature(s).

A plugin is a function that will be called with the Doot class you have to extend.

Licence

MIT

Copyright (c) 2018-present @nash403