linear-array
Advanced tools
Sorry, the diff of this file is not supported yet
| !function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.lineArr=r():e.lineArr=r()}(this,(function(){return function(e){var r={};function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:n})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,r){if(1&r&&(e=t(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var o in e)t.d(n,o,function(r){return e[r]}.bind(null,o));return n},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="",t(t.s=0)}([function(e,r,t){"use strict";function n(...e){for(let r=0;r<e.length;r++){if(!Number.isFinite(e[r]))throw new TypeError("expected a number");if(!Number.isInteger(e[r]))throw new Error("expected an integer");if(!Number.isSafeInteger(e[r]))throw new Error("value above safe integer");if(e[r]<0)throw new Error("expected a number greater or equal to 0")}}function o(...e){for(let r=0;r<e.length;r++)if(!Array.isArray(e[r]))throw new Error("expected an array")}function u(e){o(e);try{if(n(...e),0!==e[0])return!1;for(let r=1;r<e.length;r++)if(e[r]-e[r-1]!=1)return!1;return!0}catch(e){return!1}}function f(e,r=!1){n(e),function(...e){for(let r=0;r<e.length;r++)if("boolean"!=typeof e[r])throw new Error("expected a boolean")}(r);const t=new Array(e);for(let n=0;n<(r?e+1:e);n++)t[n]=n;return t}function i(e,r=1,t=0){o(e),n(r,t);try{if(n(...e),e[0]!==t)return!1;for(let t=1;t<e.length;t++)if(e[t]-e[t-1]!==r)return!1;return!0}catch(e){return!1}}function c(e,r=1,t=0){n(e,r,t);const o=new Array(e);for(let n=0;n<e;n++)o[n]=n*r+t;return o} | ||
| /*! | ||
| * linear-array <https://github.com/ispoljari/linear-array> | ||
| * | ||
| * Copyright (c) 2020, Ivan Spoljaric. | ||
| * Released under the MIT License. | ||
| */t.r(r),t.d(r,"isSeqNaturalNumbers",(function(){return u})),t.d(r,"fillSeqNaturalNumbers",(function(){return f})),t.d(r,"isStepSequenceWithOffset",(function(){return i})),t.d(r,"fillStepSequenceWithOffset",(function(){return c}))}])})); |
| /*! | ||
| * linear-array <https://github.com/ispoljari/linear-array> | ||
| * | ||
| * Copyright (c) 2020, Ivan Spoljaric. | ||
| * Released under the MIT License. | ||
| */ | ||
| export * from './natural-sequence'; | ||
| export * from './step-with-offset-sequence'; |
| import * as util from './utils'; | ||
| /** | ||
| * (arr: number[]) => boolean. | ||
| * | ||
| * examples: | ||
| * | ||
| * [0,1,2,3,4] -> true | ||
| * | ||
| * [2,3,4,5,6] -> false | ||
| * | ||
| * [0,1,2,3,5] -> false | ||
| */ | ||
| export function isSeqNaturalNumbers(arr) { | ||
| util.areValidArrays(...[arr]); | ||
| try { | ||
| util.areValidNumbers(...arr); | ||
| if (arr[0] !== 0) { | ||
| return false; | ||
| } | ||
| for (let i = 1; i < arr.length; i++) { | ||
| if (arr[i] - arr[i - 1] !== 1) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } catch (error) { | ||
| return false; | ||
| } | ||
| } | ||
| /** | ||
| * (limiter: number, [includeLast = false]: boolean) => number[] | ||
| * | ||
| * Returns the following array: | ||
| * | ||
| * [0, 1, 2, 3, ... , includeLast ? limiter : (limiter - 1)] | ||
| * | ||
| * examples: | ||
| * | ||
| * (7) - > [0, 1, 2, 3, 4, 5, 6] | ||
| * | ||
| * (7, true) - > [0, 1, 2, 3, 4, 5, 6, 7] | ||
| * | ||
| * | ||
| */ | ||
| export function fillSeqNaturalNumbers(limiter, includeLast = false) { | ||
| util.areValidNumbers(limiter); | ||
| util.areValidBooleans(includeLast); | ||
| const output = new Array(limiter); | ||
| for (let index = 0; index < (includeLast ? limiter + 1 : limiter); index++) { | ||
| output[index] = index; | ||
| } | ||
| return output; | ||
| } |
| import * as util from './utils'; | ||
| /** | ||
| * (arr: number[], [step = 1]: number, [offset = 0]: number) => boolean. | ||
| * | ||
| * examples: | ||
| * | ||
| * ([0,1,2,3,4]) -> true | ||
| * | ||
| * ([0,1,2,3,4], 2) -> false | ||
| * | ||
| * ([0,1,2,3,4], 1, 1) -> false | ||
| * | ||
| * ([1,2,3,4,5], 1, 1) -> true | ||
| * | ||
| * ([6,9,12,15,18], 3, 6) -> true | ||
| * | ||
| * ([5,9,12,15,19], 3, 6) -> false | ||
| */ | ||
| export function isStepSequenceWithOffset(arr, step = 1, offset = 0) { | ||
| util.areValidArrays(...[arr]); | ||
| util.areValidNumbers(step, offset); | ||
| try { | ||
| util.areValidNumbers(...[...arr]); | ||
| if (arr[0] !== offset) { | ||
| return false; | ||
| } | ||
| for (let i = 1; i < arr.length; i++) { | ||
| if (arr[i] - arr[i - 1] !== step) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } catch (error) { | ||
| return false; | ||
| } | ||
| } | ||
| /** | ||
| * (limiter: number, [step = 1]: number, [offset = 0]: number) => number[]. | ||
| * | ||
| * Returns the following array: | ||
| * | ||
| * [0+offset, step + offset, 2*step + offset, ..., (limiter - 1)*step + offset] | ||
| * | ||
| * examples: | ||
| * | ||
| * (5) - > [0, 1, 2, 3, 4] | ||
| * | ||
| * (5,1,3) -> [3, 4, 5, 6, 7] | ||
| * | ||
| * (5,3) -> [0, 3, 6, 9, 12] | ||
| * | ||
| * (5,2,2) -> [2, 4, 6, 8, 10] | ||
| */ | ||
| export function fillStepSequenceWithOffset(limiter, step = 1, offset = 0) { | ||
| util.areValidNumbers(limiter, step, offset); | ||
| const output = new Array(limiter); | ||
| for (let index = 0; index < limiter; index++) { | ||
| output[index] = index * step + offset; | ||
| } | ||
| return output; | ||
| } |
| export function areValidNumbers(...values) { | ||
| for (let i = 0; i < values.length; i++) { | ||
| if (!Number.isFinite(values[i])) { | ||
| throw new TypeError('expected a number'); | ||
| } | ||
| if (!Number.isInteger(values[i])) { | ||
| throw new Error('expected an integer'); | ||
| } | ||
| if (!Number.isSafeInteger(values[i])) { | ||
| throw new Error('value above safe integer'); | ||
| } | ||
| if (values[i] < 0) { | ||
| throw new Error('expected a number greater or equal to 0'); | ||
| } | ||
| } | ||
| } | ||
| export function areValidBooleans(...values) { | ||
| for (let i = 0; i < values.length; i++) { | ||
| if (typeof values[i] !== 'boolean') { | ||
| throw new Error('expected a boolean'); | ||
| } | ||
| } | ||
| } | ||
| export function areValidArrays(...values) { | ||
| for (let i = 0; i < values.length; i++) { | ||
| if (!Array.isArray(values[i])) { | ||
| throw new Error('expected an array'); | ||
| } | ||
| } | ||
| } |
| const lineArr = require('../dist/linear-array'); | ||
| const { | ||
| notNumberErrorMsgPairs, | ||
| notIntegerErrorMsgPairs, | ||
| notSafeIntegerErrorMsgPairs, | ||
| notNaturalNumberErrorMsgPairs, | ||
| notBooleanErrorMsgPairs, | ||
| notArrayErrorMsgPairs, | ||
| } = require('./utils/input-expected-pairs'); | ||
| const { | ||
| throwsArgumentAsserter, | ||
| deepEqualsAsserter, | ||
| } = require('./utils/asserters'); | ||
| describe('lineArr > natural-sequence', function () { | ||
| describe('fillSeqNaturalNumbers', function () { | ||
| describe('type error > limiter (first argument)', function () { | ||
| it('should throw an error if the limiter value is not a number', function () { | ||
| throwsArgumentAsserter( | ||
| notNumberErrorMsgPairs, | ||
| lineArr.fillSeqNaturalNumbers | ||
| ); | ||
| }); | ||
| it('should throw an error if the limiter is not an integer', function () { | ||
| throwsArgumentAsserter( | ||
| notIntegerErrorMsgPairs, | ||
| lineArr.fillSeqNaturalNumbers | ||
| ); | ||
| }); | ||
| it('should throw an error if the limiter is not a safe integer', function () { | ||
| throwsArgumentAsserter( | ||
| notSafeIntegerErrorMsgPairs, | ||
| lineArr.fillSeqNaturalNumbers | ||
| ); | ||
| }); | ||
| it('should throw an error if the limiter is not equal to or bigger than 0', function () { | ||
| throwsArgumentAsserter( | ||
| notNaturalNumberErrorMsgPairs, | ||
| lineArr.fillSeqNaturalNumbers | ||
| ); | ||
| }); | ||
| }); | ||
| describe('type error > includeLast (second argument)', function () { | ||
| const FIXED_LIMITER = 10; | ||
| it('should throw an error if includeLast is not a boolean', function () { | ||
| throwsArgumentAsserter( | ||
| notBooleanErrorMsgPairs, | ||
| lineArr.fillSeqNaturalNumbers, | ||
| FIXED_LIMITER | ||
| ); | ||
| }); | ||
| }); | ||
| describe('results (given valid arguments)', function () { | ||
| const validInputResultPairs = [ | ||
| { | ||
| input: [0], | ||
| expected: [], | ||
| }, | ||
| { | ||
| input: [1], | ||
| expected: [0], | ||
| }, | ||
| { | ||
| input: [3], | ||
| expected: [0, 1, 2], | ||
| }, | ||
| { | ||
| input: [5], | ||
| expected: [0, 1, 2, 3, 4], | ||
| }, | ||
| { | ||
| input: [0, true], | ||
| expected: [0], | ||
| }, | ||
| { | ||
| input: [1, true], | ||
| expected: [0, 1], | ||
| }, | ||
| { | ||
| input: [3, true], | ||
| expected: [0, 1, 2, 3], | ||
| }, | ||
| { | ||
| input: [5, true], | ||
| expected: [0, 1, 2, 3, 4, 5], | ||
| }, | ||
| ]; | ||
| it('should return correct results', function () { | ||
| deepEqualsAsserter( | ||
| validInputResultPairs, | ||
| lineArr.fillSeqNaturalNumbers | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| describe('isSeqNaturalNumbers', function () { | ||
| describe('type error > arr (only argument)', function () { | ||
| it('should throw an error if arr is not of type Array', function () { | ||
| throwsArgumentAsserter( | ||
| notArrayErrorMsgPairs, | ||
| lineArr.isSeqNaturalNumbers | ||
| ); | ||
| }); | ||
| }); | ||
| describe('results (given a valid argument)', function () { | ||
| const validInputFalseResultPairs = [ | ||
| { | ||
| input: [[Infinity, 0, 1, 2, 3, 4]], | ||
| expected: false, | ||
| }, | ||
| { | ||
| input: [[1, 2, 3, 4]], | ||
| expected: false, | ||
| }, | ||
| { | ||
| input: [[0, 1, true, 3, 'dkdk', 5]], | ||
| expected: false, | ||
| }, | ||
| { | ||
| input: [[0, 1, 2, 3, 4, 5, 7]], | ||
| expected: false, | ||
| }, | ||
| { | ||
| input: [[1, 2, 3, 4, 5]], | ||
| expected: false, | ||
| }, | ||
| { | ||
| input: [[-1, 0, 1, 2, 3, 4, 5]], | ||
| expected: false, | ||
| }, | ||
| { | ||
| input: [[NaN, false, 'true', 2]], | ||
| expected: false, | ||
| }, | ||
| { | ||
| input: [[5, 5, 5, 5]], | ||
| expected: false, | ||
| }, | ||
| { | ||
| input: [[5, 4, 3, 2, 1, 0]], | ||
| expected: false, | ||
| }, | ||
| ]; | ||
| const validInputTrueResultPairs = [ | ||
| { | ||
| input: [[0]], | ||
| expected: true, | ||
| }, | ||
| { | ||
| input: [[0, 1, 2, 3, 4]], | ||
| expected: true, | ||
| }, | ||
| { | ||
| input: [[0, 1, 2, 3, 4, 5, 6, 7, 8]], | ||
| expected: true, | ||
| }, | ||
| ]; | ||
| it('should return false', function () { | ||
| deepEqualsAsserter( | ||
| validInputFalseResultPairs, | ||
| lineArr.isSeqNaturalNumbers | ||
| ); | ||
| }); | ||
| it('should return true', function () { | ||
| deepEqualsAsserter( | ||
| validInputTrueResultPairs, | ||
| lineArr.isSeqNaturalNumbers | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
| const lineArr = require('../dist/linear-array'); | ||
| const { | ||
| notNumberErrorMsgPairs, | ||
| notIntegerErrorMsgPairs, | ||
| notSafeIntegerErrorMsgPairs, | ||
| notNaturalNumberErrorMsgPairs, | ||
| notArrayErrorMsgPairs, | ||
| } = require('./utils/input-expected-pairs'); | ||
| const { | ||
| throwsArgumentAsserter, | ||
| deepEqualsAsserter, | ||
| } = require('./utils/asserters'); | ||
| const FIXED_LIMITER = 10; | ||
| const FIXED_STEP = 10; | ||
| describe('lineArr > step-with-offset-sequence', function () { | ||
| describe('fillStepSequenceWithOffset', function () { | ||
| describe('type error > argument not a number', function () { | ||
| it('should throw an error if the limiter (first argument) is not valid', function () { | ||
| throwsArgumentAsserter( | ||
| notNumberErrorMsgPairs, | ||
| lineArr.fillStepSequenceWithOffset | ||
| ); | ||
| }); | ||
| it('should throw an error if the step (second argument) is not valid', function () { | ||
| throwsArgumentAsserter( | ||
| notNumberErrorMsgPairs, | ||
| lineArr.fillStepSequenceWithOffset, | ||
| FIXED_LIMITER | ||
| ); | ||
| }); | ||
| it('should throw an error if the offset (third argument) is not valid', function () { | ||
| throwsArgumentAsserter( | ||
| notNumberErrorMsgPairs, | ||
| lineArr.fillStepSequenceWithOffset, | ||
| FIXED_LIMITER, | ||
| FIXED_STEP | ||
| ); | ||
| }); | ||
| }); | ||
| describe('type error > argument not an integer', function () { | ||
| it('should throw an error if the limiter (first argument) is not valid', function () { | ||
| throwsArgumentAsserter( | ||
| notIntegerErrorMsgPairs, | ||
| lineArr.fillStepSequenceWithOffset | ||
| ); | ||
| }); | ||
| it('should throw an error if the step (second argument) is not valid', function () { | ||
| throwsArgumentAsserter( | ||
| notIntegerErrorMsgPairs, | ||
| lineArr.fillStepSequenceWithOffset, | ||
| FIXED_LIMITER | ||
| ); | ||
| }); | ||
| it('should throw an error if the offset (third argument) is not valid', function () { | ||
| throwsArgumentAsserter( | ||
| notIntegerErrorMsgPairs, | ||
| lineArr.fillStepSequenceWithOffset, | ||
| FIXED_LIMITER, | ||
| FIXED_STEP | ||
| ); | ||
| }); | ||
| }); | ||
| describe('type error > argument not a safe integer', function () { | ||
| it('should throw an error if the limiter (first argument) is not valid', function () { | ||
| throwsArgumentAsserter( | ||
| notSafeIntegerErrorMsgPairs, | ||
| lineArr.fillStepSequenceWithOffset | ||
| ); | ||
| }); | ||
| it('should throw an error if the step (second argument) is not valid', function () { | ||
| throwsArgumentAsserter( | ||
| notSafeIntegerErrorMsgPairs, | ||
| lineArr.fillStepSequenceWithOffset, | ||
| FIXED_LIMITER | ||
| ); | ||
| }); | ||
| it('should throw an error if the offset (third argument) is not valid', function () { | ||
| throwsArgumentAsserter( | ||
| notSafeIntegerErrorMsgPairs, | ||
| lineArr.fillStepSequenceWithOffset, | ||
| FIXED_LIMITER, | ||
| FIXED_STEP | ||
| ); | ||
| }); | ||
| }); | ||
| describe('type error > argument is not equal to or bigger than 0', function () { | ||
| it('should throw an error if the limiter (first argument) is not valid', function () { | ||
| throwsArgumentAsserter( | ||
| notNaturalNumberErrorMsgPairs, | ||
| lineArr.fillStepSequenceWithOffset | ||
| ); | ||
| }); | ||
| it('should throw an error if the step (second argument) is not valid', function () { | ||
| throwsArgumentAsserter( | ||
| notNaturalNumberErrorMsgPairs, | ||
| lineArr.fillStepSequenceWithOffset, | ||
| FIXED_LIMITER | ||
| ); | ||
| }); | ||
| it('should throw an error if the offset (third argument) is not valid', function () { | ||
| throwsArgumentAsserter( | ||
| notNaturalNumberErrorMsgPairs, | ||
| lineArr.fillStepSequenceWithOffset, | ||
| FIXED_LIMITER, | ||
| FIXED_STEP | ||
| ); | ||
| }); | ||
| }); | ||
| describe('results (given valid arguments)', function () { | ||
| const validInputResultPairs = [ | ||
| { | ||
| input: [5], | ||
| expected: [0, 1, 2, 3, 4], | ||
| }, | ||
| { | ||
| input: [5, 1, 3], | ||
| expected: [3, 4, 5, 6, 7], | ||
| }, | ||
| { | ||
| input: [5, 3], | ||
| expected: [0, 3, 6, 9, 12], | ||
| }, | ||
| { | ||
| input: [5, 2, 2], | ||
| expected: [2, 4, 6, 8, 10], | ||
| }, | ||
| ]; | ||
| it('should return correct results', function () { | ||
| deepEqualsAsserter( | ||
| validInputResultPairs, | ||
| lineArr.fillStepSequenceWithOffset | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| describe('isStepSequenceWithOffset', function () { | ||
| describe('type error > arr', function () { | ||
| it('should throw an error if arr is not of type Array', function () { | ||
| throwsArgumentAsserter( | ||
| notArrayErrorMsgPairs, | ||
| lineArr.isStepSequenceWithOffset | ||
| ); | ||
| }); | ||
| }); | ||
| // add other type error checks | ||
| describe('results (given valid arguments)', function () { | ||
| const validInputResultPairs = [ | ||
| { | ||
| input: [[0, 1, 2, 3, 4]], | ||
| expected: true, | ||
| }, | ||
| { | ||
| input: [[0, 1, 2, 3, 4], 2], | ||
| expected: false, | ||
| }, | ||
| { | ||
| input: [[0, 1, 2, 3, 4], 1, 1], | ||
| expected: false, | ||
| }, | ||
| { | ||
| input: [[1, 2, 3, 4, 5], 1, 1], | ||
| expected: true, | ||
| }, | ||
| { | ||
| input: [[6, 9, 12, 15, 18], 3, 6], | ||
| expected: true, | ||
| }, | ||
| { | ||
| input: [[5, 9, 12, 15, 19], 3, 6], | ||
| expected: false, | ||
| }, | ||
| ]; | ||
| it('should return correct results', function () { | ||
| deepEqualsAsserter( | ||
| validInputResultPairs, | ||
| lineArr.isStepSequenceWithOffset | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
| const assert = require('assert'); | ||
| const throwsArgumentAsserter = (assertList, fn, ...fixedArgs) => { | ||
| for (let i = 0; i < assertList.length; i++) { | ||
| assert.throws( | ||
| () => | ||
| fixedArgs.length > 0 | ||
| ? fn(...fixedArgs, assertList[i].input) | ||
| : fn(assertList[i].input), | ||
| assertList[i].expected | ||
| ); | ||
| } | ||
| }; | ||
| const deepEqualsAsserter = (assertList, fn) => { | ||
| for (let i = 0; i < assertList.length; i++) { | ||
| assert.deepStrictEqual(fn(...assertList[i].input), assertList[i].expected); | ||
| } | ||
| }; | ||
| module.exports = { | ||
| throwsArgumentAsserter, | ||
| deepEqualsAsserter, | ||
| }; |
| const ERR_EXPECTED_NUMBER = /expected a number/; | ||
| const ERR_EXPECTED_INTEGER = /expected an integer/; | ||
| const ERR_EXPECTED_SAFE_INTEGER = /value above safe integer/; | ||
| const ERR_EXPECTED_NATURAL_NUMBER = /expected a number greater or equal to 0/; | ||
| const ERR_EXPECTED_BOOLEAN = /expected a boolean/; | ||
| const ERR_EXPECTED_ARRAY = /expected an array/; | ||
| const createBasePairs = (errMsg) => [ | ||
| { | ||
| input: 'random string', | ||
| expected: errMsg, | ||
| }, | ||
| { | ||
| input: 23, | ||
| expected: errMsg, | ||
| }, | ||
| { | ||
| input: true, | ||
| expected: errMsg, | ||
| }, | ||
| { | ||
| input: {}, | ||
| expected: errMsg, | ||
| }, | ||
| { | ||
| input: { demo: 'stuff' }, | ||
| expected: errMsg, | ||
| }, | ||
| { | ||
| input: [], | ||
| expected: errMsg, | ||
| }, | ||
| { | ||
| input: [1, 2, 3], | ||
| expected: errMsg, | ||
| }, | ||
| { | ||
| input: NaN, | ||
| expected: errMsg, | ||
| }, | ||
| { | ||
| input: Infinity, | ||
| expected: errMsg, | ||
| }, | ||
| ]; | ||
| const notArrayErrorMsgPairs = [ | ||
| ...createBasePairs(ERR_EXPECTED_ARRAY).filter( | ||
| (item) => !Array.isArray(item.input) | ||
| ), | ||
| ]; | ||
| const notBooleanErrorMsgPairs = [ | ||
| ...createBasePairs(ERR_EXPECTED_BOOLEAN).filter( | ||
| (item) => typeof item.input !== 'boolean' | ||
| ), | ||
| ]; | ||
| const notNaturalNumberErrorMsgPairs = [ | ||
| { | ||
| input: -2393920, | ||
| expected: ERR_EXPECTED_NATURAL_NUMBER, | ||
| }, | ||
| { | ||
| input: -1, | ||
| expected: ERR_EXPECTED_NATURAL_NUMBER, | ||
| }, | ||
| ]; | ||
| const notSafeIntegerErrorMsgPairs = [ | ||
| { | ||
| input: 9999999999999999, | ||
| expected: ERR_EXPECTED_SAFE_INTEGER, | ||
| }, | ||
| ]; | ||
| const notIntegerErrorMsgPairs = [ | ||
| { | ||
| input: 3.14, | ||
| expected: ERR_EXPECTED_INTEGER, | ||
| }, | ||
| { | ||
| input: 2.67, | ||
| expected: ERR_EXPECTED_INTEGER, | ||
| }, | ||
| ]; | ||
| const notNumberErrorMsgPairs = [ | ||
| ...createBasePairs(ERR_EXPECTED_NUMBER).filter( | ||
| (item) => !Number.isFinite(item.input) | ||
| ), | ||
| ]; | ||
| module.exports = { | ||
| notNumberErrorMsgPairs, | ||
| notIntegerErrorMsgPairs, | ||
| notSafeIntegerErrorMsgPairs, | ||
| notNaturalNumberErrorMsgPairs, | ||
| notBooleanErrorMsgPairs, | ||
| notArrayErrorMsgPairs, | ||
| }; |
| const path = require('path'); | ||
| module.exports = { | ||
| mode: 'production', | ||
| entry: './src/index.js', | ||
| output: { | ||
| globalObject: 'this', | ||
| path: path.resolve(__dirname, 'dist'), | ||
| filename: 'linear-array.js', | ||
| library: 'lineArr', | ||
| libraryTarget: 'umd', | ||
| }, | ||
| }; |
+15
-7
| { | ||
| "name": "linear-array", | ||
| "version": "1.0.1", | ||
| "version": "2.0.0", | ||
| "description": "Returns an array filed with linearly increasing numbers, starting from 0 up to the given value - 1 (without offset), or from 1 to the value itself (with offset).", | ||
| "main": "index.js", | ||
| "main": "./dist/linear-array.js", | ||
| "module": "./src/index.js", | ||
| "scripts": { | ||
| "test": "mocha" | ||
| "test": "webpack && mocha", | ||
| "build": "webpack" | ||
| }, | ||
@@ -32,8 +34,14 @@ "repository": { | ||
| "homepage": "https://github.com/ispoljari/linear-array#readme", | ||
| "dependencies": { | ||
| "is-number": "^7.0.0" | ||
| "dependencies": {}, | ||
| "devDependencies": { | ||
| "husky": "^4.2.5", | ||
| "mocha": "^8.0.1", | ||
| "webpack": "^4.44.0", | ||
| "webpack-cli": "^3.3.12" | ||
| }, | ||
| "devDependencies": { | ||
| "mocha": "^8.0.1" | ||
| "husky": { | ||
| "hooks": { | ||
| "pre-push": "npm test" | ||
| } | ||
| } | ||
| } |
+164
-16
| # linear-array | ||
| > Returns an array filed with linearly increasing numbers, starting from 0 up to the given value - 1 (without offset), or from 1 to the value itself (with offset). | ||
| > Javascript / Node.js utility library. Returns a set of utility functions, available through the lineArr namespace, which serve to generate arrays populated with sequences such as natural or stepWithOffset sequence, and to check if an array is a sequence of a certain type (like a natural sequence [0,1,2,3,4]). | ||
| ## Install | ||
| Install with [npm](https://www.npmjs.com/): | ||
| There are 2 ways to install and use the library. | ||
| 1. [`npm`](https://www.npmjs.com/) | ||
| ```sh | ||
@@ -13,24 +15,148 @@ $ npm install --save linear-array | ||
| 2. import with a [`<script>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script) tag | ||
| ```js | ||
| <script | ||
| type="module" | ||
| src="https://unpkg.com/linear-array@2.0.0/linear-array.js" | ||
| ></script> | ||
| ``` | ||
| ## Importing in project | ||
| Since this package can be used on the client side, as `UMD` or `ES6` module, and server side, as `CommonJS` module, there are 2 ways to import it into your .js file: | ||
| - `CommonJS` | ||
| ```js | ||
| const lineArr = require('linear-array'); | ||
| ``` | ||
| - `ES6` | ||
| ```js | ||
| import lineArr from 'linear-array'; | ||
| ``` | ||
| - `UMD` | ||
| Use the globally available `lineArr` module directly in your code. | ||
| It is being imported from the `linear-array` liibrary with the `<script>` tag, and is attached to the globally available `window` variable. | ||
| ## Usage | ||
| Works with safe integers greater than 0. | ||
| Currently, there are 4 methods available through the `lineArr` module / namespace. | ||
| linearArray(value, [offset]) | ||
| Based on the differences in their input and output data types, they can be divided into 2 categories; | ||
| 1. inputs / arguments are finite integers, the output value is a filled array of finite integers sorted in an increasing order according to a specific pattern | ||
| 2. input / argument is an array of finite integers, and the output value is a boolean which indicates it the given array conforms to a specific pattern | ||
| But both categories operate on the same pattern of data types, and these patterns can be categorized in a mathematical sense as [Sequences](https://en.wikipedia.org/wiki/Sequence). | ||
| But, repetition of numbers in the arrays is not allowed. | ||
| ### **Sequence of natural numbers** | ||
| ### `fillSeqNaturalNumbers (limiter: number, [includeLast = false]: boolean) => number[]` | ||
| ```js | ||
| import linearArray from 'linear-array'; // web | ||
| fillSeqNaturalNumbers(limiter, inludeLast?) === [0, 1, 2, 3, ... , includeLast ? limiter : (limiter - 1)] | ||
| OR; | ||
| (0) -> [] | ||
| const linearArray = require('linear-array'); // server | ||
| (7) -> [0, 1, 2, 3, 4, 5, 6] | ||
| console.log(linearArray(1)); //=> [0] | ||
| console.log(linearArray(3)); //=> [0,1,2] | ||
| console.log(linearArray(5)); //=> [0,1,2,3,4] | ||
| (7, true) -> [0, 1, 2, 3, 4, 5, 6, 7] | ||
| console.log(linearArray(1, true)); //=> [1] | ||
| console.log(linearArray(3, true)); //=> [1,2,3] | ||
| console.log(linearArray(5, true)); //=> [1,2,3,4,5] | ||
| ``` | ||
| ### `isSeqNaturalNumbers(arr: number[]) => boolean` | ||
| ```js | ||
| isSeqNaturalNumbers(arr) === true || false | ||
| ([]) -> false | ||
| ([0]) -> true | ||
| ([0,1,2,3,4]) -> true | ||
| ([2,3,4,5,6]) -> false | ||
| ([0,1,2,3,5]) -> false | ||
| ``` | ||
| ### **Sequence of numbers with a specified fixed step and offset** | ||
| ### `fillStepSequenceWithOffset (limiter: number, [step = 1]: number, [offset = 0]: number) => number[]` | ||
| ```js | ||
| fillStepSequenceWithOffset(limiter, step?, offset?) === [0+offset, step + offset, 2*step + offset, ..., (limiter - 1)*step + offset] | ||
| (0) -> [] | ||
| (0,1,1) -> [] | ||
| (1,1,1) -> [1] | ||
| (5) -> [0, 1, 2, 3, 4] | ||
| (5,1,3) -> [3, 4, 5, 6, 7] | ||
| (5,3) -> [0, 3, 6, 9, 12] | ||
| (5,2,2) -> [2, 4, 6, 8, 10] | ||
| ``` | ||
| ### `isStepSequenceWithOffset (arr: number[], [step = 1]: number, [offset = 0]: number) => boolean` | ||
| ```js | ||
| isStepSequenceWithOffset(arr, step?, offset?) === true || false | ||
| ([]) -> false | ||
| ([0]) -> true | ||
| ([0],1,1) -> false | ||
| ([0,1,2,3,4]) -> true | ||
| ([0,1,2,3,4], 2) -> false | ||
| ([0,1,2,3,4], 1, 1) -> false | ||
| ([1,2,3,4,5], 1, 1) -> true | ||
| ([6,9,12,15,18], 3, 6) -> true | ||
| ([5,9,12,15,19], 3, 6) -> false | ||
| ``` | ||
| ## Release history | ||
| ### **2.0.0.** | ||
| **BREAKING CHANGES:** | ||
| The _linearArray_ method no longer exists and it is not imported directly from the library. | ||
| Instead, a module called `lineArr` is imported and it contains a set of methods. See the **Usage** section on what they are and how they are used. | ||
| The closes equivalent to the old `linearArray` method is now `fillSeqNaturalNumbers` | ||
| **NEW FEATURES:** | ||
| This update has 4 new methods which can be accessed through the `lineArr` module. | ||
| - `lineArr.fillSeqNaturalNumbers` | ||
| - `lineArr.isSeqNaturalNumbers` | ||
| - `lineArr.fillStepSequenceWithOffset` | ||
| - `lineArr.isStepSequenceWithOffset` | ||
| ## About | ||
@@ -56,7 +182,29 @@ | ||
| ### License | ||
| ## Contributors ✨ | ||
| Many thanks goes to these people for helping me maintain and upgrade the project: | ||
| <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> | ||
| <!-- prettier-ignore-start --> | ||
| <!-- markdownlint-disable --> | ||
| <table> | ||
| <tr> | ||
| <td align="center"><a href="https://github.com/aminnairi"><img src="https://avatars3.githubusercontent.com/u/18418459?v=4" width="100px;" alt=""/><br /><sub><b>Amin NAIRI</b></sub></a><br /><a href="https://github.com/ispoljari/linear-array/commits?author=aminnairi" title="Code">💻</a> <a href="#ideas-aminnairi" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/ispoljari/linear-array/commits?author=aminnairi" title="Tests">⚠️</a></td> | ||
| </tr> | ||
| </table> | ||
| <!-- markdownlint-enable --> | ||
| <!-- prettier-ignore-end --> | ||
| <!-- ALL-CONTRIBUTORS-LIST:END --> | ||
| ## Author | ||
| **Ivan Spoljaric** | ||
| - [LinkedIn](https://www.linkedin.com/in/ivan-špoljarić-2206a184) | ||
| ## License | ||
| Copyright © 2020, [Ivan Spoljaric](https://github.com/ispoljari). | ||
| Released under the [MIT License](https://github.com/ispoljari/linear-array/blob/master/LICENSE.md). | ||
| --- |
-30
| /*! | ||
| * linear-array <https://github.com/ispoljari/linear-array> | ||
| * | ||
| * Copyright (c) 2020, Ivan Spoljaric. | ||
| * Released under the MIT License. | ||
| */ | ||
| 'use strict'; | ||
| const isNumber = require('is-number'); | ||
| module.exports = function linearArray(n, offset = false) { | ||
| if (!isNumber(n)) { | ||
| throw new TypeError('expected a number'); | ||
| } | ||
| if (!Number.isInteger(n)) { | ||
| throw new Error('expected an integer'); | ||
| } | ||
| if (!Number.isSafeInteger(n)) { | ||
| throw new Error('value above safe integer'); | ||
| } | ||
| if (n <= 0) { | ||
| throw new Error('expected a number greater than 0'); | ||
| } | ||
| return !offset ? [...Array(n).keys()] : Array.from(Array(n), (_, i) => i + 1); | ||
| }; |
-43
| 'use strict'; | ||
| require('mocha'); | ||
| const assert = require('assert'); | ||
| const linearArray = require('./'); | ||
| describe('linearArray', function () { | ||
| it('should throw an error if an invalid value is passed', function () { | ||
| assert.throws(() => linearArray(), /expected a number/); | ||
| assert.throws(() => linearArray('random string'), /expected a number/); | ||
| assert.throws(() => linearArray(true), /expected a number/); | ||
| assert.throws( | ||
| () => linearArray(999999999999999999999), | ||
| /value above safe integer/ | ||
| ); | ||
| assert.throws(() => linearArray(0), /expected a number greater than 0/); | ||
| assert.throws(() => linearArray(-3), /expected a number greater than 0/); | ||
| }); | ||
| it('should return correct results given a valid n without offset', function () { | ||
| assert.deepStrictEqual(linearArray(1), [0]); | ||
| assert.deepStrictEqual(linearArray(5), [0, 1, 2, 3, 4]); | ||
| assert.deepStrictEqual(linearArray(10), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
| }); | ||
| it('should return correct results given a valid n with offset', function () { | ||
| assert.deepStrictEqual(linearArray(1, true), [1]); | ||
| assert.deepStrictEqual(linearArray(5, true), [1, 2, 3, 4, 5]); | ||
| assert.deepStrictEqual(linearArray(10, true), [ | ||
| 1, | ||
| 2, | ||
| 3, | ||
| 4, | ||
| 5, | ||
| 6, | ||
| 7, | ||
| 8, | ||
| 9, | ||
| 10, | ||
| ]); | ||
| }); | ||
| }); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
27443
412.95%0
-100%14
180%644
955.74%209
242.62%4
300%1
Infinity%1
Infinity%- Removed
- Removed