Initial commit

master
Sven Slootweg 2 months ago
commit 46a87dfc0d

@ -0,0 +1,24 @@
# objid
Tiny development utility. Assigns a unique ID to every object you give it, that persists for the lifetime of that object. Requires WeakMap support (to ensure it doesn't cause memory leaks by preventing garbage collection).
Particularly useful for eg. logging debug messages that may relate to any number of instances of the same kind of object, and you don't want to pass around a bunch of extra arguments to keep track of which one came from where. Instead you can correlate by logging the IDs of the instances, without having to pass any data anywhere other than the value itself.
## Example
```js
"use strict";
const objid = require("objid");
let a = {};
let b = {};
let c = new Date();
let d = function() {};
console.log(objid(a)); // 0
console.log(objid(b)); // 1
console.log(objid(c)); // 2
console.log(objid(d)); // 3
console.log(objid(b)); // 1 (an ID was assigned to that object previously)
```

@ -0,0 +1,14 @@
"use strict";
const objid = require("./");
let a = {};
let b = {};
let c = new Date();
let d = function() {};
console.log(objid(a)); // 0
console.log(objid(b)); // 1
console.log(objid(c)); // 2
console.log(objid(d)); // 3
console.log(objid(b)); // 1 (an ID was assigned to that object previously)

@ -0,0 +1,12 @@
"use strict";
const objectMap = new WeakMap();
let counter = 0;
module.exports = function objid(object) {
if (!objectMap.has(object)) {
objectMap.set(object, counter++);
}
return objectMap.get(object);
};

@ -0,0 +1,20 @@
{
"name": "objid",
"version": "1.0.0",
"description": "Small debugging utility that assigns unique IDs to objects without polluting them",
"main": "index.js",
"files": [
"index.js",
"example.js",
"README.md"
],
"repository": {
"url": "https://git.cryto.net/joepie91/objid.git"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": ["debugging", "object", "identifier", "unique"],
"author": "Sven Slootweg <admin@cryto.net>",
"license": "WTFPL OR CC0-1.0"
}
Loading…
Cancel
Save