Nodejs Number Calculate set(key, value)

Here you can find the source of set(key, value)

Method Source Code

'use strict';//from  w  w w .  j  a  v  a2  s.  c  o  m
const assert = require('assert');

// NaN space
const nan = Symbol('NaN');

// hide nan space
const define = Object.defineProperty;


// NaN space
Number.prototype[nan] = Object.create(null);

// secret setter
Number.prototype.set = function (key, value) {
  if (null == this || !isNaN(this)) { return this; }
  const space = this[nan];
  const accessor = {
    get() {
      return isNaN(this) ? space[key] : undefined;
    },

    set(value) { space[key] = isNaN(this) ? value : space[key]; }
  }

  space[key] = value;
  define(Number.prototype, key, accessor);

  return this;
};

assert('function' == typeof 4..set)

let variants = [
  (Infinity / -Infinity),
  (Number.NaN),
  ('' / ''),
  ('' / 0),
  (0 / 0),
  (NaN),
]

const key = 'key';
const value = 'value';

NaN.set(key, value);

// the following fail
4..set(key, value);
0..set(key, value);
0xff..set(key, value);

for (let variant of variants) {
  assert(value == variant[key])
}

assert(!4[key])
assert(!0[key])
assert(!0xff[key])

Related

  1. rnd2()
    Number.prototype.rnd2 = function () {
      return Math.round(this.valueOf() * 100) / 100;
    
  2. rotate()
    Number.prototype.rotate = Number.prototype.rotate || function () {
      return Math.floor((this / 10) + ((this % 10) * Math.pow(10, exports.digitLength(this) - 1)));
    };
    
  3. safeDivideBy(x)
    Number.prototype.safeDivideBy = function(x) {
        var result = this.valueOf() / x;
        if(result === Infinity) {
            return 0;
        else {
            return result;
    
  4. send(method)
    Number.prototype.send = function(method) {
      return this[method].call();
    };
    
  5. separatorsseparators()
    Number.prototype.separators = function separators(){
      var string = this.toString();
      var x      = string.split('.');
      var x1     = x[0];
      var x2     = x.length > 1 ? '.' + x[1] : '';
      var regex  = /(\d+)(\d{3})/;
      while (regex.test(x1)) {
          x1 = x1.replace(regex, '$1' + ',' + '$2');
      return x1 + x2;
    };
    
  6. setInCirclesetInCircle(lower, upper)
    Number.prototype.setInCircle = function setInCircle(lower, upper){
      var zahl = this;
      if (lower === upper){
        return lower;
      if (lower > upper){
        var l = lower;
        lower = upper;
        upper = l;
    ...
    
  7. setInRangesetInRange(lower, upper)
    Number.prototype.setInRange = function setInRange(lower, upper){
      if (typeof lower === "undefined" || lower === null){
        lower = this;
      if (typeof upper === "undefined" || upper === null){
        upper = this;
      lower = parseFloat(lower) || 0;
      upper = parseFloat(upper) || 0;
    ...
    
  8. sigFig(precision)
    'use strict'
    Number.prototype.sigFig = function(precision){
        if( !precision ) precision = 2;
        var d = Math.pow(10, precision);
        return ( parseInt( this.valueOf() * d) / d ).toFixed(precision);
    };
    
  9. simEq(number, digits)
    DEFAULT_DIGIT = 5;
    Number.prototype.simEq = function(number, digits) {
      digits = digits == undefined ? DEFAULT_DIGIT : digits;
      var n1 = this.toFixed(digits);
      var n2 = number.toFixed(digits);
      if (n1 == -0)
        n1 = 0;
      if (n2 == -0)
        n2 = 0;
    ...