aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils
diff options
context:
space:
mode:
Diffstat (limited to 'packages/utils')
-rw-r--r--packages/utils/CHANGELOG.md6
-rw-r--r--packages/utils/README.md43
-rw-r--r--packages/utils/package.json63
-rw-r--r--packages/utils/scripts/postpublish.js14
-rw-r--r--packages/utils/src/address_utils.ts36
-rw-r--r--packages/utils/src/class_utils.ts18
-rw-r--r--packages/utils/src/configured_bignumber.ts9
-rw-r--r--packages/utils/src/index.ts6
-rw-r--r--packages/utils/src/interval_utils.ts37
-rw-r--r--packages/utils/src/promisify.ts11
-rw-r--r--packages/utils/tsconfig.json18
-rw-r--r--packages/utils/tslint.json4
12 files changed, 206 insertions, 59 deletions
diff --git a/packages/utils/CHANGELOG.md b/packages/utils/CHANGELOG.md
new file mode 100644
index 000000000..efee30dd1
--- /dev/null
+++ b/packages/utils/CHANGELOG.md
@@ -0,0 +1,6 @@
+# CHANGELOG
+
+## v0.2.0 - _January 17, 2018_
+
+* Add `onError` parameter to `intervalUtils.setAsyncExcludingInterval` (#312)
+* Add `intervalUtils.setInterval` (#312)
diff --git a/packages/utils/README.md b/packages/utils/README.md
index 5191e0350..d6cacfa11 100644
--- a/packages/utils/README.md
+++ b/packages/utils/README.md
@@ -1,10 +1,47 @@
-utils
-------
+## @0xproject/utils
Utils to be shared across 0x projects and packages
-## Install
+## Installation
```bash
yarn add @0xproject/utils
```
+
+## Usage
+
+```javascript
+import { addressUtils, bigNumberConfigs, classUtils, intervalUtils, promisify } from '@0xproject/utils';
+```
+
+## Contributing
+
+We strongly recommend that the community help us make improvements and determine the future direction of the protocol. To report bugs within this package, please create an issue in this repository.
+
+Please read our [contribution guidelines](../../CONTRIBUTING.md) before getting started.
+
+### Install Dependencies
+
+If you don't have yarn workspaces enabled (Yarn < v1.0) - enable them:
+
+```bash
+yarn config set workspaces-experimental true
+```
+
+Then install dependencies
+
+```bash
+yarn install
+```
+
+### Build
+
+```bash
+yarn build
+```
+
+### Lint
+
+```bash
+yarn lint
+```
diff --git a/packages/utils/package.json b/packages/utils/package.json
index f49a95f28..b1f8b3792 100644
--- a/packages/utils/package.json
+++ b/packages/utils/package.json
@@ -1,33 +1,34 @@
{
- "name": "@0xproject/utils",
- "version": "0.0.1",
- "description": "0x TS utils",
- "main": "lib/index.js",
- "types": "lib/index.d.ts",
- "scripts": {
- "build": "tsc",
- "clean": "shx rm -rf lib",
- "lint": "tslint --project . 'src/**/*.ts'"
- },
- "license": "Apache-2.0",
- "repository": {
- "type": "git",
- "url": "https://github.com/0xProject/0x.js.git"
- },
- "bugs": {
- "url": "https://github.com/0xProject/0x.js/issues"
- },
- "homepage": "https://github.com/0xProject/0x.js/packages/utils/README.md",
- "devDependencies": {
- "@0xproject/tslint-config": "^0.2.0",
- "@types/lodash": "^4.14.86",
- "npm-run-all": "^4.1.2",
- "shx": "^0.2.2",
- "tslint": "5.8.0",
- "typescript": "~2.6.1"
- },
- "dependencies": {
- "bignumber.js": "~4.1.0",
- "lodash": "^4.17.4"
- }
+ "name": "@0xproject/utils",
+ "version": "0.2.1",
+ "description": "0x TS utils",
+ "main": "lib/index.js",
+ "types": "lib/index.d.ts",
+ "scripts": {
+ "build": "tsc",
+ "clean": "shx rm -rf lib",
+ "lint": "tslint --project . 'src/**/*.ts'"
+ },
+ "license": "Apache-2.0",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/0xProject/0x.js.git"
+ },
+ "bugs": {
+ "url": "https://github.com/0xProject/0x.js/issues"
+ },
+ "homepage": "https://github.com/0xProject/0x.js/packages/utils/README.md",
+ "devDependencies": {
+ "@0xproject/tslint-config": "^0.4.3",
+ "@types/lodash": "^4.14.86",
+ "npm-run-all": "^4.1.2",
+ "shx": "^0.2.2",
+ "tslint": "5.8.0",
+ "typescript": "~2.6.1"
+ },
+ "dependencies": {
+ "bignumber.js": "~4.1.0",
+ "js-sha3": "^0.7.0",
+ "lodash": "^4.17.4"
+ }
}
diff --git a/packages/utils/scripts/postpublish.js b/packages/utils/scripts/postpublish.js
new file mode 100644
index 000000000..7fa452b08
--- /dev/null
+++ b/packages/utils/scripts/postpublish.js
@@ -0,0 +1,14 @@
+const postpublish_utils = require('../../../scripts/postpublish_utils');
+const packageJSON = require('../package.json');
+
+const subPackageName = packageJSON.name;
+
+postpublish_utils.getLatestTagAndVersionAsync(subPackageName)
+ .then(function(result) {
+ const releaseName = postpublish_utils.getReleaseName(subPackageName, result.version);
+ const assets = [];
+ return postpublish_utils.publishReleaseNotes(result.tag, releaseName, assets);
+ })
+ .catch (function(err) {
+ throw err;
+ });
diff --git a/packages/utils/src/address_utils.ts b/packages/utils/src/address_utils.ts
new file mode 100644
index 000000000..f94985441
--- /dev/null
+++ b/packages/utils/src/address_utils.ts
@@ -0,0 +1,36 @@
+import * as jsSHA3 from 'js-sha3';
+
+const BASIC_ADDRESS_REGEX = /^(0x)?[0-9a-f]{40}$/i;
+const SAME_CASE_ADDRESS_REGEX = /^(0x)?([0-9a-f]{40}|[0-9A-F]{40})$/;
+
+export const addressUtils = {
+ isChecksumAddress(address: string): boolean {
+ // Check each case
+ const unprefixedAddress = address.replace('0x', '');
+ const addressHash = jsSHA3.keccak256(unprefixedAddress.toLowerCase());
+
+ for (let i = 0; i < 40; i++) {
+ // The nth letter should be uppercase if the nth digit of casemap is 1
+ if (
+ (parseInt(addressHash[i], 16) > 7 && unprefixedAddress[i].toUpperCase() !== unprefixedAddress[i]) ||
+ (parseInt(addressHash[i], 16) <= 7 && unprefixedAddress[i].toLowerCase() !== unprefixedAddress[i])
+ ) {
+ return false;
+ }
+ }
+ return true;
+ },
+ isAddress(address: string): boolean {
+ if (!BASIC_ADDRESS_REGEX.test(address)) {
+ // Check if it has the basic requirements of an address
+ return false;
+ } else if (SAME_CASE_ADDRESS_REGEX.test(address)) {
+ // If it's all small caps or all all caps, return true
+ return true;
+ } else {
+ // Otherwise check each case
+ const isValidChecksummedAddress = addressUtils.isChecksumAddress(address);
+ return isValidChecksummedAddress;
+ }
+ },
+};
diff --git a/packages/utils/src/class_utils.ts b/packages/utils/src/class_utils.ts
new file mode 100644
index 000000000..04e60ee57
--- /dev/null
+++ b/packages/utils/src/class_utils.ts
@@ -0,0 +1,18 @@
+import * as _ from 'lodash';
+
+export const classUtils = {
+ // This is useful for classes that have nested methods. Nested methods don't get bound out of the box.
+ bindAll(self: any, exclude: string[] = ['contructor'], thisArg?: any): void {
+ for (const key of Object.getOwnPropertyNames(self)) {
+ const val = self[key];
+ if (!_.includes(exclude, key)) {
+ if (_.isFunction(val)) {
+ self[key] = val.bind(thisArg || self);
+ } else if (_.isObject(val)) {
+ classUtils.bindAll(val, exclude, self);
+ }
+ }
+ }
+ return self;
+ },
+};
diff --git a/packages/utils/src/configured_bignumber.ts b/packages/utils/src/configured_bignumber.ts
new file mode 100644
index 000000000..e44c062c2
--- /dev/null
+++ b/packages/utils/src/configured_bignumber.ts
@@ -0,0 +1,9 @@
+import { BigNumber } from 'bignumber.js';
+
+// By default BigNumber's `toString` method converts to exponential notation if the value has
+// more then 20 digits. We want to avoid this behavior, so we set EXPONENTIAL_AT to a high number
+BigNumber.config({
+ EXPONENTIAL_AT: 1000,
+});
+
+export { BigNumber };
diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts
index a61f04ad2..2768e49ab 100644
--- a/packages/utils/src/index.ts
+++ b/packages/utils/src/index.ts
@@ -1 +1,5 @@
-export {promisify} from './promisify';
+export { promisify } from './promisify';
+export { addressUtils } from './address_utils';
+export { classUtils } from './class_utils';
+export { intervalUtils } from './interval_utils';
+export { BigNumber } from './configured_bignumber';
diff --git a/packages/utils/src/interval_utils.ts b/packages/utils/src/interval_utils.ts
new file mode 100644
index 000000000..ebecc7015
--- /dev/null
+++ b/packages/utils/src/interval_utils.ts
@@ -0,0 +1,37 @@
+import * as _ from 'lodash';
+
+export const intervalUtils = {
+ setAsyncExcludingInterval(fn: () => Promise<void>, intervalMs: number, onError: (err: Error) => void) {
+ let locked = false;
+ const intervalId = setInterval(async () => {
+ if (locked) {
+ return;
+ } else {
+ locked = true;
+ try {
+ await fn();
+ } catch (err) {
+ onError(err);
+ }
+ locked = false;
+ }
+ }, intervalMs);
+ return intervalId;
+ },
+ clearAsyncExcludingInterval(intervalId: NodeJS.Timer): void {
+ clearInterval(intervalId);
+ },
+ setInterval(fn: () => void, intervalMs: number, onError: (err: Error) => void) {
+ const intervalId = setInterval(() => {
+ try {
+ fn();
+ } catch (err) {
+ onError(err);
+ }
+ }, intervalMs);
+ return intervalId;
+ },
+ clearInterval(intervalId: NodeJS.Timer): void {
+ clearInterval(intervalId);
+ },
+};
diff --git a/packages/utils/src/promisify.ts b/packages/utils/src/promisify.ts
index c114cf32f..29d626b61 100644
--- a/packages/utils/src/promisify.ts
+++ b/packages/utils/src/promisify.ts
@@ -5,16 +5,11 @@ import * as _ from 'lodash';
* Promisify provides a default callback of the form (error, result) and rejects when `error` is not null. You can also
* supply thisArg object as the second argument which will be passed to `apply`.
*/
-export function promisify<T>(
- originalFn: (
- ...args: any[],
- // HACK: This can't be properly typed without variadic kinds https://github.com/Microsoft/TypeScript/issues/5453
- ) => void,
- thisArg?: any,
-): (...callArgs: any[]) => Promise<T> {
+// HACK: This can't be properly typed without variadic kinds https://github.com/Microsoft/TypeScript/issues/5453
+export function promisify<T>(originalFn: (...args: any[]) => void, thisArg?: any): (...callArgs: any[]) => Promise<T> {
const promisifiedFunction = async (...callArgs: any[]): Promise<T> => {
return new Promise<T>((resolve, reject) => {
- const callback = (err: Error|null, data?: T) => {
+ const callback = (err: Error | null, data?: T) => {
_.isNull(err) ? resolve(data) : reject(err);
};
originalFn.apply(thisArg, [...callArgs, callback]);
diff --git a/packages/utils/tsconfig.json b/packages/utils/tsconfig.json
index 111df6d3b..3d967d05f 100644
--- a/packages/utils/tsconfig.json
+++ b/packages/utils/tsconfig.json
@@ -1,15 +1,7 @@
{
- "compilerOptions": {
- "module": "commonjs",
- "target": "es5",
- "lib": [ "es2017", "dom"],
- "outDir": "lib",
- "sourceMap": true,
- "declaration": true,
- "noImplicitAny": true,
- "strictNullChecks": true
- },
- "include": [
- "./src/**/*"
- ]
+ "extends": "../../tsconfig",
+ "compilerOptions": {
+ "outDir": "lib"
+ },
+ "include": ["./src/**/*", "../../node_modules/web3-typescript-typings/index.d.ts"]
}
diff --git a/packages/utils/tslint.json b/packages/utils/tslint.json
index a07795151..ffaefe83a 100644
--- a/packages/utils/tslint.json
+++ b/packages/utils/tslint.json
@@ -1,5 +1,3 @@
{
- "extends": [
- "@0xproject/tslint-config"
- ]
+ "extends": ["@0xproject/tslint-config"]
}