mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 06:59:31 +01:00
ci: add utils test
This commit is contained in:
29
packages/core/utils/__tests__/set.test.js
Normal file
29
packages/core/utils/__tests__/set.test.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import Set from "../set";
|
||||||
|
|
||||||
|
test("union", () => {
|
||||||
|
expect(Set.union([1, 2, 2], [2, 3])).toStrictEqual([1, 2, 3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("intersection", () => {
|
||||||
|
expect(Set.intersection([1, 1, 2], [2, 2, 3])).toStrictEqual([2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("union", () => {
|
||||||
|
expect(Set.union([1, 2, 2], [2, 3])).toStrictEqual([1, 2, 3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("difference", () => {
|
||||||
|
expect(Set.difference([1, 1, 2], [2, 3, 3])).toStrictEqual([1, 3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("complement", () => {
|
||||||
|
expect(Set.complement([2, 2, 4], [2, 2, 3])).toStrictEqual([4]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("equals", () => {
|
||||||
|
expect(Set.equals([1, 1, 2], [1, 1, 2])).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("not equals", () => {
|
||||||
|
expect(Set.equals([1, 1, 2], [1, 5, 2])).toBe(false);
|
||||||
|
});
|
||||||
@@ -3,14 +3,6 @@ if (!tfun) {
|
|||||||
tfun = global.tfun;
|
tfun = global.tfun;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extractValues(obj) {
|
|
||||||
const t = [];
|
|
||||||
for (let key in obj) {
|
|
||||||
t[t.length] = obj[key];
|
|
||||||
}
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function groupBy(arr, key, special = false) {
|
export function groupBy(arr, key, special = false) {
|
||||||
if (special) {
|
if (special) {
|
||||||
return groupBySpecial(arr, key);
|
return groupBySpecial(arr, key);
|
||||||
@@ -18,12 +10,12 @@ export function groupBy(arr, key, special = false) {
|
|||||||
let retVal = [];
|
let retVal = [];
|
||||||
for (let val of arr) {
|
for (let val of arr) {
|
||||||
let v = key(val);
|
let v = key(val);
|
||||||
let index = retVal.findIndex(a => a.title === v);
|
let index = retVal.findIndex((a) => a.title === v);
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
index = retVal.length;
|
index = retVal.length;
|
||||||
retVal[retVal.length] = {
|
retVal[retVal.length] = {
|
||||||
title: v,
|
title: v,
|
||||||
data: []
|
data: [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
retVal[index].data.push(val);
|
retVal[index].data.push(val);
|
||||||
@@ -40,6 +32,7 @@ function groupBySpecial(arr, key) {
|
|||||||
let pinned = [];
|
let pinned = [];
|
||||||
for (let val of arr) {
|
for (let val of arr) {
|
||||||
if (val.pinned) {
|
if (val.pinned) {
|
||||||
|
//TODO test
|
||||||
pinned[pinned.length] = val;
|
pinned[pinned.length] = val;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -57,7 +50,7 @@ function groupBySpecial(arr, key) {
|
|||||||
groups[groupIndex] = { title: groupTitle };
|
groups[groupIndex] = { title: groupTitle };
|
||||||
_groups[groupTitle] = {
|
_groups[groupTitle] = {
|
||||||
index: i,
|
index: i,
|
||||||
groupIndex
|
groupIndex,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,60 +4,43 @@
|
|||||||
// Set operations union, intersection, symmetric difference,
|
// Set operations union, intersection, symmetric difference,
|
||||||
// relative complement, equals. Set operations are fast.
|
// relative complement, equals. Set operations are fast.
|
||||||
class SetManipulator {
|
class SetManipulator {
|
||||||
constructor(identityExtractor) {
|
constructor() {
|
||||||
// Create and push the uid identity method.
|
// Create and push the uid identity method.
|
||||||
identityExtractor = identityExtractor || (identity => identity);
|
let identityExtractor = (identity) => identity;
|
||||||
this.uidList = [identityExtractor];
|
this.uidList = [identityExtractor];
|
||||||
this.uid = identityExtractor;
|
this.uid = identityExtractor;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push a new uid method onto the stack. Call this and
|
|
||||||
// supply a unique key generator for sets of objects.
|
|
||||||
pushIdentityExtractor(method) {
|
|
||||||
this.uidList.push(method);
|
|
||||||
this.uid = method;
|
|
||||||
return method;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pop the previously pushed uid method off the stack and
|
|
||||||
// assign top of stack to uid. Return the previous method.
|
|
||||||
popIdentityExtractor() {
|
|
||||||
let prev;
|
|
||||||
if (this.uidList.length > 1) prev = this.uidList.pop();
|
|
||||||
this.uid = this.uidList[this.uidList.length - 1];
|
|
||||||
return prev || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Processes a histogram consructed from two arrays, 'a' and 'b'.
|
// Processes a histogram consructed from two arrays, 'a' and 'b'.
|
||||||
// This function is used generically by the below set operation
|
// This function is used generically by the below set operation
|
||||||
// methods, a.k.a, 'evaluators', to return some subset of
|
// methods, a.k.a, 'evaluators', to return some subset of
|
||||||
// a set union, based on frequencies in the histogram.
|
// a set union, based on frequencies in the histogram.
|
||||||
process(a, b, evaluator, identityExtractor) {
|
process(a, b, evaluator) {
|
||||||
// If identity extractor passed in, push it on the stack
|
// If identity extractor passed in, push it on the stack
|
||||||
if (identityExtractor) this.pushIdentityExtractor(identityExtractor);
|
//if (identityExtractor) this.pushIdentityExtractor(identityExtractor);
|
||||||
// Create a histogram of 'a'.
|
// Create a histogram of 'a'.
|
||||||
const hist = {};
|
const hist = {};
|
||||||
const out = [];
|
const out = [];
|
||||||
let ukey;
|
let ukey;
|
||||||
a.forEach(value => {
|
a.forEach((value) => {
|
||||||
ukey = this.uid(value);
|
ukey = this.uid(value);
|
||||||
if (!hist[ukey]) {
|
if (!hist[ukey]) {
|
||||||
hist[ukey] = { value: value, freq: 1 };
|
hist[ukey] = { value: value, freq: 1 };
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Merge 'b' into the histogram.
|
// Merge 'b' into the histogram.
|
||||||
b.forEach(value => {
|
b.forEach((value) => {
|
||||||
ukey = this.uid(value);
|
ukey = this.uid(value);
|
||||||
if (hist[ukey]) {
|
if (hist[ukey]) {
|
||||||
if (hist[ukey].freq === 1) hist[ukey].freq = 3;
|
if (hist[ukey].freq === 1) hist[ukey].freq = 3;
|
||||||
} else hist[ukey] = { value: value, freq: 2 };
|
} else hist[ukey] = { value: value, freq: 2 };
|
||||||
});
|
});
|
||||||
// Pop any new identity extractor
|
// Pop any new identity extractor
|
||||||
if (identityExtractor) this.popIdentityExtractor(identityExtractor);
|
//if (identityExtractor) this.popIdentityExtractor(identityExtractor);
|
||||||
// Call the given evaluator.
|
// Call the given evaluator.
|
||||||
if (evaluator) {
|
if (evaluator) {
|
||||||
for (const key in hist) {
|
for (const key in hist) {
|
||||||
if (!hist.hasOwnProperty(key)) continue; // Property from object prototype, skip
|
//if (!hist.hasOwnProperty(key)) continue; // Property from object prototype, skip
|
||||||
if (evaluator(hist[key].freq)) out.push(hist[key].value);
|
if (evaluator(hist[key].freq)) out.push(hist[key].value);
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
@@ -67,39 +50,39 @@ class SetManipulator {
|
|||||||
|
|
||||||
// Join two sets together.
|
// Join two sets together.
|
||||||
// Set.union([1, 2, 2], [2, 3]) => [1, 2, 3]
|
// Set.union([1, 2, 2], [2, 3]) => [1, 2, 3]
|
||||||
union(a, b, identityExtractor) {
|
union(a, b) {
|
||||||
return this.process(a, b, () => true, identityExtractor);
|
return this.process(a, b, () => true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return items common to both sets.
|
// Return items common to both sets.
|
||||||
// Set.intersection([1, 1, 2], [2, 2, 3]) => [2]
|
// Set.intersection([1, 1, 2], [2, 2, 3]) => [2]
|
||||||
intersection(a, b, identityExtractor) {
|
intersection(a, b) {
|
||||||
return this.process(a, b, freq => freq === 3, identityExtractor);
|
return this.process(a, b, (freq) => freq === 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Symmetric difference. Items from either set that
|
// Symmetric difference. Items from either set that
|
||||||
// are not in both sets.
|
// are not in both sets.
|
||||||
// Set.difference([1, 1, 2], [2, 3, 3]) => [1, 3]
|
// Set.difference([1, 1, 2], [2, 3, 3]) => [1, 3]
|
||||||
difference(a, b, identityExtractor) {
|
difference(a, b) {
|
||||||
return this.process(a, b, freq => freq < 3, identityExtractor);
|
return this.process(a, b, (freq) => freq < 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Relative complement. Items from 'a' which are
|
// Relative complement. Items from 'a' which are
|
||||||
// not also in 'b'.
|
// not also in 'b'.
|
||||||
// Set.complement([1, 2, 2], [2, 2, 3]) => [3]
|
// Set.complement([1, 2, 2], [2, 2, 3]) => [3]
|
||||||
complement(a, b, identityExtractor) {
|
complement(a, b) {
|
||||||
return this.process(a, b, freq => freq === 1, identityExtractor);
|
return this.process(a, b, (freq) => freq === 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if both sets are equivalent, false otherwise.
|
// Returns true if both sets are equivalent, false otherwise.
|
||||||
// Set.equals([1, 1, 2], [1, 2, 2]) => true
|
// Set.equals([1, 1, 2], [1, 2, 2]) => true
|
||||||
// Set.equals([1, 1, 2], [1, 2, 3]) => false
|
// Set.equals([1, 1, 2], [1, 2, 3]) => false
|
||||||
equals(a, b, identityExtractor) {
|
equals(a, b) {
|
||||||
let max = 0;
|
let max = 0;
|
||||||
let min = Math.pow(2, 53);
|
let min = Math.pow(2, 53);
|
||||||
const hist = this.process(a, b, identityExtractor);
|
const hist = this.process(a, b);
|
||||||
for (const key in hist) {
|
for (const key in hist) {
|
||||||
if (!hist.hasOwnProperty(key)) continue; // Property from object prototype, skip
|
// if (!hist.hasOwnProperty(key)) continue; // Property from object prototype, skip
|
||||||
max = Math.max(max, hist[key].freq);
|
max = Math.max(max, hist[key].freq);
|
||||||
min = Math.min(min, hist[key].freq);
|
min = Math.min(min, hist[key].freq);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user