@x-oasis/unique-array-object
Advanced tools
Comparing version 0.1.1 to 0.1.2
declare const _default: (arr: { | ||
[key: string]: any; | ||
}[], getter: string | Function) => { | ||
}[], getter: string | ((item: any) => any), keepFirst?: boolean) => { | ||
[key: string]: any; | ||
}[]; | ||
export default _default; |
@@ -5,8 +5,25 @@ 'use strict'; | ||
var index = (function (arr, getter) { | ||
return [].concat(new Map(arr.filter(function (v) { | ||
return v; | ||
}).map(function (item) { | ||
return [typeof getter === 'function' ? getter(item) : item[getter], item]; | ||
})).values()); | ||
var shimFindLastIndex = function shimFindLastIndex(arr, filter) { | ||
if (typeof Array.prototype.findLastIndex === 'function') return Array.prototype.findLastIndex.apply(arr, [filter]); | ||
var len = arr.length; | ||
for (var index = len - 1; index >= 0; index--) { | ||
var item = arr[index]; | ||
if (filter(item, index)) return index; | ||
} | ||
return -1; | ||
}; | ||
var index = (function (arr, getter, keepFirst) { | ||
var filter = function filter(item, _item) { | ||
return typeof getter === 'function' ? getter(_item) === getter(item) : _item[getter] === item[getter]; | ||
}; | ||
if (keepFirst) return arr.filter(function (item, index, _arr) { | ||
return _arr.findIndex(function (_item) { | ||
return filter(item, _item); | ||
}) === index; | ||
}); | ||
return arr.filter(function (item, index, _arr) { | ||
return shimFindLastIndex(arr, function (_item) { | ||
return filter(item, _item); | ||
}) === index; | ||
}); | ||
}); | ||
@@ -13,0 +30,0 @@ |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=function(e,t){return[].concat(new Map(e.filter((function(e){return e})).map((function(e){return["function"==typeof t?t(e):e[t],e]}))).values())}; | ||
"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})}; | ||
//# sourceMappingURL=unique-array-object.cjs.production.min.js.map |
@@ -1,7 +0,24 @@ | ||
var index = (function (arr, getter) { | ||
return [].concat(new Map(arr.filter(function (v) { | ||
return v; | ||
}).map(function (item) { | ||
return [typeof getter === 'function' ? getter(item) : item[getter], item]; | ||
})).values()); | ||
var shimFindLastIndex = function shimFindLastIndex(arr, filter) { | ||
if (typeof Array.prototype.findLastIndex === 'function') return Array.prototype.findLastIndex.apply(arr, [filter]); | ||
var len = arr.length; | ||
for (var index = len - 1; index >= 0; index--) { | ||
var item = arr[index]; | ||
if (filter(item, index)) return index; | ||
} | ||
return -1; | ||
}; | ||
var index = (function (arr, getter, keepFirst) { | ||
var filter = function filter(item, _item) { | ||
return typeof getter === 'function' ? getter(_item) === getter(item) : _item[getter] === item[getter]; | ||
}; | ||
if (keepFirst) return arr.filter(function (item, index, _arr) { | ||
return _arr.findIndex(function (_item) { | ||
return filter(item, _item); | ||
}) === index; | ||
}); | ||
return arr.filter(function (item, index, _arr) { | ||
return shimFindLastIndex(arr, function (_item) { | ||
return filter(item, _item); | ||
}) === index; | ||
}); | ||
}); | ||
@@ -8,0 +25,0 @@ |
{ | ||
"name": "@x-oasis/unique-array-object", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "unique-array-object function", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
# @x-oasis/unique-array-object | ||
keep the last occurrence | ||
keep the last on default | ||
@@ -21,8 +21,8 @@ ## Installation | ||
uniqueArrayObject(state) | ||
uniqueArrayObject(state, 'name') | ||
// output | ||
// [ | ||
// { name: 'imageViewable', value: 9 }, | ||
// { name: 'viewable', value: 8 }, | ||
// { name: 'imageViewable', value: 9 }, | ||
// ] | ||
@@ -29,0 +29,0 @@ ``` |
// https://stackoverflow.com/a/56757215 | ||
const shimFindLastIndex = (arr: Array<any>, filter: Function) => { | ||
// @ts-ignore | ||
if (typeof Array.prototype.findLastIndex === 'function') | ||
// @ts-ignore | ||
return Array.prototype.findLastIndex.apply(arr, [filter]); | ||
const len = arr.length; | ||
for (let index = len - 1; index >= 0; index--) { | ||
const item = arr[index]; | ||
if (filter(item, index)) return index; | ||
} | ||
return -1; | ||
}; | ||
export default ( | ||
@@ -7,14 +21,31 @@ arr: Array<{ | ||
}>, | ||
getter: string | Function | ||
getter: string | { (item: any): any }, | ||
keepFirst?: boolean | ||
) => { | ||
return [ | ||
...new Map( | ||
arr | ||
.filter((v) => v) | ||
.map((item) => [ | ||
typeof getter === 'function' ? getter(item) : item[getter], | ||
item, | ||
]) | ||
).values(), | ||
]; | ||
const filter = (item, _item) => | ||
typeof getter === 'function' | ||
? getter(_item) === getter(item) | ||
: _item[getter] === item[getter]; | ||
if (keepFirst) | ||
return arr.filter( | ||
(item, index, _arr) => | ||
_arr.findIndex((_item) => filter(item, _item)) === index | ||
); | ||
return arr.filter( | ||
(item, index, _arr) => | ||
shimFindLastIndex(arr, (_item) => filter(item, _item)) === index | ||
); | ||
}; | ||
// return [ | ||
// ...new Map( | ||
// arr | ||
// .filter((v) => v) | ||
// .map((item) => [ | ||
// typeof getter === 'function' ? getter(item) : item[getter], | ||
// item, | ||
// ]) | ||
// ).values(), | ||
// ] |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
12541
113