New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

array-prototype-functions

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-prototype-functions - npm Package Compare versions

Comparing version 2.6.1 to 2.7.0-beta.0

group-by-as-map.d.ts

9

group-by.d.ts

@@ -13,3 +13,12 @@ import './blank-module';

};
/**
* Returns an object with keys having lists of objects
* containing the given field value
*
* @param field field to group by
*/
groupBy(mapper: (item: T) => string): {
[key: string]: T[];
};
}
}

21

group-by.js
import './blank-module';
if (Array.prototype.groupBy === undefined) {
Array.prototype.groupBy = function (field) {
if (!field) {
Array.prototype.groupBy = function (fieldOrMapper) {
if (!fieldOrMapper) {
throw new Error('Need a field to group by');
}
return this.reduce((acc, item) => {
if (acc[item[field]])
acc[item[field]].push(item);
else
acc[item[field]] = [item];
return acc;
}, {});
const result = {};
this.forEach(item => {
const value = typeof fieldOrMapper === 'function' ? fieldOrMapper(item) : item[fieldOrMapper];
if (value !== undefined) {
if (result[value] === undefined)
result[value] = [];
result[value].push(item);
}
});
return result;
};
}
{
"name": "array-prototype-functions",
"version": "2.6.1",
"version": "2.7.0-beta.0",
"description": "Array prototype augmentation for easier arithmetics and overall pleasure!",

@@ -9,3 +9,4 @@ "scripts": {

"test:unit:watch": "jest --watch",
"build": "tsc"
"build": "tsc",
"prepublish": "npm install && npm run build && npm run test"
},

@@ -12,0 +13,0 @@ "author": "Matthias Hryniszak <padcom@gmail.com>",

@@ -40,2 +40,3 @@ <p align="center">

import 'array-prototype-functions/group-by'
import 'array-prototype-functions/group-by-as-map'
import 'array-prototype-functions/head'

@@ -55,2 +56,3 @@ import 'array-prototype-functions/last'

require('array-prototype-functions/group-by')
require('array-prototype-functions/group-by-as-map')
require('array-prototype-functions/head')

@@ -180,2 +182,13 @@ require('array-prototype-functions/last')

Objects that don't have the field in them will be skipped.
### `Array.prototype.groupByAsMap(field)`
Dependencies: none
Returns a Map of lists of objects from the array grouped by a field name. The field name is required.
The difference to `groupBy(field)` is that the return value is an instance of `Map<any, T[]>` which means that the original type of the values of the field is also preserved and not converted to a string. This also means that if throughout the objects in the source array field values have different types they will be treated differently and you'll end up having more than one item in the result.
Objects that don't have the field in them will be skipped.
### `Array.prototype.uniq(fieldOrMapper = null)`

@@ -182,0 +195,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc