jessie-codes / safe-flat Goto Github PK
View Code? Open in Web Editor NEWSafely flatten a nested JavaScript object.
License: MIT License
Safely flatten a nested JavaScript object.
License: MIT License
If obj value is an instance of Date - it is simply omitted from the result.
Describe the bug
It seems like safe-flat skips empty arrays within the passed object
To Reproduce
Steps to reproduce the behavior:
Expected behavior
The key value pair for the empty array should appear in the flattened object
Desktop (please complete the following information):
Additional context
Example:
const { flatten } = require('safe-flat')
const data = {
a: [],
b: 1,
c: {
d: []
}
}
console.log(flatten(data)) // => { b: 1 }
Is your feature request related to a problem? Please describe.
This library currently only supports taking a nested object and flattening it. Ideally, it would also have a function that would take a flatten objected and return a nested object.
Describe the solution you'd like
Create a new function, unflatten
, with a function signature of (obj, delimiter)
which will return a nested version of the object.
Additional context
This will need to be a breaking change as the code exports the flatten
function directly. The new version should export an object that has both the unflatten and flatten functions.
Hello!
Did you consider this use case for your library? hughsk/flat#118 What's the motivation behind flattening custom objects? This can give unexpected surprises like property-symbols or strange buffers. Thanks!
Tested on RunKit for v2.0.0:
const { flatten } = require("safe-flat")
class Cat {
constructor() {
this.name = 'Cosmonaut';
}
meow() {
console.log('Meow.');
}
}
result = flatten({
cat: {
instance: new Cat(),
},
});
console.log(result);
console.log(result['cat.instance']?.meow());
Result:
Object {cat.instance.name: "Cosmonaut"}
undefined
See more in hughsk/flat#118.
Is your feature request related to a problem? Please describe.
I'm trying to use this package with TypeScript, and I could not find types.
Describe the solution you'd like
Have the types bundled with the project as simple declaration files.
Describe alternatives you've considered
Publish types myself to DefinitelyTyped, which I've just done โ DefinitelyTyped/DefinitelyTyped#57468 @jessie-codes would be super useful if you can review it!
Additional context
None.
Describe the bug
Unflatenning on a JSON that has 3 levels deep nesting objects, will result values to be saved as [Object]
instead of the real value
To Reproduce
Run the following example
const delimiter = "."
const data = {
key1: {
keyA: 'valueI'
},
key2: {
keyB: 'valueII'
},
key3: { a: { b: { c: 2 } } }
}
let x = flatten(data, delimiter)
console.log(`x is:` , x)
let y = unflatten(x, delimiter)
console.log(`y is:` , y)
Expected behavior
The value on y
should be:
{
key1: { keyA: 'valueI' },
key2: { keyB: 'valueII' },
key3: { a: { b: { c: 2 } } }
}
What is actually being generated:
{
key1: { keyA: 'valueI' },
key2: { keyB: 'valueII' },
key3: { a: { b: [Object] } }
}
Node v17.1.0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.