fixed a bug in Array.unique(..) (lib/util.js)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-08-08 03:55:38 +03:00
parent db5e7083a7
commit b36716c1ad
3 changed files with 17 additions and 13 deletions

View File

@ -197,6 +197,7 @@ var SlideshowActions = actions.Actions({
length_limit: that.config['slideshow-interval-max-count'], length_limit: that.config['slideshow-interval-max-count'],
check: Date.str2ms, check: Date.str2ms,
unique: Date.str2ms, unique: Date.str2ms,
normalize: function(e){ return e.trim() },
sort: function(a, b){ sort: function(a, b){
return Date.str2ms(a) - Date.str2ms(b) }, return Date.str2ms(a) - Date.str2ms(b) },
}) })

View File

@ -80,14 +80,16 @@ Array.prototype.toKeys = function(normalize){
// NOTE: normalize will slow things down... // NOTE: normalize will slow things down...
Array.prototype.toMap = function(normalize){ Array.prototype.toMap = function(normalize){
return normalize ? return normalize ?
this.reduce(function(m, e, i){ this
m.set(normalize(e), i) .reduce(function(m, e, i){
return m m.set(normalize(e), i)
}, new Map()) return m
: this.reduce(function(m, e, i){ }, new Map())
m.set(e, i) : this
return m .reduce(function(m, e, i){
}, new Map()) } m.set(e, i)
return m
}, new Map()) }
// Return an array with duplicate elements removed... // Return an array with duplicate elements removed...
@ -97,7 +99,9 @@ Array.prototype.toMap = function(normalize){
// NOTE: for an array containing only strings use a much faster .uniqueStrings(..) // NOTE: for an array containing only strings use a much faster .uniqueStrings(..)
// NOTE: this may not work on IE... // NOTE: this may not work on IE...
Array.prototype.unique = function(normalize){ Array.prototype.unique = function(normalize){
return new Array(...(new Set(normalize ? this.map(normalize) : this))) } return normalize ?
[...new Map(this.map(function(e){ return [normalize(e), e] })).values()]
: [...(new Set(this))] }
// Compare two arrays... // Compare two arrays...

View File

@ -616,9 +616,8 @@ function(data, options){
// // If a function this will be used to normalize the values before // // If a function this will be used to normalize the values before
// // uniqueness check is performed... // // uniqueness check is performed...
// // // //
// // NOTE: this (if a function) is different from normalize above // // NOTE: if this is a function the value returned is only used
// // in that this will not store the normalized value, rather // // for uniqueness checking and will not be stored.
// // just use it for uniqueness testing...
// unique: <bool> | function(value){ ... }, // unique: <bool> | function(value){ ... },
// //
// // called when new item is added to list... // // called when new item is added to list...
@ -810,7 +809,7 @@ function(list, options){
// account for '$' as key binding marker... // account for '$' as key binding marker...
lst = lst.unique(function(e){ return e.replace(/\$/g, '') }) lst = lst.unique(function(e){ return e.replace(/\$/g, '') })
// unique normalized... // unique filter...
} else if(options.unique instanceof Function){ } else if(options.unique instanceof Function){
lst = lst.unique(options.unique) lst = lst.unique(options.unique)
} }