Skip to content

trickstival/the-pirate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Pirate JS

The Pirate is a lib that has some util functions that can abstract logic and prevent errors

Install

# Using npm
npm i the-pirate
# Using yarn
yarn add the-pirate

General examples

import * as pirate from 'the-pirate'

// General functions
pirate.truthyJoin(['Hey', undefined, 'You'], ', ') // 'Hey, You'
pirate.any(false, true, false) // true
pirate.any(false, false) // false

pirate.maybe({}, 'item.price') // undefined
pirate.maybe({ item: { price: '$10.00' } }, 'item.price') // '$10.00'

DOM utils

import { dom } from 'the-pirate'
/*
imagine this span is in a structure like this:
<h2 class="parent">
    <div>
        <span class="child"></span>
    </div>
</h2>
*/
const spanChild = document.querySelector('.child')
dom.closest(spanChild, '.parent') // returns the h2 that has the .parent class

dom.scrollTo('.parent') // scrolls to parent

Numeric utils

import { numeric as numPyrate } from 'the-pirate'

numPyrate.validateCNPJ('18.837.197/0001-83') // true
numPyrate.validateCNPJ('47.372.635/4756-32') // false

numPyrate.validateCPF('93549281021') // true
numPyrate.validateCPF('34549284521') // false

numPyrate.normalizeLocalCurrency('20,000.04') // 20000.04 float
numPyrate.normalizeLocalCurrency('20.000,04') // 20000.04 float

String examples

import { string as strPi } from 'the-pirate'
strPi.isURL('http://google.com') // true
strPi.isURL('this is not an url') // false
strPi.interpol('Hello', 'World') // 'Hello World'
strPi.toSnakeCase('Hello World')//hello_world
strPi.toSnakeCase('HelloWórld')//hello_wrld

Color examples

import { color } from 'the-pirate'
// r, g, b
color.isLight(0, 0, 0) // false

color.hexToRgb('#aa7392') // { r: 170, g: 115, b: 146 }

color.blackOrWhite('#ddd') // black
color.blackOrWhite('#200') // white

Note: not all functions were made from ground up. Some of them were taken from forums or at least inspired on forum answers.

About

A lib with js utilities

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •