├── .gitignore ├── number-pad.png ├── .travis.yml ├── test.js ├── bin.js ├── index.js ├── README.md ├── package.json └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /number-pad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mafintosh/number-pad/HEAD/number-pad.png -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | - '0.12' 5 | - '4' 6 | - '6' 7 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var numberPad = require('./') 2 | var tape = require('tape') 3 | 4 | tape('works', function (t) { 5 | t.same(numberPad('data'), 3282) 6 | t.same(numberPad('hyper'), 49737) 7 | t.end() 8 | }) 9 | -------------------------------------------------------------------------------- /bin.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var numberPad = require('number-pad') 4 | 5 | if (!process.argv[2]) { 6 | console.error('Usage: number-pad string') 7 | process.exit(1) 8 | } 9 | 10 | console.log(numberPad(process.argv[2])) 11 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var map = [2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9] 2 | var a = 'a'.charCodeAt(0) 3 | 4 | module.exports = numberPad 5 | 6 | function numberPad (str) { 7 | str = str.toLowerCase() 8 | var result = 0 9 | for (var i = 0; i < str.length; i++) { 10 | var n = str.charCodeAt(i) - a 11 | if (n >= map.length || n < 0) throw new Error('String must only contain a-z') 12 | result = result * 10 + map[n] 13 | } 14 | return result 15 | } 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # number-pad 2 | 3 | Convert a string to a number based on a classic phone number pad encoding 4 | 5 | [![build status](http://img.shields.io/travis/mafintosh/number-pad.svg?style=flat)](http://travis-ci.org/mafintosh/number-pad) 6 | 7 | ![number-pad](number-pad.png) 8 | 9 | ``` 10 | npm install number-pad 11 | ``` 12 | 13 | ## Usage 14 | 15 | ``` js 16 | var numberPad = require('number-pad') 17 | console.log(numberPad('data')) // 3282 18 | ``` 19 | 20 | ## Command line 21 | 22 | You can install the command line module as well 23 | 24 | ``` 25 | npm install -g number-pad 26 | number-pad data # prints 3282 27 | ``` 28 | 29 | ## License 30 | 31 | MIT 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "number-pad", 3 | "version": "1.0.1", 4 | "description": "Convert a string to a number based on a classic phone number pad encoding", 5 | "main": "index.js", 6 | "dependencies": {}, 7 | "devDependencies": { 8 | "standard": "^8.6.0", 9 | "tape": "^4.6.3" 10 | }, 11 | "bin": { 12 | "number-pad": "./bin.js" 13 | }, 14 | "scripts": { 15 | "test": "standard && tape test.js" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/mafintosh/number-pad.git" 20 | }, 21 | "author": "Mathias Buus (@mafintosh)", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/mafintosh/number-pad/issues" 25 | }, 26 | "homepage": "https://github.com/mafintosh/number-pad" 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Mathias Buus 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 13 | all 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 21 | THE SOFTWARE. 22 | --------------------------------------------------------------------------------