├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── img.png ├── index.js ├── package.json └── test └── basic.js /.npmignore: -------------------------------------------------------------------------------- 1 | img.png 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - lts/* 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Feross Aboukhadijeh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | 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, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zero-fill [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] 2 | 3 | [travis-image]: https://img.shields.io/travis/feross/zero-fill/master.svg 4 | [travis-url]: https://travis-ci.org/feross/zero-fill 5 | [npm-image]: https://img.shields.io/npm/v/zero-fill.svg 6 | [npm-url]: https://npmjs.org/package/zero-fill 7 | [downloads-image]: https://img.shields.io/npm/dm/zero-fill.svg 8 | [downloads-url]: https://npmjs.org/package/zero-fill 9 | [standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg 10 | [standard-url]: https://standardjs.com 11 | 12 | ### Zero-fill a number to the given size. 13 | 14 | ![zero](https://raw.githubusercontent.com/feross/zero-fill/master/img.png) 15 | 16 | ## install 17 | 18 | ``` 19 | npm install zero-fill 20 | ``` 21 | 22 | ## usage 23 | 24 | ```js 25 | var zeroFill = require('zero-fill') 26 | 27 | zeroFill(4, 1) // '0001' 28 | zeroFill(10, 55) // '0000000055' 29 | zeroFill(1, 1) // '1' 30 | ``` 31 | 32 | Partial application: 33 | 34 | ```js 35 | zeroFill(4)(1) // '0001' 36 | ``` 37 | 38 | Custom padding character: 39 | 40 | ```js 41 | zeroFill(4, 55, ' ') // ' 55' 42 | zeroFill(4, 500, ' ') // ' 500' 43 | ``` 44 | 45 | ## license 46 | 47 | MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org). 48 | 49 | -------------------------------------------------------------------------------- /img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/feross/zero-fill/b8d01afd81dc20d4a8fe62b3a4be2ac045befb54/img.png -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! zero-fill. MIT License. Feross Aboukhadijeh */ 2 | /** 3 | * Given a number, return a zero-filled string. 4 | * From http://stackoverflow.com/questions/1267283/ 5 | * @param {number} width 6 | * @param {number} number 7 | * @return {string} 8 | */ 9 | module.exports = function zeroFill (width, number, pad) { 10 | if (number === undefined) { 11 | return function (number, pad) { 12 | return zeroFill(width, number, pad) 13 | } 14 | } 15 | if (pad === undefined) pad = '0' 16 | width -= number.toString().length 17 | if (width > 0) return new Array(width + (/\./.test(number) ? 2 : 1)).join(pad) + number 18 | return number + '' 19 | } 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "zero-fill", 3 | "description": "Zero-fill a number to the given size.", 4 | "version": "2.2.4", 5 | "author": { 6 | "name": "Feross Aboukhadijeh", 7 | "email": "feross@feross.org", 8 | "url": "https://feross.org" 9 | }, 10 | "bugs": { 11 | "url": "https://github.com/feross/zero-fill/issues" 12 | }, 13 | "dependencies": {}, 14 | "devDependencies": { 15 | "standard": "*", 16 | "tape": "^5.0.1" 17 | }, 18 | "keywords": [ 19 | "zero", 20 | "zero fill", 21 | "fill", 22 | "zerofill", 23 | "pad", 24 | "0", 25 | "zero pad", 26 | "zeropad", 27 | "string", 28 | "number" 29 | ], 30 | "license": "MIT", 31 | "main": "index.js", 32 | "repository": { 33 | "type": "git", 34 | "url": "git://github.com/feross/zero-fill.git" 35 | }, 36 | "scripts": { 37 | "test": "standard && tape test/*.js" 38 | }, 39 | "funding": [ 40 | { 41 | "type": "github", 42 | "url": "https://github.com/sponsors/feross" 43 | }, 44 | { 45 | "type": "patreon", 46 | "url": "https://www.patreon.com/feross" 47 | }, 48 | { 49 | "type": "consulting", 50 | "url": "https://feross.org/support" 51 | } 52 | ] 53 | } 54 | -------------------------------------------------------------------------------- /test/basic.js: -------------------------------------------------------------------------------- 1 | var zeroFill = require('../') 2 | var test = require('tape') 3 | 4 | test('basic use', function (t) { 5 | t.equal(zeroFill(4, 1), '0001') 6 | t.equal(zeroFill(10, 1), '0000000001') 7 | t.equal(zeroFill(10, 55), '0000000055') 8 | t.equal(zeroFill(1, 1), '1') 9 | t.end() 10 | }) 11 | 12 | test('width edge cases', function (t) { 13 | t.equal(zeroFill(1, 20), '20', 'do not trim string if width is too small') 14 | t.equal(zeroFill(0, 20), '20', 'zero width') 15 | t.equal(zeroFill(0, 1), '1', 'zero width') 16 | t.end() 17 | }) 18 | 19 | test('partial application', function (t) { 20 | t.equal(zeroFill(4)(1), '0001') 21 | t.equal(zeroFill(10)(1), '0000000001') 22 | t.equal(zeroFill(10)(55), '0000000055') 23 | t.equal(zeroFill(1)(1), '1') 24 | t.equal(zeroFill(3)(1, ' '), ' 1') // custom pad character 25 | t.end() 26 | }) 27 | 28 | test('custom pad character', function (t) { 29 | t.equal(zeroFill(4, 1, ' '), ' 1') 30 | t.equal(zeroFill(10, 1, 'x'), 'xxxxxxxxx1') 31 | t.equal(zeroFill(10, 55, 'x'), 'xxxxxxxx55') 32 | t.equal(zeroFill(1, 1, 'x'), '1') 33 | t.end() 34 | }) 35 | --------------------------------------------------------------------------------