Skip to content

Instantly share code, notes, and snippets.

@adamwdraper
Created December 8, 2016 17:40
Show Gist options
  • Select an option

  • Save adamwdraper/26493c5d6a1b39b66f94290b75ef6314 to your computer and use it in GitHub Desktop.

Select an option

Save adamwdraper/26493c5d6a1b39b66f94290b75ef6314 to your computer and use it in GitHub Desktop.

Revisions

  1. adamwdraper created this gist Dec 8, 2016.
    93 changes: 93 additions & 0 deletions build src numeral.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    (function (global, factory) {
    if (typeof define === "function" && define.amd) {
    define(['exports'], factory);
    } else if (typeof exports !== "undefined") {
    factory(exports);
    } else {
    var mod = {
    exports: {}
    };
    factory(mod.exports);
    global.numeral = mod.exports;
    }
    })(this, function (exports) {
    'use strict';

    Object.defineProperty(exports, "__esModule", {
    value: true
    });
    /*! @preserve
    * numeral.js
    * version : 2.0.1
    * author : Adam Draper
    * license : MIT
    * http://adamwdraper.github.com/Numeral-js/
    */

    /************************************
    Variables
    ************************************/

    var _numeral,
    _,
    VERSION = '2.0.1',
    formats = {},
    locales = {},
    defaults = {
    currentLocale: 'en',
    zeroFormat: null,
    nullFormat: null,
    defaultFormat: '0,0'
    },
    options = {
    currentLocale: defaults.currentLocale,
    zeroFormat: defaults.zeroFormat,
    nullFormat: defaults.nullFormat,
    defaultFormat: defaults.defaultFormat
    };

    /************************************
    Constructors
    ************************************/

    _numeral = function numeral(input) {
    var value, kind, unformatFunction, regexp;

    if (_numeral.isNumeral(input)) {
    value = input.value();
    } else if (input === 0 || typeof input === 'undefined') {
    value = 0;
    } else if (input === null || _.isNaN(input)) {
    value = null;
    } else if (typeof input === 'string') {
    if (options.zeroFormat && input === options.zeroFormat) {
    value = 0;
    } else if (options.nullFormat && input === options.nullFormat || !input.replace(/[^0-9]+/g, '').length) {
    value = null;
    } else {
    for (kind in formats) {
    regexp = typeof formats[kind].regexps.unformat === 'function' ? formats[kind].regexps.unformat() : formats[kind].regexps.unformat;

    if (regexp && input.match(regexp)) {
    unformatFunction = formats[kind].unformat;

    break;
    }
    }

    unformatFunction = unformatFunction || _numeral._.stringToNumber;

    value = unformatFunction(input);
    }
    } else {
    value = Number(input) || null;
    }

    return new Numeral(input, value);
    };

    // ... removed a bunch of extra code here for this gist

    exports.default = _numeral;
    });
    //# sourceMappingURL=numeral.js.map
    58 changes: 58 additions & 0 deletions build tests numeral.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    (function (global, factory) {
    if (typeof define === "function" && define.amd) {
    define(['../src/numeral', 'chai'], factory);
    } else if (typeof exports !== "undefined") {
    factory(require('../src/numeral'), require('chai'));
    } else {
    var mod = {
    exports: {}
    };
    factory(global.numeral, global.chai);
    global.numeral = mod.exports;
    }
    })(this, function (_numeral, _chai) {
    'use strict';

    var _numeral2 = _interopRequireDefault(_numeral);

    function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
    default: obj
    };
    }

    var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
    return typeof obj;
    } : function (obj) {
    return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
    };

    describe('Numeral', function () {
    afterEach(function () {
    _numeral2.default.reset();
    });

    describe('Default', function () {
    it('should set a default format', function () {
    _numeral2.default.defaultFormat('0,0');

    (0, _chai.expect)((0, _numeral2.default)(10000).format()).to.equal('10,000');
    });
    });

    describe('Value', function () {
    it('should return a value', function () {
    var tests = [[1000, 1000], [0.5, 0.5], [null, null], ['1,000', 1000], ['not a number', null]],
    num;

    for (var i = 0; i < tests.length; i++) {
    num = (0, _numeral2.default)(tests[i][0]);

    (0, _chai.expect)(num.value()).to.equal(tests[i][1]);
    }
    });
    });
    });

    // ... removed a bunch of extra code here for this gist
    });
    77 changes: 77 additions & 0 deletions src numeral.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    /*! @preserve
    * numeral.js
    * version : 2.0.1
    * author : Adam Draper
    * license : MIT
    * http://adamwdraper.github.com/Numeral-js/
    */

    /************************************
    Variables
    ************************************/

    var numeral,
    _,
    VERSION = '2.0.1',
    formats = {},
    locales = {},
    defaults = {
    currentLocale: 'en',
    zeroFormat: null,
    nullFormat: null,
    defaultFormat: '0,0'
    },
    options = {
    currentLocale: defaults.currentLocale,
    zeroFormat: defaults.zeroFormat,
    nullFormat: defaults.nullFormat,
    defaultFormat: defaults.defaultFormat
    };


    /************************************
    Constructors
    ************************************/

    numeral = function(input) {
    var value,
    kind,
    unformatFunction,
    regexp;

    if (numeral.isNumeral(input)) {
    value = input.value();
    } else if (input === 0 || typeof input === 'undefined') {
    value = 0;
    } else if (input === null || _.isNaN(input)) {
    value = null;
    } else if (typeof input === 'string') {
    if (options.zeroFormat && input === options.zeroFormat) {
    value = 0;
    } else if (options.nullFormat && input === options.nullFormat || !input.replace(/[^0-9]+/g, '').length) {
    value = null;
    } else {
    for (kind in formats) {
    regexp = typeof formats[kind].regexps.unformat === 'function' ? formats[kind].regexps.unformat() : formats[kind].regexps.unformat;

    if (regexp && input.match(regexp)) {
    unformatFunction = formats[kind].unformat;

    break;
    }
    }

    unformatFunction = unformatFunction || numeral._.stringToNumber;

    value = unformatFunction(input);
    }
    } else {
    value = Number(input)|| null;
    }

    return new Numeral(input, value);
    };

    // ... removed a bunch of extra code here for this gist

    export default numeral;
    37 changes: 37 additions & 0 deletions tests numeral.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    import numeral from '../src/numeral';
    import { expect } from 'chai';

    describe('Numeral', function() {
    afterEach(function() {
    numeral.reset();
    });

    describe('Default', function() {
    it('should set a default format', function() {
    numeral.defaultFormat('0,0');

    expect(numeral(10000).format()).to.equal('10,000');
    });
    });

    describe('Value', function() {
    it('should return a value', function() {
    var tests = [
    [1000, 1000],
    [0.5, 0.5],
    [null, null],
    ['1,000', 1000],
    ['not a number', null]
    ],
    num;

    for (var i = 0; i < tests.length; i++) {
    num = numeral(tests[i][0]);

    expect(num.value()).to.equal(tests[i][1]);
    }
    });
    });

    // ... removed a bunch of extra code here for this gist
    });