section-iterator-2.0.0/0000775000175000017500000000000015261220150014016 5ustar emfoxemfoxsection-iterator-2.0.0/package.json0000664000175000017500000000205612705667505016332 0ustar emfoxemfox{ "name": "section-iterator", "version": "2.0.0", "description": "Simple iterator for flat and multi section lists", "main": "dist/index.js", "repository": { "type": "git", "url": "https://github.com/moroshko/section-iterator.git" }, "author": "Misha Moroshko ", "scripts": { "lint": "eslint index.js index.test.js", "test": "mocha index.test.js --compilers js:babel-register", "dist": "rm -rf dist && mkdir dist && babel index.js --out-file dist/index.js", "prebuild": "npm run lint && npm test", "build": "npm run dist", "postversion": "git push && git push --tags", "prepublish": "npm run dist" }, "devDependencies": { "babel-cli": "^6.7.5", "babel-core": "^6.7.6", "babel-eslint": "^6.0.3", "babel-preset-es2015": "^6.6.0", "babel-preset-stage-0": "^6.5.0", "babel-register": "^6.7.2", "chai": "^3.5.0", "eslint": "^2.8.0", "mocha": "^2.4.5" }, "files": [ "dist" ], "keywords": [ "iterator", "array" ], "license": "MIT" } section-iterator-2.0.0/index.test.js0000664000175000017500000001460312705667505016470 0ustar emfoxemfoximport { expect } from 'chai'; import createSectionIterator from './index'; let sectionIterator, multiSection; describe('sectionIterator', () => { describe('with no sections', () => { before(() => { multiSection = false; }); describe('and no items', () => { before(() => { sectionIterator = createSectionIterator({ data: 0, multiSection }); }); it('should calculate next', () => { expect(sectionIterator.next([null, null])).to.deep.equal([null, null]); }); it('should calculate prev', () => { expect(sectionIterator.prev([null, null])).to.deep.equal([null, null]); }); it('should calculate isLast', () => { expect(sectionIterator.isLast([null, null])).to.be.true; }); }); describe('and 1 item', () => { before(() => { sectionIterator = createSectionIterator({ data: 1, multiSection }); }); it('should calculate next', () => { expect(sectionIterator.next([null, null])).to.deep.equal([null, 0]); expect(sectionIterator.next([null, 0])).to.deep.equal([null, null]); }); it('should calculate prev', () => { expect(sectionIterator.prev([null, null])).to.deep.equal([null, 0]); expect(sectionIterator.prev([null, 0])).to.deep.equal([null, null]); }); it('should calculate isLast', () => { expect(sectionIterator.isLast([null, null])).to.be.false; expect(sectionIterator.isLast([null, 0])).to.be.true; }); }); describe('and multiple items', () => { before(() => { sectionIterator = createSectionIterator({ data: 4, multiSection }); }); it('should calculate next', () => { expect(sectionIterator.next([null, null])).to.deep.equal([null, 0]); expect(sectionIterator.next([null, 0])).to.deep.equal([null, 1]); expect(sectionIterator.next([null, 3])).to.deep.equal([null, null]); }); it('should calculate prev', () => { expect(sectionIterator.prev([null, null])).to.deep.equal([null, 3]); expect(sectionIterator.prev([null, 3])).to.deep.equal([null, 2]); expect(sectionIterator.prev([null, 0])).to.deep.equal([null, null]); }); it('should calculate isLast', () => { expect(sectionIterator.isLast([null, null])).to.be.false; expect(sectionIterator.isLast([null, 0])).to.be.false; expect(sectionIterator.isLast([null, 3])).to.be.true; }); }); }); describe('with 1 section', () => { before(() => { multiSection = true; }); describe('and no items', () => { before(() => { sectionIterator = createSectionIterator({ data: [0], multiSection }); }); it('should calculate next', () => { expect(sectionIterator.next([null, null])).to.deep.equal([null, null]); }); it('should calculate prev', () => { expect(sectionIterator.prev([null, null])).to.deep.equal([null, null]); }); it('should calculate isLast', () => { expect(sectionIterator.isLast([null, null])).to.be.true; }); }); describe('and 1 item', () => { before(() => { sectionIterator = createSectionIterator({ data: [1], multiSection }); }); it('should calculate next', () => { expect(sectionIterator.next([null, null])).to.deep.equal([0, 0]); expect(sectionIterator.next([0, 0])).to.deep.equal([null, null]); }); it('should calculate prev', () => { expect(sectionIterator.prev([null, null])).to.deep.equal([0, 0]); expect(sectionIterator.prev([0, 0])).to.deep.equal([null, null]); }); it('should calculate isLast', () => { expect(sectionIterator.isLast([null, null])).to.be.false; expect(sectionIterator.isLast([0, 0])).to.be.true; }); }); describe('and multiple items', () => { before(() => { sectionIterator = createSectionIterator({ data: [4], multiSection }); }); it('should calculate next', () => { expect(sectionIterator.next([null, null])).to.deep.equal([0, 0]); expect(sectionIterator.next([0, 0])).to.deep.equal([0, 1]); expect(sectionIterator.next([0, 3])).to.deep.equal([null, null]); }); it('should calculate prev', () => { expect(sectionIterator.prev([null, null])).to.deep.equal([0, 3]); expect(sectionIterator.prev([0, 3])).to.deep.equal([0, 2]); expect(sectionIterator.prev([0, 0])).to.deep.equal([null, null]); }); it('should calculate isLast', () => { expect(sectionIterator.isLast([null, null])).to.be.false; expect(sectionIterator.isLast([0, 0])).to.be.false; expect(sectionIterator.isLast([0, 3])).to.be.true; }); }); }); describe('with multiple sections', () => { before(() => { multiSection = true; sectionIterator = createSectionIterator({ data: [2, 0, 0, 4, 1, 0, 3, 0], multiSection }); }); it('should calculate next', () => { expect(sectionIterator.next([null, null])).to.deep.equal([0, 0]); expect(sectionIterator.next([0, 0])).to.deep.equal([0, 1]); expect(sectionIterator.next([0, 1])).to.deep.equal([3, 0]); expect(sectionIterator.next([3, 0])).to.deep.equal([3, 1]); expect(sectionIterator.next([3, 3])).to.deep.equal([4, 0]); expect(sectionIterator.next([4, 0])).to.deep.equal([6, 0]); expect(sectionIterator.next([6, 2])).to.deep.equal([null, null]); }); it('should calculate prev', () => { expect(sectionIterator.prev([null, null])).to.deep.equal([6, 2]); expect(sectionIterator.prev([6, 2])).to.deep.equal([6, 1]); expect(sectionIterator.prev([6, 0])).to.deep.equal([4, 0]); expect(sectionIterator.prev([4, 0])).to.deep.equal([3, 3]); expect(sectionIterator.prev([3, 3])).to.deep.equal([3, 2]); expect(sectionIterator.prev([3, 0])).to.deep.equal([0, 1]); expect(sectionIterator.prev([0, 0])).to.deep.equal([null, null]); }); it('should calculate isLast', () => { expect(sectionIterator.isLast([null, null])).to.be.false; expect(sectionIterator.isLast([0, 0])).to.be.false; expect(sectionIterator.isLast([0, 1])).to.be.false; expect(sectionIterator.isLast([3, 0])).to.be.false; expect(sectionIterator.isLast([3, 3])).to.be.false; expect(sectionIterator.isLast([4, 0])).to.be.false; expect(sectionIterator.isLast([6, 2])).to.be.true; }); }); }); section-iterator-2.0.0/index.js0000664000175000017500000000367512705667505015521 0ustar emfoxemfoxmodule.exports = ({ data, multiSection }) => { function nextNonEmptySectionIndex(sectionIndex) { if (sectionIndex === null) { sectionIndex = 0; } else { sectionIndex++; } while (sectionIndex < data.length && data[sectionIndex] === 0) { sectionIndex++; } return sectionIndex === data.length ? null : sectionIndex; } function prevNonEmptySectionIndex(sectionIndex) { if (sectionIndex === null) { sectionIndex = data.length - 1; } else { sectionIndex--; } while (sectionIndex >= 0 && data[sectionIndex] === 0) { sectionIndex--; } return sectionIndex === -1 ? null : sectionIndex; } function next(position) { let [sectionIndex, itemIndex] = position; if (multiSection) { if (itemIndex === null || itemIndex === data[sectionIndex] - 1) { sectionIndex = nextNonEmptySectionIndex(sectionIndex); if (sectionIndex === null) { return [null, null]; } return [sectionIndex, 0]; } return [sectionIndex, itemIndex + 1]; } if (data === 0 || itemIndex === data - 1) { return [null, null]; } if (itemIndex === null) { return [null, 0]; } return [null, itemIndex + 1]; } function prev(position) { let [sectionIndex, itemIndex] = position; if (multiSection) { if (itemIndex === null || itemIndex === 0) { sectionIndex = prevNonEmptySectionIndex(sectionIndex); if (sectionIndex === null) { return [null, null]; } return [sectionIndex, data[sectionIndex] - 1]; } return [sectionIndex, itemIndex - 1]; } if (data === 0 || itemIndex === 0) { return [null, null]; } if (itemIndex === null) { return [null, data - 1]; } return [null, itemIndex - 1]; } function isLast(position) { return next(position)[1] === null; } return { next, prev, isLast }; }; section-iterator-2.0.0/README.md0000664000175000017500000000035412705667505015322 0ustar emfoxemfox[![Build Status][status-image]][status-url] # Section Iterator **TODO:** * Write docs [status-image]: https://img.shields.io/codeship/f9a1f240-261a-0133-4f87-426705708e6a/master.svg [status-url]: https://codeship.com/projects/97026 section-iterator-2.0.0/LICENSE0000664000175000017500000000210012705667505015037 0ustar emfoxemfoxThe MIT License (MIT) Copyright © 2016 Misha Moroshko 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. section-iterator-2.0.0/.gitignore0000664000175000017500000000004012705667505016023 0ustar emfoxemfoxnode_modules dist npm-debug.log section-iterator-2.0.0/.eslintrc0000664000175000017500000000211112705667505015660 0ustar emfoxemfox{ "env": { "es6": true, "node": true, "browser": true }, "parser": "babel-eslint", "rules": { "array-callback-return": 2, "brace-style": [2, "1tbs"], "camelcase": [2, { "properties": "always" }], "comma-dangle": [2, "never"], "comma-style": [2, "last"], "eol-last": 2, "indent": [2, 2, { "SwitchCase": 1 }], "key-spacing": [2, { "beforeColon": false, "afterColon": true }], "keyword-spacing": 2, "linebreak-style": [2, "unix"], "no-cond-assign": [2, "always"], "no-console": 2, "no-multiple-empty-lines": [2, { "max": 1 }], "no-spaced-func": 2, "no-trailing-spaces": 2, "no-unused-vars": 2, "no-whitespace-before-property": 2, "newline-after-var": [2, "always"], "object-curly-spacing": [2, "always"], "prefer-rest-params": 2, "quote-props": [2, "as-needed"], "quotes": [2, "single"], "semi": [2, "always"], "space-before-blocks": [2, "always"], "space-before-function-paren": [2, "never"], "space-in-parens": [2, "never"], "template-curly-spacing": [2, "never"] } } section-iterator-2.0.0/.editorconfig0000664000175000017500000000024612705667505016520 0ustar emfoxemfox# editorconfig.org root = true [*] indent_style = space indent_size = 2 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true section-iterator-2.0.0/.babelrc0000664000175000017500000000004712705667505015435 0ustar emfoxemfox{ "presets": ["es2015", "stage-0"] }