new .Suid(value)
Distributed Service-Unique IDs that are short and sweet.
When called without arguments, defaults to Suid(0).
When called with an argument, constructs a new Suid based on the given value, which may be either a:
- Number
- Base-36 String
- Other Suid
This constructor function may be called without the new keyword.
Examples
// Call without arguments var NOP = new Suid(); // call with a Number argument var ZERO = new Suid(0); var ONE = new Suid(1); NOP.equals(ZERO); // true // New-less invocation var TWO = Suid(2); // call with a base-36 string argument var suid1 = Suid('14she'); // call with another suid as argument var suid2 = Suid(suid1);
Parameters:
Name | Type | Description |
---|---|---|
value |
The value for the new Suid. |
Methods
-
<static> Suid.config()
-
Configures the suid generator or gets the current config.
This method can be used as an alternative for, or in addition to, specifying the configuration in the data-suid-server and data-suid-options script attributes.
Examples:
// Assuming no config was set yet... (defaults) var config = Suid.config(); // config => {server:'/suid/suid.json', min:3, max:4}
Suid.config({ server: '/suid.json', min: 2, max: 6, seed: ['14she', '14sky'] });
// The seed is used to fill the pool and then discarded: var config = Suid.config(); // config => {server:'/suid.json', min:2, max:6}
// Config does a merge:
config = Suid.config({max: 8}); // config => {server:'/suid.json', min:2, max:8}Returns:
The current config after the given cfg object has been processed, if any.
-
<static> Suid.looksValid(str)
-
Indicates whether the given string value looks like a valid suid.
If this method returns true, this only indicates that it's probably valid. There are no guarantees.
Parameters:
Name Type Description str
The string to test.
Returns:
true if it looks valid, false otherwise.
-
<static> Suid.next()
-
Generates the next suid.
Returns:
The next new suid.
-
<static> Suid.revive(prop)
-
Creates a reviver function to be used i.c.w. JSON.parse.
Example:
var object = { id: Suid(), name: 'Example' }; var obj, json = JSON.stringify(object); // json === '{"id":"19b","name":"Example"}' obj = JSON.parse(json, Suid.revive('id')); // OR obj = JSON.parse(json, Suid.revive(['id'])); // OR obj = JSON.parse(json, Suid.revive(function(key,val){return key === 'id';})); // obj.id instanceof Suid === true
Parameters:
Name Type Description prop
The (array of) name(s) of properties to be revived, or an evaluator function.
Returns:
A reviver function
-
Suid#compare()
-
Compares this suid with that.
Returns:
-1 when this suid is less than, 0 when it is equal to and +1 when it is greater than that.
-
Suid#equals()
-
Indicates whether this suid and that are equal.
Returns:
true when the values are equal, false otherwise.
-
Suid#toJSON()
-
Converts this suid to a JSON string.
The returned String will be in base-36 format. For example: '14she'. This method is called by JSON.stringify.
Returns:
The JSON string.
-
Suid#toString()
-
Converts this suid to a string.
The returned String will be in base-36 format. For example: '14she'.
Returns:
The base-36 string.
-
Suid#valueOf()
-
Returns the underlying value of this suid.
Returns:
The underlying primitive Number value.