1.0.3 • Published 2 years ago

sagus v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Sagus Logo

NPM Version Build Status

A Typescript helper library for server.

Install

$ npm install sagus

Usage

sagus.genUUID()

Generates a RFC4122 version 1 UUID.

Example:

import sagus from 'sagus';

sagus.genUUID();
// => '74db8250-f6ad-11eb-bcfe-7d80d5a32604'

sagus.genUID([prefix, sufix])

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'

sagus.genAutoID([namespace])

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

sagus.resetAutoID([namespace])

Resets an Auto ID counter and start again from 0.

Example:

sagus.genAutoID(); // => 0
sagus.genAutoID(); // => 1
sagus.resetAutoID();
sagus.genAutoID(); // => 0

sagus.genRandom([size, encoding])

Generates a random bytes of data in the encoding needed.

Example:

sagus.genRandom();
// => 'e58953b1068efba6367593a7392c56d179ac3ff0212c14bc0e24f5133dd1a411'

sagus.genRandom(4);
// => '630e0b2a'

sagus.genRandom(4, 'base64');
// => 'RofCEQ=='

sagus.hash(str, salt)

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'
});

sagus.compareHash(str, hashedStr)

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
});

sagus.encode(data, encoding)

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'

sagus.decode(data, encoding)

Decodes the encoded data from the specified format.

Example:

sagus.decode('eyJncmVldCI6IkhlbGxvLCBXb3JsZCAhIn0=');
// => { greet: 'Hello, World !' }

sagus.decode('7b226772656574223a2248656c6c6f2c20576f726c642021227d', 'hex');
// => { greet: 'Hello, World !' }

sagus.encrypt(data, secretKey)

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==' }

sagus.decrypt(result, secretKey)

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'

sagus.trimObject(obj)

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: {} }

sagus.pickKeys(obj, keys)

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 }

sagus.removeKeys(obj, keys)

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' }

sagus.isValid(value)

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

sagus.isValidObject(obj)

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

sagus.iterate(input)

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 }
1.0.3

2 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago