@metamask/utils 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/LICENSE +15 -0
- package/README.md +73 -0
- package/dist/collections.d.ts +38 -0
- package/dist/collections.js +111 -0
- package/dist/collections.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/json.d.ts +108 -0
- package/dist/json.js +58 -0
- package/dist/json.js.map +1 -0
- package/dist/misc.d.ts +60 -0
- package/dist/misc.js +55 -0
- package/dist/misc.js.map +1 -0
- package/dist/time.d.ts +27 -0
- package/dist/time.js +34 -0
- package/dist/time.js.map +1 -0
- package/package.json +69 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
All notable changes to this project will be documented in this file.
|
|
3
|
+
|
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [1.0.0]
|
|
10
|
+
### Added
|
|
11
|
+
- Initial release
|
|
12
|
+
|
|
13
|
+
[Unreleased]: https://github.com/MetaMask/utils/compare/v1.0.0...HEAD
|
|
14
|
+
[1.0.0]: https://github.com/MetaMask/utils/releases/tag/v1.0.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 MetaMask
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# MetaMask Utils
|
|
2
|
+
|
|
3
|
+
Various JavaScript/TypeScript utilities of wide relevance to the MetaMask codebase.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
`yarn add @metamask/utils`
|
|
8
|
+
|
|
9
|
+
or
|
|
10
|
+
|
|
11
|
+
`npm install @metamask/utils`
|
|
12
|
+
|
|
13
|
+
## API
|
|
14
|
+
|
|
15
|
+
The full API documentation for the latest published version of this library is [available here](https://metamask.github.io/eth-sig-util/index.html).
|
|
16
|
+
|
|
17
|
+
## Contributing
|
|
18
|
+
|
|
19
|
+
### Setup
|
|
20
|
+
|
|
21
|
+
- Install [Node.js](https://nodejs.org) version 12
|
|
22
|
+
- If you are using [nvm](https://github.com/creationix/nvm#installation) (recommended) running `nvm use` will automatically choose the right node version for you.
|
|
23
|
+
- Install [Yarn v1](https://yarnpkg.com/en/docs/install)
|
|
24
|
+
- Run `yarn setup` to install dependencies and run any requried post-install scripts
|
|
25
|
+
- **Warning:** Do not use the `yarn` / `yarn install` command directly. Use `yarn setup` instead. The normal install command will skip required post-install scripts, leaving your development environment in an invalid state.
|
|
26
|
+
|
|
27
|
+
### Testing and Linting
|
|
28
|
+
|
|
29
|
+
Run `yarn test` to run the tests once. To run tests on file changes, run `yarn test:watch`.
|
|
30
|
+
|
|
31
|
+
Run `yarn lint` to run the linter, or run `yarn lint:fix` to run the linter and fix any automatically fixable issues.
|
|
32
|
+
|
|
33
|
+
### Documentation
|
|
34
|
+
|
|
35
|
+
The API documentation can be generated with the command `yarn docs`, which saves it in the `./docs` directory. Open the `./docs/index.html` file to browse the documentation.
|
|
36
|
+
|
|
37
|
+
### Release & Publishing
|
|
38
|
+
|
|
39
|
+
The project follows the same release process as the other libraries in the MetaMask organization. The GitHub Actions [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) and [`action-publish-release`](https://github.com/MetaMask/action-publish-release) are used to automate the release process; see those repositories for more information about how they work.
|
|
40
|
+
|
|
41
|
+
1. Choose a release version.
|
|
42
|
+
|
|
43
|
+
- The release version should be chosen according to SemVer. Analyze the changes to see whether they include any breaking changes, new features, or deprecations, then choose the appropriate SemVer version. See [the SemVer specification](https://semver.org/) for more information.
|
|
44
|
+
|
|
45
|
+
2. If this release is backporting changes onto a previous release, then ensure there is a major version branch for that version (e.g. `1.x` for a `v1` backport release).
|
|
46
|
+
|
|
47
|
+
- The major version branch should be set to the most recent release with that major version. For example, when backporting a `v1.0.2` release, you'd want to ensure there was a `1.x` branch that was set to the `v1.0.1` tag.
|
|
48
|
+
|
|
49
|
+
3. Trigger the [`workflow_dispatch`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch) event [manually](https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow) for the `Create Release Pull Request` action to create the release PR.
|
|
50
|
+
|
|
51
|
+
- For a backport release, the base branch should be the major version branch that you ensured existed in step 2. For a normal release, the base branch should be the main branch for that repository (which should be the default value).
|
|
52
|
+
- This should trigger the [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) workflow to create the release PR.
|
|
53
|
+
|
|
54
|
+
4. Update the changelog to move each change entry into the appropriate change category ([See here](https://keepachangelog.com/en/1.0.0/#types) for the full list of change categories, and the correct ordering), and edit them to be more easily understood by users of the package.
|
|
55
|
+
|
|
56
|
+
- Generally any changes that don't affect consumers of the package (e.g. lockfile changes or development environment changes) are omitted. Exceptions may be made for changes that might be of interest despite not having an effect upon the published package (e.g. major test improvements, security improvements, improved documentation, etc.).
|
|
57
|
+
- Try to explain each change in terms that users of the package would understand (e.g. avoid referencing internal variables/concepts).
|
|
58
|
+
- Consolidate related changes into one change entry if it makes it easier to explain.
|
|
59
|
+
- Run `yarn auto-changelog validate --rc` to check that the changelog is correctly formatted.
|
|
60
|
+
|
|
61
|
+
5. Review and QA the release.
|
|
62
|
+
|
|
63
|
+
- If changes are made to the base branch, the release branch will need to be updated with these changes and review/QA will need to restart again. As such, it's probably best to avoid merging other PRs into the base branch while review is underway.
|
|
64
|
+
|
|
65
|
+
6. Squash & Merge the release.
|
|
66
|
+
|
|
67
|
+
- This should trigger the [`action-publish-release`](https://github.com/MetaMask/action-publish-release) workflow to tag the final release commit and publish the release on GitHub.
|
|
68
|
+
|
|
69
|
+
7. Publish the release on npm.
|
|
70
|
+
|
|
71
|
+
- Be very careful to use a clean local environment to publish the release, and follow exactly the same steps used during CI.
|
|
72
|
+
- Use `npm publish --dry-run` to examine the release contents to ensure the correct files are included. Compare to previous releases if necessary (e.g. using `https://unpkg.com/browse/[package name]@[package version]/`).
|
|
73
|
+
- Once you are confident the release contents are correct, publish the release using `npm publish`.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A {@link ReadonlyMap} that cannot be modified after instantiation.
|
|
3
|
+
* The implementation uses an inner map hidden via a private field, and the
|
|
4
|
+
* immutability guarantee relies on it being impossible to get a reference
|
|
5
|
+
* to this map.
|
|
6
|
+
*/
|
|
7
|
+
declare class FrozenMap<Key, Value> implements ReadonlyMap<Key, Value> {
|
|
8
|
+
#private;
|
|
9
|
+
get size(): number;
|
|
10
|
+
[Symbol.iterator](): IterableIterator<[Key, Value]>;
|
|
11
|
+
constructor(entries?: readonly (readonly [Key, Value])[] | null);
|
|
12
|
+
entries(): IterableIterator<[Key, Value]>;
|
|
13
|
+
forEach(callbackfn: (value: Value, key: Key, map: this) => void, thisArg?: any): void;
|
|
14
|
+
get(key: Key): Value | undefined;
|
|
15
|
+
has(key: Key): boolean;
|
|
16
|
+
keys(): IterableIterator<Key>;
|
|
17
|
+
values(): IterableIterator<Value>;
|
|
18
|
+
toString(): string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A {@link ReadonlySet} that cannot be modified after instantiation.
|
|
22
|
+
* The implementation uses an inner set hidden via a private field, and the
|
|
23
|
+
* immutability guarantee relies on it being impossible to get a reference
|
|
24
|
+
* to this set.
|
|
25
|
+
*/
|
|
26
|
+
declare class FrozenSet<Value> implements ReadonlySet<Value> {
|
|
27
|
+
#private;
|
|
28
|
+
get size(): number;
|
|
29
|
+
[Symbol.iterator](): IterableIterator<Value>;
|
|
30
|
+
constructor(values?: readonly Value[] | null);
|
|
31
|
+
entries(): IterableIterator<[Value, Value]>;
|
|
32
|
+
forEach(callbackfn: (value: Value, value2: Value, set: this) => void, thisArg?: any): void;
|
|
33
|
+
has(value: Value): boolean;
|
|
34
|
+
keys(): IterableIterator<Value>;
|
|
35
|
+
values(): IterableIterator<Value>;
|
|
36
|
+
toString(): string;
|
|
37
|
+
}
|
|
38
|
+
export { FrozenMap, FrozenSet };
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
|
|
3
|
+
if (!privateMap.has(receiver)) {
|
|
4
|
+
throw new TypeError("attempted to set private field on non-instance");
|
|
5
|
+
}
|
|
6
|
+
privateMap.set(receiver, value);
|
|
7
|
+
return value;
|
|
8
|
+
};
|
|
9
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
|
|
10
|
+
if (!privateMap.has(receiver)) {
|
|
11
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
12
|
+
}
|
|
13
|
+
return privateMap.get(receiver);
|
|
14
|
+
};
|
|
15
|
+
var _map_1, _set_1;
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.FrozenSet = exports.FrozenMap = void 0;
|
|
18
|
+
/**
|
|
19
|
+
* A {@link ReadonlyMap} that cannot be modified after instantiation.
|
|
20
|
+
* The implementation uses an inner map hidden via a private field, and the
|
|
21
|
+
* immutability guarantee relies on it being impossible to get a reference
|
|
22
|
+
* to this map.
|
|
23
|
+
*/
|
|
24
|
+
class FrozenMap {
|
|
25
|
+
constructor(entries) {
|
|
26
|
+
_map_1.set(this, void 0);
|
|
27
|
+
__classPrivateFieldSet(this, _map_1, new Map(entries));
|
|
28
|
+
Object.freeze(this);
|
|
29
|
+
}
|
|
30
|
+
get size() {
|
|
31
|
+
return __classPrivateFieldGet(this, _map_1).size;
|
|
32
|
+
}
|
|
33
|
+
[(_map_1 = new WeakMap(), Symbol.iterator)]() {
|
|
34
|
+
return __classPrivateFieldGet(this, _map_1)[Symbol.iterator]();
|
|
35
|
+
}
|
|
36
|
+
entries() {
|
|
37
|
+
return __classPrivateFieldGet(this, _map_1).entries();
|
|
38
|
+
}
|
|
39
|
+
forEach(callbackfn, thisArg) {
|
|
40
|
+
// We have to wrap the specified callback in order to prevent it from
|
|
41
|
+
// receiving a reference to the inner map.
|
|
42
|
+
return __classPrivateFieldGet(this, _map_1).forEach((value, key, _map) => callbackfn.call(thisArg, value, key, this));
|
|
43
|
+
}
|
|
44
|
+
get(key) {
|
|
45
|
+
return __classPrivateFieldGet(this, _map_1).get(key);
|
|
46
|
+
}
|
|
47
|
+
has(key) {
|
|
48
|
+
return __classPrivateFieldGet(this, _map_1).has(key);
|
|
49
|
+
}
|
|
50
|
+
keys() {
|
|
51
|
+
return __classPrivateFieldGet(this, _map_1).keys();
|
|
52
|
+
}
|
|
53
|
+
values() {
|
|
54
|
+
return __classPrivateFieldGet(this, _map_1).values();
|
|
55
|
+
}
|
|
56
|
+
toString() {
|
|
57
|
+
return `FrozenMap(${this.size}) {${this.size > 0
|
|
58
|
+
? ` ${[...this.entries()]
|
|
59
|
+
.map(([key, value]) => `${String(key)} => ${String(value)}`)
|
|
60
|
+
.join(', ')} `
|
|
61
|
+
: ''}}`;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.FrozenMap = FrozenMap;
|
|
65
|
+
/**
|
|
66
|
+
* A {@link ReadonlySet} that cannot be modified after instantiation.
|
|
67
|
+
* The implementation uses an inner set hidden via a private field, and the
|
|
68
|
+
* immutability guarantee relies on it being impossible to get a reference
|
|
69
|
+
* to this set.
|
|
70
|
+
*/
|
|
71
|
+
class FrozenSet {
|
|
72
|
+
constructor(values) {
|
|
73
|
+
_set_1.set(this, void 0);
|
|
74
|
+
__classPrivateFieldSet(this, _set_1, new Set(values));
|
|
75
|
+
Object.freeze(this);
|
|
76
|
+
}
|
|
77
|
+
get size() {
|
|
78
|
+
return __classPrivateFieldGet(this, _set_1).size;
|
|
79
|
+
}
|
|
80
|
+
[(_set_1 = new WeakMap(), Symbol.iterator)]() {
|
|
81
|
+
return __classPrivateFieldGet(this, _set_1)[Symbol.iterator]();
|
|
82
|
+
}
|
|
83
|
+
entries() {
|
|
84
|
+
return __classPrivateFieldGet(this, _set_1).entries();
|
|
85
|
+
}
|
|
86
|
+
forEach(callbackfn, thisArg) {
|
|
87
|
+
// We have to wrap the specified callback in order to prevent it from
|
|
88
|
+
// receiving a reference to the inner set.
|
|
89
|
+
return __classPrivateFieldGet(this, _set_1).forEach((value, value2, _set) => callbackfn.call(thisArg, value, value2, this));
|
|
90
|
+
}
|
|
91
|
+
has(value) {
|
|
92
|
+
return __classPrivateFieldGet(this, _set_1).has(value);
|
|
93
|
+
}
|
|
94
|
+
keys() {
|
|
95
|
+
return __classPrivateFieldGet(this, _set_1).keys();
|
|
96
|
+
}
|
|
97
|
+
values() {
|
|
98
|
+
return __classPrivateFieldGet(this, _set_1).values();
|
|
99
|
+
}
|
|
100
|
+
toString() {
|
|
101
|
+
return `FrozenSet(${this.size}) {${this.size > 0
|
|
102
|
+
? ` ${[...this.values()].map((member) => String(member)).join(', ')} `
|
|
103
|
+
: ''}}`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.FrozenSet = FrozenSet;
|
|
107
|
+
Object.freeze(FrozenMap);
|
|
108
|
+
Object.freeze(FrozenMap.prototype);
|
|
109
|
+
Object.freeze(FrozenSet);
|
|
110
|
+
Object.freeze(FrozenSet.prototype);
|
|
111
|
+
//# sourceMappingURL=collections.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collections.js","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":"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","sourcesContent":["/**\n * A {@link ReadonlyMap} that cannot be modified after instantiation.\n * The implementation uses an inner map hidden via a private field, and the\n * immutability guarantee relies on it being impossible to get a reference\n * to this map.\n */\nclass FrozenMap<Key, Value> implements ReadonlyMap<Key, Value> {\n readonly #map: Map<Key, Value>;\n\n public get size() {\n return this.#map.size;\n }\n\n public [Symbol.iterator]() {\n return this.#map[Symbol.iterator]();\n }\n\n constructor(entries?: readonly (readonly [Key, Value])[] | null) {\n this.#map = new Map<Key, Value>(entries);\n Object.freeze(this);\n }\n\n public entries() {\n return this.#map.entries();\n }\n\n public forEach(\n callbackfn: (value: Value, key: Key, map: this) => void,\n thisArg?: any,\n ): void {\n // We have to wrap the specified callback in order to prevent it from\n // receiving a reference to the inner map.\n return this.#map.forEach((value: Value, key: Key, _map: unknown) =>\n callbackfn.call(thisArg, value, key, this),\n );\n }\n\n public get(key: Key) {\n return this.#map.get(key);\n }\n\n public has(key: Key) {\n return this.#map.has(key);\n }\n\n public keys() {\n return this.#map.keys();\n }\n\n public values() {\n return this.#map.values();\n }\n\n public toString(): string {\n return `FrozenMap(${this.size}) {${\n this.size > 0\n ? ` ${[...this.entries()]\n .map(([key, value]) => `${String(key)} => ${String(value)}`)\n .join(', ')} `\n : ''\n }}`;\n }\n}\n\n/**\n * A {@link ReadonlySet} that cannot be modified after instantiation.\n * The implementation uses an inner set hidden via a private field, and the\n * immutability guarantee relies on it being impossible to get a reference\n * to this set.\n */\nclass FrozenSet<Value> implements ReadonlySet<Value> {\n readonly #set: Set<Value>;\n\n public get size() {\n return this.#set.size;\n }\n\n public [Symbol.iterator]() {\n return this.#set[Symbol.iterator]();\n }\n\n constructor(values?: readonly Value[] | null) {\n this.#set = new Set<Value>(values);\n Object.freeze(this);\n }\n\n public entries() {\n return this.#set.entries();\n }\n\n public forEach(\n callbackfn: (value: Value, value2: Value, set: this) => void,\n thisArg?: any,\n ): void {\n // We have to wrap the specified callback in order to prevent it from\n // receiving a reference to the inner set.\n return this.#set.forEach((value: Value, value2: Value, _set: unknown) =>\n callbackfn.call(thisArg, value, value2, this),\n );\n }\n\n public has(value: Value) {\n return this.#set.has(value);\n }\n\n public keys() {\n return this.#set.keys();\n }\n\n public values() {\n return this.#set.values();\n }\n\n public toString(): string {\n return `FrozenSet(${this.size}) {${\n this.size > 0\n ? ` ${[...this.values()].map((member) => String(member)).join(', ')} `\n : ''\n }}`;\n }\n}\n\nObject.freeze(FrozenMap);\nObject.freeze(FrozenMap.prototype);\n\nObject.freeze(FrozenSet);\nObject.freeze(FrozenSet.prototype);\n\nexport { FrozenMap, FrozenSet };\n"]}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
__exportStar(require("./json"), exports);
|
|
14
|
+
__exportStar(require("./misc"), exports);
|
|
15
|
+
__exportStar(require("./time"), exports);
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yCAAuB;AACvB,yCAAuB;AACvB,yCAAuB","sourcesContent":["export * from './json';\nexport * from './misc';\nexport * from './time';\n"]}
|
package/dist/json.d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Any JSON-compatible value.
|
|
3
|
+
*/
|
|
4
|
+
export declare type Json = null | boolean | number | string | Json[] | {
|
|
5
|
+
[prop: string]: Json;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Type guard for {@link Json}.
|
|
9
|
+
*
|
|
10
|
+
* @param value - The value to check.
|
|
11
|
+
* @returns Whether the value is valid JSON.
|
|
12
|
+
*/
|
|
13
|
+
export declare function isValidJson(value: unknown): value is Json;
|
|
14
|
+
/**
|
|
15
|
+
* The string '2.0'.
|
|
16
|
+
*/
|
|
17
|
+
export declare const jsonrpc2: "2.0";
|
|
18
|
+
/**
|
|
19
|
+
* A String specifying the version of the JSON-RPC protocol.
|
|
20
|
+
* MUST be exactly "2.0".
|
|
21
|
+
*/
|
|
22
|
+
export declare type JsonRpcVersion2 = typeof jsonrpc2;
|
|
23
|
+
/**
|
|
24
|
+
* An identifier established by the Client that MUST contain a String, Number,
|
|
25
|
+
* or NULL value if included. If it is not included it is assumed to be a
|
|
26
|
+
* notification. The value SHOULD normally not be Null and Numbers SHOULD
|
|
27
|
+
* NOT contain fractional parts.
|
|
28
|
+
*/
|
|
29
|
+
export declare type JsonRpcId = number | string | null;
|
|
30
|
+
/**
|
|
31
|
+
* A JSON-RPC error object.
|
|
32
|
+
*/
|
|
33
|
+
export declare type JsonRpcError = {
|
|
34
|
+
code: number;
|
|
35
|
+
message: string;
|
|
36
|
+
data?: unknown;
|
|
37
|
+
stack?: string;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* A JSON-RPC request object.
|
|
41
|
+
*
|
|
42
|
+
* @template Params - The type of the params.
|
|
43
|
+
*/
|
|
44
|
+
export declare type JsonRpcRequest<Params> = {
|
|
45
|
+
id: JsonRpcId;
|
|
46
|
+
jsonrpc: JsonRpcVersion2;
|
|
47
|
+
method: string;
|
|
48
|
+
params?: Params;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* A JSON-RPC notification object.
|
|
52
|
+
*
|
|
53
|
+
* @template Params - The type of the params.
|
|
54
|
+
*/
|
|
55
|
+
export declare type JsonRpcNotification<Params> = {
|
|
56
|
+
jsonrpc: JsonRpcVersion2;
|
|
57
|
+
method: string;
|
|
58
|
+
params?: Params;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* A successful JSON-RPC response object.
|
|
62
|
+
*
|
|
63
|
+
* @template Result - The type of the result.
|
|
64
|
+
*/
|
|
65
|
+
export declare type JsonRpcSuccess<Result = unknown> = {
|
|
66
|
+
id: JsonRpcId;
|
|
67
|
+
jsonrpc: JsonRpcVersion2;
|
|
68
|
+
result: Result;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* A failed JSON-RPC response object.
|
|
72
|
+
*/
|
|
73
|
+
export declare type JsonRpcFailure = {
|
|
74
|
+
id: JsonRpcId;
|
|
75
|
+
jsonrpc: JsonRpcVersion2;
|
|
76
|
+
error: JsonRpcError;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* A JSON-RPC response object. Must be checked to determine whether it's a
|
|
80
|
+
* success or failure.
|
|
81
|
+
*
|
|
82
|
+
* @template Result - The type of the result.
|
|
83
|
+
*/
|
|
84
|
+
export declare type JsonRpcResponse<Result = unknown> = JsonRpcSuccess<Result> | JsonRpcFailure;
|
|
85
|
+
/**
|
|
86
|
+
* ATTN: Assumes that only one of the `result` and `error` properties is
|
|
87
|
+
* present on the `response`, as guaranteed by e.g.
|
|
88
|
+
* [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).
|
|
89
|
+
*
|
|
90
|
+
* Type guard to narrow a JsonRpcResponse object to a success (or failure).
|
|
91
|
+
*
|
|
92
|
+
* @param response - The response object to check.
|
|
93
|
+
* @returns Whether the response object is a success, i.e. has a `result`
|
|
94
|
+
* property.
|
|
95
|
+
*/
|
|
96
|
+
export declare function isJsonRpcSuccess<Result>(response: JsonRpcResponse<Result>): response is JsonRpcSuccess<Result>;
|
|
97
|
+
/**
|
|
98
|
+
* ATTN: Assumes that only one of the `result` and `error` properties is
|
|
99
|
+
* present on the `response`, as guaranteed by e.g.
|
|
100
|
+
* [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).
|
|
101
|
+
*
|
|
102
|
+
* Type guard to narrow a JsonRpcResponse object to a failure (or success).
|
|
103
|
+
*
|
|
104
|
+
* @param response - The response object to check.
|
|
105
|
+
* @returns Whether the response object is a failure, i.e. has an `error`
|
|
106
|
+
* property.
|
|
107
|
+
*/
|
|
108
|
+
export declare function isJsonRpcFailure(response: JsonRpcResponse<unknown>): response is JsonRpcFailure;
|
package/dist/json.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isJsonRpcFailure = exports.isJsonRpcSuccess = exports.jsonrpc2 = exports.isValidJson = void 0;
|
|
7
|
+
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
|
|
8
|
+
const misc_1 = require("./misc");
|
|
9
|
+
/**
|
|
10
|
+
* Type guard for {@link Json}.
|
|
11
|
+
*
|
|
12
|
+
* @param value - The value to check.
|
|
13
|
+
* @returns Whether the value is valid JSON.
|
|
14
|
+
*/
|
|
15
|
+
function isValidJson(value) {
|
|
16
|
+
try {
|
|
17
|
+
return fast_deep_equal_1.default(value, JSON.parse(JSON.stringify(value)));
|
|
18
|
+
}
|
|
19
|
+
catch (_) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.isValidJson = isValidJson;
|
|
24
|
+
/**
|
|
25
|
+
* The string '2.0'.
|
|
26
|
+
*/
|
|
27
|
+
exports.jsonrpc2 = '2.0';
|
|
28
|
+
/**
|
|
29
|
+
* ATTN: Assumes that only one of the `result` and `error` properties is
|
|
30
|
+
* present on the `response`, as guaranteed by e.g.
|
|
31
|
+
* [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).
|
|
32
|
+
*
|
|
33
|
+
* Type guard to narrow a JsonRpcResponse object to a success (or failure).
|
|
34
|
+
*
|
|
35
|
+
* @param response - The response object to check.
|
|
36
|
+
* @returns Whether the response object is a success, i.e. has a `result`
|
|
37
|
+
* property.
|
|
38
|
+
*/
|
|
39
|
+
function isJsonRpcSuccess(response) {
|
|
40
|
+
return misc_1.hasProperty(response, 'result');
|
|
41
|
+
}
|
|
42
|
+
exports.isJsonRpcSuccess = isJsonRpcSuccess;
|
|
43
|
+
/**
|
|
44
|
+
* ATTN: Assumes that only one of the `result` and `error` properties is
|
|
45
|
+
* present on the `response`, as guaranteed by e.g.
|
|
46
|
+
* [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).
|
|
47
|
+
*
|
|
48
|
+
* Type guard to narrow a JsonRpcResponse object to a failure (or success).
|
|
49
|
+
*
|
|
50
|
+
* @param response - The response object to check.
|
|
51
|
+
* @returns Whether the response object is a failure, i.e. has an `error`
|
|
52
|
+
* property.
|
|
53
|
+
*/
|
|
54
|
+
function isJsonRpcFailure(response) {
|
|
55
|
+
return misc_1.hasProperty(response, 'error');
|
|
56
|
+
}
|
|
57
|
+
exports.isJsonRpcFailure = isJsonRpcFailure;
|
|
58
|
+
//# sourceMappingURL=json.js.map
|
package/dist/json.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":";;;;;;AAAA,sEAAwC;AACxC,iCAAqC;AAarC;;;;;GAKG;AACH,SAAgB,WAAW,CAAC,KAAc;IACxC,IAAI;QACF,OAAO,yBAAS,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAC5D;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAND,kCAMC;AAED;;GAEG;AACU,QAAA,QAAQ,GAAG,KAAc,CAAC;AA+EvC;;;;;;;;;;GAUG;AACH,SAAgB,gBAAgB,CAC9B,QAAiC;IAEjC,OAAO,kBAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;AACzC,CAAC;AAJD,4CAIC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,gBAAgB,CAC9B,QAAkC;IAElC,OAAO,kBAAW,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;AACxC,CAAC;AAJD,4CAIC","sourcesContent":["import deepEqual from 'fast-deep-equal';\nimport { hasProperty } from './misc';\n\n/**\n * Any JSON-compatible value.\n */\nexport type Json =\n | null\n | boolean\n | number\n | string\n | Json[]\n | { [prop: string]: Json };\n\n/**\n * Type guard for {@link Json}.\n *\n * @param value - The value to check.\n * @returns Whether the value is valid JSON.\n */\nexport function isValidJson(value: unknown): value is Json {\n try {\n return deepEqual(value, JSON.parse(JSON.stringify(value)));\n } catch (_) {\n return false;\n }\n}\n\n/**\n * The string '2.0'.\n */\nexport const jsonrpc2 = '2.0' as const;\n\n/**\n * A String specifying the version of the JSON-RPC protocol.\n * MUST be exactly \"2.0\".\n */\nexport type JsonRpcVersion2 = typeof jsonrpc2;\n\n/**\n * An identifier established by the Client that MUST contain a String, Number,\n * or NULL value if included. If it is not included it is assumed to be a\n * notification. The value SHOULD normally not be Null and Numbers SHOULD\n * NOT contain fractional parts.\n */\nexport type JsonRpcId = number | string | null;\n\n/**\n * A JSON-RPC error object.\n */\nexport type JsonRpcError = {\n code: number;\n message: string;\n data?: unknown;\n stack?: string;\n};\n\n/**\n * A JSON-RPC request object.\n *\n * @template Params - The type of the params.\n */\nexport type JsonRpcRequest<Params> = {\n id: JsonRpcId;\n jsonrpc: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\n/**\n * A JSON-RPC notification object.\n *\n * @template Params - The type of the params.\n */\nexport type JsonRpcNotification<Params> = {\n jsonrpc: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\n/**\n * A successful JSON-RPC response object.\n *\n * @template Result - The type of the result.\n */\nexport type JsonRpcSuccess<Result = unknown> = {\n id: JsonRpcId;\n jsonrpc: JsonRpcVersion2;\n result: Result;\n};\n\n/**\n * A failed JSON-RPC response object.\n */\nexport type JsonRpcFailure = {\n id: JsonRpcId;\n jsonrpc: JsonRpcVersion2;\n error: JsonRpcError;\n};\n\n/**\n * A JSON-RPC response object. Must be checked to determine whether it's a\n * success or failure.\n *\n * @template Result - The type of the result.\n */\nexport type JsonRpcResponse<Result = unknown> =\n | JsonRpcSuccess<Result>\n | JsonRpcFailure;\n\n/**\n * ATTN: Assumes that only one of the `result` and `error` properties is\n * present on the `response`, as guaranteed by e.g.\n * [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).\n *\n * Type guard to narrow a JsonRpcResponse object to a success (or failure).\n *\n * @param response - The response object to check.\n * @returns Whether the response object is a success, i.e. has a `result`\n * property.\n */\nexport function isJsonRpcSuccess<Result>(\n response: JsonRpcResponse<Result>,\n): response is JsonRpcSuccess<Result> {\n return hasProperty(response, 'result');\n}\n\n/**\n * ATTN: Assumes that only one of the `result` and `error` properties is\n * present on the `response`, as guaranteed by e.g.\n * [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).\n *\n * Type guard to narrow a JsonRpcResponse object to a failure (or success).\n *\n * @param response - The response object to check.\n * @returns Whether the response object is a failure, i.e. has an `error`\n * property.\n */\nexport function isJsonRpcFailure(\n response: JsonRpcResponse<unknown>,\n): response is JsonRpcFailure {\n return hasProperty(response, 'error');\n}\n"]}
|
package/dist/misc.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Makes every specified property of the specified object type mutable.
|
|
3
|
+
*
|
|
4
|
+
* @template ObjectValue - The object whose readonly properties to make mutable.
|
|
5
|
+
* @template TargetKey - The property key(s) to make mutable.
|
|
6
|
+
*/
|
|
7
|
+
export declare type Mutable<ObjectValue extends Record<string, unknown>, TargetKey extends keyof ObjectValue> = {
|
|
8
|
+
-readonly [Key in keyof Pick<ObjectValue, TargetKey>]: ObjectValue[Key];
|
|
9
|
+
} & {
|
|
10
|
+
[Key in keyof Omit<ObjectValue, TargetKey>]: ObjectValue[Key];
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Useful for representing some value that _might_ be present and / or complete.
|
|
14
|
+
*
|
|
15
|
+
* @template Value - The value that might be present or complete.
|
|
16
|
+
*/
|
|
17
|
+
export declare type PartialOrAbsent<Value> = Partial<Value> | null | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Like {@link Array}, but always non-empty.
|
|
20
|
+
*
|
|
21
|
+
* @template Element - The non-empty array member type.
|
|
22
|
+
*/
|
|
23
|
+
export declare type NonEmptyArray<Element> = [Element, ...Element[]];
|
|
24
|
+
/**
|
|
25
|
+
* A JavaScript object that is not `null`, a function, or an array. The object
|
|
26
|
+
* can still be an instance of a class.
|
|
27
|
+
*/
|
|
28
|
+
export declare type RuntimeObject = Record<number | string | symbol, unknown>;
|
|
29
|
+
/**
|
|
30
|
+
* A {@link NonEmptyArray} type guard.
|
|
31
|
+
*
|
|
32
|
+
* @template Element - The non-empty array member type.
|
|
33
|
+
* @param value - The value to check.
|
|
34
|
+
* @returns Whether the value is a non-empty array.
|
|
35
|
+
*/
|
|
36
|
+
export declare function isNonEmptyArray<Element>(value: Element[]): value is NonEmptyArray<Element>;
|
|
37
|
+
/**
|
|
38
|
+
* Type guard for "nullishness".
|
|
39
|
+
*
|
|
40
|
+
* @param value - Any value.
|
|
41
|
+
* @returns `true` if the value is null or undefined, `false` otherwise.
|
|
42
|
+
*/
|
|
43
|
+
export declare function isNullOrUndefined(value: unknown): value is null | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* A type guard for {@link RuntimeObject}.
|
|
46
|
+
*
|
|
47
|
+
* @param value - The value to check.
|
|
48
|
+
* @returns Whether the specified value has a runtime type of `object` and is
|
|
49
|
+
* neither `null` nor an `Array`.
|
|
50
|
+
*/
|
|
51
|
+
export declare function isObject(value: unknown): value is RuntimeObject;
|
|
52
|
+
/**
|
|
53
|
+
* An alias for {@link Object.hasOwnProperty}.
|
|
54
|
+
*
|
|
55
|
+
* @param object - The object to check.
|
|
56
|
+
* @param name - The property name to check for.
|
|
57
|
+
* @returns Whether the specified object has an own property with the specified
|
|
58
|
+
* name, regardless of whether it is enumerable or not.
|
|
59
|
+
*/
|
|
60
|
+
export declare const hasProperty: (object: RuntimeObject, name: string | number | symbol) => boolean;
|
package/dist/misc.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//
|
|
3
|
+
// Types
|
|
4
|
+
//
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.hasProperty = exports.isObject = exports.isNullOrUndefined = exports.isNonEmptyArray = void 0;
|
|
7
|
+
//
|
|
8
|
+
// Type Guards
|
|
9
|
+
//
|
|
10
|
+
/**
|
|
11
|
+
* A {@link NonEmptyArray} type guard.
|
|
12
|
+
*
|
|
13
|
+
* @template Element - The non-empty array member type.
|
|
14
|
+
* @param value - The value to check.
|
|
15
|
+
* @returns Whether the value is a non-empty array.
|
|
16
|
+
*/
|
|
17
|
+
function isNonEmptyArray(value) {
|
|
18
|
+
return Array.isArray(value) && value.length > 0;
|
|
19
|
+
}
|
|
20
|
+
exports.isNonEmptyArray = isNonEmptyArray;
|
|
21
|
+
/**
|
|
22
|
+
* Type guard for "nullishness".
|
|
23
|
+
*
|
|
24
|
+
* @param value - Any value.
|
|
25
|
+
* @returns `true` if the value is null or undefined, `false` otherwise.
|
|
26
|
+
*/
|
|
27
|
+
function isNullOrUndefined(value) {
|
|
28
|
+
return value === null || value === undefined;
|
|
29
|
+
}
|
|
30
|
+
exports.isNullOrUndefined = isNullOrUndefined;
|
|
31
|
+
/**
|
|
32
|
+
* A type guard for {@link RuntimeObject}.
|
|
33
|
+
*
|
|
34
|
+
* @param value - The value to check.
|
|
35
|
+
* @returns Whether the specified value has a runtime type of `object` and is
|
|
36
|
+
* neither `null` nor an `Array`.
|
|
37
|
+
*/
|
|
38
|
+
function isObject(value) {
|
|
39
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
40
|
+
}
|
|
41
|
+
exports.isObject = isObject;
|
|
42
|
+
//
|
|
43
|
+
// Other utility functions
|
|
44
|
+
//
|
|
45
|
+
/**
|
|
46
|
+
* An alias for {@link Object.hasOwnProperty}.
|
|
47
|
+
*
|
|
48
|
+
* @param object - The object to check.
|
|
49
|
+
* @param name - The property name to check for.
|
|
50
|
+
* @returns Whether the specified object has an own property with the specified
|
|
51
|
+
* name, regardless of whether it is enumerable or not.
|
|
52
|
+
*/
|
|
53
|
+
const hasProperty = (object, name) => Object.hasOwnProperty.call(object, name);
|
|
54
|
+
exports.hasProperty = hasProperty;
|
|
55
|
+
//# sourceMappingURL=misc.js.map
|
package/dist/misc.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"misc.js","sourceRoot":"","sources":["../src/misc.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,QAAQ;AACR,EAAE;;;AAsCF,EAAE;AACF,cAAc;AACd,EAAE;AAEF;;;;;;GAMG;AACH,SAAgB,eAAe,CAC7B,KAAgB;IAEhB,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;AAClD,CAAC;AAJD,0CAIC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAAC,KAAc;IAC9C,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;AAC/C,CAAC;AAFD,8CAEC;AAED;;;;;;GAMG;AACH,SAAgB,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAFD,4BAEC;AAED,EAAE;AACF,0BAA0B;AAC1B,EAAE;AAEF;;;;;;;GAOG;AACI,MAAM,WAAW,GAAG,CACzB,MAAqB,EACrB,IAA8B,EACrB,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAH1C,QAAA,WAAW,eAG+B","sourcesContent":["//\n// Types\n//\n\n/**\n * Makes every specified property of the specified object type mutable.\n *\n * @template ObjectValue - The object whose readonly properties to make mutable.\n * @template TargetKey - The property key(s) to make mutable.\n */\nexport type Mutable<\n ObjectValue extends Record<string, unknown>,\n TargetKey extends keyof ObjectValue\n> = {\n -readonly [Key in keyof Pick<ObjectValue, TargetKey>]: ObjectValue[Key];\n} &\n {\n [Key in keyof Omit<ObjectValue, TargetKey>]: ObjectValue[Key];\n };\n\n/**\n * Useful for representing some value that _might_ be present and / or complete.\n *\n * @template Value - The value that might be present or complete.\n */\nexport type PartialOrAbsent<Value> = Partial<Value> | null | undefined;\n\n/**\n * Like {@link Array}, but always non-empty.\n *\n * @template Element - The non-empty array member type.\n */\nexport type NonEmptyArray<Element> = [Element, ...Element[]];\n\n/**\n * A JavaScript object that is not `null`, a function, or an array. The object\n * can still be an instance of a class.\n */\nexport type RuntimeObject = Record<number | string | symbol, unknown>;\n\n//\n// Type Guards\n//\n\n/**\n * A {@link NonEmptyArray} type guard.\n *\n * @template Element - The non-empty array member type.\n * @param value - The value to check.\n * @returns Whether the value is a non-empty array.\n */\nexport function isNonEmptyArray<Element>(\n value: Element[],\n): value is NonEmptyArray<Element> {\n return Array.isArray(value) && value.length > 0;\n}\n\n/**\n * Type guard for \"nullishness\".\n *\n * @param value - Any value.\n * @returns `true` if the value is null or undefined, `false` otherwise.\n */\nexport function isNullOrUndefined(value: unknown): value is null | undefined {\n return value === null || value === undefined;\n}\n\n/**\n * A type guard for {@link RuntimeObject}.\n *\n * @param value - The value to check.\n * @returns Whether the specified value has a runtime type of `object` and is\n * neither `null` nor an `Array`.\n */\nexport function isObject(value: unknown): value is RuntimeObject {\n return Boolean(value) && typeof value === 'object' && !Array.isArray(value);\n}\n\n//\n// Other utility functions\n//\n\n/**\n * An alias for {@link Object.hasOwnProperty}.\n *\n * @param object - The object to check.\n * @param name - The property name to check for.\n * @returns Whether the specified object has an own property with the specified\n * name, regardless of whether it is enumerable or not.\n */\nexport const hasProperty = (\n object: RuntimeObject,\n name: string | number | symbol,\n): boolean => Object.hasOwnProperty.call(object, name);\n"]}
|
package/dist/time.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A millisecond.
|
|
3
|
+
*/
|
|
4
|
+
export declare const MILLISECOND = 1;
|
|
5
|
+
/**
|
|
6
|
+
* A second, in milliseconds.
|
|
7
|
+
*/
|
|
8
|
+
export declare const SECOND = 1000;
|
|
9
|
+
/**
|
|
10
|
+
* A minute, in milliseconds.
|
|
11
|
+
*/
|
|
12
|
+
export declare const MINUTE = 60000;
|
|
13
|
+
/**
|
|
14
|
+
* An hour, in milliseconds.
|
|
15
|
+
*/
|
|
16
|
+
export declare const HOUR = 3600000;
|
|
17
|
+
/**
|
|
18
|
+
* A day, in milliseconds.
|
|
19
|
+
*/
|
|
20
|
+
export declare const DAY = 86400000;
|
|
21
|
+
/**
|
|
22
|
+
* Gets the milliseconds since a particular Unix epoch timestamp.
|
|
23
|
+
*
|
|
24
|
+
* @param timestamp - A Unix millisecond timestamp.
|
|
25
|
+
* @returns The number of milliseconds elapsed since the specified timestamp.
|
|
26
|
+
*/
|
|
27
|
+
export declare function timeSince(timestamp: number): number;
|
package/dist/time.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.timeSince = exports.DAY = exports.HOUR = exports.MINUTE = exports.SECOND = exports.MILLISECOND = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A millisecond.
|
|
6
|
+
*/
|
|
7
|
+
exports.MILLISECOND = 1;
|
|
8
|
+
/**
|
|
9
|
+
* A second, in milliseconds.
|
|
10
|
+
*/
|
|
11
|
+
exports.SECOND = 1000; // MILLISECOND * 1000
|
|
12
|
+
/**
|
|
13
|
+
* A minute, in milliseconds.
|
|
14
|
+
*/
|
|
15
|
+
exports.MINUTE = 60000; // SECOND * 60
|
|
16
|
+
/**
|
|
17
|
+
* An hour, in milliseconds.
|
|
18
|
+
*/
|
|
19
|
+
exports.HOUR = 3600000; // MINUTE * 60
|
|
20
|
+
/**
|
|
21
|
+
* A day, in milliseconds.
|
|
22
|
+
*/
|
|
23
|
+
exports.DAY = 86400000; // HOUR * 24
|
|
24
|
+
/**
|
|
25
|
+
* Gets the milliseconds since a particular Unix epoch timestamp.
|
|
26
|
+
*
|
|
27
|
+
* @param timestamp - A Unix millisecond timestamp.
|
|
28
|
+
* @returns The number of milliseconds elapsed since the specified timestamp.
|
|
29
|
+
*/
|
|
30
|
+
function timeSince(timestamp) {
|
|
31
|
+
return Date.now() - timestamp;
|
|
32
|
+
}
|
|
33
|
+
exports.timeSince = timeSince;
|
|
34
|
+
//# sourceMappingURL=time.js.map
|
package/dist/time.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"time.js","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACU,QAAA,WAAW,GAAG,CAAC,CAAC;AAE7B;;GAEG;AACU,QAAA,MAAM,GAAG,IAAI,CAAC,CAAC,qBAAqB;AAEjD;;GAEG;AACU,QAAA,MAAM,GAAG,KAAM,CAAC,CAAC,cAAc;AAE5C;;GAEG;AACU,QAAA,IAAI,GAAG,OAAS,CAAC,CAAC,cAAc;AAE7C;;GAEG;AACU,QAAA,GAAG,GAAG,QAAU,CAAC,CAAC,YAAY;AAE3C;;;;;GAKG;AACH,SAAgB,SAAS,CAAC,SAAiB;IACzC,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC;AAChC,CAAC;AAFD,8BAEC","sourcesContent":["/**\n * A millisecond.\n */\nexport const MILLISECOND = 1;\n\n/**\n * A second, in milliseconds.\n */\nexport const SECOND = 1000; // MILLISECOND * 1000\n\n/**\n * A minute, in milliseconds.\n */\nexport const MINUTE = 60_000; // SECOND * 60\n\n/**\n * An hour, in milliseconds.\n */\nexport const HOUR = 3_600_000; // MINUTE * 60\n\n/**\n * A day, in milliseconds.\n */\nexport const DAY = 86_400_000; // HOUR * 24\n\n/**\n * Gets the milliseconds since a particular Unix epoch timestamp.\n *\n * @param timestamp - A Unix millisecond timestamp.\n * @returns The number of milliseconds elapsed since the specified timestamp.\n */\nexport function timeSince(timestamp: number): number {\n return Date.now() - timestamp;\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metamask/utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Various JavaScript/TypeScript utilities of wide relevance to the MetaMask codebase.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/MetaMask/utils.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "ISC",
|
|
10
|
+
"main": "dist/index.js",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist/"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"setup": "yarn install && yarn allow-scripts",
|
|
17
|
+
"test": "jest",
|
|
18
|
+
"test:watch": "jest --watch",
|
|
19
|
+
"prepublishOnly": "yarn build:clean && yarn lint && yarn test",
|
|
20
|
+
"lint:eslint": "eslint . --cache --ext js,ts",
|
|
21
|
+
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' --ignore-path .gitignore",
|
|
22
|
+
"lint": "yarn lint:eslint && yarn lint:misc --check",
|
|
23
|
+
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
|
|
24
|
+
"build:clean": "rimraf dist && yarn build",
|
|
25
|
+
"build": "tsc --project .",
|
|
26
|
+
"docs": "typedoc",
|
|
27
|
+
"docs:publish": "typedoc --cleanOutputDir false --gitRevision \"v$(jq -r .version < ./package.json)\""
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"fast-deep-equal": "^3.1.3"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@lavamoat/allow-scripts": "^1.0.5",
|
|
34
|
+
"@metamask/auto-changelog": "^2.3.0",
|
|
35
|
+
"@metamask/eslint-config": "^9.0.0",
|
|
36
|
+
"@metamask/eslint-config-jest": "^9.0.0",
|
|
37
|
+
"@metamask/eslint-config-nodejs": "^9.0.0",
|
|
38
|
+
"@metamask/eslint-config-typescript": "^9.0.1",
|
|
39
|
+
"@types/jest": "^26.0.13",
|
|
40
|
+
"@typescript-eslint/eslint-plugin": "^4.21.0",
|
|
41
|
+
"@typescript-eslint/parser": "^4.21.0",
|
|
42
|
+
"eslint": "^7.23.0",
|
|
43
|
+
"eslint-config-prettier": "^8.1.0",
|
|
44
|
+
"eslint-plugin-import": "^2.22.1",
|
|
45
|
+
"eslint-plugin-jest": "^24.3.4",
|
|
46
|
+
"eslint-plugin-jsdoc": "^36.1.0",
|
|
47
|
+
"eslint-plugin-node": "^11.1.0",
|
|
48
|
+
"eslint-plugin-prettier": "^3.3.1",
|
|
49
|
+
"jest": "^26.4.2",
|
|
50
|
+
"prettier": "^2.2.1",
|
|
51
|
+
"prettier-plugin-packagejson": "^2.2.11",
|
|
52
|
+
"rimraf": "^3.0.2",
|
|
53
|
+
"ts-jest": "^26.3.0",
|
|
54
|
+
"typedoc": "^0.22.15",
|
|
55
|
+
"typescript": "^4.2.4"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=14.0.0"
|
|
59
|
+
},
|
|
60
|
+
"publishConfig": {
|
|
61
|
+
"access": "public",
|
|
62
|
+
"registry": "https://registry.npmjs.org/"
|
|
63
|
+
},
|
|
64
|
+
"lavamoat": {
|
|
65
|
+
"allowScripts": {
|
|
66
|
+
"@lavamoat/preinstall-always-fail": false
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|