Coder Social home page Coder Social logo

the-pirate-utils's Introduction

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

// From 0 to 10
numPyrate.interval() // [0, 1, 2, 3, ..., 10]
// From 0 to 99
numPyrate.interval(99) // [0, 1, 2, 3, ..., 99]
// From 0 to 50 with 5 steps
numPyrate.interval(50, 5) // [0, 5, 10, 15, ..., 50]
// From 10 to 50 with 5 steps
numPyrate.interval(50, 5, 10) // [10, 15, 20, 25, ..., 50]

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.