Coder Social home page Coder Social logo

arrayish's People

Contributors

gingerchew avatar

Watchers

 avatar

arrayish's Issues

Make options accessible with a getter throughout class

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.

Change Arrayish instance 'config'

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

Merge asyncEach and forEach?

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...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.