mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2024-09-20 07:16:17 +08:00
arrays
This commit is contained in:
parent
0c3d938d75
commit
b80129760e
|
@ -187,16 +187,6 @@ export function formatSeconds(
|
|||
return `${normalized} ${unit}${normalized > 1 ? "s" : ""}`;
|
||||
}
|
||||
|
||||
export function intersect<T>(a: T[], b: T[], removeDuplicates = false): T[] {
|
||||
let t: T[];
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||
if (b.length > a.length) (t = b), (b = a), (a = t); // indexOf to loop over shorter
|
||||
const filtered = a.filter(function (e) {
|
||||
return b.includes(e);
|
||||
});
|
||||
return removeDuplicates ? [...new Set(filtered)] : filtered;
|
||||
}
|
||||
|
||||
export function isDevEnvironment(): boolean {
|
||||
return process.env["MODE"] === "dev";
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import _ from "lodash";
|
||||
import { intersect } from "./misc";
|
||||
import { default as FunboxList } from "../constants/funbox-list";
|
||||
import { CompletedEvent } from "@monkeytype/contracts/schemas/results";
|
||||
import { intersect } from "@monkeytype/util/arrays";
|
||||
|
||||
export function isTestTooShort(result: CompletedEvent): boolean {
|
||||
const { mode, mode2, customText, testDuration, bailedOut } = result;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import * as FunboxList from "./funbox-list";
|
||||
import * as Notifications from "../../elements/notifications";
|
||||
import * as Arrays from "../../utils/arrays";
|
||||
import * as Strings from "../../utils/strings";
|
||||
import { Config, ConfigValue } from "@monkeytype/contracts/schemas/configs";
|
||||
import { intersect } from "@monkeytype/util/arrays";
|
||||
|
||||
export function checkFunboxForcedConfigs(
|
||||
key: string,
|
||||
|
@ -43,7 +43,7 @@ export function checkFunboxForcedConfigs(
|
|||
if (forcedConfigs[key] === undefined) {
|
||||
forcedConfigs[key] = fb.forcedConfig[key] as ConfigValue[];
|
||||
} else {
|
||||
forcedConfigs[key] = Arrays.intersect(
|
||||
forcedConfigs[key] = intersect(
|
||||
forcedConfigs[key],
|
||||
fb.forcedConfig[key] as ConfigValue[],
|
||||
true
|
||||
|
@ -302,7 +302,7 @@ export function areFunboxesCompatible(
|
|||
for (const key in f.forcedConfig) {
|
||||
if (allowedConfig[key]) {
|
||||
if (
|
||||
Arrays.intersect(
|
||||
intersect(
|
||||
allowedConfig[key],
|
||||
f.forcedConfig[key] as ConfigValue[],
|
||||
true
|
||||
|
|
|
@ -99,19 +99,3 @@ export function nthElementFromArray<T>(
|
|||
index = index < 0 ? array.length + index : index;
|
||||
return array[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the intersection of two arrays, i.e., the elements that are present in both arrays.
|
||||
* @param a First array.
|
||||
* @param b Second array.
|
||||
* @returns An array containing the elements that are present in both input arrays.
|
||||
*/
|
||||
export function intersect<T>(a: T[], b: T[], removeDuplicates = false): T[] {
|
||||
let t;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||
if (b.length > a.length) (t = b), (b = a), (a = t); // indexOf to loop over shorter
|
||||
const filtered = a.filter(function (e) {
|
||||
return b.includes(e);
|
||||
});
|
||||
return removeDuplicates ? [...new Set(filtered)] : filtered;
|
||||
}
|
||||
|
|
48
packages/util/__test__/arrays.spec.ts
Normal file
48
packages/util/__test__/arrays.spec.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
import * as Arrays from "../src/arrays";
|
||||
|
||||
describe("arrays", () => {
|
||||
it("intersect", () => {
|
||||
const testCases = [
|
||||
{
|
||||
a: [1],
|
||||
b: [2],
|
||||
removeDuplicates: false,
|
||||
expected: [],
|
||||
},
|
||||
{
|
||||
a: [1],
|
||||
b: [1],
|
||||
removeDuplicates: false,
|
||||
expected: [1],
|
||||
},
|
||||
{
|
||||
a: [1, 1],
|
||||
b: [1],
|
||||
removeDuplicates: true,
|
||||
expected: [1],
|
||||
},
|
||||
{
|
||||
a: [1, 1],
|
||||
b: [1],
|
||||
removeDuplicates: false,
|
||||
expected: [1, 1],
|
||||
},
|
||||
{
|
||||
a: [1],
|
||||
b: [1, 2, 3],
|
||||
removeDuplicates: false,
|
||||
expected: [1],
|
||||
},
|
||||
{
|
||||
a: [1, 1],
|
||||
b: [1, 2, 3],
|
||||
removeDuplicates: true,
|
||||
expected: [1],
|
||||
},
|
||||
];
|
||||
|
||||
testCases.forEach(({ a, b, removeDuplicates, expected }) => {
|
||||
expect(Arrays.intersect(a, b, removeDuplicates)).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
15
packages/util/src/arrays.ts
Normal file
15
packages/util/src/arrays.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
/**
|
||||
* Returns the intersection of two arrays, i.e., the elements that are present in both arrays.
|
||||
* @param a First array.
|
||||
* @param b Second array.
|
||||
* @returns An array containing the elements that are present in both input arrays.
|
||||
*/
|
||||
export function intersect<T>(a: T[], b: T[], removeDuplicates = false): T[] {
|
||||
let t;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||
if (b.length > a.length) (t = b), (b = a), (a = t); // indexOf to loop over shorter
|
||||
const filtered = a.filter(function (e) {
|
||||
return b.includes(e);
|
||||
});
|
||||
return removeDuplicates ? [...new Set(filtered)] : filtered;
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
export function testIndex(): void {
|
||||
console.log("testIndex");
|
||||
}
|
|
@ -7,7 +7,8 @@
|
|||
"declarationMap": true,
|
||||
"moduleResolution": "Node",
|
||||
"module": "ES6",
|
||||
"target": "ES2015"
|
||||
"target": "ES2015",
|
||||
"lib": ["es2016"]
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
|
|
Loading…
Reference in a new issue