├── _config.yml ├── .travis.yml ├── index.js ├── package.json ├── README.md ├── LICENSE ├── .gitignore ├── ordinal.js └── test └── test.js /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = require("./ordinal"); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "js-ordinal", 3 | "version": "1.0.2", 4 | "description": "Simple utility to translate from regular numbers to their ordinal representation.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/GeekAb/js-ordinal.git" 12 | }, 13 | "keywords": [ 14 | "ordinal", 15 | "number", 16 | "1st", 17 | "2nd", 18 | "3rd", 19 | "4th", 20 | "5th", 21 | "11th", 22 | "21st", 23 | "nth" 24 | ], 25 | "author": "Abhishek", 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/GeekAb/js-ordinal/issues" 29 | }, 30 | "homepage": "https://github.com/GeekAb/js-ordinal#readme", 31 | "devDependencies": { 32 | "chai": "^4.2.0", 33 | "mocha": "^5.2.0" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # js-ordinal 2 | 3 | Simple utility to translate numeral to their english ordinal representation. 4 | 5 | ## Install 6 | 7 | First of all, you need to have npm or yarn installed. Follow these steps, [npm](https://www.npmjs.com/get-npm) or [yarn](https://yarnpkg.com/lang/en/docs/install/), to install one of them. 8 | 9 | ### npm 10 | 11 | ```js 12 | npm install --save js-ordinal 13 | ``` 14 | 15 | ### yarn 16 | 17 | ```js 18 | yarn add js-ordinal 19 | ``` 20 | 21 | ## Few Examples 22 | 23 | ``` javascript 24 | const o = require('js-ordinal') 25 | 26 | o.toOrdinal(1) // '1st' 27 | o.toOrdinal(2) // '2nd' 28 | 29 | o.toOrdinal(11) // '11th' 30 | o.toOrdinal(12) // '12th' 31 | 32 | o.toOrdinal(21) // '21st' 33 | ``` 34 | 35 | Getting only ordinal 36 | 37 | ``` javascript 38 | o.ordinalSuffix(1) // 'st' 39 | ``` 40 | ### Documentation 41 | 42 | You can know more about the project on the [npm website](https://www.npmjs.com/package/js-ordinal) and the [official website](https://GeekAb.github.io/js-ordinal/). 43 | 44 | ### TODO 45 | - Add unit test 46 | 47 | - Support additional language 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Abhishek 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 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 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 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /ordinal.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | //Array to hold on to ordinals 4 | const ordinalsArray = ["th", "st", "nd", "rd"]; 5 | 6 | // Function will check if number is valid and return false if not. 7 | const validateNumber = (num) => { 8 | return !isNaN(parseFloat( num )) && isFinite( num ); 9 | }; 10 | 11 | const ordinal = { 12 | 13 | ordinalSuffix (num) { 14 | //Get reminder of number by hundred so that we can counter number between 11-19 15 | let offset = num % 100; 16 | 17 | // Calculate position of ordinal to be used. Logic : Array index is calculated based on defined values. 18 | let ordinalPos = ordinalsArray[ ( offset-20 )%10 ] || ordinalsArray[ offset ] || ordinalsArray[0]; 19 | 20 | //Return suffix 21 | return ordinalPos; 22 | }, 23 | 24 | toOrdinal (num) { 25 | 26 | //Check if number is valid 27 | if( !validateNumber(num) ) { 28 | return `${num} is not a valid number`; 29 | } 30 | 31 | //If number is zero no need to spend time on calculation 32 | if(num === 0) { 33 | return num.toString(); 34 | } 35 | 36 | return num.toString() + this.ordinalSuffix(num); 37 | } 38 | }; 39 | 40 | module.exports = ordinal; -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | const expect = require("chai").expect; 2 | const o = require("../index.js"); 3 | 4 | describe("ordinalOf_1()", function () { 5 | it("should return number with its ordinal", function () { 6 | const x = 1; 7 | const xOrdinal = "1st"; 8 | var y = o.toOrdinal(x); 9 | expect(y).to.be.equal(xOrdinal); 10 | }); 11 | }); 12 | 13 | describe("ordinalOf_a()", function () { 14 | it("should return error", function () { 15 | const x = "a"; 16 | const xOrdinal = "a is not a valid number"; 17 | var y = o.toOrdinal(x); 18 | expect(y).to.be.equal(xOrdinal); 19 | }); 20 | }); 21 | 22 | describe("ordinalOf_11()", function () { 23 | it("should return number with its ordinal", function () { 24 | const x = 11; 25 | const xOrdinal = "11th"; 26 | var y = o.toOrdinal(x); 27 | expect(y).to.be.equal(xOrdinal); 28 | }); 29 | }); 30 | 31 | describe("ordinalOf_13()", function () { 32 | it("should return number with its ordinal", function () { 33 | const x = 13; 34 | const xOrdinal = "13th"; 35 | var y = o.toOrdinal(x); 36 | expect(y).to.be.equal(xOrdinal); 37 | }); 38 | }); 39 | 40 | describe("ordinalOf_21()", function () { 41 | it("should return number with its ordinal", function () { 42 | const x = 21; 43 | const xOrdinal = "21st"; 44 | var y = o.toOrdinal(x); 45 | expect(y).to.be.equal(xOrdinal); 46 | }); 47 | }); --------------------------------------------------------------------------------