@breautek/iterator
Advanced tools
Comparing version 1.0.2 to 2.0.0
@@ -1,1 +0,1 @@ | ||
{"processes":{"03b377b3-863b-45b6-bc51-f108a378f30d":{"parent":null,"children":[]}},"files":{"/home/totalpave/development/breautek/iterator/src/Iterator.ts":["03b377b3-863b-45b6-bc51-f108a378f30d"]},"externalIds":{}} | ||
{"processes":{"27b6d405-423a-4ec1-aa73-15cef148deec":{"parent":null,"children":[]}},"files":{"/development/breautek/iterator/src/api.ts":["27b6d405-423a-4ec1-aa73-15cef148deec"],"/development/breautek/iterator/src/Iterator.ts":["27b6d405-423a-4ec1-aa73-15cef148deec"],"/development/breautek/iterator/src/DictionaryIterator.ts":["27b6d405-423a-4ec1-aa73-15cef148deec"]},"externalIds":{}} |
# Changelog | ||
## 2.0.0 | ||
### Breaking Changes | ||
- Removed ReverseIterator class | ||
### Other Updates | ||
- Updated eslint | ||
- Updated Typescript | ||
- Updated TSUtils | ||
## 1.0.2 | ||
@@ -4,0 +14,0 @@ - Added default export |
export declare class Iterator<T> { | ||
private collection; | ||
private cursor; | ||
private _collection; | ||
private _cursor; | ||
constructor(collection?: Array<T>, index?: number); | ||
@@ -16,3 +16,4 @@ hasNext(): boolean; | ||
decrementIndex(): number; | ||
private _getIndex; | ||
} | ||
export default Iterator; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class Iterator { | ||
constructor(collection = [], index = -1) { | ||
this.collection = collection; | ||
this.cursor = index; | ||
constructor(collection = [], index = 0) { | ||
this._collection = collection; | ||
this._cursor = index; | ||
} | ||
hasNext() { | ||
return !!this.collection[this.peekNextIndex()]; | ||
return !!this._collection[this._getIndex()]; | ||
} | ||
next() { | ||
return this.collection[this.incrementIndex()]; | ||
let data = this._collection[this._getIndex()]; | ||
this.incrementIndex(); | ||
return data; | ||
} | ||
hasPrevious() { | ||
return !!this.collection[this.peekPreviousIndex()]; | ||
return !!this._collection[this.peekPreviousIndex()]; | ||
} | ||
previous() { | ||
return this.collection[this.decrementIndex()]; | ||
this.decrementIndex(); | ||
let data = this._collection[this._cursor]; | ||
return data; | ||
} | ||
@@ -24,19 +28,24 @@ reset() { | ||
bringToStart() { | ||
this.cursor = -1; | ||
this._cursor = 0; | ||
} | ||
bringToEnd() { | ||
this.cursor = this.collection.length - 1; | ||
this._cursor = this._collection.length; | ||
} | ||
peekNextIndex() { | ||
return this.cursor + 1; | ||
return this._cursor; | ||
} | ||
peekPreviousIndex() { | ||
return this.cursor; | ||
return this._cursor - 1; | ||
} | ||
incrementIndex() { | ||
return ++this.cursor; | ||
this._cursor += 1; | ||
return this._cursor; | ||
} | ||
decrementIndex() { | ||
return this.cursor--; | ||
this._cursor -= 1; | ||
return this._cursor; | ||
} | ||
_getIndex() { | ||
return this._cursor; | ||
} | ||
} | ||
@@ -43,0 +52,0 @@ exports.Iterator = Iterator; |
{ | ||
"name": "@breautek/iterator", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "Iterator implementation", | ||
"main": "lib/Iterator.js", | ||
"types": "lib/Iterator.d.ts", | ||
"main": "lib/api.js", | ||
"types": "lib/api.d.ts", | ||
"nyc": { | ||
@@ -17,3 +17,3 @@ "check-coverage": true, | ||
"build": "npm run-script lint && tsc", | ||
"lint": "eslint --ext .ts './src/**/*.ts'", | ||
"lint": "eslint --ext .ts './src/**/*.ts' && eslint --ext .ts './spec/**/*.ts'", | ||
"unit": "jasmine-ts", | ||
@@ -40,15 +40,14 @@ "test": "npm run-script lint && nyc --reporter=lcov --reporter=text jasmine-ts", | ||
"dependencies": { | ||
"@types/node": "^12.6.8", | ||
"source-map-support": "^0.5.12", | ||
"@types/node": "^12.7.3", | ||
"source-map-support": "^0.5.13", | ||
"tslib": "^1.10.0" | ||
}, | ||
"devDependencies": { | ||
"@breautek/eslint-plugin": "^1.0.0", | ||
"@breautek/eslint-plugin": "^2.0.0", | ||
"@breautek/jasmine-ts": "^1.0.0", | ||
"@types/jasmine": "^3.3.13", | ||
"@typescript-eslint/eslint-plugin": "^1.12.0", | ||
"@typescript-eslint/parser": "^1.12.0", | ||
"@types/jasmine": "^3.4.0", | ||
"@typescript-eslint/eslint-plugin": "^2.1.0", | ||
"@typescript-eslint/parser": "^2.1.0", | ||
"codecov": "^3.5.0", | ||
"eslint": "^6.0.1", | ||
"eslint-config-prettier": "^6.0.0", | ||
"eslint": "^6.3.0", | ||
"jasmine": "^3.4.0", | ||
@@ -59,5 +58,5 @@ "jasmine-spec-reporter": "^4.2.1", | ||
"ts-node": "^8.3.0", | ||
"tsutils": "^3.14.0", | ||
"typescript": "^3.5.3" | ||
"tsutils": "^3.17.1", | ||
"typescript": "^3.6.2" | ||
} | ||
} |
@@ -1,25 +0,2 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2019 Norman Breau | ||
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. | ||
*/ | ||
import {Iterator} from '../src/Iterator'; | ||
@@ -31,89 +8,124 @@ | ||
beforeEach(() => { | ||
iterator = new Iterator<number>([1,2,3]); | ||
const data: Array<number> = [ | ||
1, | ||
2, | ||
3 | ||
]; | ||
iterator = new Iterator<number>(data); | ||
}); | ||
it('Constructs with custom index position', () => { | ||
var iter: Iterator<number> = new Iterator<number>([1,2,3], 0); | ||
expect(iter.hasPrevious()).toBe(true); | ||
expect(iter.next()).toBe(2); | ||
expect(iter.next()).toBe(3); | ||
describe('Constructs with custom index position', () => { | ||
it('Iterator', () => { | ||
var iter: Iterator<number> = new Iterator<number>([ | ||
1, | ||
2, | ||
3 | ||
], 0); | ||
expect(iter.hasPrevious()).toBe(false); | ||
expect(iter.next()).toBe(1); | ||
expect(iter.next()).toBe(2); | ||
expect(iter.next()).toBe(3); | ||
}); | ||
}); | ||
it('hasNext()', () => { | ||
expect(iterator.hasNext()).toBe(true); | ||
iterator.next(); | ||
iterator.next(); | ||
iterator.next(); | ||
expect(iterator.hasNext()).toBe(false); | ||
describe('hasNext()', () => { | ||
it('Iterator', () => { | ||
expect(iterator.hasNext()).toBe(true); | ||
iterator.next(); | ||
iterator.next(); | ||
iterator.next(); | ||
expect(iterator.hasNext()).toBe(false); | ||
}); | ||
}); | ||
it('next()', () => { | ||
expect(iterator.next()).toBe(1); | ||
expect(iterator.next()).toBe(2); | ||
expect(iterator.next()).toBe(3); | ||
describe('next()', () => { | ||
it('Iterator', () => { | ||
expect(iterator.next()).toBe(1); | ||
expect(iterator.next()).toBe(2); | ||
expect(iterator.next()).toBe(3); | ||
}); | ||
}); | ||
it('hasPrevious()', () => { | ||
expect(iterator.hasPrevious()).toBe(false); | ||
iterator.next(); | ||
expect(iterator.hasPrevious()).toBe(true); | ||
describe('hasPrevious()', () => { | ||
it('Iterator', () => { | ||
expect(iterator.hasPrevious()).toBe(false); | ||
iterator.next(); | ||
expect(iterator.hasPrevious()).toBe(true); | ||
}); | ||
}); | ||
it('previous()', () => { | ||
iterator.next(); | ||
iterator.next(); | ||
iterator.next(); | ||
expect(iterator.previous()).toBe(3); | ||
expect(iterator.previous()).toBe(2); | ||
expect(iterator.previous()).toBe(1); | ||
describe('previous()', () => { | ||
it('Iterator', () => { | ||
iterator.next(); | ||
iterator.next(); | ||
iterator.next(); | ||
expect(iterator.previous()).toBe(3); | ||
expect(iterator.previous()).toBe(2); | ||
expect(iterator.previous()).toBe(1); | ||
}); | ||
}); | ||
it('reset()', () => { | ||
iterator.next(); | ||
iterator.next(); | ||
iterator.reset(); | ||
expect(iterator.next()).toBe(1); | ||
describe('reset()', () => { | ||
it('Iterator', () => { | ||
iterator.next(); | ||
iterator.next(); | ||
iterator.reset(); | ||
expect(iterator.next()).toBe(1); | ||
}); | ||
}); | ||
it('bringToStart()', () => { | ||
iterator.next(); | ||
iterator.next(); | ||
iterator.reset(); | ||
expect(iterator.next()).toBe(1); | ||
describe('bringToStart()', () => { | ||
it('Iterator', () => { | ||
iterator.next(); | ||
iterator.next(); | ||
iterator.reset(); | ||
expect(iterator.next()).toBe(1); | ||
}); | ||
}); | ||
it('bringToEnd()', () => { | ||
iterator.bringToEnd(); | ||
expect(iterator.previous()).toBe(3); | ||
describe('bringToEnd()', () => { | ||
it('Iterator', () => { | ||
iterator.bringToEnd(); | ||
expect(iterator.previous()).toBe(3); | ||
}); | ||
}); | ||
it('peekNextIndex()', () => { | ||
expect(iterator.peekNextIndex()).toBe(0); | ||
expect(iterator.next()).toBe(1); | ||
expect(iterator.peekNextIndex()).toBe(1); | ||
expect(iterator.next()).toBe(2); | ||
expect(iterator.peekNextIndex()).toBe(2); | ||
expect(iterator.next()).toBe(3); | ||
describe('peekNextIndex()', () => { | ||
it('Iterator', () => { | ||
expect(iterator.peekNextIndex()).toBe(0); | ||
expect(iterator.next()).toBe(1); | ||
expect(iterator.peekNextIndex()).toBe(1); | ||
expect(iterator.next()).toBe(2); | ||
expect(iterator.peekNextIndex()).toBe(2); | ||
expect(iterator.next()).toBe(3); | ||
}); | ||
}); | ||
it('peekNextIndex()', () => { | ||
iterator.bringToEnd(); | ||
expect(iterator.peekPreviousIndex()).toBe(2); | ||
expect(iterator.previous()).toBe(3); | ||
expect(iterator.peekPreviousIndex()).toBe(1); | ||
expect(iterator.previous()).toBe(2); | ||
expect(iterator.peekPreviousIndex()).toBe(0); | ||
expect(iterator.previous()).toBe(1); | ||
describe('peekNextIndex()', () => { | ||
it('Iterator', () => { | ||
iterator.bringToEnd(); | ||
expect(iterator.peekPreviousIndex()).toBe(2); | ||
expect(iterator.previous()).toBe(3); | ||
expect(iterator.peekPreviousIndex()).toBe(1); | ||
expect(iterator.previous()).toBe(2); | ||
expect(iterator.peekPreviousIndex()).toBe(0); | ||
expect(iterator.previous()).toBe(1); | ||
}); | ||
}); | ||
it('incrementIndex()', () => { | ||
iterator.incrementIndex(); | ||
expect(iterator.next()).toBe(2); | ||
describe('incrementIndex()', () => { | ||
it('Iterator', () => { | ||
iterator.incrementIndex(); | ||
expect(iterator.next()).toBe(2); | ||
}); | ||
}); | ||
it('decrementIndex()', () => { | ||
iterator.bringToEnd(); | ||
iterator.decrementIndex(); | ||
expect(iterator.next()).toBe(3); | ||
describe('decrementIndex()', () => { | ||
it('Iterator', () => { | ||
iterator.bringToEnd(); | ||
iterator.decrementIndex(); | ||
expect(iterator.next()).toBe(3); | ||
}); | ||
}); | ||
}); |
@@ -1,52 +0,29 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2019 Norman Breau | ||
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. | ||
*/ | ||
export class Iterator<T> { | ||
private collection: Array<T>; | ||
private cursor: number; | ||
private _collection: Array<T>; | ||
private _cursor: number; | ||
constructor(collection: Array<T> = [], index: number = -1) { | ||
this.collection = collection; | ||
this.cursor = index; | ||
constructor(collection: Array<T> = [], index: number = 0) { | ||
this._collection = collection; | ||
this._cursor = index; | ||
} | ||
public hasNext(): boolean { | ||
// return this.cursor + 1 < this.collection.length; | ||
return !!this.collection[this.peekNextIndex()]; | ||
return !!this._collection[this._getIndex()]; | ||
} | ||
public next(): T { | ||
// return this.collection[++this.cursor]; | ||
return this.collection[this.incrementIndex()]; | ||
let data: T = this._collection[this._getIndex()]; | ||
this.incrementIndex(); | ||
return data; | ||
} | ||
public hasPrevious(): boolean { | ||
// return this.cursor > 0; | ||
return !!this.collection[this.peekPreviousIndex()]; | ||
return !!this._collection[this.peekPreviousIndex()]; | ||
} | ||
public previous(): T { | ||
// return this.collection[this.cursor--]; | ||
return this.collection[this.decrementIndex()]; | ||
this.decrementIndex(); | ||
let data: T = this._collection[this._cursor]; | ||
return data; | ||
} | ||
@@ -59,26 +36,32 @@ | ||
public bringToStart(): void { | ||
this.cursor = -1; | ||
this._cursor = 0; | ||
} | ||
public bringToEnd(): void { | ||
this.cursor = this.collection.length - 1; | ||
this._cursor = this._collection.length; | ||
} | ||
public peekNextIndex(): number { | ||
return this.cursor + 1; | ||
return this._cursor; | ||
} | ||
public peekPreviousIndex(): number { | ||
return this.cursor; | ||
return this._cursor - 1; | ||
} | ||
public incrementIndex(): number { | ||
return ++this.cursor; | ||
this._cursor += 1; | ||
return this._cursor; | ||
} | ||
public decrementIndex(): number { | ||
return this.cursor--; | ||
this._cursor -= 1; | ||
return this._cursor; | ||
} | ||
private _getIndex(): number { | ||
return this._cursor; | ||
} | ||
} | ||
export default Iterator; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
88103
14
38
926
Updated@types/node@^12.7.3
Updatedsource-map-support@^0.5.13