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

kenote-node-utils

v1.0.9

Published

nodej utils

Downloads

25

Readme

kenote-node-utils

Node.js Utils

NPM

NPM Version NPM Downloads Build Status Codecov Status Gratipay

Engines

  • Node.js >= 8.0.0

Install

yarn add kenote-node-utils

Features

loadConfig

config.json

{
  "HOST": "0.0.0.0",
  "PORT": 4000
}

config.ini

; Configure Production

HOST = 0.0.0.0
PORT = 4000

app.js

import { loadConfig } from 'kenote-node-utils'

const configJson = loadConfig('config.json')
/*
{
  HOST: '0.0.0.0',
  POST: 4000
}
*/

const configIni = loadConfig('config.ini')
/*
{
  HOST: '0.0.0.0',
  POST: 4000
}
*/

getMongooseOptions

app.js

import mongoose from 'mongoose'
import { getMongooseOptions } from 'kenote-node-utils'

const mongoUri = 'mongodb://localhost:27017/collection-name'
const options = getMongooseOptions(mongoose.version)

mongoose.connect(mongoUri, options, err => {
  if (err) {
    console.error(`connect to ${mongoUri} error: ${err.message}`)
    process.exit(1)
  }
})

md5, sha1

app.js

import { md5, sha1 } from 'kenote-node-utils'

const md5String = utils.md5('123456')
const sha1String = utils.sha1('123456')

console.log('md5: %s, sha1: %s', md5String, sha1String)

encryptPwd

app.js

import { encryptPwd } from 'kenote-node-utils'

const salt = Math.random().toString(36).substr(8)
const encryptPwd = utils.encryptPwd('admin888', salt)
/*
{
  encrypt: '045c859a309a459a04c9de24b8b6b03295c3e46a',
  salt: 'zff6t'
}
*/

validPassword

app.js

import { validPassword } from 'kenote-node-utils'

const encryptPwd = {
  encrypt: '045c859a309a459a04c9de24b8b6b03295c3e46a',
  salt: 'zff6t'
}
const validPassword = validPassword('admin888', encryptPwd.salt, encryptPwd.encrypt) // true or false

isAccess

app.js

import { isAccess } from 'kenote-node-utils'

const access = ({ level }) => level >= 1000
const result = isAccess({ level: 9999 }, access) // true or false

callback

app.js

import { callback } from 'kenote-node-utils'

(async () => {
  const result = await new Promise((resolve, reject) => callback(resolve, reject, null, 1))
  console.log(result)
})()

isNull

app.js

import { isNull } from 'kenote-node-utils'

console.log(isNull(null))

checkLength

app.js

import { checkLength } from 'kenote-node-utils'

console.log(checkLength('你好123')) // 7

isPattern

app.js

import { isPattern } from 'kenote-node-utils'

const result1 = isPattern('123456', { pattern: /^[0-9]+$/, min: 4, max: 10 }) // true
const result2 = isPattern('123456', { pattern: '^[0-9]+$', min: 4, max: 10 }) // true

validRule

app.js

import { validRule } from 'kenote-node-utils'

const rules = [
  { required: true, message: 'Value cannot be empty.', code: 1001 },
  { pattern: /^[0-9]+$/, message: 'Wrong value format.', code: 1002 }
]
const result = utils.validRule('123456', rules) // null
const result = utils.validRule('', rules) // { message: 'Value cannot be empty.', code: 1001 }
const result = utils.validRule('abc', rules) // { message: 'Wrong value format.', code: 1002 }

filterData

app.js

import { filterData } from 'kenote-node-utils'

const rules = {
  username: [
    { required: true, message: 'Name cannot be empty.', code: 1001 },
    { pattern: /^[a-zA-Z]{1}[a-zA-Z0-9_]{3,11}$/, message: 'Wrong name format.', code: 1002 }
  ],
  email: [
    { required: true, message: 'Email cannot be empty.', code: 1003 },
    { pattern: /^[a-z_0-9.-]{1,64}@([a-z0-9-]{1,200}.){1,5}[a-z]{1,6}/, message: 'Wrong email format.', code: 1004 }
  ],
  phone: [
    { required: true, message: 'Phone cannot be empty.', code: 1005 },
    { pattern: /^0?(12[0-9]|13[0-9]|14[57]|15[012356789]|16[0-9]|17[0-9]|18[0-9]|19[0-9])[0-9]{8}$/, message: 'Wrong phone format.', code: 1006 }
  ],
  password: [
    { required: true, message: 'Password cannot be empty.', code: 1007 },
    { pattern: /^(?=.*[A-Za-z])[A-Za-z0-9$@$!%*#?&]{6,32}$/, message: 'Wrong password format.', code: 1008 }
  ],
}
const info = {
  username: 'thondery',
  email: 'thondery163.com',
  password: 'a123456'
}
const { username, email, phone, password } = info
const filters = [
  { key: 'username', rules: rules.username, value: username },
  { key: 'email', rules: rules.email, value: email, ignore: true },
  { key: 'phone', rules: rules.phone, value: phone, ignore: true },
  { key: 'password', rules: rules.password, value: password }
]
const options = {
  picks: [
    { 
      data: [ email, phone ],
      message: 'Email, phone number must be set one', 
      code: 1009
    }
  ]
}
filterData(filters, (data, message) => {
  if (message) {
    console.log(message)
    return
  }
  console.log(data)
}, options)

License

this repo is released under the MIT License.