diff --git a/js/object/api.html b/js/object/api.html index 197939e0c..b00142899 100644 --- a/js/object/api.html +++ b/js/object/api.html @@ -137,48 +137,6 @@

Exports

- diff --git a/js/object/module.fifty.ts b/js/object/module.fifty.ts index a743d309c..675b4d879 100644 --- a/js/object/module.fifty.ts +++ b/js/object/module.fifty.ts @@ -652,7 +652,46 @@ namespace slime.$api.old { )(fifty); export interface Exports { - Array: any + Array: { + (ts: Array): Array & { each: any } + new (ts: Array): Array + + /** + * Converts an array into a single element. First applies the given Filter, if one is given: the Filter should be a + * filter that only accepts zero or one element of the array. + + * @param array An array. + * @param filter An optional Filter that is used to preprocess the array. + * @returns The single element of the array (after applying the given Filter, if applicable), or `null` if the array has + * zero elements. An error is thrown if more than one element matches. + */ + choose: (array: Array, filter?: (t: T) => boolean) => T + + toValue: any + categorize: any + } + } + + ( + function( + fifty: slime.fifty.test.Kit + ) { + const module = old.test.subject; + + const test = function(b) { + fifty.verify(b).is(true); + }; + + fifty.tests.exports.Array = function() { + test( module.Array.choose([ 3 ]) == 3 ); + test( module.Array.choose([]) === null ); + test( module.Array.choose([ 1, 2, 3 ], function(a) { return a == 3; } ) == 3 ); + } + } + //@ts-ignore + )(fifty); + + export interface Exports { Error: any Task: any diff --git a/js/object/module.js b/js/object/module.js index d582a64c3..79e525e71 100644 --- a/js/object/module.js +++ b/js/object/module.js @@ -612,19 +612,39 @@ }; }; - $exports.Array = function(array) { - if (this.constructor == arguments.callee.prototype) { - // called with new - array = array.slice(); - } + //@ts-ignore Confused because of function called both as constructor and function; + $exports.Array = Object.assign( + /** + * @param { Array & { one: any, each: any, fold: any, select: any } } array + * @returns { Array & { one: any, each: any, fold: any, select: any } } + */ + function(array) { + if (this.constructor == arguments.callee.prototype) { + // called with new + array = Object.assign( + array.slice(), + { + one: void(0), + each: void(0), + fold: void(0), + select: void(0) + } + ); + } - array.one = ArrayMethods.one; - array.each = ArrayMethods.each; - array.fold = ArrayMethods.fold; - array.select = ArrayMethods.select; + array.one = ArrayMethods.one; + array.each = ArrayMethods.each; + array.fold = ArrayMethods.fold; + array.select = ArrayMethods.select; - return array; - } + return array; + }, + { + choose: void(0), + toValue: void(0), + categorize: void(0) + } + ) for (var x in ArrayMethods) { $exports.Array[x] = (function(underlying) { return function(array) {