README
Waelio Utilities
WaelioUtils Utilities library.
A pure JavaScriptTypeScript
Rewritten in Usage
List of Tools
- Strings
- Arrays
- Objects
- Other
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
that converts a FunctionJSON
to URL Query String
{"first":"John", "last": "Smith"}
Example IN: first=John&last=Smith
Example Out: {}
JSON payload
@param String
Returns .js
or .ts
file:
Example: In your import { jsonToQueryString } from 'waelio-utils';
const payload = { first: 'John', last: 'Smith' };
const Result = jsonToQueryString(payload);
Result:
'name=John&last=smith';
queryStringToJson
Function that converts a URL Query String to JSON
payload
Type @param {string}
as String
Param toObject
Type @param {boolean}
as Boolean
Param JS Object
OR JSON
Output: JSON || Object
Returns .js
or .ts
file:
Example: In your import { queryStringToJson } from 'waelio-utils';
const query = (name = 'John&last=smith');
const Result = queryStringToJson(query);
Result:
{ first: 'John', last: 'Smith' }
resetString
.js
or .ts
file:
Example: In your import { resetString } from 'waelio-utils';
const payload = 'https%3A%2F%2Fwaelio.com';
const Result = resetString(payload);
Result === 'https://waelio.com';
snakeToCamel
Function that converts snake_case or snake-case to camelCase "snakeCase"
snake_case
Example IN: snakeCase
Example Out: @name snakeToCamel
payload
QueryString
@param {string} {string}
Returns .js
or .ts
file:
Example: In your import { snakeToCamel } from 'waelio-utils';
const payload = 'north-west_meta';
const Result = snakeToCamel(payload);
Result:
'northWestMeta';
camelToSnake
Function that converts camelCase to snake_case or snake-case "snake-case"
snakeCase
Example IN: snake-case
Example Out: @name camelToSnake
@param {string} payload
@param {boolean} hyphenated controls the delimiter: true = "-" / false = "_"
{string}
Returns .js
or .ts
file:
Example: In your import { camelToSnake } from 'waelio-utils';
const payload = 'northWestMeta';
const Result = camelToSnake(payload);
camelToSnake( payload )
Result 1- 'north_west_meta';
camelToSnake( payload, true )
Result 2- 'north-west-meta';
Base64
toBase64
< --
-- > Renamed as Base64
Converts a string to .js
or .ts
file:
Example: In your import { toBase64 } from 'waelio-utils';
const payload = 'north-west_meta';
const Result = Base64(payload);
Result:
'bm9ydGgtd2VzdF9tZXRh';
reParseString
Simple object Standardization
OR object Deep Cloning <- Not best practice
null
s, undefined
, NaN
and date
s
Warning: Watchout for JSON.parse(JSON.stringify(payload))
Returns .js
or .ts
file:
Example: In your import { reParseString } from 'waelio-utils';
// No magic here, use wisely
generateId
Generate random string/id
{number}
start 2
OPTIONAL
@param {number}
len 9
OPTIONAL
@param {string}
Returns .js
or .ts
file:
Example: In your import { generateId } from 'waelio-utils';
const result = generateId();
Result: (random)
// result === '3uqi11wg9'
Quasar/Vue JS Stuff:
@waelio\utils
in the future
Will be relocated to Quasar Framework
Perfect when Usingmeta
Example:
quasar.conf.js
in // quasar.conf.js
return {
framework: {
plugins: ['Meta']
}
};
.vue
file
In a <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>
notifyMe
Send PWA Notifications to Site
Works only in Browser
to send @param {string}
.js
or .ts
file:
Example: In your import { notifyMe } from 'waelio-utils';
notifyMe('Hello World!');
SniffId
id,Id,_Id,id
from Object
Deconstruct Example:
var response = { _id: 1234, name: 'John' };
var newId = sniffId(response);
// newId === 1234
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]
*/
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]
*/
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
Repeat
Repeat function N times
Example:
import { _repeat } from 'waelio-utils';
let counter = 0;
const f1 = () => counter++;
_repeat(5)(f1);
// counter === 5
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
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
_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
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
encrypt
string
, object
& array
or function
Possible payloads _encrypt(salt, payload)
_encrypt: If salt
is not provided it will revert to the string "salt"
as the default salt
.
decrypt
payload
is NOT a function
Scenario 1 _decrypt(salt, payload)
_decrypt: If salt
is not provided and asFunction
is false
it will revert to the string "salt"
as the default salt
payload
IS function
Scenario 2 _decrypt(salt, payload, asFunction = false)
_decrypt: 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