Skip to content

Commit

Permalink
Merge pull request #2 from bluealba/bugfix/make-it-run-on-non-es6-bro…
Browse files Browse the repository at this point in the history
…wsers

make the code browser compatible
  • Loading branch information
aitrusgit authored Sep 7, 2017
2 parents af14dae + 6391357 commit 8b6eae4
Show file tree
Hide file tree
Showing 9 changed files with 814 additions and 91 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets" : ["es2015"]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
npm-debug*
coverage
.nyc_output
/dist/*
59 changes: 1 addition & 58 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,3 @@
"use strict";

const suffixes = ["th", "st", "nd", "rd"];

const enhance = require("./lib/enhance"),
identity = require("./lib/identity");

/**
* Returns the ordinal suffix (st, nd, th) for the provided number.
*
* @param {Number} number a number, if type of the provided element
* is not a number then function will not fail
* and will return undefined.
* @return {String} a string containing the number ordinal suffix.
*/
function ordinalSuffix(number) {
if (typeof(number) !== "number") {
throw new TypeError(`Cannot determine ordinal suffix of something of type ${typeof(number)}`)
}

if (Number.isNaN(number)) {
return undefined;
}
const v = Math.abs(number) % 100;
return suffixes[(v - 20) % 10] || suffixes[v] || suffixes[0];
}

/**
* Returns the ordinal reprentation of a certain number.
*
* @param {Number} number a number, if type of the provided element
* is not a number then function will fail. If NaN
* then "NaN" will be returned.
* @param {Function} transform an optional function that will be invoked
* with the suffix associated to the number.
* This can be used to perform some transformation
* before appending the suffix to the number
* itself.
*
* @return {String} a string containing the number ordinal
* representation.
*/
function toOrdinal(number, transform) {
transform = transform || identity;

const s = ordinalSuffix(number);
return s ? number + transform(s) : number.toString();
}

/**
* Extends Number prototype adding the toOrdinal method
*/
const ordinal = function() {
enhance(Number, "toOrdinal", toOrdinal);
enhance(Number, "ordinalSuffix", ordinalSuffix);
}
ordinal.toOrdinal = toOrdinal;
ordinal.ordinalSuffix = ordinalSuffix;

module.exports = ordinal;
module.exports = require("./dist/ordinal-js");
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
{
"name": "ordinal-js",
"version": "1.0.1",
"version": "1.1.0",
"description": "Utility to convert from numbers to their ordinal representations",
"main": "index.js",
"author": "Claudio Fernandez <[email protected]>",
"license": "ISC",
"devDependencies": {
"babel": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babelify": "^7.3.0",
"browserify": "^14.4.0",
"chai": "^4.1.1",
"coveralls": "^2.13.1",
"eslint": "^4.5.0",
"mocha": "^3.5.0",
"nyc": "^11.1.0"
},
"scripts": {
"test": "eslint '.' && nyc --reporter=html --reporter=text mocha",
"lint": "eslint '.'",
"build": "babel src --out-dir dist",
"test": "eslint './src' && nyc --reporter=html --reporter=text mocha",
"lint": "eslint './src'",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
"keywords": [
Expand Down
File renamed without changes.
File renamed without changes.
60 changes: 60 additions & 0 deletions src/ordinal-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"use strict";

const suffixes = ["th", "st", "nd", "rd"];

const enhance = require("./enhance"),
identity = require("./identity");

/**
* Returns the ordinal suffix (st, nd, th) for the provided number.
*
* @param {Number} number a number, if type of the provided element
* is not a number then function will not fail
* and will return undefined.
* @return {String} a string containing the number ordinal suffix.
*/
function ordinalSuffix(number) {
if (typeof(number) !== "number") {
throw new TypeError(`Cannot determine ordinal suffix of something of type ${typeof(number)}`)
}

if (Number.isNaN(number)) {
return undefined;
}
const v = Math.abs(number) % 100;
return suffixes[(v - 20) % 10] || suffixes[v] || suffixes[0];
}

/**
* Returns the ordinal reprentation of a certain number.
*
* @param {Number} number a number, if type of the provided element
* is not a number then function will fail. If NaN
* then "NaN" will be returned.
* @param {Function} transform an optional function that will be invoked
* with the suffix associated to the number.
* This can be used to perform some transformation
* before appending the suffix to the number
* itself.
*
* @return {String} a string containing the number ordinal
* representation.
*/
function toOrdinal(number, transform) {
transform = transform || identity;

const s = ordinalSuffix(number);
return s ? number + transform(s) : number.toString();
}

/**
* Extends Number prototype adding the toOrdinal method
*/
const ordinal = function() {
enhance(Number, "toOrdinal", toOrdinal);
enhance(Number, "ordinalSuffix", ordinalSuffix);
}
ordinal.toOrdinal = toOrdinal;
ordinal.ordinalSuffix = ordinalSuffix;

module.exports = ordinal;
4 changes: 1 addition & 3 deletions test/test-index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const ordinal = require("../index"),
const ordinal = require("../src/ordinal-js"),
chai = require("chai");

chai.should();
Expand All @@ -11,8 +11,6 @@ describe("ordinal-js", () => {

context("when language is english", () => {



// Numbers ending in 1
it("1 should be transformed to 1st", () => {
ordinal.toOrdinal(1).should.be.equal("1st");
Expand Down
Loading

0 comments on commit 8b6eae4

Please sign in to comment.