input-number
Advanced tools
Comparing version 1.0.2 to 1.0.3
33
index.js
/** | ||
* @module num-input | ||
* @module input-number | ||
*/ | ||
const caret = require('caret-position2'); | ||
module.exports = Enhance; | ||
const clamp = require('mumath/clamp'); | ||
const round = require('mumath/round'); | ||
const keys = { | ||
38: 'up', | ||
40: 'down' | ||
} | ||
}; | ||
const numRE = /[\.0-9]/; | ||
function Enhance (input) { | ||
module.exports = numerify; | ||
function numerify (input, opts) { | ||
opts = opts || {}; | ||
opts.step = opts.step || ((opts.min && opts.max) ? (opts.max - opts.min / 100) : 1); | ||
opts.max = opts.max || Infinity; | ||
opts.min = opts.min || -Infinity; | ||
opts.precision = opts.precision || 0.00001; | ||
input.addEventListener('keydown', e => { | ||
var key = keys[e.which]; | ||
let key = keys[e.which]; | ||
@@ -25,3 +33,2 @@ if (!key) return; | ||
let numRE = /[\.0-9]/; | ||
@@ -45,4 +52,12 @@ //parse left side | ||
let number = parseFloat(numStr); | ||
number = key === 'up' ? number+1 : number-1; | ||
if (key === 'up') { | ||
number = clamp((number+opts.step), opts.min, opts.max); | ||
} | ||
else { | ||
number = clamp((number-opts.step), opts.min, opts.max); | ||
} | ||
number = round(number, opts.precision); | ||
let leftStr = str.slice(0, left); | ||
@@ -49,0 +64,0 @@ let rightStr = str.slice(right); |
{ | ||
"name": "input-number", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Make input recognize numbers", | ||
@@ -27,7 +27,9 @@ "main": "index.js", | ||
"dependencies": { | ||
"caret-position2": "^1.0.2" | ||
"caret-position2": "^1.0.2", | ||
"mumath": "^2.3.0" | ||
}, | ||
"devDependencies": { | ||
"autosize-input": "^0.2.1", | ||
"bubleify": "^0.5.0" | ||
} | ||
} |
@@ -17,3 +17,7 @@ # input-number [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges) | ||
num(input); | ||
num(input, { | ||
min: 0, | ||
max: 255, | ||
step: 1 | ||
}); | ||
``` | ||
@@ -20,0 +24,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3278
50
28
2
2
+ Addedmumath@^2.3.0
+ Addedmumath@2.3.0(transitive)