Skip to content

Commit

Permalink
0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 12, 2014
0 parents commit 228e832
Show file tree
Hide file tree
Showing 13 changed files with 578 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"env": {
"node": true,
"browser": true
},
"rules": {
"quotes": [2, "single"]
}
}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
*.log
bower_components/
build/
components/
node_modules/
coverage/
135 changes: 135 additions & 0 deletions .jscs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"requireCurlyBraces": [
"if",
"else",
"for",
"while",
"do",
"try",
"catch"
],
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"return",
"try",
"catch"
],
"requireSpaceBeforeBlockStatements": true,
"requireParenthesesAroundIIFE": true,
"requireSpacesInConditionalExpression": true,
"requireSpacesInFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireSpacesInAnonymousFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
},
"requireSpacesInNamedFunctionExpression": {
"beforeOpeningRoundBrace": true,
"beforeOpeningCurlyBrace": true
},
"requireSpacesInFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireMultipleVarDecl": true,
"requireBlocksOnNewline": true,
"disallowPaddingNewlinesInBlocks": true,
"disallowEmptyBlocks": true,
"disallowSpacesInsideObjectBrackets": true,
"disallowSpacesInsideArrayBrackets": true,
"disallowSpacesInsideParentheses": true,
"requireSpacesInsideObjectBrackets": "all",
"disallowDanglingUnderscores": true,
"disallowSpaceAfterObjectKeys": true,
"requireCommaBeforeLineBreak": true,
"requireOperatorBeforeLineBreak": [
"?",
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
],
"requireSpaceBeforeBinaryOperators": [
"=",
",",
"+",
"-",
"/",
"*",
"==",
"===",
"!=",
"!=="
],
"requireSpaceAfterBinaryOperators": [
"=",
",",
"+",
"-",
"/",
"*",
"==",
"===",
"!=",
"!=="
],
"disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"],
"disallowSpaceBeforePostfixUnaryOperators": ["++", "--"],
"requireSpaceBeforeBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
],
"requireSpaceAfterBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
],
"disallowImplicitTypeConversion": ["numeric", "boolean", "binary", "string"],
"requireCamelCaseOrUpperCaseIdentifiers": true,
"disallowKeywords": ["with"],
"disallowMultipleLineStrings": true,
"disallowMultipleLineBreaks": true,
"validateLineBreaks": "LF",
"validateQuoteMarks": "'",
"disallowMixedSpacesAndTabs": true,
"disallowTrailingWhitespace": true,
"disallowTrailingComma": true,
"disallowKeywordsOnNewLine": ["else"],
"requireLineFeedAtFileEnd": true,
"maximumLineLength": 78,
"requireCapitalizedConstructors": true,
"requireDotNotation": true,
"disallowYodaConditions": true,
"validateJSDoc": {
"checkParamNames": true,
"checkRedundantParams": true,
"requireParamTypes": true
}
}
11 changes: 11 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.*
*.log
build/
bower_components/
components/
coverage/
node_modules/
test.js
bower.json
component.json
History.md
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: node_js
script: npm run-script test-travis
node_js:
- '0.10'
- '0.11'
after_script: npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls
deploy:
provider: npm
email: tituswormer@gmail.com
api_key:
secure: EpsOgmqYn+7QvS0BPJfb24oRuXZnxFaXkpW2R1G0yNe06lI5Gjx/r/oSbRoTzWgzo2MGIeGArF4Gbi1uAZSNEsUCAzikounQzLjlzkKq6Emy/xac5Q7qCiaY2BgvIwHsd3bdp1XPOMnUK3iQRfoRyvHhOOrTOZAZK1I+dTkUxL0=
on:
repo: wooorm/array-iterate
Empty file added History.md
Empty file.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2014 Titus Wormer <tituswormer@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
58 changes: 58 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# array-iterate [![Build Status](https://img.shields.io/travis/wooorm/array-iterate.svg?style=flat)](https://travis-ci.org/wooorm/array-iterate) [![Coverage Status](https://img.shields.io/coveralls/wooorm/array-iterate.svg?style=flat)](https://coveralls.io/r/wooorm/array-iterate?branch=master)

