waelio-utils

Helper Utils often used in websites

Usage no npm install needed!

<script type="module">
  import waelioUtils from 'https://cdn.skypack.dev/waelio-utils';
</script>

README

Waelio Utilities

Donate NPM version NPM monthly downloads NPM total downloads Join the chat at https://discord.gg/tBZ2Fmdb7E

A pure JavaScript WaelioUtils Utilities library.

Rewritten in TypeScript

Usage

List of Tools

  1. Strings

  2. Arrays

  3. Objects

  4. Other
  5. calculateClockDrift

Installation

Using npm:

npm install waelio-utils

In browser:

<script src="https://unpkg.com/waelio-utils@latest/dist/waelioUtils.js"></script>

Tree Shaking (Recommended)

// ES6
import { snakeToCamel, meta, notifyMe } from 'waelio-utils';
// NodeJS
const { snakeToCamel, meta, notifyMe } = require('waelio-utils');

jsonToQueryString

Function that converts a JSON to URL Query String

Example IN: {"first":"John", "last": "Smith"}

Example Out: first=John&last=Smith

@param {} JSON payload

Returns String

Example: In your .js or .ts file:

import { jsonToQueryString } from 'waelio-utils';
const payload = { first: 'John', last: 'Smith' };
const Result = jsonToQueryString(payload);

Result:

'name=John&last=smith';

Back to TOP

queryStringToJson

Function that converts a URL Query String to JSON

Param payload Type @param {string} as String

Param toObject Type @param {boolean} as Boolean

Output: JS Object OR JSON

Returns JSON || Object

Example: In your .js or .ts file:

import { queryStringToJson } from 'waelio-utils';
const query = (name = 'John&last=smith');
const Result = queryStringToJson(query);

Result:

{ first: 'John', last: 'Smith' }

Back to TOP

resetString

Example: In your .js or .ts file:

import { resetString } from 'waelio-utils';

const payload = 'https%3A%2F%2Fwaelio.com';
const Result = resetString(payload);

Result === 'https://waelio.com';

Back to TOP

snakeToCamel

Function that converts snake_case or snake-case to camelCase "snakeCase"

Example IN: snake_case

Example Out: snakeCase

@name snakeToCamel

@param {string} payload QueryString

Returns {string}

Example: In your .js or .ts file:

import { snakeToCamel } from 'waelio-utils';
const payload = 'north-west_meta';
const Result = snakeToCamel(payload);

Result:

'northWestMeta';

Back to TOP

camelToSnake

Function that converts camelCase to snake_case or snake-case "snake-case"

Example IN: snakeCase

Example Out: snake-case

@name camelToSnake

@param {string} payload

@param {boolean} hyphenated controls the delimiter: true = "-" / false = "_"

Returns {string}

Example: In your .js or .ts file:

import { camelToSnake } from 'waelio-utils';
const payload = 'northWestMeta';
const Result = camelToSnake(payload);

Result 1- camelToSnake( payload )

'north_west_meta';

Result 2- camelToSnake( payload, true )

'north-west-meta';

Back to TOP

Base64

-- > Renamed as toBase64 < --

Converts a string to Base64

Example: In your .js or .ts file:

import { toBase64 } from 'waelio-utils';
const payload = 'north-west_meta';
const Result = Base64(payload);

Result:

'bm9ydGgtd2VzdF9tZXRh';

Back to TOP

reParseString

Simple object Standardization

OR object Deep Cloning <- Not best practice

Warning: Watchout for nulls, undefined, NaN and dates

Returns JSON.parse(JSON.stringify(payload))

Example: In your .js or .ts file:

import { reParseString } from 'waelio-utils';

// No magic here, use wisely

Back to TOP

generateId

Generate random string/id

@param {number} start 2 OPTIONAL

@param {number} len 9 OPTIONAL

Returns {string}

Example: In your .js or .ts file:

import { generateId } from 'waelio-utils';
const result = generateId();

Result: (random)

// result === '3uqi11wg9'

Back to TOP

Quasar/Vue JS Stuff:

Will be relocated to @waelio\utils in the future

Perfect when Using Quasar Framework

meta

Example:

in quasar.conf.js

// quasar.conf.js
return {
  framework: {
    plugins: ['Meta']
  }
};

In a .vue file

<script>
  import { meta } from 'waelio-utils'
  export default {
    name:'foo',
    data () {
      return {
        metaTags: {
          title: 'Site Title',
          description: 'Website Utilities',
          url: 'https://github.com/waelio/waelio-utils',
          image: 'nwm_logo.png'
        }
      }
    },
    meta
  }
</script>

Back to TOP

notifyMe

Send PWA Notifications to Site

Works only in Browser

@param {string} to send

Example: In your .js or .ts file:

import { notifyMe } from 'waelio-utils';
notifyMe('Hello World!');

Back to TOP

SniffId

Deconstruct id,Id,_Id,id from Object

Example:

var response = { _id: 1234, name: 'John' };
var newId = sniffId(response);
// newId === 1234

Back to TOP

HideRandom

Hide random array indexes

@params array
@params difficulty = 3
@params replacement = ''

Example:

