@aryth/rank
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -5,33 +5,26 @@ 'use strict'; | ||
/** | ||
* | ||
* @param {*[]} ar | ||
* @param {function(*,*):number} comparer Compare 'prev' & 'next' element in an array. If return < 0, 'prev' comes first. If return > 0, 'next' comes first. | ||
* @param {function(*):boolean} [filter] | ||
* @return {number[]} Rank order array, where 0 denote the first. | ||
*/ | ||
const rank = (ar, comparer, filter) => { | ||
const sorted = (!filter ? ar.slice() : ar.filter(filter)).sort(comparer); | ||
return ar.map(x => sorted.indexOf(x)); | ||
}; | ||
/** | ||
* | ||
* @param {*[]} ar | ||
* @param {number[]} ranks array of the same length as 'arr', containing rank order of 'arr', 0 comes first. | ||
* @return {*[]} | ||
*/ | ||
var RankVector = require('@aryth/rank-vector'); | ||
var RankMatrix = require('@aryth/rank-matrix'); | ||
var RankColumn = require('@aryth/rank-column'); | ||
var ComparerCollection = require('@aryth/comparer'); | ||
const reorderBy = (ar, ranks) => { | ||
const ve = Array(ar.length); | ||
const { | ||
NUM_ASC, | ||
NUM_DESC, | ||
STR_ASC, | ||
STR_DESC | ||
} = ComparerCollection; | ||
const { | ||
rank: rankVector | ||
} = RankVector; | ||
const { | ||
rank: rankMatrix | ||
} = RankMatrix; | ||
const { | ||
ColumnRank, | ||
MutateRank | ||
} = RankColumn; | ||
for (let [i, ord] of ranks.entries()) ve[ord] = ar[i]; | ||
return ve; | ||
}; | ||
const STR_ASC = (a, b) => a.localeCompare(b); | ||
const STR_DESC = (a, b) => b.localeCompare(a); | ||
const NUM_ASC = (a, b) => a - b; | ||
const NUM_DESC = (a, b) => b - a; | ||
exports.ColumnRank = ColumnRank; | ||
exports.MutateRank = MutateRank; | ||
exports.NUM_ASC = NUM_ASC; | ||
@@ -41,3 +34,3 @@ exports.NUM_DESC = NUM_DESC; | ||
exports.STR_DESC = STR_DESC; | ||
exports.rank = rank; | ||
exports.reorderBy = reorderBy; | ||
exports.rankMatrix = rankMatrix; | ||
exports.rankVector = rankVector; |
@@ -1,32 +0,23 @@ | ||
/** | ||
* | ||
* @param {*[]} ar | ||
* @param {function(*,*):number} comparer Compare 'prev' & 'next' element in an array. If return < 0, 'prev' comes first. If return > 0, 'next' comes first. | ||
* @param {function(*):boolean} [filter] | ||
* @return {number[]} Rank order array, where 0 denote the first. | ||
*/ | ||
const rank = (ar, comparer, filter) => { | ||
const sorted = (!filter ? ar.slice() : ar.filter(filter)).sort(comparer); | ||
return ar.map(x => sorted.indexOf(x)); | ||
}; | ||
/** | ||
* | ||
* @param {*[]} ar | ||
* @param {number[]} ranks array of the same length as 'arr', containing rank order of 'arr', 0 comes first. | ||
* @return {*[]} | ||
*/ | ||
import * as RankVector from '@aryth/rank-vector'; | ||
import * as RankMatrix from '@aryth/rank-matrix'; | ||
import * as RankColumn from '@aryth/rank-column'; | ||
import * as ComparerCollection from '@aryth/comparer'; | ||
const reorderBy = (ar, ranks) => { | ||
const ve = Array(ar.length); | ||
const { | ||
NUM_ASC, | ||
NUM_DESC, | ||
STR_ASC, | ||
STR_DESC | ||
} = ComparerCollection; | ||
const { | ||
rank: rankVector | ||
} = RankVector; | ||
const { | ||
rank: rankMatrix | ||
} = RankMatrix; | ||
const { | ||
ColumnRank, | ||
MutateRank | ||
} = RankColumn; | ||
for (let [i, ord] of ranks.entries()) ve[ord] = ar[i]; | ||
return ve; | ||
}; | ||
const STR_ASC = (a, b) => a.localeCompare(b); | ||
const STR_DESC = (a, b) => b.localeCompare(a); | ||
const NUM_ASC = (a, b) => a - b; | ||
const NUM_DESC = (a, b) => b - a; | ||
export { NUM_ASC, NUM_DESC, STR_ASC, STR_DESC, rank, reorderBy }; | ||
export { ColumnRank, MutateRank, NUM_ASC, NUM_DESC, STR_ASC, STR_DESC, rankMatrix, rankVector }; |
{ | ||
"name": "@aryth/rank", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "A math util library", | ||
@@ -17,2 +17,8 @@ "main": "dist/index.cjs.js", | ||
}, | ||
"dependencies": { | ||
"@aryth/comparer": "^0.0.7", | ||
"@aryth/rank-column": "^0.0.7", | ||
"@aryth/rank-matrix": "^0.0.7", | ||
"@aryth/rank-vector": "^0.0.7" | ||
}, | ||
"repository": { | ||
@@ -33,3 +39,3 @@ "type": "git", | ||
"homepage": "https://github.com/hoyeungw/aryth#readme", | ||
"gitHead": "2439c9922ca5c0f3b46e0bc91495355877f07cdb" | ||
"gitHead": "60dc39d0a05d205a93131c0e12af701f8bb7c094" | ||
} |
4646
4
51
+ Added@aryth/comparer@^0.0.7
+ Added@aryth/rank-column@^0.0.7
+ Added@aryth/rank-matrix@^0.0.7
+ Added@aryth/rank-vector@^0.0.7
+ Added@aryth/comparer@0.0.7(transitive)
+ Added@aryth/rank-column@0.0.7(transitive)
+ Added@aryth/rank-matrix@0.0.7(transitive)
+ Added@aryth/rank-vector@0.0.7(transitive)
+ Added@vect/column@0.0.10(transitive)
+ Added@vect/column-mapper@0.0.10(transitive)
+ Added@vect/column-quantifier@0.0.10(transitive)
+ Added@vect/column-zipper@0.0.10(transitive)
+ Added@vect/matrix-mapper@0.0.7(transitive)
+ Added@vect/matrix-size@0.0.100.0.7(transitive)
+ Added@vect/vector@0.0.10(transitive)
+ Added@vect/vector-mapper@0.0.100.0.7(transitive)
+ Added@vect/vector-zipper@0.0.10(transitive)