npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

array-group-polyfill

v0.1.3

Published

A polyfill for the yet to be supported by all browsers Array.prototype.group()

Downloads

1

Readme

array-group-polyfill

A project that aims to provide a polyfill for the upcoming array.prototype.group and array.prototype.groupToMap functions.

Array.prototype.group MDN docs

Array.prototype.groupToMap MDN docs

Directory Structure

.
├── .changeset
├── .github/
│   └── workflows/
│       ├── main.yml
│       └── publish.yml
├── src/
│   └── index.ts
├── .gitignore
├── CHANGELOG.md
├── package-lock.json
├── package.json
└── tsconfig.json

Installing

Clone the repository

git clone https://github.com/ToniMaunde/array-group-polyfill.git

Then navigate inside the folder

cd array-group-polyfill

Install the dependencies with

npm install

After that is done, just run

npm run dev

How it works

You pass an array of objects and specify a property from an object that you intend to be used for the grouping. If you want to use a criterion alongside the property for grouping, you can pass a third argument that is an object with the criterion (as an enum), the value (as number, string or boolean), the case sensitity for a string related criterion (as a boolean), a name for the property that will hold the array of the elements of the original collection that satify the criterion and a name for the property for those who do not. Since you're weird like me, find below the type and function declaration since I'm sure that'll be easier to understand than my fat paragraph above.

type GroupingCondition = {
	value: string | number | boolean;
	criterion: "EQUAL_STRINGS" | "STRING_INCLUDES" | "EQUAL_BOOLEANS" | "EQUAL_NUMBERS" | "GREATER_THAN" | "LESS_THAN" | "GREATER_THAN_OR_EQUAL" | "LESS_THAN_OR_EQUAL";
	caseSensitive?: boolean;
	groupMeetsCriterion?: string;
	groupFailsCriterion?: string;
}

function group<T extends Object, KeyType extends keyof T>(collection: T[], attributeName: KeyType, condition?: GroupingCondition) {

	const groupingObject: Record<string, T[]> = {};

	if (condition !== undefined) {

		const caseSensitive = condition.caseSensitive ?? true;
		const meetsCriterion = condition.groupMeetsCriterion ?? "passes";
		const failsCriterion = condition.groupFailsCriterion ?? "fails";

		if (condition.criterion === "EQUAL_BOOLEANS") {
			const innerCollectionForMatchingCriterion: T[] = collection.filter(element => {
				return (element[attributeName] as unknown as boolean) === condition.value
			});

			const innerCollectionForFailingCriterion: T[] = collection.filter(element => {
				return (element[attributeName] as unknown as boolean) !== condition.value
			});

			groupingObject[meetsCriterion] = innerCollectionForMatchingCriterion;
			groupingObject[failsCriterion] = innerCollectionForFailingCriterion;

			return groupingObject;
		}

		if (condition.criterion === "EQUAL_STRINGS") {
			const valueAsString = condition.value as string;

			const innerCollectionForMatchingCriterion: T[] = collection.filter(element => {
				if (caseSensitive) {
					return (element[attributeName] as unknown as string) === valueAsString
				}
				return (element[attributeName] as unknown as string).toLowerCase() === valueAsString.toLowerCase()
			});

			const innerCollectionForFailingCriterion: T[] = collection.filter(element => {
				if (caseSensitive) {
					return (element[attributeName] as unknown as string) !== valueAsString
				}
				return (element[attributeName] as unknown as string).toLowerCase() !== valueAsString.toLowerCase()
			});

			groupingObject[meetsCriterion] = innerCollectionForMatchingCriterion;
			groupingObject[failsCriterion] = innerCollectionForFailingCriterion;

			return groupingObject;
		}

		if (condition.criterion === "STRING_INCLUDES") {
			const valueAsString = condition.value as string;

			const innerCollectionForMatchingCriterion: T[] = collection.filter(element => {
				if (caseSensitive) {
					return (element[attributeName] as unknown as string).includes(valueAsString)
				}
				return (element[attributeName] as unknown as string).toLowerCase().includes(valueAsString.toLowerCase())
			});

			const innerCollectionForFailingCriterion: T[] = collection.filter(element => {
				if (caseSensitive) {
				return !(element[attributeName] as unknown as string).includes(valueAsString)
				}
				return !(element[attributeName] as unknown as string).toLowerCase().includes(valueAsString.toLowerCase())
			});

			groupingObject[meetsCriterion] = innerCollectionForMatchingCriterion;
			groupingObject[failsCriterion] = innerCollectionForFailingCriterion;

			return groupingObject;
		}

		if (condition.criterion === "EQUAL_NUMBERS") {
			const valueAsNumber = condition.value as number;

			const innerCollectionForMatchingCriterion: T[] = collection.filter(element => {
				return (element[attributeName] as unknown as number) === valueAsNumber
			});

			const innerCollectionForFailingCriterion: T[] = collection.filter(element => {
				return (element[attributeName] as unknown as number) !== valueAsNumber
			});

			groupingObject[meetsCriterion] = innerCollectionForMatchingCriterion;
			groupingObject[failsCriterion] = innerCollectionForFailingCriterion;

			return groupingObject;
		}

		if (condition.criterion === "GREATER_THAN") {
			const valueAsNumber = condition.value as number;

			const innerCollectionForMatchingCriterion: T[] = collection.filter(element => {
				return (element[attributeName] as unknown as number) > valueAsNumber
			});
			const innerCollectionForFailingCriterion: T[] = collection.filter(element => {
				return (element[attributeName] as unknown as number) <= valueAsNumber
			});

			groupingObject[meetsCriterion] = innerCollectionForMatchingCriterion;
			groupingObject[failsCriterion] = innerCollectionForFailingCriterion;

			return groupingObject;
		}

		if (condition.criterion === "GREATER_THAN_OR_EQUAL") {
			const valueAsNumber = condition.value as number;

			const innerCollectionForMatchingCriterion: T[] = collection.filter(element => {
				return (element[attributeName] as unknown as number) >= valueAsNumber
			});
			const innerCollectionForFailingCriterion: T[] = collection.filter(element => {
				return (element[attributeName] as unknown as number) < valueAsNumber
			});

			groupingObject[meetsCriterion] = innerCollectionForMatchingCriterion;
			groupingObject[failsCriterion] = innerCollectionForFailingCriterion;

			return groupingObject;
		}

		if (condition.criterion === "LESS_THAN") {
			const valueAsNumber = condition.value as number;

			const innerCollectionForMatchingCriterion: T[] = collection.filter(element => {
				return (element[attributeName] as unknown as number) < valueAsNumber
			});
			const innerCollectionForFailingCriterion: T[] = collection.filter(element => {
				return (element[attributeName] as unknown as number) >= valueAsNumber
			});

			groupingObject[meetsCriterion] = innerCollectionForMatchingCriterion;
			groupingObject[failsCriterion] = innerCollectionForFailingCriterion;

			return groupingObject;
		}

		if (condition.criterion === "LESS_THAN_OR_EQUAL") {
			const valueAsNumber = condition.value as number;

			const innerCollectionForMatchingCriterion: T[] = collection.filter(element => {
				return (element[attributeName] as unknown as number) <= valueAsNumber
			});
			const innerCollectionForFailingCriterion: T[] = collection.filter(element => {
				return (element[attributeName] as unknown as number) > valueAsNumber
			});

			groupingObject[meetsCriterion] = innerCollectionForMatchingCriterion;
			groupingObject[failsCriterion] = innerCollectionForFailingCriterion;

			return groupingObject;
		}

		return groupingObject;
	}

	// Default grouping behavior
	const valuesForAttributeToGroupBy = collection.map(item => item[attributeName]);

	for (const attributeValue of valuesForAttributeToGroupBy) {
		const itemsGroup = collection.filter(item => item[attributeName] === attributeValue);

		const attributeValueAsAString = attributeValue as string;
		groupingObject[attributeValueAsAString] = itemsGroup;
	};

	return groupingObject;
}

