├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | .DS_Store 3 | 4 | # Logs 5 | logs 6 | *.log 7 | npm-debug.log* 8 | 9 | # Runtime data 10 | pids 11 | *.pid 12 | *.seed 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directory 30 | node_modules 31 | 32 | # Optional npm cache directory 33 | .npm 34 | 35 | # Optional REPL history 36 | .node_repl_history 37 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - 0.10 5 | - 0.12 6 | - 4 7 | - 5 8 | 9 | sudo: false 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Tejesh Mehta 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # is-positive-integer [![Build Status](https://travis-ci.org/tjmehta/is-positive-integer.svg?branch=master)](https://travis-ci.org/tjmehta/is-positive-integer) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) 2 | check if a number is a positive integer 3 | 4 | # Installation 5 | ```bash 6 | npm --save i is-positive-integer 7 | ``` 8 | 9 | # Usage 10 | ```js 11 | var isPositiveInteger = require('is-positive-integer') 12 | 13 | isPositiveInteger(1) // true 14 | isPositiveInteger(10) // true 15 | isPositiveInteger(100) // true 16 | isPositiveInteger(1000) // true 17 | isPositiveInteger(0) // false 18 | isPositiveInteger(-1) // false 19 | isPositiveInteger(-10) // false 20 | isPositiveInteger(-100) // false 21 | isPositiveInteger(-1000) // false 22 | isPositiveInteger(1.1) // false 23 | isPositiveInteger(10.1) // false 24 | isPositiveInteger(100.1) // false 25 | isPositiveInteger(1000.1) // false 26 | isPositiveInteger(-1.1) // false 27 | isPositiveInteger(-10.1) // false 28 | isPositiveInteger(-100.1) // false 29 | isPositiveInteger(-1000.1) // false 30 | isPositiveInteger(Infinity) // false 31 | isPositiveInteger({}) // false 32 | isPositiveInteger([]) // false 33 | isPositiveInteger('10') // false 34 | isPositiveInteger('what') // false 35 | isPositiveInteger(/what/) // false 36 | isPositiveInteger(null) // false 37 | // ... 38 | ``` 39 | 40 | ### isSafePositiveInteger 41 | Ensure positive integer less than `Number.MAX_SAFE_INTEGER` 42 | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER 43 | ```js 44 | var isSafePositiveInteger = require('is-positive-integer').isSafePositiveInteger 45 | // max safe integer doesn't exist in older engines 46 | var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991 47 | 48 | isSafePositiveInteger(MAX_SAFE_INTEGER) // true 49 | isSafePositiveInteger(MAX_SAFE_INTEGER + 1) // false 50 | isSafePositiveInteger(Number.MAX_VALUE) // false 51 | // ... 52 | ``` 53 | 54 | # License 55 | MIT 56 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* $lab:coverage:off$ */ 2 | var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991 3 | /* $lab:coverage:on$ */ 4 | 5 | module.exports = isPositiveInteger 6 | module.exports.isSafePositiveInteger = isSafePositiveInteger 7 | 8 | function isPositiveInteger (x) { 9 | // Is it a number? 10 | return Object.prototype.toString.call(x) === '[object Number]' && 11 | // Is it an integer? 12 | x % 1 === 0 && 13 | // Is it positive? 14 | x > 0 15 | } 16 | 17 | // strict positive integer check: 18 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER 19 | function isSafePositiveInteger (x) { 20 | return isPositiveInteger(x) && 21 | x <= MAX_SAFE_INTEGER 22 | } 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-positive-integer", 3 | "version": "1.1.1", 4 | "description": "check if a number is a positive integer", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "lab --verbose --assert code --threshold 100", 8 | "lint": "standard" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/tjmehta/is-positive-integer.git" 13 | }, 14 | "keywords": [ 15 | "positive", 16 | "integer", 17 | "number", 18 | "assert", 19 | "check", 20 | "validate", 21 | "negative", 22 | "validation" 23 | ], 24 | "author": "Tejesh Mehta", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/tjmehta/is-positive-integer/issues" 28 | }, 29 | "homepage": "https://github.com/tjmehta/is-positive-integer#readme", 30 | "devDependencies": { 31 | "code": "^1.5.0", 32 | "lab": "^5.18.1", 33 | "standard": "^6.0.5" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var Code = require('code') 2 | var Lab = require('lab') 3 | 4 | var isPositiveInteger = require('../index.js') 5 | var isSafePositiveInteger = isPositiveInteger.isSafePositiveInteger 6 | 7 | var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991 8 | 9 | var lab = exports.lab = Lab.script() 10 | var describe = lab.describe 11 | var it = lab.it 12 | var expect = Code.expect 13 | 14 | describe('is-positive-integer', function () { 15 | it('should return true for positive integers', function (done) { 16 | expect(isPositiveInteger(1)).to.equal(true) 17 | expect(isPositiveInteger(10)).to.equal(true) 18 | expect(isPositiveInteger(100)).to.equal(true) 19 | expect(isPositiveInteger(1000)).to.equal(true) 20 | expect(isPositiveInteger(90071992555)).to.equal(true) 21 | expect(isPositiveInteger(999999999999)).to.equal(true) 22 | expect(isPositiveInteger(MAX_SAFE_INTEGER)).to.equal(true) 23 | expect(isPositiveInteger(Number.MAX_VALUE)).to.equal(true) 24 | expect(isPositiveInteger(new Number(12))).to.equal(true) 25 | done() 26 | }) 27 | 28 | it('should return false for negative integers or zero', function (done) { 29 | expect(isPositiveInteger(0)).to.equal(false) 30 | expect(isPositiveInteger(-1)).to.equal(false) 31 | expect(isPositiveInteger(-10)).to.equal(false) 32 | expect(isPositiveInteger(-100)).to.equal(false) 33 | expect(isPositiveInteger(-1000)).to.equal(false) 34 | expect(isPositiveInteger(new Number(0))).to.equal(false) 35 | expect(isPositiveInteger(new Number(-12))).to.equal(false) 36 | done() 37 | }) 38 | 39 | it('should return false for floats', function (done) { 40 | expect(isPositiveInteger(1.1)).to.equal(false) 41 | expect(isPositiveInteger(10.1)).to.equal(false) 42 | expect(isPositiveInteger(100.1)).to.equal(false) 43 | expect(isPositiveInteger(1000.1)).to.equal(false) 44 | expect(isPositiveInteger(-1.1)).to.equal(false) 45 | expect(isPositiveInteger(-10.1)).to.equal(false) 46 | expect(isPositiveInteger(-100.1)).to.equal(false) 47 | expect(isPositiveInteger(-1000.1)).to.equal(false) 48 | expect(isPositiveInteger(new Number(12.45))).to.equal(false) 49 | done() 50 | }) 51 | 52 | it('should return false for others', function (done) { 53 | expect(isPositiveInteger(Infinity)).to.equal(false) 54 | expect(isPositiveInteger({})).to.equal(false) 55 | expect(isPositiveInteger([])).to.equal(false) 56 | expect(isPositiveInteger('10')).to.equal(false) 57 | expect(isPositiveInteger('what')).to.equal(false) 58 | expect(isPositiveInteger(/what/)).to.equal(false) 59 | expect(isPositiveInteger(null)).to.equal(false) 60 | expect(isPositiveInteger(undefined)).to.equal(false) 61 | expect(isPositiveInteger(true)).to.equal(false) 62 | expect(isPositiveInteger({ valueOf: function () { return 42 } })).to.equal(false) 63 | expect(isPositiveInteger(Number.NaN)).to.equal(false) 64 | done() 65 | }) 66 | 67 | describe('isSafePositiveInteger', function() { 68 | it('should return true for positive integers', function (done) { 69 | expect(isSafePositiveInteger(1)).to.equal(true) 70 | expect(isSafePositiveInteger(10)).to.equal(true) 71 | expect(isSafePositiveInteger(100)).to.equal(true) 72 | expect(isSafePositiveInteger(1000)).to.equal(true) 73 | expect(isSafePositiveInteger(90071992555)).to.equal(true) 74 | expect(isSafePositiveInteger(999999999999)).to.equal(true) 75 | expect(isSafePositiveInteger(MAX_SAFE_INTEGER)).to.equal(true) 76 | expect(isSafePositiveInteger(Number.MAX_VALUE)).to.equal(false) 77 | done() 78 | }) 79 | }) 80 | }) 81 | --------------------------------------------------------------------------------