-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from bluealba/bugfix/make-it-run-on-non-es6-bro…
…wsers make the code browser compatible
- Loading branch information
Showing
9 changed files
with
814 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets" : ["es2015"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ node_modules | |
npm-debug* | ||
coverage | ||
.nyc_output | ||
/dist/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [ | ||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.