Code Monkey home page Code Monkey logo

flat-object's Introduction

Installation

$ npm install flat-object

Methods

flatten({ data })

Returns an array of "flattened" paths for the Object specified.

import { flatten } from 'flat-object'

const data = {
  nestedArray: [
    'string',
    {
      boolean: true,
      string: 'yes'
    }
  ]
}

flatten({ data }) // "nestedArray[0]", "nestedArray[1]", "nestedArray[1].boolean", "nestedArray[1].string"
read({ data, path })

Returns the property value of the flattened path or undefined if the property does not exist.

import { read } from 'flat-object'

const data = {
  name: 'Example',
  nestedArray: [
    { id: 1 },
    { id: 2 }
  ]
}

read({
  data,
  path: 'name'
}) // "Example"

read({
  data,
  path: 'nestedArray[1].id'
}) // 2
write({ data, path, value })

Writes a value to the specified path. This method just-in-time creates any missing properties along the specified path.

This method will throw an error if an Array value is specified for a property containing an Object (or vice versa).

import { write } from 'flat-object'

const data = {}

write({
  data,
  path: 'name',
  value: 'Example'
}) // data == { name: "Example" }

flatten({
  data,
  path: 'address.street',
  value: '123 Bird Ave'
}) // data == { name: "Example", address: { street: "123 Bird Ave" } }

flatten({
  data,
  path: 'address[0]',
  value: 123
}) // Throws error (since address is already defined as an Object)

flat-object's People

Contributors

bvaughn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.