Skip to content

Commit

Permalink
Merge pull request #1639 from davidpcaldwell/davidpcaldwell/issue/#1625
Browse files Browse the repository at this point in the history
#1625: js/object Order JSAPI -> Fifty
  • Loading branch information
davidpcaldwell authored Aug 31, 2024
2 parents f423458 + 596de72 commit 2a45c95
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 77 deletions.
76 changes: 0 additions & 76 deletions js/object/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,82 +138,6 @@ <h1>Exports</h1>
</div>
</div>
<ul>
<!-- TODO convert Map/Order to types -->
<li class="object" jsapi:id="Order">
<div class="name">Order</div>
<span>
An <i>Order</i> is a function that implements a specific interface, intended for sorting: it takes two
arguments and returns a value less than zero if the first argument belongs before the second argument, and a
value greater than zero if the second argument belongs before the first argument. This is commensurate with the
<code>Array.prototype.sort</code> method.
</span>
<div class="label">has properties:</div>
<ul>
<li class="function">
<div class="name">property</div>
<div class="arguments">
<div class="label">Arguments</div>
<ol>
<li class="value">
<span class="type">string</span>
<span>A property name.</span>
</li>
<li class="value">
<span class="type">Order</span>
<span>An Order to use to compare values of the given named property.</span>
</li>
</ol>
</div>
<div class="returns">
<div class="label">Returns</div>
<span class="type">Order</span>
<span>
An Order that uses the given Order to compare the value of the named property for each of its
arguments and returns the result of that comparison.
</span>
</div>
</li>
<li class="function">
<div class="name">map</div>
<div class="arguments">
<div class="label">Arguments</div>
<ol>
<li class="value">
<span class="type">Map</span>
<span>A Map that transforms the arguments to this Order before they are compared.</span>
</li>
<li class="value">
<span class="type">Order</span>
<span>An Order to use to compare values produced by the given Map.</span>
</li>
</ol>
</div>
<div class="returns">
<div class="label">Returns</div>
<span class="type">Order</span>
<span>
An Order that uses the given Order to compare the value the given Map produces for each of its
arguments and returns the result of that comparison.
</span>
</div>
</li>
</ul>
<script type="application/x.jsapi#tests" jsapi:id="Order">
var numerical = function(a,b) { return b - a; };
var ascending = function(a,b) { return a - b; };
var array = [ { s: "a", v: 2 }, { s: "b", v: 1 }, { s: "c", v: 3 } ];
var propertyOrder = module.Order.property("v", numerical);
var mapOrder = module.Order.map(module.Map.property("v"), ascending);
array.sort( propertyOrder );
test(array[0].s == "c");
test(array[1].s == "a");
test(array[2].s == "b");
array.sort( mapOrder );
test(array[0].s == "b");
test(array[1].s == "a");
test(array[2].s == "c");
</script>
</li>
<li class="object" jsapi:id="Array">
<div class="name">Array</div>
<div class="label">has properties:</div>
Expand Down
60 changes: 59 additions & 1 deletion js/object/module.fifty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,66 @@ namespace slime.$api.old {
//@ts-ignore
)(fifty);

/**
* An *Order* is a function that implements a specific interface, intended for sorting: it takes two arguments and returns a
* value less than zero if the first argument belongs before the second argument, and a value greater than zero if the second
* argument belongs before the first argument. This is commensurate with the `Array.prototype.sort` method.
*/
export type Order<T> = Parameters<Array<T>["sort"]>[0]

export interface Exports {
Order: {
/**
*
* @param property A property name.
* @param order An Order to use to compare values of the given named property.
* @returns An Order that uses the given Order to compare the value of the named property for each of its arguments and
* returns the result of that comparison.
*/
property: <T extends object, N extends keyof T>(property: N, order: Order<T[N]>) => Order<T>

/**
*
* @param map A Map that transforms the arguments to this Order before they are compared.
* @param order An Order to use to compare values produced by the given Map.
* @returns An Order that uses the given Order to compare the value the given Map produces for each of its arguments and
* returns the result of that comparison.
*/
map: <T extends object, A>(map: (t: T) => A, order: Order<A>) => Order<T>
}
}

(
function(
fifty: slime.fifty.test.Kit
) {
const module = old.test.subject;

const test = function(b) {
fifty.verify(b).is(true);
};

fifty.tests.exports.Order = function() {
var numerical: Order<number> = function(a,b) { return b - a; };
var ascending: Order<number> = function(a,b) { return a - b; };
type SV = { s: string, v: number };
var array: SV[] = [ { s: "a", v: 2 }, { s: "b", v: 1 }, { s: "c", v: 3 } ];
var propertyOrder: Order<SV> = module.Order.property("v", numerical);
var mapOrder: Order<SV> = module.Order.map(module.Map.property("v"), ascending);
array.sort( propertyOrder );
test(array[0].s == "c");
test(array[1].s == "a");
test(array[2].s == "b");
array.sort( mapOrder );
test(array[0].s == "b");
test(array[1].s == "a");
test(array[2].s == "c");
}
}
//@ts-ignore
)(fifty);

export interface Exports {
Order: any
Array: any
Error: any
Task: any
Expand Down

0 comments on commit 2a45c95

Please sign in to comment.