@ts-common/iterator
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -22,2 +22,3 @@ import { Tuple2 } from "@ts-common/tuple"; | ||
export declare const take: <T>(input: Iterable<T> | undefined, n?: number) => Iterable<T>; | ||
export declare const findEntry: <T>(input: Iterable<T> | undefined, func: (v: T, i: number) => boolean) => Tuple2<number, T> | undefined; | ||
export declare const find: <T>(input: Iterable<T> | undefined, func: (v: T, i: number) => boolean) => T | undefined; | ||
@@ -24,0 +25,0 @@ export declare const flatMap: <T, I>(input: Iterable<I> | undefined, func: (v: I, i: number) => Iterable<T>) => Iterable<T>; |
@@ -6,2 +6,4 @@ "use strict"; | ||
exports.entry = tuple_1.tuple2; | ||
const ENTRY_KEY = 0; | ||
const ENTRY_VALUE = 1; | ||
exports.entries = (input) => exports.iterable(function* () { | ||
@@ -53,8 +55,8 @@ // tslint:disable-next-line:no-if-statement | ||
exports.take = (input, n = 1) => exports.takeWhile(input, (_, i) => i < n); | ||
exports.find = (input, func) => { | ||
exports.findEntry = (input, func) => { | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, value] of exports.entries(input)) { | ||
for (const e of exports.entries(input)) { | ||
// tslint:disable-next-line:no-if-statement | ||
if (func(value, index)) { | ||
return value; | ||
if (func(e[ENTRY_VALUE], e[ENTRY_KEY])) { | ||
return e; | ||
} | ||
@@ -64,2 +66,6 @@ } | ||
}; | ||
exports.find = (input, func) => { | ||
const e = exports.findEntry(input, func); | ||
return e === undefined ? undefined : e[ENTRY_VALUE]; | ||
}; | ||
exports.flatMap = (input, func) => exports.flatten(exports.map(input, func)); | ||
@@ -88,3 +94,3 @@ exports.optionalToArray = (v) => v === undefined ? [] : [v]; | ||
exports.last = (input) => exports.reduce(input, (_, v) => v); | ||
exports.some = (input, func) => exports.find(input, func) !== undefined; | ||
exports.some = (input, func) => exports.findEntry(input, func) !== undefined; | ||
exports.every = (input, func) => !exports.some(input, (v, i) => !func(v, i)); | ||
@@ -146,3 +152,3 @@ exports.forEach = (input, func) => | ||
exports.reverse = (i) => exports.fold(i, (a, b) => [b, ...a], new Array()); | ||
exports.isEmpty = (i) => exports.find(i, () => true) === undefined; | ||
exports.isEmpty = (i) => !exports.some(i, () => true); | ||
exports.join = (i, separator) => { | ||
@@ -149,0 +155,0 @@ const result = exports.reduce(i, (a, b) => a + separator + b); |
{ | ||
"name": "@ts-common/iterator", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Iterator library for JavaScript and TypeScript", | ||
@@ -26,3 +26,3 @@ "main": "dist/index.js", | ||
"prepack": "npm install && tsc", | ||
"test": "tsc && npm run tslint && nyc mocha ./dist/test/*.js --reporter mocha-junit-reporter" | ||
"test": "tsc && npm run tslint && nyc mocha ./dist/test/*.js --reporter mocha-multi-reporters --reporter-options configFile=mocha-reporters.json" | ||
}, | ||
@@ -55,2 +55,3 @@ "repository": { | ||
"mocha-junit-reporter": "^1.18.0", | ||
"mocha-multi-reporters": "^1.1.7", | ||
"nyc": "^13.1.0", | ||
@@ -57,0 +58,0 @@ "tslint": "^5.11.0", |
@@ -23,2 +23,5 @@ import { Tuple2, tuple2 } from "@ts-common/tuple" | ||
const ENTRY_KEY = 0 | ||
const ENTRY_VALUE = 1 | ||
export const entries = <T>(input: Iterable<T>|undefined): Iterable<Entry<T>> => | ||
@@ -90,11 +93,11 @@ iterable(function *(): Iterator<Entry<T>> { | ||
export const find = <T>( | ||
export const findEntry = <T>( | ||
input: Iterable<T>|undefined, | ||
func: (v: T, i: number) => boolean, | ||
): T|undefined => { | ||
): Entry<T>|undefined => { | ||
// tslint:disable-next-line:no-loop-statement | ||
for (const [index, value] of entries(input)) { | ||
for (const e of entries(input)) { | ||
// tslint:disable-next-line:no-if-statement | ||
if (func(value, index)) { | ||
return value | ||
if (func(e[ENTRY_VALUE], e[ENTRY_KEY])) { | ||
return e | ||
} | ||
@@ -105,2 +108,10 @@ } | ||
export const find = <T>( | ||
input: Iterable<T>|undefined, | ||
func: (v: T, i: number) => boolean, | ||
): T|undefined => { | ||
const e = findEntry(input, func) | ||
return e === undefined ? undefined : e[ENTRY_VALUE] | ||
} | ||
export const flatMap = <T, I>( | ||
@@ -170,3 +181,3 @@ input: Iterable<I>|undefined, | ||
): boolean => | ||
find(input, func) !== undefined | ||
findEntry(input, func) !== undefined | ||
@@ -258,3 +269,3 @@ export const every = <T>( | ||
export const isEmpty = <T>(i: Iterable<T>|undefined): boolean => | ||
find(i, () => true) === undefined | ||
!some(i, () => true) | ||
@@ -261,0 +272,0 @@ export const join = (i: Iterable<string>|undefined, separator: string): string => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41145
432
10