Class: .Suid

Class: .Suid

.suid. .Suid

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.

Source:

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-server and data-suid-options script 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}

Source:
Returns:

The current config after the given cfg object has been processed (if any).

<static> Suid.fromJSON(json)

Creates a new suid from the given JSON.

Parameters:
Name Type Description
json

The JSON string.

Source:
See:
  • ws.suid.Suid#toJSON
Returns:

The newly created suid.

<static> Suid.fromString(str)

Creates a new suid from the given string.

Parameters:
Name Type Description
str

The base-36 string.

Source:
See:
  • ws.suid.Suid#toString
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
str

The JSON string.

Source:
See:
  • ws.suid.Suid.fromString
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
str

The JSON string.

Source:
See:
  • ws.suid.Suid.looksValid
  • ws.suid.Suid.fromJSON
Returns:

True if it looks valid, false otherwise.

<static> Suid.next()

Generates the next suid.

Source:
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 === true

Parameters:
Name Type Description
key

The name of the property to be revived.

value

The value of the property to be revived.

Source:
See:
  • ws.suid.Suid.looksValidJSON
  • ws.suid.Suid.fromJSON
Returns:

A suid if the JSON looks like a valid suid, the original value otherwise.

Suid#equals()

Returns the underlying value of this suid.

Source:
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', where PREFIX is the string 'Suid:' and base-36 is 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.

Source:
Returns:

The base-36 string.

Suid#valueOf()

Returns the underlying value of this suid.

Source:
Returns:

The underlying primitive Number value.