Examples

All examples will be based on this array

const inventory = [
  { name: "asparagus", type: "vegetables", quantity: 5 },
  { name: "bananas", type: "fruit", quantity: 0 },
  { name: "goat", type: "meat", quantity: 23 },
  { name: "cherries", type: "fruit", quantity: 5 },
  { name: "fish", type: "meat", quantity: 22 },
];
  1. Grouping by the type attribute
const grouping = arrayGroup(inventory, "type");

/* Result is:
{
  vegetables: [
    { name: 'asparagus', type: 'vegetables', quantity: 5 },
  ],
  fruit: [
    { name: "bananas", type: "fruit", quantity: 0 },
    { name: "cherries", type: "fruit", quantity: 5 }
  ],
  meat: [
    { name: "goat", type: "meat", quantity: 23 },
    { name: "fish", type: "meat", quantity: 22 }
  ]
}
*/
  1. Grouping by the quantity attribute with a criterion of 'quantity' > 5
const grouping = arrayGroup(inventory, "quantity", {
  value: 5,
  criterion: "GREATER_THAN",
  groupMeetsCriterion: "ok",
  groupFailsCriterion: "restock"
});

/* Result is:
{
  restock: [
    { name: "asparagus", type: "vegetables", quantity: 5 },
    { name: "bananas", type: "fruit", quantity: 0 },
    { name: "cherries", type: "fruit", quantity: 5 }
  ],
  ok: [
    { name: "goat", type: "meat", quantity: 23 },
    { name: "fish", type: "meat", quantity: 22 }
  ]
}
*/

Need more examples? Let me know.

Built with

I used Typescript for building this package.

Contributing

  1. Check the existing issue backlog
  2. Comment that you'll be working on it
  3. Add a changeset to the issue/feature branch you'll use for the development
  4. Submit a PR All help is welcome :).

Authors

Me, myself and I...thus far.

License

The MIT license.