Skip to content

Commit

Permalink
Merge pull request #1640 from davidpcaldwell/davidpcaldwell/issue/#1625
Browse files Browse the repository at this point in the history
#1625: js/object Array JSAPI -> Fifty
  • Loading branch information
davidpcaldwell authored Aug 31, 2024
2 parents 2a45c95 + 31db99b commit 020ff87
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 54 deletions.
42 changes: 0 additions & 42 deletions js/object/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,48 +137,6 @@ <h1>Exports</h1>
</span>
</div>
</div>
<ul>
<li class="object" jsapi:id="Array">
<div class="name">Array</div>
<div class="label">has properties:</div>
<ul>
<li class="function">
<div class="name">choose</div>
<span>
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.
</span>
<div class="arguments">
<div class="label">Arguments</div>
<ol>
<li class="value">
<span class="type">Array</span>
<span>An array.</span>
</li>
<li class="value">
<span class="type">Filter</span>
<span>(optional) A Filter that is used to preprocess the array.</span>
</li>
</ol>
</div>
<div class="returns">
<div class="label">Returns</div>
<span>
The single element of the array (after applying the given Filter, if applicable), or
<code>null</code> if the array has zero elements. An error is thrown if more than one element
matches.
</span>
</div>
<script type="application/x.jsapi#tests" jsapi:id="Array.choose">
test( module.Array.choose([ 3 ]) == 3 );
test( module.Array.choose([]) === null );
test( module.Array.choose([ 1, 2, 3 ], function(a) { return a == 3; } ) == 3 );
</script>
</li>
</ul>
<!-- TODO decide whether Array.categorize is experimental -->
</li>
</ul>
</div>
</body>
</html>
41 changes: 40 additions & 1 deletion js/object/module.fifty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,46 @@ namespace slime.$api.old {
)(fifty);

export interface Exports {
Array: any
Array: {
<T>(ts: Array<T>): Array<T> & { each: any }
new <T>(ts: Array<T>): Array<T>

/**
* 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: <T>(array: Array<T>, 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

Expand Down
42 changes: 31 additions & 11 deletions js/object/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 020ff87

Please sign in to comment.