A Typescript helper library for server.
$ npm install sagus
Generates a RFC4122 version 1 UUID.
Example:
import sagus from 'sagus';
sagus.genUUID();
// => '74db8250-f6ad-11eb-bcfe-7d80d5a32604'
Generates a unique ID using the time, process and mac address.
Example:
sagus.genUID();
// => '2kabh61y04ks0asvvp'
sagus.genUID('prefix-');
// => 'prefix-2kabh61y04ks0asvvp'
sagus.genUID('', '-suffix');
// => '2kabh61y04ks0asvvp-suffix'
sagus.genUID('prefix-', '-suffix');
// => 'prefix-2kabh61y04ks0asvvp-suffix'
Generates a incremental ID for each namespace.
Example:
sagus.genAutoID(); // => 0
sagus.genAutoID(); // => 1
sagus.genAutoID('users'); // => 0
sagus.genAutoID('users'); // => 1
sagus.genAutoID(); // => 2
sagus.genAutoID('users'); // => 2
Resets an Auto ID counter and start again from 0.
Example:
sagus.genAutoID(); // => 0
sagus.genAutoID(); // => 1
sagus.resetAutoID();
sagus.genAutoID(); // => 0
Generates a random bytes of data in the encoding needed.
Example:
sagus.genRandom();
// => 'e58953b1068efba6367593a7392c56d179ac3ff0212c14bc0e24f5133dd1a411'
sagus.genRandom(4);
// => '630e0b2a'
sagus.genRandom(4, 'base64');
// => 'RofCEQ=='
Asynchronously generates a hash for the given string.
Example:
sagus.hash('password').then((hashedStr) => {
// hashedStr = '$2b$10$bb2gOp7GY7oXgljPwskaAuhdxM3HMrV7dBbawFjb9phvSFZOJ4MSK'
});
sagus.hash('password', 8).then((hashedStr) => {
// hashedStr = '$2b$08$mONvULNP4hN1ky4AMbxY4.1jIRFUYLrsxNfselWWaYJ9POwqh1Qbe'
});
Asynchronously compares the string and the hash.
Example:
sagus.compareHash('password', hashedStr).then((res) => {
// res = true
});
sagus.compareHash('not-password', hashedStr).then((res) => {
// res = false
});
Encodes the data in the format that is needed.
Example:
const encoded = sagus.encode({ greet: 'Hello, World !' });
// => 'eyJncmVldCI6IkhlbGxvLCBXb3JsZCAhIn0='
const encodedHex = sagus.encode({ greet: 'Hello, World !' }, 'hex');
// => '7b226772656574223a2248656c6c6f2c20576f726c642021227d'
Decodes the encoded data from the specified format.
Example:
sagus.decode('eyJncmVldCI6IkhlbGxvLCBXb3JsZCAhIn0=');
// => { greet: 'Hello, World !' }
sagus.decode('7b226772656574223a2248656c6c6f2c20576f726c642021227d', 'hex');
// => { greet: 'Hello, World !' }
Encrypts the data using the secret key and returns an initialization vector and encrypted data.
The Secret key must have exactly 32 bytes
Example:
const secretKey = 'xxxxxx 32 byte secret key xxxxxx';
const result = sagus.encrypt('secret data', secretKey);
// => { iv: 'N6pI0p0UKG1PdAYx8AtOzw==', encryptedData: 'b+oggS9U9tyk/Uyqhw==' }
Decrypts the encrypted data using the secret key and initialization vector and return the original data.
The Secret key must have exactly 32 bytes
Example:
const secretKey = 'xxxxxx 32 byte secret key xxxxxx';
const data = sagus.decrypt(result, secretKey);
// => 'secret data'
Trims and removes invalid fields from the object.
Example:
let obj = { name: 'Danny Joe', age: undefined, gender: '', dob: null };
sagus.trimObject(obj);
// => { name: 'Danny Joe' }
obj = { name: 'Diana', isMale: false, children: {} };
sagus.trimObject(obj);
// => { name: 'Diana', isMale: false, children: {} }
Returns a new object with only the keys selected. This method is type safe and the returned object will have the correct types.
Example:
const obj = { name: 'John Doe', age: 40, gender: 'male' };
const newObj = sagus.pickKeys(obj, ['name', 'age']);
// => { name: 'John Doe', age: 40 }
Returns a new object with the keys selected removed from the original object. This method is type safe and the returned object will have the correct types.
const newObj = sagus.removeKeys(obj, ['age']);
// => { name: 'John Doe', gender: 'male' }
Checks whether the provied value is valid or not.
Example:
sagus.isValid('Hello'); // => true
sagus.isValid(''); // => false
sagus.isValid(false); // => true
sagus.isValid(undefined); // => false
sagus.isValid(null); // => false
sagus.isValid(NaN); // => false
sagus.isValid({}); // => true
sagus.isValid([]); // => true
Checks whether the provied value is valid or not and also validates that objects are not empty.
Example:
// same as the above example for all cases except the last two
sagus.isValidObject({}); // => false
sagus.isValidObject([]); // => false
sagus.isValidObject({ name: 'John Doe' }); // => true
sagus.isValidObject({ name: '' }); // => true
sagus.isValidObject(['hello']); // => true
sagus.isValidObject([undefined]); // => true
creates a generator function that can iterate the array or object provided as the input.
Example:
const arr = [1, '2', 3, '4'];
const iterator = sagus.iterate(arr);
iterator.next(); // => { value: { key: 0, value: 1 }, done: false }
iterator.next(); // => { value: { key: 1, value: '2' }, done: false }
iterator.next(); // => { value: { key: 2, value: 3 }, done: false }
iterator.next(); // => { value: { key: 3, value: '4' }, done: true }
const obj = { 1: 'One', 2: 'Two' };
const iterator = sagus.iterate(obj);
iterator.next(); // => { value: { key: '1', value: 'One' }, done: false }
iterator.next(); // => { value: { key: '2', value: 'Two' }, done: true }