npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

paliari-js-utils

v0.3.1

Published

Biblioteca de utilidades para JS

Downloads

67

Readme

paliari-js-utils

Biblioteca de utilidades para JS

Installation

yarn add paliari-js-utils

Development

# install dependencies
yarn install

# build with minification
yarn release

# publish
bin/publish

asyncJs

import { asyncJs } from 'paliari-js-utils'

// Insere tag <script  src="" /> no HTML programaticamente
asyncJs('https://your-url.com/your-lib.min.js')

i18n

import { I18n } from 'paliari-js-utils'

// config locale
const locales = {
  pt_br: {enums: {...}, db: {...}}
}
const i18n = new I18n(locales, 'pt_br')

// change locale
i18n.setLocale('en')

// Get locale
i18n.getLocale() // 'en'

// Obtem os locais disponives
i18n.getAvailableLocales() //['en', 'pt_br', ...]

// usage
i18n.t('db.attributes.animal.nome')
i18n.t('enums.castracao.status.solicitado')

// com replaces
i18n.t('error.messasges.long_chars', {max: '15'})

Exemplo de estrutura dos locales:

pt_br:
  buttons:
    save: 'Salvar'
    #...
  db:
    attributes:
      pessoa:
        id: ID
        cpf_cnpj: 'CPF/CNPJ'
        #...
  enums:
    castracao:
      status:
        solicitado: Solicitado
        #...
  errors:
    permission_denied: 'Permissão negada'
    auth:
      invalid_cpf_cnpj: 'O CPF/CNPJ é inválido'
      #...
  messages:
    sign_out_confirm: 'Tem certeza que deseja sair?'
    could_not_load_record: 'Não foi possível carregar o registro'
    #...
#...

Exemplo de config para Vuejs

import { I18n } from 'paliari-js-utils'

const i18n = new I18n(locales, 'pt_br')

export default {
  install(vue) {
    vue.prototype.$i18n = i18n
    vue.i18n = vue.prototype.$i18n
  }
}

Language

Para alterar a linguagem no browser

import fecha from 'fecha'
import { I18n, Language } from 'paliari-js-utils'

const i18n = new I18n(locales, 'pt_br')

const language = new Language(i18n, fecha)

// Exemplo para Vuejs
import { Language } from 'paliari-js-utils'

export default {
  install(vue, i18n, fecha) {
    vue.prototype.$language = new Language(i18n, fecha)
    vue.language = vue.prototype.$language
  }
}

// Your component
export default {
  methods:{
    changeLanguage(language) {
      if (language !== this.$language.current) {
        this.$language.current = language
        this.$router.push({name: 'changing_language'})
      }
    }
  }
}

BaseModel

Extendendo um model.

import { BaseModel, idGeneratorDesc } from 'paliari-js-utils'

const attrs = {
  id: idGeneratorDesc(),
  cidade_id: '',
  animal: {},
  dono: {},
  clinica: {},
  status: '',
  solicitado_em: Date.now(),
  aprovado_em: null
}

class Castracao extends BaseModel {
  constructor(obj) {
    super(attrs, obj)
  }
}

export default Castracao

// instanciando um model
var castracao = new Castracao({ status: 'solicitado' })

ID generator key for Firebase

// gera id alfanumerico baseado no timestamp ordenado decrescente
import { idGeneratorDesc } from 'paliari-js-utils'
var id = idGeneratorDesc() // zdja0I19bKcIxA73QjIA

// extrai o timestamp do ID desc
import { idDescTimestamp } from 'paliari-js-utils'
var timestamp = idDescTimestamp('zdja0I19bKcIxA73QjIA') // 1529444224885

// gera ID numerico
import { idGeneratorNumeric } from 'paliari-js-utils'
// default desc
var id = idGeneratorNumeric() // 84704117442938738166
var id = idGeneratorNumeric('desc') // 84704117442938738166
// asc
var id = idGeneratorNumeric('asc') // 15295883565621743713

Consultar próximo ID (Usado na paginação)

import { nextId, prevId } from 'paliari-js-utils'

nextId('zdit9URuO2T9VdC0h-2l') //zdit9URuO2T9VdC0h-2m
prevID('zdit9URuO2T9VdC0h-2l') //zdit9URuO2T9VdC0h-2k

starRating

/**
 * Calcula media das notas.
 *
 * @param notas Object
 * @param precision Integer quantidade de casas decimais
 *
 * @returns Number
 */
import { starRating } from 'paliari-js-utils'

/**
 * Onde as keys sao as notas possiveis
 * e os values a quantidade de vezes que se repete.
 */