[`Array#forEach()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) with the possibility to change the next position.

## Installation

npm:
```sh
$ npm install array-iterate
```

Component.js:
```sh
$ component install wooorm/array-iterate
```

Bower:
```sh
$ bower install array-iterate
```

## Usage

````js
var iterate = require('array-iterate');
var isFirst = true;
var context = 'iterate';

iterate([1, 2, 3, 4], function (value, index, values) {
console.log(this, value, index, values)

if (isFirst && index + 1 === values.length) {
isFirst = false;

return 0;
}
}, context);
/**
* [String: 'iterate'], 1, 0, [ 1, 2, 3, 4 ]
* [String: 'iterate'], 2, 1, [ 1, 2, 3, 4 ]
* [String: 'iterate'], 3, 2, [ 1, 2, 3, 4 ]
* [String: 'iterate'], 4, 3, [ 1, 2, 3, 4 ]
* [String: 'iterate'], 1, 0, [ 1, 2, 3, 4 ]
* [String: 'iterate'], 2, 1, [ 1, 2, 3, 4 ]
* [String: 'iterate'], 3, 2, [ 1, 2, 3, 4 ]
* [String: 'iterate'], 4, 3, [ 1, 2, 3, 4 ]
*/
````

## API

### iterate(arrayLike, callback, context)

Functions just like [`Array#forEach()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach), but when `callback` returns a `number`, iterates over the item at `number` next.

## License

MIT © Titus Wormer
31 changes: 31 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "array-iterate",
"main": "index.js",
"description": "forEach with the possibility to change the next position",
"license": "MIT",
"keywords": [
"array",
"list",
"iterate",
"walk"
],
"repository": {
"type": "git",
"url": "https://github.com/wooorm/array-iterate.git"
},
"authors": [
"Titus Wormer <tituswormer@gmail.com>"
],
"ignore": [
".*",
"*.log",
"*.md",
"build/",
"components/",
"coverage/",
"node_modules/",
"component.json",
"package.json",
"test.js"
]
}
16 changes: 16 additions & 0 deletions component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "array-iterate",
"version": "0.0.1",
"description": "forEach with the possibility to change the next position",
"license": "MIT",
"keywords": [
"array",
"list",
"iterate",
"walk"
],
"repository": "wooorm/array-iterate",
"scripts" : [
"index.js"
]
}
85 changes: 85 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict';

/**
* Cache `hasOwnProperty`.
*/

var has;

has = Object.prototype.hasOwnProperty;

/**
* `Array#forEach()` with the possibility to change
* the next position.
*
* @param {{length: number}} values
* @param {function(*, number, {length: number}): number|undefined} callback
* @param {*} context
*/

function iterate(values, callback, context) {
var index,
result;

if (!values) {
throw new Error(
'TypeError: Iterate requires that |this| ' +
'not be ' + values
);
}

if (!has.call(values, 'length')) {
throw new Error(
'TypeError: Iterate requires that |this| ' +
'has a `length`'
);
}

if (typeof callback !== 'function') {
throw new Error(
'TypeError: callback must be a function'
);
}

index = -1;

/**
* The length might change, so we do not cache it.
*/

while (++index < values.length) {
/**
* Skip missing values.
*/

if (!(index in values)) {
continue;
}

result = callback.call(context, values[index], index, values);

/**
* If `callback` returns a `number`, move `index` over to
* `number`.
*/

if (typeof result === 'number') {
/**
* Make sure that negative numbers do not
* break the loop.
*/

if (result < 0) {
index = 0;
}

index = result - 1;
}
}
}

/**
* Expose `iterate`.
*/

module.exports = iterate;
Loading

0 comments on commit 228e832

Please sign in to comment.