1.0.1 • Published 10 months ago
wf_core_nodejs v1.0.1
WF_Core_NodeJS
core library of reusable code for use in various projects
Installation
npm install @weaverfundraising/wf_core_nodejs
Usage
HTTPS
Send GET Request
const {getRequest} = require('@weaverfundraising/wf_core_nodejs/https');
const options = {
hostname: 'somedomain.com',
path: '/api/route/id/1'
};
const headers = {
"X-App": "my-x-app-token"
}
const result = await getRequest(options.hostname, options.path, headers).catch(err => {});
if (result && typeof result === 'string') {
try {
result = JSON.parse(result);
} catch (err) {}
}
Send POST Request
const {postRequest} = require('@weaverfundraising/wf_core_nodejs/https');
const options = {
hostname: 'somedomain.com',
path: '/api/route/id/1'
};
const payload = {
apiKey: 'my-api-key',
id: 1,
name: 'some variable'
};
const headers = {
"X-App": "my-x-app-token"
}
const result = await postRequest(options.hostname, options.path, payload, headers).catch(err => {});
if (result && typeof result === 'string') {
try {
result = JSON.parse(result);
} catch (err) {}
}
Util
isNull | notNull
const {isNull, notNull} = require('@weaverfundraising/wf_core_nodejs/util');
let myVar = null;
if (isNull(myVar)) { ... }
if (notNull(myVar)) { ... }
isEmpty | notEmpty
const {isEmpty} = require('@weaverfundraising/wf_core_nodejs/util');
const str = 'not empty';
const empty = ' ';
const arr = [1,2,3];
const nil = null;
if (isEmpty(str)) { ... }
if (isEmpty(empty)) { ... }
if (isEmpty(arr)) { ... }
if (isEmpty(nil)) { ... }
if (notEmpty(str)) { ... }
if (notEmpty(empty)) { ... }
if (notEmpty(arr)) { ... }
if (notEmpty(nil)) { ... }
isAFunction
const {isAFunction} = require('@weaverfundraising/wf_core_nodejs/util');
const fn = () => {};
const notAFn = "hello, world!";
if (isAFunction(fn)) { ... }
if (isAFunction(notAFn)) { ... }
toString
const {toString} = require('@weaverfundraising/wf_core_nodejs/util');
const obj = {Desc: "Object to convert to String"};
const str = "String will not change";
const num = 10.45;
const objStr = toString(obj);
const strStr = toString(str);
const numStr = toString(num);
toNumber
const {toNumber} = require('@weaverfundraising/wf_core_nodejs/util');
const num_1 = toNumber("123.45");
const num_2 = toNumber("NaN");
const num_3 = toNumber(35);
toBoolean
const {toBoolean} = require('@weaverfundraising/wf_core_nodejs/util');
const bool_1 = toBoolean(1);
const bool_2 = toBoolean("true");
deepClone
const {deepClone} = require('@weaverfundraising/wf_core_nodejs/util');
const obj1 = {
value: {
sub1: {
deep: "clone"
},
sub2: "Weird"
},
desc: "something blah blah",
data: [
{some: "data"},
"other data",
123
]
};
const clone = deepClone(obj1);
randomNumber
const {randomNumber} = require('@weaverfundraising/wf_core_nodejs/util');
const num_1 = randomNumber();
const num_2 = randomNumber(50);
const num_3 = randomNumber(100, 1000);
randomString
const {randomString} = require('@weaverfundraising/wf_core_nodejs/util');
const str_1 = randomString();
const str_2 = randomString(50);
const str_3 = randomString(-1);
Validation
Validate Phone Number Format
const {isValidPhone} = require('@weaverfundraising/wf_core_nodejs/validation');
const isValid = isValidPhone('123-456-7890');
Validate Email Format
const {isValidEmail} = require('@weaverfundraising/wf_core_nodejs/validation');
const isValid = isValidEmail('my.name@myemail.com');
Money
const {WFMoney} = require('@weaverfundraising/wf_core_nodejs/money');
const money_1 = new WFMoney(10.50);
money_1.add(5);
money_1.subtract(1);
const currency = money_1.amount('c');
const pennies = money_1.amount('p');
const value = money_1.value;