var notas = {
  1: 23,
  2: 0,
  3: 223,
  4: 343,
  5: 312
}
var precision = 2

var rate = starRating(notas, precision) // 4.02

Transliterate

import { transliterate } from 'paliari-js-utils'

var str = transliterate('São José') // sao jose

deepKey

import { deepKey } from 'paliari-js-utils'

var obj = {a: {a1: a2: 'Content'}}
var value = deepKey(obj, 'a.a1.a2') // Content

getClassName

import { getClassName } from 'paliari-js-utils'

getClassName({}) // Object
getClassName(function() {}) // Function
getClassName([]) // Array
getClassName(new Date()) // Date
getClassName(/a/) // RegExp
getClassName(1) // Number
getClassName('') // String
getClassName(null) // Null
getClassName(undefined) // Undefined

isObject

import { isObject } from 'paliari-js-utils'

isObject({}) // true
isObject({ a: 1 }) // true
isObject([]) // true
isObject(1) // false
isObject(null) // false

isHash

import { isHash } from 'paliari-js-utils'

isHash({}) // true
isHash({ a: 1 }) // true
isHash('') // false
isHash([]) // false
isHash(1) // false
isHash(null) // false

isDate

import { isDate } from 'paliari-js-utils'

isDate(new Date()) // true
isDate('') // false
isDate(null) // false
isDate(1) // false

isRegExp

import { isRegExp } from 'paliari-js-utils'

isRegExp(new RegExp()) // true
isRegExp(/[0-9]/) // true
isRegExp(/[0-9]/g) // true
isRegExp(/ab1/g) // true
isRegExp('[0-9]') // false

deepMerge

import { deepMerge } from 'paliari-js-utils'

var a = {
  a: { a1: 'a1' }
}
var b = {
  a: { a2: 'a2' },
  b: { b1: 'a' }
}
deepMerge(a, b) // {  a: { a1: 'a1', a2: 'a2' },b: { b1: 'a' }}

deepClone

import { deepClone } from 'paliari-js-utils'

var a = {
  a: { a1: 'a1' }
}
deepClone(a)

sanitize

import { sanitize } from 'paliari-js-utils'

var obj = {
  a: { a1: 'a1', a2: 'a2' },
  b: { b1: 'b1', b2: 'b2' },
  c: 1,
  d: 1
}
var rules = {
  a: { a2: true},
  b: true,
  c: true }
}
sanitize(obj, rules) // {a: {a2: 'a2'}, b: {b1: 'b1', b2: 'b2'}, c: 1}

jsEncode

import { jsEncode } from 'paliari-js-utils'

jsEncode('Abacaxi') // Mg0W0f1

ucfirst

import { ucfirst } from 'paliari-js-utils'

ucfirst('abc') // Abc

prepareKey

import { prepareKey } from 'paliari-js-utils'

prepareKey('a.b/c-D') // a.b.c_d

flattenObject

import { flattenObject } from 'paliari-js-utils'

const a = { castracoes: {'-41iosdoii': { nome: 'Bilu', ... }} }

// { 'castracoes/-41iosdoii/nome': 'Bilu', ... }

LocalStorageFacade

  • set item in local storage:
import { LocalStorageFacade } from 'paliari-js-utils'

// key, item
LocalStorageFacade.setItem('your-local-storage-key', { id: 1 })
  • get item in local storage:
import { LocalStorageFacade } from 'paliari-js-utils'

// key
LocalStorageFacade.getItem('your-local-storage-key') // { id: 1 }
  • remove item to local storage
import { LocalStorageFacade } from 'paliari-js-utils'

// key
LocalStorageFacade.removeItem('your-local-storage-key')
  • clear local storage
import { LocalStorageFacade } from 'paliari-js-utils'

LocalStorageFacade.clear()

Facade for google Maps

Extrair ID da cidade

import { idCidadeAddressComponent, idCidade } from 'paliari-js-utils'

// param address_components do google geo
var cidade_id = idCidadeAddressComponent(geo_address_components)

// uf, nome
var cidade_id = idCidade(uf, 'nome da cidade')

Extrair nome da cidade

import { nomeCidadeAddressComponent } from 'paliari-js-utils'

// Param address_components do google geo
// Ex: cidade_nome = Maringá - PR - Brasil
var cidade_nome = nomeCidadeAddressComponent(geo_address_components)

Converter address_component para string formatada

import { convertPlace } from 'paliari-js-utils'

// param address_components do google geo
var formatted = convertPlace(address_components)