Skip to content

Instantly share code, notes, and snippets.

@colingourlay
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colingourlay/08f78b1eded17cbac950 to your computer and use it in GitHub Desktop.
Save colingourlay/08f78b1eded17cbac950 to your computer and use it in GitHub Desktop.
Simple unique ids from no-so-unique values
var uniquify = require('uniquifier')();
console.log(uniquify(1));
// > "1"
console.log(uniquify(2));
// > "2"
console.log(uniquify(1));
// > "1_1"
console.log(uniquify(3));
// > "3"
console.log(uniquify(1));
// > "1_2"
console.log(uniquify(3));
// > "3_1"
module.exports = function uniquifier(delimiter) {
var ids = {};
delimiter = delimiter || '_';
return function uniquify(value) {
var id = String(value);
if (ids[id] == null) {
ids[id] = 0;
return id;
}
ids[id]++;
return id + delimiter + ids[id];
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment