Nodejs Array Check isNumberArray()

Here you can find the source of isNumberArray()

Method Source Code

'use strict';//from w  w  w  .  j a  v  a  2  s. c o m

Array.prototype.isNumberArray = function() {
    return !this.some(x => typeof x != 'number');
}

module.exports = function segmentNumbers(arr) {
    arr = arr.sort();
    if (!arr.isNumberArray()) return [];
    let container = [],
        items = [],
        expectedVal = arr[0];
    for (let i = 0 ; i <= arr.length - 1; i++) {
        if (arr[i] !== expectedVal) {
            if (items.length !== 0) {
                container.push(items);
                items = [];
            }
        }
        items.push(arr[i]);
        expectedVal = arr[i] + 1;
    }
    container.push(items);
    return container;
}

Related

  1. isInt()
    Array.prototype.isInt = function (){
      return this.filter(function(n){ return (typeof(n) === 'number') && (n%1 === 0)})
    };
    Array.prototype.even = function (){
      return this.isInt().filter(function(n){ return n%2 === 0})
    };
    Array.prototype.odd = function (){
      return this.isInt().filter(function(n){ return n%2 !== 0})
    };
    ...
    
  2. isLastIndex(index)
    Array.prototype.isLastIndex = function(index) {
      return index == (this.length - 1);
    
  3. isMember(b)
    Array.prototype.isMember = function (b) {
      return this.some(function (i) {
        return this == i
      }, b);
    
  4. isNFromLastIndex(index, n)
    Array.prototype.isNFromLastIndex = function(index, n) {
      return (this.length - 1 - n) == index;
    
  5. isNothing()
    Array.prototype.isNothing = function() {
      return this.length === 0;
    };
    
  6. isSparse()
    Array.prototype.isSparse = function() {
      return this.length !== Object.keys(this).length;