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
- JSON string
- Other Suid
Examples
// Call Suid.next() get the next id
var id = Suid.next();
// call with a Number argument
var ZERO = Suid(0);
var ONE = new Suid(1);
// call with a base-36 string argument
var suid = Suid('14she');
// call with a JSON string of a suid
var revived = Suid('Suid:14she');
Parameters:
| Name | Type | Description |
|---|---|---|
value |
The Number or String value for the new Suid. |
Methods
-
<static> Suid.config()
-
Configures the suid generator and gets the current config.
This method can be used as an alternative for, or in addition to, specifying the configuration in the
data-suid-serveranddata-suid-optionsscript attributes.Examples:
// Assuming no config was set through script tag attributes... var config = Suid.config(); // config => {server:'', min:3, max:4} (defaults)Suid.config({ server: '/suid/suid.json', min: 5, max: 6 });
var config = Suid.config(); // config => {server:'/suid/suid.json', min:5, max:6}
config = Suid.config({max: 8}); // config => {server:'/suid/suid.json', min:5, max:8}
Returns:
The current config after the given
cfgobject has been processed (if any). -
<static> Suid.fromJSON(json)
-
Creates a new suid from the given JSON.
Parameters:
Name Type Description jsonThe JSON string.
Returns:
The newly created suid.
-
<static> Suid.fromString(str)
-
Creates a new suid from the given string.
Parameters:
Name Type Description strThe base-36 string.
Returns:
The newly created suid.
-
<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 might be a valid suid. There are no guarantees.
Parameters:
Name Type Description strThe JSON string.
Returns:
True if it looks valid, false otherwise.
-
<static> Suid.looksValidJSON(str)
-
Indicates whether the given JSON value looks like a valid suid.
If this method returns true, this only indicates that the JSON might be a valid suid. There are no guarantees.
Parameters:
Name Type Description strThe JSON string.
Returns:
True if it looks valid, false otherwise.
-
<static> Suid.next()
-
Generates the next suid.
Returns:
The next new suid.
-
<static> Suid.revive(key, value)
-
Reviver function to be used i.c.w. JSON.parse.
Example:
var object = { id: Suid(), name: 'Example' }; var json = JSON.stringify(object); // json === '{"id":"Suid:19b","name":"Example"}' var obj = JSON.parse(object, Suid.revive); // obj.id instanceof Suid === trueParameters:
Name Type Description keyThe name of the property to be revived.
valueThe value of the property to be revived.
Returns:
A suid if the JSON looks like a valid suid, the original value otherwise.
-
Suid#equals()
-
Returns the underlying value of this suid.
Returns:
The underlying primitive Number value.
-
Suid#toJSON()
-
Converts this suid to a JSON string.
The returned String will be of the format
'PREFIX:base-36', wherePREFIXis the string'Suid:'andbase-36is the suid in base-36.For example:
'Suid:14she'.- Source:
- See:
-
- ws.suid.Suid.PREFIX
- ws.suid.Suid.fromJSON
- ws.suid.Suid.looksValidJSON
- ws.suid.Suid.revive
Returns:
The JSON string.
-
Suid#toString()
-
Converts this suid to a base-36 string.
Returns:
The base-36 string.
-
Suid#valueOf()
-
Returns the underlying value of this suid.
Returns:
The underlying primitive Number value.