Skip to content

Commit

Permalink
Rename, add typescript, move to avoid dep cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Jan 16, 2025
1 parent 9402f35 commit 2678518
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
24 changes: 24 additions & 0 deletions src/scval.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,27 @@ export function scValToNative(scv) {
return scv.value();
}
}

/// Inject a sortable map builder into the xdr module.
xdr.scvSortedMap = (items) => {
let sorted = Array.from(items).sort((a, b) => {

Check warning on line 382 in src/scval.js

View workflow job for this annotation

GitHub Actions / build

'sorted' is never reassigned. Use 'const' instead

Check warning on line 382 in src/scval.js

View workflow job for this annotation

GitHub Actions / build (18)

'sorted' is never reassigned. Use 'const' instead

Check warning on line 382 in src/scval.js

View workflow job for this annotation

GitHub Actions / build

'sorted' is never reassigned. Use 'const' instead

Check warning on line 382 in src/scval.js

View workflow job for this annotation

GitHub Actions / build (20)

'sorted' is never reassigned. Use 'const' instead

Check warning on line 382 in src/scval.js

View workflow job for this annotation

GitHub Actions / build (21)

'sorted' is never reassigned. Use 'const' instead
// Both a and b are `ScMapEntry`s, so we need to sort by underlying key.
//
// We couldn't possibly handle every combination of keys since Soroban
// maps don't enforce consistent types, so we do a best-effort and try
// sorting by "number-like" or "string-like."
let nativeA = scValToNative(a.key()),

Check warning on line 388 in src/scval.js

View workflow job for this annotation

GitHub Actions / build

'nativeA' is never reassigned. Use 'const' instead

Check warning on line 388 in src/scval.js

View workflow job for this annotation

GitHub Actions / build (18)

'nativeA' is never reassigned. Use 'const' instead

Check warning on line 388 in src/scval.js

View workflow job for this annotation

GitHub Actions / build

'nativeA' is never reassigned. Use 'const' instead

Check warning on line 388 in src/scval.js

View workflow job for this annotation

GitHub Actions / build (20)

'nativeA' is never reassigned. Use 'const' instead

Check warning on line 388 in src/scval.js

View workflow job for this annotation

GitHub Actions / build (21)

'nativeA' is never reassigned. Use 'const' instead
nativeB = scValToNative(b.key());

Check warning on line 389 in src/scval.js

View workflow job for this annotation

GitHub Actions / build

'nativeB' is never reassigned. Use 'const' instead

Check warning on line 389 in src/scval.js

View workflow job for this annotation

GitHub Actions / build (18)

'nativeB' is never reassigned. Use 'const' instead

Check warning on line 389 in src/scval.js

View workflow job for this annotation

GitHub Actions / build

'nativeB' is never reassigned. Use 'const' instead

Check warning on line 389 in src/scval.js

View workflow job for this annotation

GitHub Actions / build (20)

'nativeB' is never reassigned. Use 'const' instead

Check warning on line 389 in src/scval.js

View workflow job for this annotation

GitHub Actions / build (21)

'nativeB' is never reassigned. Use 'const' instead

switch (typeof nativeA) {
case 'number':
case 'bigint':
return nativeA < nativeB ? -1 : 1;

default:
return nativeA.toString().localeCompare(nativeB.toString());
}
});

return xdr.ScVal.scvMap(sorted);
};
24 changes: 0 additions & 24 deletions src/xdr.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
import xdr from './generated/curr_generated';
import { scValToNative } from './scval';

xdr.scvMapSorted = (items) => {
let sorted = Array.from(items).sort((a, b) => {
// Both a and b are `ScMapEntry`s, so we need to sort by underlying key.
//
// We couldn't possibly handle every combination of keys since Soroban
// maps don't enforce consistent types, so we do a best-effort and try
// sorting by "number-like" or "string-like."
let nativeA = scValToNative(a.key()),
nativeB = scValToNative(b.key());

switch (typeof nativeA) {
case 'number':
case 'bigint':
return nativeA < nativeB ? -1 : 1;

default:
return nativeA.toString().localeCompare(nativeB.toString());
}
});

return xdr.ScVal.scvMap(sorted);
};

export default xdr;
4 changes: 2 additions & 2 deletions test/unit/scval_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ describe('parsing and building ScVals', function () {
expect(sample.value()[idx].key().value()).to.equal(val);
});

const sorted = xdr.scvMapSorted(sample.value());
const sorted = xdr.scvSortedMap(sample.value());
expect(sorted.switch().name).to.equal('scvMap');
['a', 'b', 'c'].forEach((val, idx) => {
expect(sorted.value()[idx].key().value()).to.equal(val);
Expand Down Expand Up @@ -323,7 +323,7 @@ describe('parsing and building ScVals', function () {
expect(sample.value()[idx].key().value().toBigInt()).to.equal(val);
});

const sorted = xdr.scvMapSorted(sample.value());
const sorted = xdr.scvSortedMap(sample.value());
expect(sorted.switch().name).to.equal('scvMap');
[1n, 2n, 3n].forEach((val, idx) => {
expect(sorted.value()[idx].key().value().toBigInt()).to.equal(val);
Expand Down
10 changes: 10 additions & 0 deletions types/curr.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ export namespace xdr {

type Hash = Opaque[]; // workaround, cause unknown

/**
* Returns an {@link ScVal} with a map type and sorted entries.
*
* @param items the key-value pairs to sort.
*
* @warning This only performs "best-effort" sorting, working best when the
* keys are all either numeric or string-like.
*/
function scvSortedMap(items: ScMapEntry[]): ScVal;

interface SignedInt {
readonly MAX_VALUE: 2147483647;
readonly MIN_VALUE: -2147483648;
Expand Down

0 comments on commit 2678518

Please sign in to comment.