GithubHelp home page GithubHelp logo

ispoljari / linear-array Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 1.0 48 KB

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])

Home Page: https://github.com/ispoljari/linear-array

License: MIT License

JavaScript 100.00%
array number integer increment natural sequence linear step increase from

linear-array's Introduction

linear-array

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

There are 2 ways to install and use the library.

  1. npm
$ npm install --save linear-array
  1. import with a <script> tag
<script type="module" src="https://unpkg.com/[email protected]"></script>

Importing and using 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
const lineArr = require('linear-array');

// ...

linear.isSeqNaturalNumbers([1, 2, 3]); // false
  • ES6
import {
  isSeqNaturalNumbers,
  fillSeqNaturalNumbers,
  fillStepSequenceWithOffset,
  isStepSequenceWithOffset,
} from 'linear-array';

// ...

isSeqNaturalNumbers([0, 1, 2, 3]); // true

// OR

import * as lineArr from 'linear-array';

// ...

lineArr.isSeqNaturalNumbers([0, 1, 2, 3]); // true
  • UMD

If you imported the linear-array library with the <script> tag it will be attached to the globally available window object.

That's why you can use the module directly in your code by referencing the lineArr module directly

<!-- index.html -->
<html>
  <head>
    <!--  -->
  </head>
  <body>
    <script src="https://unpkg.com/[email protected]"></script>
    <script src="custom.js"></script>
  </body>
</html>
// custom.js

lineArr.isStepSequenceWithOffset([1, 2, 3]); // false

Usage

Currently, there are 4 methods available in the package.

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.

But, repetition of numbers in the arrays is not allowed.

Sequence of natural numbers

fillSeqNaturalNumbers (limiter: number, [includeLast = false]: boolean) => number[]

fillSeqNaturalNumbers(limiter, inludeLast?) === [0, 1, 2, 3, ... , includeLast ? limiter : (limiter - 1)]

(0) -> []

(7) -> [0, 1, 2, 3, 4, 5, 6]

(7, true) -> [0, 1, 2, 3, 4, 5, 6, 7]

isSeqNaturalNumbers(arr: number[]) => boolean

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[]

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

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.

The closes equivalent to the old linearArray method is now fillSeqNaturalNumbers

NEW FEATURES:

This update has 4 new methods:

  • fillSeqNaturalNumbers

  • isSeqNaturalNumbers

  • fillStepSequenceWithOffset

  • isStepSequenceWithOffset

See the Usage section on what they are and how they are used.

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Contributors ✨

Many thanks goes to these people for helping me maintain and upgrade the project:


Amin NAIRI

💻 🤔 ⚠️

Author

Ivan Spoljaric

License

Copyright © 2020, Ivan Spoljaric. Released under the MIT License.

linear-array's People

Contributors

allcontributors[bot] avatar aminnairi avatar ispoljari avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

xpressabhi

linear-array's Issues

Why not zero length

Thanks for creating this package.
Consider allowong the array size to be 0.
Cheers

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.