/
can-cid.js
54 lines (52 loc) · 1.57 KB
/
can-cid.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
"use strict";
var namespace = require('can-namespace');
/**
* @module {function} can-cid
* @parent can-typed-data
* @collection can-infrastructure
* @package ./package.json
* @description Utility for getting a unique identifier for an object.
* @signature `cid(object, optionalObjectType)`
*
* Get a unique identifier for the object, optionally prefixed by a type name.
*
* Once set, the unique identifier does not change, even if the type name
* changes on subsequent calls.
*
* ```js
* var cid = require("can-cid");
* var x = {};
* var y = {};
*
* console.log(cid(x, "demo")); // -> "demo1"
* console.log(cid(x, "prod")); // -> "demo1"
* console.log(cid(y)); // -> "2"
* ```
*
* @param {Object} object The object to uniquely identify.
* @param {String} name An optional type name with which to prefix the identifier
*
* @return {String} Returns the unique identifier
*/
var _cid = 0;
// DOM nodes shouldn't all use the same property
var domExpando = "can" + new Date();
var cid = function (object, name) {
var propertyName = object.nodeName ? domExpando : "_cid";
if (!object[propertyName]) {
_cid++;
object[propertyName] = (name || '') + _cid;
}
return object[propertyName];
};
cid.domExpando = domExpando;
cid.get = function(object){
var type = typeof object;
var isObject = type !== null && (type === "object" || type === "function");
return isObject ? cid(object) : (type + ":" + object);
};
if (namespace.cid) {
throw new Error("You can't have two versions of can-cid, check your dependencies");
} else {
module.exports = namespace.cid = cid;
}