-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add set multiple abis in setRecords + get abi by content type
- Loading branch information
1 parent
2141c8a
commit 595093d
Showing
9 changed files
with
212 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/ensjs/src/utils/generateSupportedContentTypes.test.ts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { generateSupportedContentTypes } from './generateSupportedContentTypes.js' | ||
|
||
type FunctionParameters = Parameters<typeof generateSupportedContentTypes> | ||
type EncodeAsParameter = FunctionParameters[0] | ||
|
||
describe('generateSupportedContentTypes', () => { | ||
it.each([ | ||
['json', 1n], | ||
['zlib', 2n], | ||
['cbor', 4n], | ||
['uri', 8n], | ||
['unknown', 0n], | ||
[['json', 'zlib'], 3n], | ||
[['zlib', 'cbor'], 6n], | ||
[['cbor', 'uri'], 12n], | ||
[['json', 'zlib', 'cbor', 'uri'], 15n], | ||
] as [EncodeAsParameter, bigint][])( | ||
'should return the correct bitwise value supportedContentTypes %p (%p)', | ||
(encodedAs, expected) => { | ||
const result = generateSupportedContentTypes(encodedAs) | ||
expect(result).toEqual(expected) | ||
}, | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { EncodeAbiParameters } from './encoders/encodeAbi.js' | ||
|
||
type AbiEncodeAs = EncodeAbiParameters['encodeAs'] | ||
|
||
const abiEncodeMap: { [key in AbiEncodeAs]: bigint } = { | ||
json: 1n, | ||
zlib: 2n, | ||
cbor: 4n, | ||
uri: 8n, | ||
} as const | ||
|
||
export const generateSupportedContentTypes = ( | ||
encodeAsItemOrList: AbiEncodeAs | AbiEncodeAs[], | ||
): bigint => { | ||
const encodeAsList = Array.isArray(encodeAsItemOrList) | ||
? encodeAsItemOrList | ||
: [encodeAsItemOrList] | ||
return encodeAsList.reduce<bigint>((result, encodeAs) => { | ||
const contentType = abiEncodeMap[encodeAs] | ||
if (contentType) result |= contentType | ||
return result | ||
}, 0n) | ||
} |