-
Notifications
You must be signed in to change notification settings - Fork 565
/
dedupe.js
70 lines (56 loc) · 1.55 KB
/
dedupe.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Don't inherit from Object so we can skip hasOwnProperty check later.
function StorageObject () {}
StorageObject.prototype = Object.create(null);
export default function classNames () {
const classSet = new StorageObject();
appendArray(classSet, arguments);
let classes = '';
for (const key in classSet) {
if (classSet[key]) {
classes += classes ? (' ' + key) : key;
}
}
return classes;
}
function appendValue (classSet, arg) {
if (!arg) return;
const argType = typeof arg;
if (argType === 'string') {
appendString(classSet, arg);
} else if (Array.isArray(arg)) {
appendArray(classSet, arg);
} else if (argType === 'object') {
appendObject(classSet, arg);
}
}
const SPACE = /\s+/;
function appendString (classSet, str) {
const array = str.split(SPACE);
const length = array.length;
for (let i = 0; i < length; i++) {
classSet[array[i]] = true;
}
}
function appendArray (classSet, array) {
const length = array.length;
for (let i = 0; i < length; i++) {
appendValue(classSet, array[i]);
}
}
const hasOwn = {}.hasOwnProperty;
function appendObject (classSet, object) {
if (
object.toString !== Object.prototype.toString &&
!object.toString.toString().includes('[native code]')
) {
classSet[object.toString()] = true;
return;
}
for (const k in object) {
if (hasOwn.call(object, k)) {
// Set value to false instead of deleting it to avoid changing object structure.
// https://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/#de-referencing-misconceptions
classSet[k] = !!object[k];
}
}
}