@x-oasis/unique-array-object 0.1.1 → 0.1.3
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/README.md +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/unique-array-object.cjs.development.js +23 -6
- package/dist/unique-array-object.cjs.development.js.map +1 -1
- package/dist/unique-array-object.cjs.production.min.js +1 -1
- package/dist/unique-array-object.cjs.production.min.js.map +1 -1
- package/dist/unique-array-object.esm.js +23 -6
- package/dist/unique-array-object.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +42 -11
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @x-oasis/unique-array-object
|
|
2
2
|
|
|
3
|
-
keep the last
|
|
3
|
+
keep the last on default
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -18,12 +18,12 @@ const state = [
|
|
|
18
18
|
{ name: 'viewable', value: 8 }
|
|
19
19
|
]
|
|
20
20
|
|
|
21
|
-
uniqueArrayObject(state)
|
|
21
|
+
uniqueArrayObject(state, 'name')
|
|
22
22
|
|
|
23
23
|
// output
|
|
24
24
|
// [
|
|
25
|
-
// { name: 'viewable', value: 8 },
|
|
26
25
|
// { name: 'imageViewable', value: 9 },
|
|
26
|
+
// { name: 'viewable', value: 8 },
|
|
27
27
|
// ]
|
|
28
28
|
```
|
|
29
29
|
|
package/dist/index.d.ts
CHANGED
|
@@ -2,12 +2,29 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
var shimFindLastIndex = function shimFindLastIndex(arr, filter) {
|
|
6
|
+
if (typeof Array.prototype.findLastIndex === 'function') return Array.prototype.findLastIndex.apply(arr, [filter]);
|
|
7
|
+
var len = arr.length;
|
|
8
|
+
for (var index = len - 1; index >= 0; index--) {
|
|
9
|
+
var item = arr[index];
|
|
10
|
+
if (filter(item, index)) return index;
|
|
11
|
+
}
|
|
12
|
+
return -1;
|
|
13
|
+
};
|
|
14
|
+
var index = (function (arr, getter, keepFirst) {
|
|
15
|
+
var filter = function filter(item, _item) {
|
|
16
|
+
return typeof getter === 'function' ? getter(_item) === getter(item) : _item[getter] === item[getter];
|
|
17
|
+
};
|
|
18
|
+
if (keepFirst) return arr.filter(function (item, index, _arr) {
|
|
19
|
+
return _arr.findIndex(function (_item) {
|
|
20
|
+
return filter(item, _item);
|
|
21
|
+
}) === index;
|
|
22
|
+
});
|
|
23
|
+
return arr.filter(function (item, index, _arr) {
|
|
24
|
+
return shimFindLastIndex(arr, function (_item) {
|
|
25
|
+
return filter(item, _item);
|
|
26
|
+
}) === index;
|
|
27
|
+
});
|
|
11
28
|
});
|
|
12
29
|
|
|
13
30
|
exports.default = index;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unique-array-object.cjs.development.js","sources":["../src/index.ts"],"sourcesContent":["// https://stackoverflow.com/a/56757215\n\nexport default (\n arr: Array<{\n [key: string]: any;\n }>,\n getter: string |
|
|
1
|
+
{"version":3,"file":"unique-array-object.cjs.development.js","sources":["../src/index.ts"],"sourcesContent":["// https://stackoverflow.com/a/56757215\n\nconst shimFindLastIndex = (arr: Array<any>, filter: Function) => {\n // @ts-ignore\n if (typeof Array.prototype.findLastIndex === 'function')\n // @ts-ignore\n return Array.prototype.findLastIndex.apply(arr, [filter]);\n\n const len = arr.length;\n for (let index = len - 1; index >= 0; index--) {\n const item = arr[index];\n if (filter(item, index)) return index;\n }\n return -1;\n};\n\nexport default (\n arr: Array<{\n [key: string]: any;\n }>,\n getter: string | { (item: any): any },\n keepFirst?: boolean\n) => {\n const filter = (item, _item) =>\n typeof getter === 'function'\n ? getter(_item) === getter(item)\n : _item[getter] === item[getter];\n\n if (keepFirst)\n return arr.filter(\n (item, index, _arr) =>\n _arr.findIndex((_item) => filter(item, _item)) === index\n );\n\n return arr.filter(\n (item, index, _arr) =>\n shimFindLastIndex(arr, (_item) => filter(item, _item)) === index\n );\n};\n\n// return [\n// ...new Map(\n// arr\n// .filter((v) => v)\n// .map((item) => [\n// typeof getter === 'function' ? getter(item) : item[getter],\n// item,\n// ])\n// ).values(),\n// ]\n"],"names":["shimFindLastIndex","arr","filter","Array","prototype","findLastIndex","apply","len","length","index","item","getter","keepFirst","_item","_arr","findIndex"],"mappings":";;;;AAEA,IAAMA,iBAAiB,GAAG,SAApBA,iBAAiBA,CAAIC,GAAe,EAAEC,MAAgB;EAE1D,IAAI,OAAOC,KAAK,CAACC,SAAS,CAACC,aAAa,KAAK,UAAU,EAErD,OAAOF,KAAK,CAACC,SAAS,CAACC,aAAa,CAACC,KAAK,CAACL,GAAG,EAAE,CAACC,MAAM,CAAC,CAAC;EAE3D,IAAMK,GAAG,GAAGN,GAAG,CAACO,MAAM;EACtB,KAAK,IAAIC,KAAK,GAAGF,GAAG,GAAG,CAAC,EAAEE,KAAK,IAAI,CAAC,EAAEA,KAAK,EAAE,EAAE;IAC7C,IAAMC,IAAI,GAAGT,GAAG,CAACQ,KAAK,CAAC;IACvB,IAAIP,MAAM,CAACQ,IAAI,EAAED,KAAK,CAAC,EAAE,OAAOA,KAAK;;EAEvC,OAAO,CAAC,CAAC;AACX,CAAC;AAED,aAAe,UACbR,GAEE,EACFU,MAAqC,EACrCC,SAAmB;EAEnB,IAAMV,MAAM,GAAG,SAATA,MAAMA,CAAIQ,IAAI,EAAEG,KAAK;IAAA,OACzB,OAAOF,MAAM,KAAK,UAAU,GACxBA,MAAM,CAACE,KAAK,CAAC,KAAKF,MAAM,CAACD,IAAI,CAAC,GAC9BG,KAAK,CAACF,MAAM,CAAC,KAAKD,IAAI,CAACC,MAAM,CAAC;;EAEpC,IAAIC,SAAS,EACX,OAAOX,GAAG,CAACC,MAAM,CACf,UAACQ,IAAI,EAAED,KAAK,EAAEK,IAAI;IAAA,OAChBA,IAAI,CAACC,SAAS,CAAC,UAACF,KAAK;MAAA,OAAKX,MAAM,CAACQ,IAAI,EAAEG,KAAK,CAAC;MAAC,KAAKJ,KAAK;IAC3D;EAEH,OAAOR,GAAG,CAACC,MAAM,CACf,UAACQ,IAAI,EAAED,KAAK,EAAEK,IAAI;IAAA,OAChBd,iBAAiB,CAACC,GAAG,EAAE,UAACY,KAAK;MAAA,OAAKX,MAAM,CAACQ,IAAI,EAAEG,KAAK,CAAC;MAAC,KAAKJ,KAAK;IACnE;AACH,CAAC;;;;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=function(
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=function(n,t,r){var e=function(n,r){return"function"==typeof t?t(r)===t(n):r[t]===n[t]};return n.filter(r?function(n,t,r){return r.findIndex((function(t){return e(n,t)}))===t}:function(t,r,u){return function(n,t){if("function"==typeof Array.prototype.findLastIndex)return Array.prototype.findLastIndex.apply(n,[t]);for(var r=n.length-1;r>=0;r--)if(t(n[r]))return r;return-1}(n,(function(n){return e(t,n)}))===r})};
|
|
2
2
|
//# sourceMappingURL=unique-array-object.cjs.production.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unique-array-object.cjs.production.min.js","sources":["../src/index.ts"],"sourcesContent":["// https://stackoverflow.com/a/56757215\n\nexport default (\n arr: Array<{\n [key: string]: any;\n }>,\n getter: string |
|
|
1
|
+
{"version":3,"file":"unique-array-object.cjs.production.min.js","sources":["../src/index.ts"],"sourcesContent":["// https://stackoverflow.com/a/56757215\n\nconst shimFindLastIndex = (arr: Array<any>, filter: Function) => {\n // @ts-ignore\n if (typeof Array.prototype.findLastIndex === 'function')\n // @ts-ignore\n return Array.prototype.findLastIndex.apply(arr, [filter]);\n\n const len = arr.length;\n for (let index = len - 1; index >= 0; index--) {\n const item = arr[index];\n if (filter(item, index)) return index;\n }\n return -1;\n};\n\nexport default (\n arr: Array<{\n [key: string]: any;\n }>,\n getter: string | { (item: any): any },\n keepFirst?: boolean\n) => {\n const filter = (item, _item) =>\n typeof getter === 'function'\n ? getter(_item) === getter(item)\n : _item[getter] === item[getter];\n\n if (keepFirst)\n return arr.filter(\n (item, index, _arr) =>\n _arr.findIndex((_item) => filter(item, _item)) === index\n );\n\n return arr.filter(\n (item, index, _arr) =>\n shimFindLastIndex(arr, (_item) => filter(item, _item)) === index\n );\n};\n\n// return [\n// ...new Map(\n// arr\n// .filter((v) => v)\n// .map((item) => [\n// typeof getter === 'function' ? getter(item) : item[getter],\n// item,\n// ])\n// ).values(),\n// ]\n"],"names":["arr","getter","keepFirst","filter","item","_item","index","_arr","findIndex","Array","prototype","findLastIndex","apply","length","shimFindLastIndex"],"mappings":"6FAiBEA,EAGAC,EACAC,GAEA,IAAMC,EAAS,SAACC,EAAMC,GAAK,MACP,mBAAXJ,EACHA,EAAOI,KAAWJ,EAAOG,GACzBC,EAAMJ,KAAYG,EAAKH,IAE7B,OACSD,EAAIG,OADTD,EAEA,SAACE,EAAME,EAAOC,GAAI,OAChBA,EAAKC,WAAU,SAACH,GAAK,OAAKF,EAAOC,EAAMC,QAAYC,GAIvD,SAACF,EAAME,EAAOC,GAAI,OAjCI,SAACP,EAAiBG,GAE1C,GAA6C,mBAAlCM,MAAMC,UAAUC,cAEzB,OAAOF,MAAMC,UAAUC,cAAcC,MAAMZ,EAAK,CAACG,IAGnD,IADA,IACSG,EADGN,EAAIa,OACO,EAAGP,GAAS,EAAGA,IAEpC,GAAIH,EADSH,EAAIM,IACQ,OAAOA,EAElC,OAAQ,EAuBJQ,CAAkBd,GAAK,SAACK,GAAK,OAAKF,EAAOC,EAAMC,QAAYC"}
|
|
@@ -1,9 +1,26 @@
|
|
|
1
|
-
var
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
var shimFindLastIndex = function shimFindLastIndex(arr, filter) {
|
|
2
|
+
if (typeof Array.prototype.findLastIndex === 'function') return Array.prototype.findLastIndex.apply(arr, [filter]);
|
|
3
|
+
var len = arr.length;
|
|
4
|
+
for (var index = len - 1; index >= 0; index--) {
|
|
5
|
+
var item = arr[index];
|
|
6
|
+
if (filter(item, index)) return index;
|
|
7
|
+
}
|
|
8
|
+
return -1;
|
|
9
|
+
};
|
|
10
|
+
var index = (function (arr, getter, keepFirst) {
|
|
11
|
+
var filter = function filter(item, _item) {
|
|
12
|
+
return typeof getter === 'function' ? getter(_item) === getter(item) : _item[getter] === item[getter];
|
|
13
|
+
};
|
|
14
|
+
if (keepFirst) return arr.filter(function (item, index, _arr) {
|
|
15
|
+
return _arr.findIndex(function (_item) {
|
|
16
|
+
return filter(item, _item);
|
|
17
|
+
}) === index;
|
|
18
|
+
});
|
|
19
|
+
return arr.filter(function (item, index, _arr) {
|
|
20
|
+
return shimFindLastIndex(arr, function (_item) {
|
|
21
|
+
return filter(item, _item);
|
|
22
|
+
}) === index;
|
|
23
|
+
});
|
|
7
24
|
});
|
|
8
25
|
|
|
9
26
|
export default index;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unique-array-object.esm.js","sources":["../src/index.ts"],"sourcesContent":["// https://stackoverflow.com/a/56757215\n\nexport default (\n arr: Array<{\n [key: string]: any;\n }>,\n getter: string |
|
|
1
|
+
{"version":3,"file":"unique-array-object.esm.js","sources":["../src/index.ts"],"sourcesContent":["// https://stackoverflow.com/a/56757215\n\nconst shimFindLastIndex = (arr: Array<any>, filter: Function) => {\n // @ts-ignore\n if (typeof Array.prototype.findLastIndex === 'function')\n // @ts-ignore\n return Array.prototype.findLastIndex.apply(arr, [filter]);\n\n const len = arr.length;\n for (let index = len - 1; index >= 0; index--) {\n const item = arr[index];\n if (filter(item, index)) return index;\n }\n return -1;\n};\n\nexport default (\n arr: Array<{\n [key: string]: any;\n }>,\n getter: string | { (item: any): any },\n keepFirst?: boolean\n) => {\n const filter = (item, _item) =>\n typeof getter === 'function'\n ? getter(_item) === getter(item)\n : _item[getter] === item[getter];\n\n if (keepFirst)\n return arr.filter(\n (item, index, _arr) =>\n _arr.findIndex((_item) => filter(item, _item)) === index\n );\n\n return arr.filter(\n (item, index, _arr) =>\n shimFindLastIndex(arr, (_item) => filter(item, _item)) === index\n );\n};\n\n// return [\n// ...new Map(\n// arr\n// .filter((v) => v)\n// .map((item) => [\n// typeof getter === 'function' ? getter(item) : item[getter],\n// item,\n// ])\n// ).values(),\n// ]\n"],"names":["shimFindLastIndex","arr","filter","Array","prototype","findLastIndex","apply","len","length","index","item","getter","keepFirst","_item","_arr","findIndex"],"mappings":"AAEA,IAAMA,iBAAiB,GAAG,SAApBA,iBAAiBA,CAAIC,GAAe,EAAEC,MAAgB;EAE1D,IAAI,OAAOC,KAAK,CAACC,SAAS,CAACC,aAAa,KAAK,UAAU,EAErD,OAAOF,KAAK,CAACC,SAAS,CAACC,aAAa,CAACC,KAAK,CAACL,GAAG,EAAE,CAACC,MAAM,CAAC,CAAC;EAE3D,IAAMK,GAAG,GAAGN,GAAG,CAACO,MAAM;EACtB,KAAK,IAAIC,KAAK,GAAGF,GAAG,GAAG,CAAC,EAAEE,KAAK,IAAI,CAAC,EAAEA,KAAK,EAAE,EAAE;IAC7C,IAAMC,IAAI,GAAGT,GAAG,CAACQ,KAAK,CAAC;IACvB,IAAIP,MAAM,CAACQ,IAAI,EAAED,KAAK,CAAC,EAAE,OAAOA,KAAK;;EAEvC,OAAO,CAAC,CAAC;AACX,CAAC;AAED,aAAe,UACbR,GAEE,EACFU,MAAqC,EACrCC,SAAmB;EAEnB,IAAMV,MAAM,GAAG,SAATA,MAAMA,CAAIQ,IAAI,EAAEG,KAAK;IAAA,OACzB,OAAOF,MAAM,KAAK,UAAU,GACxBA,MAAM,CAACE,KAAK,CAAC,KAAKF,MAAM,CAACD,IAAI,CAAC,GAC9BG,KAAK,CAACF,MAAM,CAAC,KAAKD,IAAI,CAACC,MAAM,CAAC;;EAEpC,IAAIC,SAAS,EACX,OAAOX,GAAG,CAACC,MAAM,CACf,UAACQ,IAAI,EAAED,KAAK,EAAEK,IAAI;IAAA,OAChBA,IAAI,CAACC,SAAS,CAAC,UAACF,KAAK;MAAA,OAAKX,MAAM,CAACQ,IAAI,EAAEG,KAAK,CAAC;MAAC,KAAKJ,KAAK;IAC3D;EAEH,OAAOR,GAAG,CAACC,MAAM,CACf,UAACQ,IAAI,EAAED,KAAK,EAAEK,IAAI;IAAA,OAChBd,iBAAiB,CAACC,GAAG,EAAE,UAACY,KAAK;MAAA,OAAKX,MAAM,CAACQ,IAAI,EAAEG,KAAK,CAAC;MAAC,KAAKJ,KAAK;IACnE;AACH,CAAC;;;;"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,19 +1,50 @@
|
|
|
1
1
|
// https://stackoverflow.com/a/56757215
|
|
2
2
|
|
|
3
|
+
const shimFindLastIndex = (arr: Array<any>, filter: Function) => {
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
if (typeof Array.prototype.findLastIndex === 'function')
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
return Array.prototype.findLastIndex.apply(arr, [filter]);
|
|
8
|
+
|
|
9
|
+
const len = arr.length;
|
|
10
|
+
for (let index = len - 1; index >= 0; index--) {
|
|
11
|
+
const item = arr[index];
|
|
12
|
+
if (filter(item, index)) return index;
|
|
13
|
+
}
|
|
14
|
+
return -1;
|
|
15
|
+
};
|
|
16
|
+
|
|
3
17
|
export default (
|
|
4
18
|
arr: Array<{
|
|
5
19
|
[key: string]: any;
|
|
6
20
|
}>,
|
|
7
|
-
getter: string |
|
|
21
|
+
getter: string | { (item: any): any },
|
|
22
|
+
keepFirst?: boolean
|
|
8
23
|
) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
const filter = (item, _item) =>
|
|
25
|
+
typeof getter === 'function'
|
|
26
|
+
? getter(_item) === getter(item)
|
|
27
|
+
: _item[getter] === item[getter];
|
|
28
|
+
|
|
29
|
+
if (keepFirst)
|
|
30
|
+
return arr.filter(
|
|
31
|
+
(item, index, _arr) =>
|
|
32
|
+
_arr.findIndex((_item) => filter(item, _item)) === index
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
return arr.filter(
|
|
36
|
+
(item, index, _arr) =>
|
|
37
|
+
shimFindLastIndex(arr, (_item) => filter(item, _item)) === index
|
|
38
|
+
);
|
|
19
39
|
};
|
|
40
|
+
|
|
41
|
+
// return [
|
|
42
|
+
// ...new Map(
|
|
43
|
+
// arr
|
|
44
|
+
// .filter((v) => v)
|
|
45
|
+
// .map((item) => [
|
|
46
|
+
// typeof getter === 'function' ? getter(item) : item[getter],
|
|
47
|
+
// item,
|
|
48
|
+
// ])
|
|
49
|
+
// ).values(),
|
|
50
|
+
// ]
|