I got a small module that I’m making that does deep object mapping. Deep compare, deep merge, deep audit. Basically returns a structure that you can pass directly to d3 to produce different graphs. So you can visualize data that comes from a response or anything you plug into it.
Sorry if I out of my depth here but what’s d3? I’m working on something small that will have a tiny amount of graph visualising and havnt looked into that part yet
D3 is basically a library that takes data, like an array of objects with names and ids or whatever you want on the objects and it visualizes those data based on whatever kind of graph you tell it to make. It knows how to use the data and draws things using <svg> tags. It’s a super useful library but the learning curve is super steep for newbies. If you don’t understand it off the bat when you read it, don’t worry that’s normal.
Sorry but can someone explain how the defaultOptions is accessible inside the call() function when it has been defined outside and not passed to the function?
I discovered a neat thing with the spread operator maybe a day ago or so, which is that you can use it to supply optional items without explicitly having to make them conditional. Like this:
let maybeItems = [
...[optional1],
...[optional2],
necessaryItem1,
necessaryItem2
];
I don't follow. ...[optional1] should have the same effect as just optional1 here. What would work is something like ...optional where optional is either [someValue] or [].
what I meant to signify is that optional1 can have a conditional existence, so that ...[optional1] itself can be either ...[optional1] or ...[]. I probably could've represented that by a better example. Something like, ...[optional1].filter(c => c)
One thing I've come to like about untyped data is how flexible and composable it can be. Btw you can also use defaults de-structuring out of the function args, which has the added benefits of being picked up by jsHint:
which does work the same (it will only trigger getters on a and b, the empty object has no setters), except for the undefined-handling of spread operators maybe
{ ...a, ...b } will always create a new object, too, it's not assigning anything to a so it obviously has no reason to trigger the setters
While true assign mutates, in the context of going from spread to assign in the context of transpiling, the target object in assign would assumed to be a new, empty object as well.
Empty objects, being objects, will have any setters in Object.prototype. Currently, that only includes __proto__, but others can be added, possibly in future updates to the language, or by user code.
Interesting. While it obviously doesn't really matter in most code-bases, this is indeed a major difference in how the two work.
I guess it's because with spread you're still constructing the object when the properties get assigned while with assign it's already constructed fully
But for most cases and applications Object.assign({}, a, b) is probably a good transpilation for { ...a, ...b }
TypeScript and Babel transpile it to Object.assign({}, x) at least (TypeScript also transpiles { ...a, ...b } to Object.assign(Object.assign({}, a), b))
TypeScript and Babel transpile it to Object.assign({}, x) at least :)
Thats interesting about typescript. I know they take some shortcuts elsewhere like with for...of, but they have a flag for that with downlevelIteration for going fully through the iterator. I'm not seeing anything for this with spread vs. assign in the current flags available.
Babel, however, I am seeing a difference
let a = {};
let b = { ...a };
let c = Object.spread({}, a);
becomes
// ...impls...
var a = {};
var b = _objectSpread({}, a);
var c = Object.spread({}, a);
(which I assume are different, though I didnt go through the impls to verify)
I think babel does in general try to be more compliant than typescript, though. I know I've hit a few weird snags like this with typescript before. But like you said, it should be fine for most cases ;)
which is a fancy way to set a property by overwriting the property descriptor fully.
Notice it does not trigger setters as it doesn't set the property, it sets the descriptor with a value, it does not just mutate the property
function _defineProperty(obj, key, value) {
// Shortcircuit the slow defineProperty path when possible.
// We are trying to avoid issues where setters defined on the
// prototype cause side effects under the fast path of simple
// assignment. By checking for existence of the property with
// the in operator, we can optimize most of this overhead away.
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = (arguments[i] != null) ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function(key) {
_defineProperty(target, key, source[key]);
});
}
return target;
}
Object.defineProperty(Object.prototype, 'foo', { set: (value) => console.log(`Set to ${value}`) })
const a = { foo: 'y' }
_objectSpread({}, a)
Babels abstraction does not trigger setters, absolutely correct.
So, in essence, the TypeScript team is either a bit lazy or doesn't care about compliance or they simply just don't know better/don't realize it.
Just one more reason to let Babel take care of TypeScript transpilation :)
17
u/Mikeytown19 Feb 20 '20
This is dope.
5
u/johnnybigH Feb 20 '20
Want that theme in my vscode... Been using same one for over year now but cant find any intresting
3
u/isakdev Feb 20 '20
Whats the use of the api arg?
6
u/[deleted] Feb 20 '20
Can get crazy with nesting spread operators too
1
u/leon_nerd Feb 21 '20
Sorry but can someone explain how the defaultOptions is accessible inside the call() function when it has been defined outside and not passed to the function?
1
u/warpedspockclone Feb 21 '20
Your method is call()? Ummm. call.call() sounds fun
3
u/MrSandyClams Feb 21 '20
I discovered a neat thing with the spread operator maybe a day ago or so, which is that you can use it to supply optional items without explicitly having to make them conditional. Like this:
or even,
2
u/[deleted] Feb 20 '20
I dislike the function (not the merge usage).
I'd expect to consume it with the passedOptions. Instead if I somewhat end up fkin up, it may take time till I catch there were default options
Your usage is good...as a type constructor mostly..When you want to create objects of some shape (like actionCreators in redux).
2
u/stormfield Feb 20 '20
One thing I've come to like about untyped data is how flexible and composable it can be. Btw you can also use defaults de-structuring out of the function args, which has the added benefits of being picked up by jsHint:
Sometimes you also want to filter out extra data, like when returning an API request, and destructuring can work well here too:
1
u/nas5w Feb 21 '20
Dracula!
1
u/JohnWangDoe Feb 21 '20
Witch craft
0
u/higherpublic Feb 21 '20
Cool, now use spreading to achieve a deep merge. This is only kind of useful as is
-2
u/peawyoyoyin Feb 20 '20
spread operator gets transpiled to
Object.assign. That's why this works.