@shgysk8zer0/polyfills 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/array.js CHANGED
@@ -85,59 +85,45 @@
85
85
  }
86
86
 
87
87
  /**
88
+ * @deprecated [moved to `Object.groupBy()`]
88
89
  * @see https://github.com/tc39/proposal-array-grouping
89
90
  */
90
- if (! (Array.prototype.group instanceof Function)) {
91
- Array.prototype.group = function group(callback, thisArg = globalThis) {
92
- return this.reduce((groups, item, index, arr) => {
93
- const key = callback.call(thisArg, item, index, arr);
94
-
95
- if (! groups.hasOwnProperty(key)) {
96
- groups[key] = [item];
97
- } else {
98
- groups[key].push(item);
99
- }
100
-
101
- return groups;
102
- }, {});
91
+ if (! (Array.prototype.group instanceof Function) && Object.groupBy instanceof Function) {
92
+ Array.prototype.group = function group(callback) {
93
+ console.warn('`Array.group()` is deprecated. Please use `Object.groupBy()` instead.');
94
+ return Object.groupBy(this, callback);
103
95
  };
104
96
  }
105
97
 
106
98
  /**
107
- * @deprecated [renamed to `group()`]
99
+ * @deprecated [moved to `Object.groupBy()`]
108
100
  */
109
- Array.prototype.groupBy = function groupBy(...args) {
110
- console.warn('`goupBy` is deprecated. Please use `group` instead.');
111
- return this.group(...args);
112
- };
101
+ if (! (Array.prototype.groupBy instanceof Function) && Object.groupBy instanceof Function) {
102
+ Array.prototype.groupBy = function groupBy(callback) {
103
+ console.warn('`Array.goupBy()` is deprecated. Please use `Object.groupBy()` instead.');
104
+ return Object.groupBy(this, callback);
105
+ };
106
+ }
113
107
 
114
108
  /**
115
109
  * @see https://github.com/tc39/proposal-array-grouping
110
+ * @deprecated [moved to `Map.groupBy()`]
116
111
  * @requires `Map.prototype.emplace`
117
112
  */
118
- if (! (Array.prototype.groupToMap instanceof Function) && (Map.prototype.emplace instanceof Function)) {
119
- Array.prototype.groupToMap = function groupToMap(callback, thisArg = globalThis) {
120
- return this.reduce((map, item, index, arr) => {
121
- map.emplace(callback.call(thisArg, item, index, arr), {
122
- insert: () => [item],
123
- update: existing => {
124
- existing.push(item);
125
- return existing;
126
- }
127
- });
128
-
129
- return map;
130
- }, new Map());
113
+ if (! (Array.prototype.groupToMap instanceof Function) && (Map.groupBy instanceof Function)) {
114
+ Array.prototype.groupToMap = function groupToMap(callback) {
115
+ console.warn('`Array.groupToMap()` is deprecated. Please use `Map.groupBy()` instead.');
116
+ return Map.groupBy(this, callback);
131
117
  };
132
118
  }
133
119
 
134
120
  /**
135
- * @deprecated [renamed to `groupToMap()`]
121
+ * @deprecated [moved to `Map.groupBy()`]
136
122
  */
137
- if (Map.prototype.emplace instanceof Function) {
138
- Array.prototype.groupByToMap = function groupByToMap(...args) {
139
- console.warn('`goupByToMap` is deprecated. Please use `groupToMap` instead.');
140
- return this.groupToMap(...args);
123
+ if (Map.groupBy instanceof Function) {
124
+ Array.prototype.groupByToMap = function groupByToMap(callback) {
125
+ console.warn('`Array.groupByToMap()` is deprecated. Please use `Map.groupBy()` instead.');
126
+ return Map.groupBy(this, callback);
141
127
  };
142
128
  }
143
129
 
package/map.js CHANGED
@@ -29,4 +29,20 @@
29
29
  }
30
30
  };
31
31
  }
32
+
33
+ if (! (Map.groupBy instanceof Function)) {
34
+ Map.groupBy = function groupTo(items, callbackFn) {
35
+ return Array.from(items).reduce((map, element, index) => {
36
+ map.emplace(callbackFn.call(map, element, index), {
37
+ insert: () => [element],
38
+ update: existing => {
39
+ existing.push(element);
40
+ return existing;
41
+ }
42
+ });
43
+
44
+ return map;
45
+ }, new Map());
46
+ };
47
+ }
32
48
  })();
package/object.js ADDED
@@ -0,0 +1,15 @@
1
+ if (! (Object.groupBy instanceof Function)) {
2
+ Object.groupBy = function(items, callbackFn) {
3
+ return Array.from(items).reduce((groups, element, index) => {
4
+ const key = callbackFn.call(groups, element, index);
5
+
6
+ if (! groups.hasOwnProperty(key)) {
7
+ groups[key] = [element];
8
+ } else {
9
+ groups[key].push(element);
10
+ }
11
+
12
+ return groups;
13
+ }, {});
14
+ };
15
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shgysk8zer0/polyfills",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "A collection of JavaScript polyfills",