Coder Social home page Coder Social logo

writetome51 / error-if-index-not-valid-after-offset-was-added Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 4 KB

Function triggers error if passed index is not valid, assuming an offset was recently added to it

License: MIT License

JavaScript 100.00%
javascript array index validation checking error error-message

error-if-index-not-valid-after-offset-was-added's Introduction

errorIfIndexNotValidAfterOffsetWasAdded(
      index,
      arrayLength
): void

Triggers error if index is not valid, assuming an offset was recently added to it.
For example, if you're writing a getIndex() function that gives you the option of adding
an offset to modify the index it returns, errorIfIndexNotValidAfterOffsetWasAdded()
might come in handy. For more info, read the example below.

Example

function getIndexSlightlyOff(value, array, offset){
    let index = array.indexOf(value);
    index += offset;
    // Now check if index still valid:
    errorIfIndexNotValidAfterOffsetWasAdded(index, array.length);
    return index;
}

let arr = [1,2,3,4,5];
let index = getIndexSlightlyOff(5, arr, 1);
// Error: "After the offset was added to the index, the resulting 
// index was too high for this array"

let arr = [1,2,3,4,5];
let index = getIndexSlightlyOff(2, arr, -2);
// Error: "After the offset was added to the index, the resulting 
// index was a negative number. This is not allowed."

Installation

npm install error-if-index-not-valid-after-offset-was-added

Loading

// if using TypeScript:
import { errorIfIndexNotValidAfterOffsetWasAdded } 
    from 'error-if-index-not-valid-after-offset-was-added';
// if using ES5 JavaScript:
var errorIfIndexNotValidAfterOffsetWasAdded = 
    require('error-if-index-not-valid-after-offset-was-added')
    .errorIfIndexNotValidAfterOffsetWasAdded;

License

MIT

error-if-index-not-valid-after-offset-was-added's People

Contributors

writetome51 avatar

Watchers

 avatar  avatar

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.