1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import type { Range } from 'vscode-languageserver'
import { rangesEqual } from './rangesEqual'
export function dedupe<T>(arr: Array<T>): Array<T> {
return arr.filter((value, index, self) => self.indexOf(value) === index)
}
export function dedupeBy<T>(arr: Array<T>, transform: (item: T) => any): Array<T> {
return arr.filter((value, index, self) => self.map(transform).indexOf(transform(value)) === index)
}
export function dedupeByRange<T extends { range: Range }>(arr: Array<T>): Array<T> {
return arr.filter(
(classList, classListIndex) =>
classListIndex === arr.findIndex((c) => rangesEqual(c.range, classList.range))
)
}
export function ensureArray<T>(value: T | T[]): T[] {
return Array.isArray(value) ? value : [value]
}
export function flatten<T>(arrays: T[][]): T[] {
return [].concat.apply([], arrays)
}
export function equal(a: any[], b: any[]): boolean {
if (a === b) return true
if (a.length !== b.length) return false
let aSorted = a.concat().sort()
let bSorted = b.concat().sort()
for (let i = 0; i < aSorted.length; ++i) {
if (aSorted[i] !== bSorted[i]) return false
}
return true
}
export function equalExact(a: any[], b: any[]): boolean {
if (a === b) return true
if (a.length !== b.length) return false
for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false
}
return true
}
|