import { _hideRandom } from 'waelio-utils';
const arr = [
  [1, 2, 3],
  [1, 2, 3],
  [1, 2, 3]
];
const test = _hideRandom(arr, 3);

/* random

  [1, 2, ""]
  ["", 2, ""]
  ["", "", 3]

*/

Back to TOP

rotateArray

Rotate array

import { _rotateArray } from 'waelio-utils';
const testArray = [
  [1, 2, 3],
  [1, 2, 3],
  [1, 2, 3]
];

const test1 = _rotateArray(testArray);
/* 
  [1, 1, 1],
  [2, 2, 2],
  [3, 3, 3]
*/

Back to TOP

equals (_equals)

compare 2 arrays with equal size

Example:

import { _equals } from 'waelio-utils';
const array1 = [123456];
const array2 = [123455];
const test = _equals(array1, array2);

// test === false

Back to TOP

Repeat

Repeat function N times

Example:

import { _repeat } from 'waelio-utils';
let counter = 0;
const f1 = () => counter++;

_repeat(5)(f1);

// counter === 5

Back to TOP

Equals

Example:

import { _equals } from 'waelio-utils';
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [1, 2, 3, 4, 5];
const arr3 = [1, 2, 3, 4, 5, 6];

_equals(arr1, arr2); // true
_equals(arr1, arr3); // false

Back to TOP

CleanResponse

Example:

import { _cleanResponse } from 'waelio-utils';

const demoRes = {
  total: 1,
  limit: 10,
  skip: 0,
  data: [
    {
      _id: '650937936xc8b143d8c575d2a',
      name: 'Some Data',
      user: '679363c6dc8b123d8c575d29',
      createdAt: '2021-05-06T06:14:09.209Z',
      updatedAt: '2021-05-06T06:14:09.209Z',
      __v: 0
    }
  ]
};

const cleanRes = _cleanResponse(demoRes);
/* [ 
      {
        "_id": "650937936xc8b143d8c575d2a",
        "name": "Some Data",
        "user": "679363c6dc8b123d8c575d29",
        "createdAt": "2021-05-06T06:14:09.209Z",
        "updatedAt": "2021-05-06T06:14:09.209Z",
        "__v": 0
      }
    ]
  */

clean response

Back to TOP

_To

Turn any function to Promise

[null, resolve][(reject, null)]; // resolve // reject

Example:

import { _To } from 'waelio-utils';
import axios = 'axios';
const testEndpoint = 'https://api.picmymenu.com/restaurants';
const response = await _To(axios(testEndpoint));
const [ reject, resolve ] = response;

expect(response).toBeTruthy() // true
expect(resolve).toBeTruthy() // true
expect(reject).not.toBeTruthy() //true
expect(resolve.data.length).toBeTruthy(); //true

Back to TOP

AOrAn

Example:

import { a_or_an } from 'waelio-utils'
const payload1 = "apple";
const payload2 = "bananas";
const payload3 = "orange";

a_or_an(payload1 // an
a_or_an(payload2) // a
a_or_an(payload3) // an

Back to TOP

encrypt

Possible payloads string, object & array or function

_encrypt: _encrypt(salt, payload)

If salt is not provided it will revert to the string "salt" as the default salt.


decrypt

Scenario 1 payload is NOT a function

_decrypt: _decrypt(salt, payload)

If salt is not provided and asFunction is false it will revert to the string "salt" as the default salt


Scenario 2 payload IS function

_decrypt: _decrypt(salt, payload, asFunction = false)

salt IS required, payload is required and asFunction must be true like _decrypt(salt, function, true)


Example:

import { _encrypt, _decrypt, generateId, _equal } from 'waelio-utils';

const salt = generateId(); // "g9rlygzjd"
const payload1 = 'What ever you want';
const payload2 = { message: 'What ever you want' };
const payload3 = function (params) {
  return { name: params };
};
const encrypted1 = _encrypt(salt, payload1); //7d424b5e0a4f5c4f580a53455f0a5d4b445e
const decrypted2 = _decrypt(salt, encrypted1); // "What ever you want"

const encrypted3 = _encrypt(salt, payload2); //5108474f59594b4d4f0810087d424b5e0a4f5c4f580a53455f0a5d4b445e0857
const decrypted3 = _decrypt(salt, encrypted3); // {"message":"What ever you want"}

const encrypted4 = _encrypt(salt, payload3); // 4c5f44495e4345440a025a4b584b4759030a51200a0a0a0a0a0a0a0a5c4b580a0e750e490a170a0e750e5d4c021b0311200a0a0a0a0a0a0a0a584f5e5f58440a0e750e5d021b060a1e1c060a0e750e4903060a0e750e5d5c021b060a1e1c060a0d1b061e1c0d060a0e750e49060a0d510a444b474f100a5a4b584b47590a570d060a510a444b474f100a5a4b584b47590a57060a0d0e750e444f0d060a1a060a1b060a510a49424b444d4f634e100a0d5c5258524d0d0a570311200a0a0a0a57

// Function
const decrypted4 = _decrypt(salt, encrypted4, true); // [FN]
decrypted4('Wael'); // {name: 'Wael' }

const dblCheck = _equal(payload2, JSON.parse(decrypted2)); // true

Back to TOP