futils-lite

A modern JavaScript utility library.

Usage no npm install needed!

<script type="module">
  import futilsLite from 'https://cdn.skypack.dev/futils-lite';
</script>

README

futils-lite

A modern JavaScript utility library.

📦 Install

npm install futils-lite --save
yarn add futils-lite

🔨 Usage

_join

_join(arr, identifier = 'name', separator = ', ')

Converts all elements in array object into a string separated by separator.

Arguments

  • arr (array).
  • [identifier = 'name'] (string)
  • [separator = ', '] (string)

Returns

  • String: Return string

Example

const authors = [
    { id: 1, username: 'Hunt' },
    { id: 2, username: 'Sam' }
];

console.log(_join(authors, 'username'));
//expected output: "Hunt, Sam"

arrayMove

arrayMove(array, from, to)

Move an array item to a different index.

Arguments

  • array (array).
  • from (integer)
  • to (integer)

Returns

  • array: Return array

Example

const authors = ['Hunt', 'Sam'];

console.log(arrayMove(authors, 0, 1));
//expected output: ['Sam', 'Hunt']

currencyFormatter

currencyFormatter(value, options, [locales = null])

Enable language sensitive currency or number formatting

Arguments

  • value (Number).
  • options (Object): An object with some or all of the following properties.
  • [locales = null] (String): A string with a BCP 47 language tag, or an array of such strings.

Returns

  • String: Return string with locale

Example

const number = 123456.789;

console.log(currencyFormatter(number,  { style: 'currency', currency: 'EUR' } , 'de-DE'));
// expected output: "123.456,79 €"

// the Japanese yen doesn't use a minor unit
console.log(currencyFormatter(number,   { style: 'currency', currency: 'JPY' } , 'ja-JP'));
// expected output: "ï¿¥123,457"

// limit to three significant digits
console.log(currencyFormatter(number,   { maximumSignificantDigits: 3 } , 'en-IN'));
// expected output: "1,23,000"

numberFormatter

numberFormatter(value, options = {}, [locales = null])

Enable language sensitive number formatting

Arguments

  • value (Number).
  • options (Object): An object with some or all of the following properties.
  • [locales = null] (String): A string with a BCP 47 language tag, or an array of such strings.

Returns

  • String: Return string with locale

Example

const number = 123456.789;

// limit to three significant digits
console.log(currencyFormatter(number,   { maximumSignificantDigits: 3 } , 'en-IN'));
// expected output: "1,23,000"

filterBuilder

filterBuilder(schema, values)

Arguments

  • schema (object)
  • values (object)

Returns

  • Object: Return as object

Schema Type

  • bt: between
  • eq: equal
  • ne: not equal
  • gt: greater than
  • gte: greater than equal
  • lt: less than
  • lte: less than equal
  • rl: like (as regex)

Example

const schema = { name: { type: 'rl' }, price: { type: 'eq'} };
const values = { name: 'saas', price: 29.00 }

console.log(filterBuilder(schema values));
// expected output: { name: { $regex: `.*saas.*`, $options: 'i' }, price: 29 }

findURLQueryParams

findURLQueryParams(paramsName, [url = null])

Arguments

  • paramsName (array string): array names of url query string.
  • URL (window.location.search): search url of window location

Returns

  • Object: Return object of param(s) name

Example

const URL = '<URL>?type=new&id=123';
const search = 'type=new&id=123'

console.log(findURLQueryParams( ['type'] , search));
// expected output: { type: new }

console.log(findURLQueryParams( ['type', 'id'] , search));
// expected output: { type: new, id: 123 }

http

async http(options)

Arguments

Example

const [ status, payload ] = await http(
    {
        method: 'post',
        url: '/user/12345',
        data: {
        firstName: 'Fred',
        lastName: 'Flintstone'
        }
    }
);

http{Post, Put, Patch, Get, Delete}

async httpPost(options)
async httpPut(options)
async httpPatch(options)
async httpGet(options)
async httpDelete(options)

Arguments

  • options (post, put, patch): Object of { url: string, [ data: any , config: AxiosRequestConfig ] }.
  • options (get, delete): Object of { url: string, [ config: AxiosRequestConfig ] }.

Example

const [ status, payload ] = await httpPost(
    {
        url: '/user/12345',
        data: {
            firstName: 'Hunt',
            lastName: 'RK'
        }
    }
);
const [ status, payload ] = await httpGet({ url: '/user/12345' });

slug

slug(value, separator = '-')

Arguments

  • value (string)
  • [separator = '-'] (string)

Example

console.log(slug('Hunter Blog'));
//expected output: hunter-blog

toUTC

toUTC(value, dateOnly = false, format = null)

Arguments

  • value (string)
  • dateOnly (boolean): false
  • format (string | boolean): null

Example

[ Notes: local timezone: Asia/Jakarta = +07:00 ]
const value = '2019-07-25T13:00:00.48Z';

console.log(toUTC(value));
// expected output: (as moment)  2019-07-25T06:00:00.48Z

console.log(toUTC(value, false, 'dd-mm-YYYY'));
// expected output:  25-07-2019

console.log(toUTC(value, true));
// expected output: (as moment)  2019-07-25T12:00:00.00Z

console.log(toUTC(value, true, true));
// expected output:  (as string) 2019-07-25T12:00:00.00Z

toTZ

toTZ(value, dateOnly = false, format = null)

Arguments

  • value (string)
  • timezone (string)
  • format (string | boolean): null

Example

const utcString = '2019-07-25T13:00:00.48Z';

console.log(toTZ(utcString, 'Asia/Jakarta'));
// expected output: (as moment)  2019-07-25T20:00:00.48Z

console.log(toTZ(utcString, 'Asia/Jakarta', 'dd-mm-YYYY'));
// expected output:  25-07-2019