1.0.7 • Published 3 years ago
mini-helper v1.0.7
mini-helper
Small JavaScript library. Just some useful functions :) is typed
install
yarn add mini-helper
or
npm install mini-helper --save
test
yarn install
yarn test
some things
Environment
Checks if execution is on node or browser
import { isNode, isBrowser } from 'mini-helper'
console.log(isNode()) // boolean
console.log(isBrowser()) // boolean
Array
Forces a variable into an array
import { forceArray } from 'mini-helper'
forceArray('a') // ['a']
forceArray(['a']) // ['a']
String
import { removeAll, urlEncodeObject } from 'mini-helper'
removeAll('TEST#$_', '#$_') // return 'TEST'
urlEncodeObject({foo: 'fooValue', bar: 'barValue'}) // foo=fooValue&bar=barValue
Number
import { round } from 'mini-helper'
round(1234.5678, 1); // 1234.6
Object
import { isObject, compareValues } from 'mini-helper'
isObject({a:1}) // true
isObject([{a:1}]) // false
// Sorts array of objects
const myArray = [
{ a: 'A', b: 1 },
{ a: 'B', b: 3 },
{ a: 'C', b: 2 }
]
const sorted = myArray.sort(compareValues('a', 'desc'));
// sorted = [
// { a: 'C', b: 2 },
// { a: 'B', b: 3 },
// { a: 'A', b: 1 }
// ]
Image
import { toBase64Img } from 'mini-helper'
const filePath = 'path/to/image.png'
const stringBase64 = toBase64Img(filePath) // return -> string base64
File
import { renameFile, copyFile, deleteFile } from 'mini-helper'
renameFile(oldPath, newPath)
copyFile(source, target)
deleteFile(filePath)
Date
import { dateToStrTimeZone, strDateFromTo } from 'mini-helper'
dateToStrTimeZone(new Date('12/10/2021 08:00:00'), 'YYYY-MM-DD HH:mm:ss', 'America/Fortaleza'); // '2021-12-10 05:00:00'
strDateFromTo('01/12/21', 'DD/MM/YY', 'YYYY-MM-DD') // 2021-12-01
Variable
import { isDefined, hexToAlphaNumeric } from 'mini-helper'
let value;
isDefined(value) // false
value = false
isDefined(value) // true
hexToAlphaNumeric('414243') // return 'ABC'
hexToAlphaNumeric('413943', true) // return 'A C'