Gathering detailed insights and metrics for @bemoje/arr-sort-comparator
Gathering detailed insights and metrics for @bemoje/arr-sort-comparator
Gathering detailed insights and metrics for @bemoje/arr-sort-comparator
Gathering detailed insights and metrics for @bemoje/arr-sort-comparator
@bemoje/arr-sort
Sort an array considerably faster than the native Array.prototype.sort as a drop-in replacement. Fork of of the famous timsort module, but this module allows for passing comparator-builder options instead of a comparator function. In short, advanced compa
@bemoje/arr-sorted-add
For a sorted array, add an element. Whichever comparator function was used to sort the array, can be passed. Also supports comparator-builder options. For reference, see: https://github.com/bemoje/bemoje-arr-sort-comparator
npm install @bemoje/arr-sort-comparator
Typescript
Module System
Node Version
NPM Version
71
Supply Chain
53.8
Quality
75.4
Maintenance
100
Vulnerability
100
License
JavaScript (98.05%)
Shell (1.95%)
Total Downloads
2,288
Last Day
1
Last Week
1
Last Month
30
Last Year
270
1 Stars
6 Commits
2 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.5
Package Id
@bemoje/arr-sort-comparator@1.0.5
Unpacked Size
26.70 kB
Size
6.41 kB
File Count
5
NPM Version
6.12.1
Node Version
12.13.0
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-92.3%
1
Compared to previous week
Last month
-36.2%
30
Compared to previous month
Last year
-32.2%
270
Compared to previous year
6
Create array comparator function.
1npm install @bemoje/arr-sort-comparator 2npm install --save @bemoje/arr-sort-comparator 3npm install --save-dev @bemoje/arr-sort-comparator
1 2import compare from '@bemoje/arr-sort-comparator' 3 4let arr 5 6/** 7 * DATA: STRINGS 8 * -------------- 9 */ 10 11arr = ['5', '2', '4', '30', '1', '3'] 12 13/** 14 * SORT ALPHABETICALLY BY DEFAULT 15 * ------------------------------ 16 */ 17 18arr.sort(compare()) 19//=> ['1', '2', '3', '30', '4', '5'] 20 21/** 22 * DATA: NUMERIC VALUES 23 * ---------------------- 24 */ 25 26arr = [5, 2, 4, 30, 1, 3] 27 28/** 29 * SORT NUMERICALLY 30 * ---------------- 31 */ 32 33arr.sort( 34 compare({ 35 numeric: true, 36 }), 37) 38//=> [1, 2, 3, 4, 5, 30] 39 40/** 41 * SORT DESCENDING 42 * --------------- 43 */ 44 45arr.sort( 46 compare({ 47 numeric: true, 48 descending: true, 49 }), 50) 51//=> [30, 5, 4, 3, 2, 1] 52 53/** 54 * DATA: PERSON OBJECTS 55 * -------------------- 56 */ 57 58arr = [ 59 { name: 'john', age: 4 }, 60 { name: 'bill', age: 8 }, 61] 62 63/** 64 * SORT OBJECTS BY PROPERTY 65 * ------------------------ 66 */ 67 68arr.sort( 69 compare({ 70 by: 'name', 71 }), 72) 73 74/* => 75 [ 76 { name: 'bill', age: 8 }, 77 { name: 'john', age: 4 }, 78 ] 79*/ 80 81arr.sort( 82 compare({ 83 numeric: true, 84 by: 'age', 85 }), 86) 87 88/* => 89 [ 90 { name: 'john', age: 4 }, 91 { name: 'bill', age: 8 }, 92 ] 93*/ 94 95/** 96 * DATA: PERSON OBJECTS WITH NESTED NAME OBJECTS 97 * --------------------------------------------- 98 */ 99 100arr = [ 101 { id: 0, name: { first: 'snoop', last: 'doggy' } }, 102 { id: 1, name: { first: 'kurt', last: 'qobain' } }, 103] 104 105/** 106 * SORT OBJECTS BY NESTED PROPERTY WITH DOT NOTATION 107 * ------------------------------------------------- 108 */ 109 110arr.sort( 111 compare({ 112 by: 'name.first', 113 }), 114) 115 116/* => 117 [ 118 { id: 1, name: { first: 'kurt', last: 'qobain' } }, 119 { id: 0, name: { first: 'snoop', last: 'doggy' } }, 120 ] 121*/ 122 123arr.sort( 124 compare({ 125 by: 'name.last', 126 }), 127) 128 129/* => 130 [ 131 { id: 0, name: { first: 'snoop', last: 'doggy' } }, 132 { id: 1, name: { first: 'kurt', last: 'qobain' } }, 133 ] 134*/ 135 136/** 137 * DATA: STRING DIRECTORY PATHS SPLIT IN ARRAYS 138 * -------------------------------------------- 139 */ 140 141arr = [ 142 ['repo', 'src', 'compare.js'], 143 ['repo', 'docs', 'index.html'], 144] 145 146/** 147 * SORT BY ARRAY INDEX 148 * ------------------- 149 */ 150 151arr.sort( 152 compare({ 153 by: 2, 154 }), 155) 156 157/* => 158 [ 159 ['repo', 'src', 'compare.js'], 160 ['repo', 'docs', 'index.html'], 161 ] 162*/ 163 164arr.sort( 165 compare({ 166 by: 1, 167 }), 168) 169 170/* => 171 [ 172 ['repo', 'docs', 'index.html' ], 173 ['repo', 'src', 'compare.js'], 174 ] 175*/ 176 177/** 178 * DATA: DIRECTORY PATHS ARRAYS WITH SUB-ARRAYS 179 * -------------------------------------------- 180 */ 181 182arr = [ 183 ['repo', 'src', ['compare', 'json']], 184 ['repo', 'src', ['compare', 'ts']], 185 ['repo', 'src', ['compare', 'js']], 186] 187 188/** 189 * SORT ARRAYS AND SUB-ARRAYS RECURSIVELY 190 * ------------------------------------ 191 */ 192 193arr.sort( 194 compare({ 195 arrays: true, 196 }), 197) 198 199/* => 200 [ 201 ['repo', 'src', ['compare', 'js']], 202 ['repo', 'src', ['compare', 'json']], 203 ['repo', 'src', ['compare', 'ts']], 204 ] 205*/ 206 207/** 208 * DATA: IP ADDRESSES AS ARRAYS 209 * ---------------------------- 210 */ 211 212arr = [ 213 [192, 168, 0, 1], 214 [192, 168, 0, 101], 215 [172, 0, 0, 1], 216] 217 218/** 219 * SORT NUMERIC IP-ADDRESSES AS ARRAYS 220 * ----------------------------------- 221 */ 222 223arr.sort( 224 compare({ 225 numeric: true, 226 arrays: true, 227 }), 228) 229 230/* => 231 [ 232 [172, 0, 0, 1], 233 [192, 168, 0, 1], 234 [192, 168, 0, 101], 235 ] 236*/ 237 238/** 239 * DATA: USER CLASS INSTANCES 240 * -------------------------- 241 */ 242 243class User { 244 constructor(firstName, lastName) { 245 this.firstName = firstName 246 this.lastName = lastName 247 } 248 get fullName() { 249 return this.firstName + ' ' + this.lastName 250 } 251} 252 253arr = [ 254 new User('john', 'doe'), 255 new User('peter', 'wick'), 256 new User('peter', 'johnson'), 257 new User('les', 'paul'), 258] 259 260/** 261 * SORT BY GETTER-FUNCTION 262 * ------------------------ 263 */ 264 265arr.sort( 266 compare({ 267 by: (user) => { 268 return user.fullName 269 }, 270 }), 271) 272 273/* => 274 [ 275 { firstName: 'john', lastName: 'doe'}, 276 { firstName: 'les', lastName: 'paul'}, 277 { firstName: 'peter', lastName: 'johnson'}, 278 { firstName: 'peter', lastName: 'wick'}, 279 ] 280*/ 281
Uses Jest to test module functionality. Run tests to get coverage details.
1npm run test
Create array comparator function.
options
object?
options.numeric
boolean Sort numerically. Defaults to lexicographic/alphabetic sort. (optional, default false
)
options.descending
boolean Sort in descending order. Defaults to ascending order. (optional, default false
)
options.array
boolean Sort arrays. Nested arrays are also compared recursively. (optional, default false
)
options.by
(number | string | getter) Sort by either array index, a callback(element): any - or by object keys with dot-notation support. (optional, default undefined
)
Returns comparator
Type: Function
a
any The first value to compare
b
any The second value to compare
Returns number A negative number if a > b, a positive number if a < b, 0 otherwise.
Type: Function
a
any The valueReturns any The value to be compared
Numerical comparison of items. If the passed objects are not numbers, their .valueOf() methods are called to retrieve a numeric value representation of them.
Returns number A positive number if a > b, a negative number if a < b, 0 otherwise.
Alphabetical comparison of items.
Returns number A positive number if a.toString() > b.toString(), a negative number if .toString() < b.toString(), 0 otherwise.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/6 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
53 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More