gingerchew / arrayish Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
I couldn't seem to get this to work,
This is what I figured out so far:
set options(options: PropertyDescriptor[]) {
const [k, v] = options.map(option => Object.entries(option));
const config = Object.assign(
{},
Object.getOwnPropertyDescriptor(this, 'config'),
{
value: {
[`${k}`]: v,
},
}
);
Object.defineProperties(this, {
config,
});
}
get options() {
const descriptors = Object.getOwnPropertyDescriptors(this);
return {
...descriptors
};
}
I'm wondering if I might be better off using Object.getOwnPropertyDescriptor(this,'config')
? Maybe that will return the config option I am looking for.
I will have to double check but I believe the setter will merge the value of the config descriptor and the other value, but I may need to do more testing.
The goal here is to make it so that options don't need to be tracked by passing the initial options object over and over through out the Arrayish
instance, and instead can be added on construct.
I need to do some research, as this may be a foot-gun/anti-pattern.
The idea would work something like this:
const alwaysUniqueArrayish = new Arrayish({ unique: true }, 1, 1, 1);
console.log(alwaysUniqueArrayish) // [ 1 ]
console.log(Object.getPropertyDescriptors(alwaysUniqueArrayish)) // { ... unique: true }
Arrayish.setUnique(alwaysUniqueArrayish, false);
alwaysUniqueArrayish.push(1);
console.log(alwaysUniqueArrayish) // [ 1, 1 ]
console.log(Object.getPropertyDescriptors(alwaysUniqueArrayish)) // { ... unique: false }
But again, part of me feels like this is a foot-gun waiting to happen
The functions forEach
and asyncEach
are almost exactly the same,
forEach(fn: Function) {
var i = 0,
len = this.length;
for (; i < len; ) {
fn(this[i], i, this);
i++;
}
}
async asyncEach(fn: Function) {
var i = 0,
len = this.length;
for (; i < len; ) {
await fn(this[i], i, this);
i++;
}
}
I'm wondering if it is possible to merge the two? Couldn't something like this do essentially the same thing?
async asyncEach(fn: Function, isAsync:? boolean) {
var i = 0,
len = this.length,
wait = this.is('async'); // or check instance for a property descriptor labeling it as async?
for (; i < len; ) {
if (wait) {
await fn(this[i], i, this);
} else {
fn(this[i], i, this);
}
i++;
}
}
Or is this not possible due to how async functions work? I know that async/await is basically sugar over Promises, but if an await
falls in a forest and no one is around to hear it...
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.