-
-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathhash.js
More file actions
68 lines (62 loc) · 1.93 KB
/
hash.js
File metadata and controls
68 lines (62 loc) · 1.93 KB
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
import { toHash } from './to-hash';
import { update } from './update';
import { astish } from './astish';
import { parse } from './parse';
/**
* In-memory cache.
*/
let cache = {};
/**
* Stringifies a object structure
* @param {Object} data
* @returns {String}
*/
let stringify = (data) => {
if (typeof data == 'object') {
let out = '';
for (let p in data) out += p + stringify(data[p]);
return out;
} else {
return data;
}
};
/**
* Generates the needed className
* @param {String|Object} compiled
* @param {Object} sheet StyleSheet target
* @param {Object} global Global flag
* @param {Boolean} append Append or not
* @param {Boolean} keyframes Keyframes mode. The input is the keyframes body that needs to be wrapped.
* @returns {String}
*/
export let hash = (compiled, sheet, global, append, keyframes) => {
// Get a string representation of the object or the value that is called 'compiled'
let stringifiedCompiled = stringify(compiled);
// Retrieve the className from cache or hash it in place
let className =
cache[stringifiedCompiled] || (cache[stringifiedCompiled] = toHash(stringifiedCompiled));
// add or update
update(
(cache[className] =
cache[className] ||
// If there's no entry for the current className
// Parse it
parse(
// For keyframes
keyframes
? // Build the _ast_-ish structure if needed
{
['@keyframes ' + className]:
stringifiedCompiled !== compiled ? compiled : astish(compiled)
}
: stringifiedCompiled !== compiled
? compiled
: astish(compiled),
global ? '' : '.' + className
)),
sheet,
append
);
// return hash
return className;
};