Skip to content

Commit 74159a8

Browse files
committedSep 27, 2022
feat: Improve date serialization
Remove prototype override and add key based type checking Add `validateAccountId` util
1 parent d843af6 commit 74159a8

File tree

6 files changed

+66
-24
lines changed

6 files changed

+66
-24
lines changed
 

‎lib/index.d.ts

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/index.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/utils.d.ts

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/utils.js

+22-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
UnorderedSet,
1010
} from "./collections";
1111

12-
import { bytes, Bytes, assert } from "./utils";
12+
import { bytes, Bytes, assert, validateAccountId } from "./utils";
1313

1414
import { NearPromise, PromiseOrValue } from "./promise";
1515

@@ -27,6 +27,7 @@ export {
2727
bytes,
2828
Bytes,
2929
assert,
30+
validateAccountId,
3031
NearPromise,
3132
PromiseOrValue,
3233
};

‎src/utils.ts

+30-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export const ERR_INCONSISTENT_STATE =
1515
"The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?";
1616
export const ERR_INDEX_OUT_OF_BOUNDS = "Index out of bounds";
1717

18+
const ACCOUNT_ID_REGEX =
19+
/^(([a-z\d]+[\-_])*[a-z\d]+\.)*([a-z\d]+[\-_])*[a-z\d]+$/;
20+
1821
export function u8ArrayToBytes(array: Uint8Array): Bytes {
1922
return array.reduce(
2023
(result, value) => `${result}${String.fromCharCode(value)}`,
@@ -88,23 +91,25 @@ export function serializeValueWithOptions<DataType>(
8891
}
8992

9093
export function serialize(valueToSerialize: unknown): string {
91-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
92-
// @ts-ignore
93-
Date.prototype.toJSON = function () {
94-
return {
95-
value: this.toISOString(),
96-
[TYPE_KEY]: TypeBrand.DATE,
97-
};
98-
};
99-
100-
return JSON.stringify(valueToSerialize, (_, value) => {
94+
return JSON.stringify(valueToSerialize, function (key, value) {
10195
if (typeof value === "bigint") {
10296
return {
10397
value: value.toString(),
10498
[TYPE_KEY]: TypeBrand.BIGINT,
10599
};
106100
}
107101

102+
if (
103+
typeof this[key] === "object" &&
104+
this[key] !== null &&
105+
this[key] instanceof Date
106+
) {
107+
return {
108+
value: this[key].toISOString(),
109+
[TYPE_KEY]: TypeBrand.DATE,
110+
};
111+
}
112+
108113
return value;
109114
});
110115
}
@@ -128,3 +133,18 @@ export function deserialize(valueToDeserialize: string): unknown {
128133
return value;
129134
});
130135
}
136+
137+
/**
138+
* Validates the Account ID according to the NEAR protocol
139+
* [Account ID rules](https://nomicon.io/DataStructures/Account#account-id-rules).
140+
*
141+
* @param accountId - The Account ID string you want to validate.
142+
* @returns boolean
143+
*/
144+
export function validateAccountId(accountId: string): boolean {
145+
return (
146+
accountId.length >= 2 &&
147+
accountId.length <= 64 &&
148+
ACCOUNT_ID_REGEX.test(accountId)
149+
);
150+
}

0 commit comments

Comments
 (0)