Giter VIP home page Giter VIP logo

mesqueeb / flatten-anything Goto Github PK

View Code? Open in Web Editor NEW
23.0 1.0 1.0 421 KB

Flatten objects and replace nested props with 'prop.subprop'. A simple and small integration.

Home Page: https://npmjs.com/flatten-anything

License: MIT License

JavaScript 13.51% TypeScript 86.49%
flatten object paths pathify flatten-object flatten-paths flatten-deep-object flatten-nested-props pathify-props get-flat-object

flatten-anything's Introduction

Flatten anything ๐Ÿ

Total Downloads Latest Stable Version

npm i flatten-anything

Flatten objects and replace nested props with 'prop.subprop'. A simple and small integration.

I was looking for:

  • A simple solution to flatten objects & arrays
  • Only flatten plain objects and not special class instances!

This last one is crucial! So many libraries use custom classes that create objects with special prototypes, and such objects all break when trying to flatten them. So we gotta be careful!

flatten-anything will flatten objects and their nested properties, but only as long as they're "plain objects". As soon as a sub-prop is not a "plain object" and has a special prototype, it will stop flattening there and reference that instance "as is". โ™ป๏ธ

Very usable for creating a payload for Firebase Firestore update function, which only accepts flat objects! With Firestore for example Firebase.firestore.FieldValue.delete() does not break.

Can be used in combination with nestify-anything, which does the exact opposite of this one! ๐Ÿ˜‰

Meet the family (more tiny utils with TS support)

Usage

  • If you pass an object, it will flatten all nested properties
  • If you pass an array, it will flatten all nested arrays

Flatten objects

import { flatten } from 'flatten-anything'

const target = { name: 'Ho-oh', types: { fire: true, flying: true } }

flatten(target)
// returns {
//   name: 'Ho-oh',
//   types.fire: true,
//   types.flying: true
// }

Please note that when you pass an object it will only flatten nested object props, and do nothing to arrays inside the object.

Flatten arrays

import { flatten } from 'flatten-anything'

const target = [1, 2, ['a', 'b', ['y', 'z']], 3]

flatten(target)
// returns [1, 2, 'a', 'b', 'y', 'z', 3]

Please note that when you pass an array it will only flatten direct arrays, and do nothing to objects inside the array.

Limit the depth to flatten

currently only works with objects. Please open an issue if you want it to work with arrays as well!

const target = {
  name: 'Lugia',
  appearence: {
    // let's only flatten 1 level until here:
    parts: { wings: true },
    colors: { white: true, blue: true },
  },
}
const untilDepth = 1

flatten(target, untilDepth)
// returns {
//   name: 'Lugia',
//   appearence.parts: { wings: true },
//   appearence.colors: { white: true, blue: true },
// }

Flatten only certain props

It's possible to only flatten eg. one prop.

import { flattenObjectProps } from 'flatten-anything'

const target = {
  appearence: { hair: 'orange' },
  traits: { strength: 9000 },
}
const propsToFlatten = ['traits']

flattenObjectProps(target, propsToFlatten)
// returns {
//   appearence: { hair: 'orange' },
//   traits.strength: 9000,
// }

You can also point to a nested object property to only flatten specific nested props, but keep the rest as an object!

const target = {
  traits: {
    strength: 9000,
    range: { min: 8000, max: 10000 },
  },
}

const propsToFlatten = ['traits.range']
// only flatten `traits.range` and nothing else.

flattenObjectProps(target, propsToFlatten)
// returns {
//   traits: { strength: 9000 },
//   'traits.range': {min: 8000, max: 10000},
// }

Treeshaking

Importing flatten allows you to use it for both objects and arrays.

If you use webpack, rollup, etc. you can import less code by specifying the exact flatten function you need:

import { flattenObject } from 'flatten-anything'
// or
import { flattenArray } from 'flatten-anything'

Example for Firestore

const pokemon = {
  name: 'Charizard',
  types: { dark: true, fire: true, flying: true },
}
// we want to delete the `dark` type from this Pokemon

const payload = flatten({
  types: { dark: Firebase.firestore.FieldValue.delete() },
})
Firebase.firestore()
  .doc('pokemon/charizard')
  .update(payload)

flatten-anything's People

Contributors

dependabot[bot] avatar mesqueeb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

rigidoont

flatten-anything's Issues

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.