Skip to content

Commit

Permalink
feat: Improve date serialization
Browse files Browse the repository at this point in the history
Remove prototype override and add key based type checking
Add `validateAccountId` util
  • Loading branch information
petarvujovic98 committed Sep 27, 2022
1 parent d843af6 commit 74159a8
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/utils.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 22 additions & 9 deletions lib/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
UnorderedSet,
} from "./collections";

import { bytes, Bytes, assert } from "./utils";
import { bytes, Bytes, assert, validateAccountId } from "./utils";

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

Expand All @@ -27,6 +27,7 @@ export {
bytes,
Bytes,
assert,
validateAccountId,
NearPromise,
PromiseOrValue,
};
40 changes: 30 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export const ERR_INCONSISTENT_STATE =
"The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?";
export const ERR_INDEX_OUT_OF_BOUNDS = "Index out of bounds";

const ACCOUNT_ID_REGEX =
/^(([a-z\d]+[\-_])*[a-z\d]+\.)*([a-z\d]+[\-_])*[a-z\d]+$/;

export function u8ArrayToBytes(array: Uint8Array): Bytes {
return array.reduce(
(result, value) => `${result}${String.fromCharCode(value)}`,
Expand Down Expand Up @@ -88,23 +91,25 @@ export function serializeValueWithOptions<DataType>(
}

export function serialize(valueToSerialize: unknown): string {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Date.prototype.toJSON = function () {
return {
value: this.toISOString(),
[TYPE_KEY]: TypeBrand.DATE,
};
};

return JSON.stringify(valueToSerialize, (_, value) => {
return JSON.stringify(valueToSerialize, function (key, value) {
if (typeof value === "bigint") {
return {
value: value.toString(),
[TYPE_KEY]: TypeBrand.BIGINT,
};
}

if (
typeof this[key] === "object" &&
this[key] !== null &&
this[key] instanceof Date
) {
return {
value: this[key].toISOString(),
[TYPE_KEY]: TypeBrand.DATE,
};
}

return value;
});
}
Expand All @@ -128,3 +133,18 @@ export function deserialize(valueToDeserialize: string): unknown {
return value;
});
}

/**
* Validates the Account ID according to the NEAR protocol
* [Account ID rules](https://nomicon.io/DataStructures/Account#account-id-rules).
*
* @param accountId - The Account ID string you want to validate.
* @returns boolean
*/
export function validateAccountId(accountId: string): boolean {
return (
accountId.length >= 2 &&
accountId.length <= 64 &&
ACCOUNT_ID_REGEX.test(accountId)
);
}

0 comments on commit 74159a8

Please sign in to comment.