Socket
Socket
Sign inDemoInstall

flattie

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flattie - npm Package Compare versions

Comparing version 0.0.0 to 1.0.0

42

dist/index.js

@@ -1,30 +0,24 @@

module.exports = function flatten(obj, sep) {
var k, v, j, tmp, out={};
sep = sep || '_';
function iter(output, sep, val, key) {
var k, pfx = key ? (key + sep) : key;
if (Array.isArray(obj)) {
for (k=0; k < obj.length; k++) {
v = obj[k];
if (v == null) {
} else if (typeof v == 'object') {
tmp = flatten(v, sep);
for (j in tmp) out[k + sep + j] = tmp[j];
} else {
out[k] = v;
}
if (val == null) {
} else if (typeof val != 'object') {
output[key] = val;
} else if (Array.isArray(val)) {
for (k=0; k < val.length; k++) {
iter(output, sep, val[k], pfx + k);
}
} else if (typeof obj == 'object') {
for (k in obj) {
v = obj[k];
if (v == null) {
} else if (typeof v == 'object') {
tmp = flatten(v, sep);
for (j in tmp) out[k + sep + j] = tmp[j];
} else {
out[k] = v;
}
} else {
for (k in val) {
iter(output, sep, val[k], pfx + k);
}
}
}
return out;
function flattie(input, sep) {
var output = {};
if (typeof input == 'object') iter(output, sep || '.', input, '');
return output;
}
exports.flattie = flattie;

@@ -1,1 +0,1 @@

!function(e,f){"object"==typeof exports&&"undefined"!=typeof module?module.exports=f():"function"==typeof define&&define.amd?define(f):e.flattie=f()}(this,(function(){return function e(f,o){var t,n,i,r,l={};if(o=o||"_",Array.isArray(f))for(t=0;t<f.length;t++)if(null==(n=f[t]));else if("object"==typeof n)for(i in r=e(n,o))l[t+o+i]=r[i];else l[t]=n;else if("object"==typeof f)for(t in f)if(null==(n=f[t]));else if("object"==typeof n)for(i in r=e(n,o))l[t+o+i]=r[i];else l[t]=n;return l}}));
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.flattie={})}(this,(function(e){e.flattie=function(e,t){var f={};return"object"==typeof e&&function e(t,f,n,o){var i,r=o?o+f:o;if(null==n);else if("object"!=typeof n)t[o]=n;else if(Array.isArray(n))for(i=0;i<n.length;i++)e(t,f,n[i],r+i);else for(i in n)e(t,f,n[i],r+i)}(f,t||".",e,""),f}}));

@@ -1,1 +0,1 @@

export default function<X=Record<string, any>, Y=unknown>(input: Y, glue?: string): X;
export function flattie<X=Record<string, any>, Y=unknown>(input: Y, glue?: string): X;
{
"name": "flattie",
"version": "0.0.0",
"version": "1.0.0",
"repository": "lukeed/flattie",
"description": "A tiny (188B) utility to flatten an object with customizable glue",
"description": "A tiny (187B) and fast utility to flatten an object with customizable glue",
"unpkg": "dist/index.min.js",

@@ -28,3 +28,3 @@ "module": "dist/index.mjs",

"engines": {
"node": ">= 4"
"node": ">=8"
},

@@ -37,2 +37,3 @@ "scripts": {

"keywords": [
"keys",
"flatten",

@@ -39,0 +40,0 @@ "object",

@@ -1,7 +0,9 @@

# flat-obj [![build status](https://badgen.net/github/status/lukeed/flat-obj)](https://github.com/lukeed/flat-obj/actions) [![codecov](https://badgen.now.sh/codecov/c/github/lukeed/flat-obj)](https://codecov.io/gh/lukeed/flat-obj)
# flattie [![CI](https://github.com/lukeed/flattie/workflows/CI/badge.svg)](https://github.com/lukeed/flattie/actions) [![codecov](https://badgen.now.sh/codecov/c/github/lukeed/flattie)](https://codecov.io/gh/lukeed/flattie)
> A tiny (188B) utility to flatten an object with customizable glue
> A tiny (187B) and [fast](#benchmarks) utility to flatten an object with customizable glue
This module squashes a nested object (including its internal Arrays) so that the output is a flat object – AKA, it has a single level of depth. By default, the `_` character is used to glue/join layers' keys together. This is customizable, of course.
This module recursively squashes an Object/Array. The output is a flat object – AKA, it has a single level of depth.
By default, the `.` character is used to glue/join layers' keys together. This is customizable.
Finally, any keys with nullish values (`null` and `undefined`) are **not** included in the return object.

@@ -12,3 +14,3 @@

```
$ npm install --save flat-obj
$ npm install --save flattie
```

@@ -20,5 +22,5 @@

```js
import flatObj from 'flat-obj';
import { flattie } from 'flattie';
flatObj({
flattie({
a: 'hi',

@@ -43,15 +45,15 @@ b: {

// {
// a: 'hi',
// b_b_0: 'foo',
// b_b_1: '',
// b_b_3: 'bar',
// b_d: 'hello',
// b_e_a: 'yo',
// b_e_c: 'sup',
// b_e_d: 0,
// b_e_f_0_foo: 123,
// b_e_f_0_bar: 123,
// b_e_f_1_foo: 465,
// b_e_f_1_bar: 456,
// c: 'world'
// 'a': 'hi',
// 'b.b.0': 'foo',
// 'b.b.1': '',
// 'b.b.3': 'bar',
// 'b.d': 'hello',
// 'b.e.a': 'yo',
// 'b.e.c': 'sup',
// 'b.e.d': 0,
// 'b.e.f.0.foo': 123,
// 'b.e.f.0.bar': 123,
// 'b.e.f.1.foo': 465,
// 'b.e.f.1.bar': 456,
// 'c': 'world'
// }

@@ -64,3 +66,3 @@ ```

### flatObj(input, [glue])
### flattie(input, glue?)
Returns: `Object`

@@ -79,3 +81,3 @@

Type: `String`<br>
Default: `_`
Default: `.`

@@ -87,4 +89,4 @@ A string used to join parent key names to nested child key names.

flatObj({ foo }); //=> { foo_bar: 123 }
flatObj({ foo }, '.'); //=> { 'foo.bar': 123 }
flattie({ foo }); //=> { 'foo.bar': 123 }
flattie({ foo }, '???'); //=> { 'foo???bar': 123 }
```

@@ -98,13 +100,19 @@

```
Load Time:
flat 1.004ms
flatten-object 1.223ms
flat-obj 0.971ms
flattie 0.239ms
Validation:
✔ flat
✔ flatten-object
✔ flat-obj@1.x
✔ flat-obj
✔ flattie
Benchmark:
flat x 186,002 ops/sec ±1.28% (89 runs sampled)
flatten-object x 188,715 ops/sec ±0.22% (94 runs sampled)
flat-obj@1.x x 274,414 ops/sec ±1.03% (95 runs sampled)
flat-obj x 363,332 ops/sec ±0.57% (97 runs sampled)
flat x 183,670 ops/sec ±1.30% (86 runs sampled)
flatten-object x 209,886 ops/sec ±0.32% (93 runs sampled)
flat-obj x 383,326 ops/sec ±1.65% (89 runs sampled)
flattie x 901,407 ops/sec ±0.72% (90 runs sampled)
```

@@ -111,0 +119,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc