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

hagim

v1.0.5

Published

a validation lib

Downloads

7

Readme

hagim

Build Status Join the chat at https://gitter.im/hagim-lib/Lobby

a validation lib for browser environment only. https://www.npmjs.com/package/hagim

why

It has been a long time since I have needed to find an easy to use javascript validation lib for browser environments. Every lib I found on github is not suitable for me. So finally I decided to make a new wheel.

The most significant different between Node.js environment and browser environment is that in the latter, numbers may be represented by a string variable(e.g: if using a Two-Way Data Binding framework). So, validating a number in the browser may be more difficult than in Node.js.

Another different is that there are more logic disjunction cases in browser environment.

installation

To install via npm, run:

npm install hagim

load

To load hagim in node.js:

const Hagim = require('hagim');

design

let hagim = new Hagim(rules)
hagim.validate("value")

It is the basic form of usage of Hagim. The rules should be an array, e.g:

[{is: "number"}, {is: "integer"}]
  1. Number and String can be judged by the same API
  2. multiple rules can be gathered togather to form a complex judgement
  3. rules may be treated in a and behavior by default, but you can change this by adding a additional true literal in the rules array
  4. rules can be nested to form a complex judgement

The elements of the array consists of a validator and an operand. e.g is is the validator and "number" is the operand. Each rule will be performed one by one, follow the order of they are in the array. If one rule fails, by default the whole process will be failed.

let hagim = new Hagim([{is: "empty"}, {is: "number"}, {is: "email"}, true])
hagim.validate("")  // true
hagim.validate("123.4")  // true
hagim.validate("[email protected]")  // true

The element itself can be an array too, e.g:

let rules = new Hagim([{is: "empty"}, [{is: "number"}, {is: "integer"}]])

usage

Rules are organized in an array, and every rule contains a validator and an operand. Operands can be an element or an entity or any kinds of data.

const Hagim = require('hagim');
new Hagim([{is: "number"}, {is: "integer"}]).validate(2) // true

validators

is

whether the string represent a special string or number(more details on entities section)

isNot

the opposite of is

are

whether all the members of a string belong to a certain character set(more details on elements section)

match

whether the string match a certain regular express. Under the hood the function re.test() is used

exist

whether the string includes a certain kind of characters(more details on elements section)

haveString

the same as exist

contains

whether the string contains a certain substring

required

whether the string is not empty

gt

whether it is greater than the operand

lt

whether it is lower than the operand

goe

whether it is greater than or equal to the operand

loe

whether it is lower than or equal to the operand

equal

whether it equals the operand, under the hood == is used for comparing

dplacesGt

whether the digits of decimal part is greater than the operand

dplacesLt

whether the digits of decimal part is Lower than the operand

dlengthOf

whether the digits of decimal part equals the operand

lengthGt

whether the length of the string is greater than the operand

lengthLt

whether the length of the string is lower than the operand

lengthOf

whether the length of the string equsls the operand

beginWithSub

whether the string begin with a certain word

notBeginWithSub

whether the string don't begin with a certain word

startWithSet

whether the first member of the string belongs to a certain kind of characters(more details on elements section)

notStartWithSet

whether the first member of the string does not belong to a certain kind of characters(more details on elements section)

entities

Each entity represents a certain kind of strings, like a number or an email. is and isNot can be used to judge whether the string represents a certain entity.

let hagim = new Hagim([{is: "number"}])
hagim.validate("2")  // true

If there is no such a entity match you need, you can extend this lib by yourself, check plugin section for details.

number

whether the string represent a number

integer

whether the string represent an integer

decimal

whether the string represent a decimal number

positive

whether the string represent a positive number

negative

whether the string represent a negative number

email

whether the string represents a email

empty

whether the string is "", null, undefined or []

ip

whether the string represent an ip address

url

whether the string represent a url

elements

Each element represents a certain character set, like Latin letters or digits. are can be used to judge whether all of the members of a string belong to a certain set.

latin

whether the character belongs to latin letter set

enLetter

currently the same to latin

digit

whether the character belongs to 0-9

logic conjunction and disjunction

By default the rules in a array will be treated in an and-like manner, but it depends on you whether to change it. You can set it by appending an additional truthy value into the rules array. e.g:

new Hagim([{is: "number"}, {is: "empty"}, true]).validate("2.3") //true
new Hagim([{is: "number"}, {is: "empty"}, true]).validate("") //true

You can specify a disjunction manner in every array, includes nested arrays, or leave it in a conjunction manner by default.

plugin

Third-party plugins are available by means of the extension API. Currently only entities and elements can be extended. For instance, if you want to define a plugin which extends Hagim to have a capability to judge whether the operand is a binary number, it should be like this:

Hagim.extend("entities", {
  binary: funciton(value){
    return /[01]+/.test(value)
  }
})