Coder Social home page Coder Social logo

percent's Introduction

Percent

Build status devDependencies status

Percent control done right

Percent is a npm module which gives you nice options to control percentages. It's like semver for percentages.

Install

$ npm install percent

Examples

Calculate percentage

const percent = require('percent');

console.log(percent.calc(5, 20, 0)); // => 25

Validate percentage

const percent = require('percent');

if (percent.valid(5)) { // => true
  console.log('5 is a valid percent value');
}

Compare percentages

const percent = require('percent');

if (percent.lt('5%', 6)) { // => true
  console.log('a is smaller than b');
}

API

const percent = require('percent');

.calc()

Usage: percent.calc(value, total, decimal, [sign])
Example: percent.calc(5, 20, 0)

Calculates percentage from the given number (value) and total number (total) with specified number of decimals (decimal).

sign may be boolean which turns percent sign (%) addition. Returns percent value with percent sign if sign is true (it's false by default). If sign is string, it will be used as value suffix.

.re

Usage: percent.re

Returns supreme percent regexp.

.valid()

Usage: percent.valid(value)
Example: percent.valid('5%')

Checks if value is valid percent value. It is valid if it's a number, number-like string (e.g. '10', not 10), or string with number and percent sign. Spaces are allowed in strings.

.sign()

Usage: percent.sign(value)
Example: percent.sign(5)

Adds percent sign to value.

.unsign()

Usage: percent.unsign(value)
Example: percent.unsign('5%')

Removes percent sign(s) from value.

.clean()

Usage: percent.clean(value)
Example: percent.clean(' 5 % ')

Removes percent sign(s) and spaces from value.

.convert()

Usage: percent.convert(value, [negative])
Example: percent.convert(' 5 % ')

Converts percent-like string to number. Returns negative number if negative is true.

.lt()

Usage: percent.lt(l, t)
Example: percent.lt('5%', 6)

Checks is the first argument smaller than second.

.gt()

Usage: percent.gt(g, t)
Example: percent.gt('6%', 5)

Checks is the first argument greater than second.

.eq()

Usage: percent.eq(e, q)
Example: percent.eq('5%', 5)

Checks are the arguments logically equal.

.neq()

Usage: percent.neq(ne, q)
Example: percent.neq('6%', 5)

Checks are the arguments logically unequal.

.satisfies()

Usage: percent.satisfies(value, min, max)
Example: percent.satisfies(5.5, 5, 6)

Checks does the value satisfy the given range. It will exchange min and max values if min is bigger than max.

License

MIT © Zlatan Vasović

percent's People

Contributors

zlatanvasovic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

percent's Issues

SyntaxError while building React app that uses percent

Hi there

I have a React app built with [email protected] that uses [email protected].

When I run yarn run build to build optimized bundle, I get

yarn run v0.22.0
$ react-scripts build
Creating an optimized production build...
Failed to compile.

static/js/main.190dfa22.js from UglifyJs
SyntaxError: Unexpected token: operator (>) [./~/percent/index.js:12,0]

I'll actually won't be using percent anymore in this project (that is a toy project after all) since I'll probably will implement myself the calculation, but just to let you know.

In development mode (yarn start) it works just fine.

Typescript

Hi! Could you plz add TS typings?
Thanks for the lib :)

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.