MCPcopy Create free account
hub / github.com/d3/d3-array / rank

Function rank

src/rank.js:4–24  ·  view source on GitHub ↗
(values, valueof = ascending)

Source from the content-addressed store, hash-verified

2import {ascendingDefined, compareDefined} from "./sort.js";
3
4export default function rank(values, valueof = ascending) {
5 if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable");
6 let V = Array.from(values);
7 const R = new Float64Array(V.length);
8 if (valueof.length !== 2) V = V.map(valueof), valueof = ascending;
9 const compareIndex = (i, j) => valueof(V[i], V[j]);
10 let k, r;
11 values = Uint32Array.from(V, (_, i) => i);
12 // Risky chaining due to Safari 14 https://github.com/d3/d3-array/issues/123
13 values.sort(valueof === ascending ? (i, j) => ascendingDefined(V[i], V[j]) : compareDefined(compareIndex));
14 values.forEach((j, i) => {
15 const c = compareIndex(j, k === undefined ? j : k);
16 if (c >= 0) {
17 if (k === undefined || c > 0) k = j, r = i;
18 R[j] = r;
19 } else {
20 R[j] = NaN;
21 }
22 });
23 return R;
24}

Callers 1

rank-test.jsFile · 0.85

Calls 3

ascendingDefinedFunction · 0.90
compareDefinedFunction · 0.90
compareIndexFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…