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

form.l

v0.0.3

Published

utility functions for forms, including functions, formatters, normalizers, sanitizers, sorters, and validations.

Downloads

106

Readme

form.l

utility functions for forms, including functions, formatters, normalizers, sanitizers, sorters, and validations.

Functions

diff(baseObj = object, targetObj = object) - deep difference between two objects. Uses lodash's transform, isEqual, isObject.

import ifValidText from 'form.l/lib/functions/ifValidText'

const objA = {a: 1, b: { c: 1, d: 0 }, e: 0}
const objB = {a: 1, b: { c: 1, d: 1 }, e: 1}

const objC = diff(objA, objB)  // => { b: { d: 1 }, e: 1 }

ifValidText(text = str, arr = [func]) - returns function(txt). if array of functions are true , it returns defined text, else it returns undefined. Useful for validation functions.

import ifValidText from 'form.l/lib/functions/ifValidText'

const includesText = (txt) => txt.includes('Match me.')
const isThisText = (txt) => txt === 'Match me.'
const validIncludeText = ifValidText('I include text.', [includesText])
const validText = ifValidText('I'm valid.', [includesText, isThisText])

const result = validIncludeText('Match me. Match you.') // => "I include text."
const result2 = validText('Match me.') // => "I'm valid."
const result3 = validText('Match me. Match you.') // => undefined

Formatters

Formatters in this context, formats string and returns it.

includePrefix(prefix = string) - returns function(str) to add prefix to beginning of str. Useful for currency.

import includePrefix from 'form.l/lib/formatters/includePrefix'

const currencyPrefix = includePrefix('$')

const result = currencyPrefix('100') // => '$100'

includeSuffix(suffix = string) - returns function(str) to add suffix to end of str. Useful for percentages.

import includeSuffix from 'form.l/lib/formatters/includeSuffix'

const percentagePrefix = includeSuffix('$')

const result = percentagePrefix('100') // => '100%'

Parsers

Parsers parse strings, returning a string that conforms to a certain pattern.

regFind(rgx, num = 0) - returns function(str) to parse string with regex. Use num to tell function which array index should be returned.

import regFind from 'form.l/lib/parsers/regFind'

const trimZeros = regFind(/^0*(\d*)/,  1)

const result = trimZeros('00001111mmm') // => '1111'

regReplace(rgx, str = '') - returns function(str) to replace strings with regex. Use str to tell function what to replace found matches with.

import regReplace from 'form.l/lib/parsers/regReplace'

const trimZeros = regReplace(/^0*(\d*)/,  'h')

const result = trimZeros('00001111mmm') // => 'hmmm'

Sanitizers

Sanitizers are functions to clean data, either while getting from database to use in a form, or to ready data for posting to a database.

Sorters

Sorters are functions to sort list arrays.

sortRegex(rgx, min = 0, max = 0, func) - returns function(arr = [], order = 'asc') for sorting to apply func to sliced matches of the regex. Useful to apply

import sortRegex from 'form.l/lib/sorters/sortRegex'

import toNumber from 'lodash/toNumber'
import padStart from 'lodash/padStart'

const ltrNum = new RegExp(/(\D*)(\d*)/, 'i')
const sortTxtandNum = a => a.map(m => (isNaN(toNumber(m)) ? m : padStart(m, 4, '0'))).join('')

export const sortStateAreas = sortRegex(ltrNum, 1, 3, sortTxtandNum)

const result = sortStateAreas(['CA11', 'BC02', 'BC01', 'CB01']) // => ['BC01', 'BC02', 'CA11', 'CB01']

Validations

Validations will return a boolean if data is valid. Used mostly to validate field data, but might be used across form data.

ifBlank(str) - returns true if str is blank ('').

import ifBlank from 'form.l/lib/validations/ifBlank'

const result = ifBlank('') // => true
const result2 = ifBlank('true') // => false

ifRegex(rgx) - returns function(str) that returns true if rgx finds a match within str.

import ifRegex from 'form.l/lib/validations/ifRegex'

const ltrnum = ifRegex(/(\D*)(\d*)/) // => true

const result = ltrnum('CA10') // => true
const result2 = ltrnum('10CA') // => false

ifYear(year, range = 0) - returns function(str) if str is a year between 'year' and 'year plus range'.

import ifYear from 'form.l/lib/validations/ifYear'

const year2000 = ifYear(2000, 1) // => true

const result = year2000('10/11/2000') // => true
const result2 = year2000('10/11/2001') // => false