sindresorhus / map-obj Goto Github PK
View Code? Open in Web Editor NEWMap object keys and values into a new object
License: MIT License
Map object keys and values into a new object
License: MIT License
Index.js line 28 , const used in iterator for loop , should be let or var. It breaks in old browser (Firefox 46.0.1) with following error https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Errors/Missing_initializer_in_const
I know you don't want to publish babelified versions of your module, and you prefer your users takes care of that.
But in order to do that, I have to manually change map-obj
package.json every time I deploy, to add:
"browserify": {
"transform": [
"babelify"
]
}
would you accept a PR to add that code to pkg file?
If they return a falsie value, skip this key/value pair.
It might be safest if you make them explicitly return a specific value(false
or null
), to guard against accidental returns of undefined
.
The types type the key
and value
arguments as keys and values of the passed root object, but when passing { deep: true }
, my understanding is these could be deeply nested keys and values (i.e. key: string
and value: unknown
)
very good
If a mapper function produces ['__proto__', value]
, rather than create a corresponding own property, mapObject
sets the prototype of the target object to the output value.
This prevents objects containing __proto__
keys from round-tripping through the identity function, and prevents mappers from producing __proto__
keys.
One possible fix is to use Object.defineProperty()
for creating properties, rather than assignment. However, this would also change mapObject
's behavior in certain edge cases when given custom target
objects: their setters would not invoked, their non-writable own properties would be overwritten with writable properties, and writable non-configurable own properties would throw TypeError
s. Would that be considered a breaking change?
I'll submit a PR adding failing test cases.
I have to transpile this package to make it useable for IE11. Maybe you could provide two build targets?
Or at least state clearly in the README that it is not es5.
Right now, the source
argument passed to the mapper
function is not the actual original source
argument the user passed to mapObject
, but the "owner" object of the key/value pair for the current iteration.
Example:
const { default: mapObject } = await import('./index.js');
const subject = { nestedA: { one: 1, two: 2 }, nestedB: { three: 3, four: 4 } };
const mapper = (key, value, source) => source !== subject ? [key + '_different', value] : [key + '_same', value];
console.log(mapObject(subject, mapper, { deep: true }));
{
nestedA_same: { one_different: 1, two_different: 2 },
nestedB_same: { three_different: 3, four_different: 4 }
}
Notice source
is only equal to subject
for the top level keys.
Fixing this would be a breaking change, so should probably be released as v6.0.0
?
Would you accept a pull request which implements the following?
mapObject({one: 1, two: 2}, (key, value) => value === 1 ? [key, value] : null);
//=> {one: 1}
The code change will look like this
-let [newKey, newValue, {shouldRecurse = true} = {}] = mapper(key, value, object);
+let mapResult = mapper(key, value, object)
+if (mapResult === null) continue
+let [newKey, newValue, {shouldRecurse = true} = {}] = mapResult
mapObject(schema, (key, value) => {
let goDeeper = true;
if (key === 'MAGIC_VALUE') {
goDeeper = false;
}
return [
key,
value,
goDeeper,
];
}, {
deep: true,
});
This would be useful when the intent is to modify only a subset of the schema.
https://github.com/sindresorhus/map-obj/blob/master/index.js#L28
this line causes an error in Firefox v49.0.1
I found that if you have a Blob instance or File instance within object which passed in mapObject
and deep is true the function will go inside this objects. I guess it is not correct because Blob and File are js's special objects and this logic breaks them. If you add a simple check like !(value instanceof Blob) here that will fix this problem. I can create PR.
Hey,
I have a use case where I don't map keys but values.
It would be useful for me to be able to access the parent's key (which is unchanged in my case) or even a list of all parents, grand-parents etc.
I realize that when mapping keys this may not make much sense but I also think there could be use cases.
The library is not supposed to accept arrays. And it does not throw any errors on passing one too.
const newObj = mapObject([2, 1], (key, value) => [value, key]);
console.log(newObj)
//=> outputs
[ 2, 1 ]
This is due to this check. Since arrays are objects too, It lets them slide in.
Line 3 in 1e5c466
plain objects
.import mapObject from 'map-obj-async';
const newObject = await mapObject({
url: 'https://www.google.com/'
}, async (key, value) => [key, await fetchHtml(value)]);
//=> { url: `<!DOCTYPE html>...` }
I forked and published https://github.com/richardscarrott/map-obj-async but thought it was worth opening an issue here just in case you wanted to support it? (Although not 100% sure there's a particularly nice way to support both sync and async in a single impl.)
Hey,
I just looked through your code to see if your package does what I need.
This library could consume less memory (I think) by moving mapArray
out of mapObj
and pass the arguments along - just like you pass them into mapObj
.
Currently a new function is created for each recursive call. For big objects that might be quite some memory, I guess.
Hi, is it possible for you to publish a new release ?
I would like to have the new shouldRecurse
feature.
Thanks.
node_modules/map-obj/index.d.ts:72:3 - error TS2344: Type 'SourceObjectType' does not satisfy the constraint '{ [key: string]: unknown; }'.
Type 'object' is not assignable to type '{ [key: string]: unknown; }'.
72 SourceObjectType,
~~~~~~~~~~~~~~~~
node_modules/p-queue/index.d.ts:79:47 - error TS2344: Type 'DefaultAddOptions' does not satisfy the constraint 'QueueAddOptions'.
Index signature is missing in type 'DefaultAddOptions'.
79 EnqueueOptionsType extends QueueAddOptions = DefaultAddOptions
Sorry, I opened an issue in your oher repo, not sure where I should open this issue
TypeError: Invalid value used as weak map key
at WeakMap.set (native)
at mapObj (/home/jperelli/celerative/wysh/WYSHAPI/node_modules/map-obj/index.js:23:7)
at module.exports (/home/jperelli/celerative/wysh/WYSHAPI/node_modules/camelcase-keys/index.js:16:9)
What could be causing this error?
Useful for pinpointing exactly where a key is in an object. Useful to determine where a key is located in a deep object:
const mapObject = require("map-obj")
mapObject({
a: {
b: "๐ฆ"
}
}, (key, value) => {
console.log(key)
return [key, value]
}, {
deep: true,
dotPropKeys: true
})
//=> "a"
//=> "a.b"
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.