-
Notifications
You must be signed in to change notification settings - Fork 1
/
can-symbol.js
130 lines (116 loc) · 2.91 KB
/
can-symbol.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"use strict";
var namespace = require("can-namespace");
var supportsNativeSymbols = (function() {
var symbolExists = typeof Symbol !== "undefined" && typeof Symbol.for === "function";
if (!symbolExists) {
return false;
}
var symbol = Symbol("a symbol for testing symbols");
return typeof symbol === "symbol";
}());
var CanSymbol;
if(supportsNativeSymbols) {
CanSymbol = Symbol;
} else {
var symbolNum = 0;
CanSymbol = function CanSymbolPolyfill(description){
var symbolValue = "@@symbol"+(symbolNum++)+(description);
var symbol = {}; // make it object type
Object.defineProperties(symbol, {
toString: {
value: function(){
return symbolValue;
}
}
});
return symbol;
};
var descriptionToSymbol = {};
var symbolToDescription = {};
/**
* @function can-symbol.for for
* @parent can-symbol/methods
* @description Get a symbol based on a known string identifier, or create it if it doesn't exist.
*
* @signature `canSymbol.for(String)`
*
* @param { String } description The string value of the symbol
* @return { CanSymbol } The globally unique and consistent symbol with the given string value.
*/
CanSymbol.for = function(description){
var symbol = descriptionToSymbol[description];
if(!symbol) {
symbol = descriptionToSymbol[description] = CanSymbol(description);
symbolToDescription[symbol] = description;
}
return symbol;
};
/**
* @function can-symbol.keyFor keyFor
* @parent can-symbol
* @description Get the description for a symbol.
*
* @signature `canSymbol.keyFor(CanSymbol)`
*
* @param { String } description The string value of the symbol
* @return { CanSymbol } The globally unique and consistent symbol with the given string value.
*/
CanSymbol.keyFor = function(symbol) {
return symbolToDescription[symbol];
};
["hasInstance","isConcatSpreadable",
"iterator","match","prototype","replace","search","species","split",
"toPrimitive","toStringTag","unscopables"].forEach(function(name){
CanSymbol[name] = CanSymbol("Symbol."+name);
});
}
// Generate can. symbols.
[
// ======= Type detection ==========
"isMapLike",
"isListLike",
"isValueLike",
"isFunctionLike",
"isScopeLike",
// ======= Shape detection =========
"getOwnKeys",
"getOwnKeyDescriptor",
"proto",
// optional
"getOwnEnumerableKeys",
"hasOwnKey",
"hasKey",
"size",
"getName",
"getIdentity",
// shape manipulation
"assignDeep",
"updateDeep",
// ======= GET / SET
"getValue",
"setValue",
"getKeyValue",
"setKeyValue",
"updateValues",
"addValue",
"removeValues",
// ======= Call =========
"apply",
"new",
// ======= Observe =========
"onValue",
"offValue",
"onKeyValue",
"offKeyValue",
"getKeyDependencies",
"getValueDependencies",
"keyHasDependencies",
"valueHasDependencies",
"onKeys",
"onKeysAdded",
"onKeysRemoved",
"onPatches"
].forEach(function(name){
CanSymbol.for("can."+name);
});
module.exports = namespace.Symbol = CanSymbol;