Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Types & utils improvements #1212

Merged
merged 20 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion __tests__/cairo1v2_typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {
types,
} from '../src';
import { hexToDecimalString } from '../src/utils/num';
import { encodeShortString, isString } from '../src/utils/shortString';
import { encodeShortString } from '../src/utils/shortString';
import { isString } from '../src/utils/typed';
import {
TEST_TX_VERSION,
compiledC1Account,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import componentSchemas from '../schemas/component.json';
import libSchemas from '../schemas/lib.json';
import providerSchemas from '../schemas/provider.json';
import rpcSchemas from '../schemas/rpc.json';
import { isBigInt } from '../../src/utils/num';
import { isBigInt } from '../../src/utils/typed';

const matcherSchemas = [accountSchemas, libSchemas, providerSchemas, rpcSchemas];
const starknetSchemas = [
Expand Down
15 changes: 15 additions & 0 deletions __tests__/utils/assert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assert from '../../src/utils/assert';

describe('assert', () => {
test('should throw an error if condition is not true', () => {
expect(() => assert(false)).toThrow(new Error('Assertion failure'));
});

test('should throw an error with a specific message', () => {
expect(() => assert(false, 'Error message')).toThrow(new Error('Error message'));
});

test('should not throw an error if condition is true', () => {
expect(() => assert(true)).toBeTruthy();
});
});
53 changes: 0 additions & 53 deletions __tests__/utils/num.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
isHex,
toBigInt,
isBigInt,
toHex,
hexToDecimalString,
cleanHex,
Expand All @@ -15,8 +14,6 @@ import {
toCairoBool,
hexToBytes,
addPercent,
isNumber,
isBoolean,
} from '../../src/utils/num';
import { num } from '../../src';

Expand Down Expand Up @@ -49,23 +46,6 @@ describe('toBigInt', () => {
});
});

describe('isBigInt', () => {
test('should return true for big integers', () => {
expect(isBigInt(BigInt(10))).toBe(true);
expect(isBigInt(BigInt('9007199254740991'))).toBe(true);
});

test('should return false for non-big integers', () => {
expect(isBigInt(10)).toBe(false);
expect(isBigInt('10')).toBe(false);
expect(isBigInt(undefined)).toBe(false);
expect(isBigInt(null)).toBe(false);
expect(isBigInt({})).toBe(false);
expect(isBigInt([])).toBe(false);
expect(isBigInt(true)).toBe(false);
});
});

describe('toHex', () => {
test('should properly convert to hex-string', () => {
expect(toHex(100)).toBe('0x64');
Expand Down Expand Up @@ -177,39 +157,6 @@ describe('addPercent', () => {
});
});

describe('isNumber', () => {
test('should correctly determine if value is a number', () => {
expect(isNumber(0)).toBe(true);
expect(isNumber(123)).toBe(true);
expect(isNumber(-123)).toBe(true);

expect(isNumber(123n)).toBe(false);
expect(isNumber('')).toBe(false);
expect(isNumber('123')).toBe(false);
expect(isNumber(true)).toBe(false);
expect(isNumber(false)).toBe(false);
expect(isNumber(null)).toBe(false);
expect(isBoolean([])).toBe(false);
expect(isBoolean({})).toBe(false);
});
});

describe('isBoolean', () => {
test('should correctly determine if value is a boolean', () => {
expect(isBoolean(true)).toBe(true);
expect(isBoolean(false)).toBe(true);

expect(isBoolean(0)).toBe(false);
expect(isBoolean(1)).toBe(false);
expect(isBoolean('')).toBe(false);
expect(isBoolean('true')).toBe(false);
expect(isBoolean('false')).toBe(false);
expect(isBoolean(null)).toBe(false);
expect(isBoolean([])).toBe(false);
expect(isBoolean({})).toBe(false);
});
});

describe('stringToSha256ToArrayBuff4', () => {
test('should correctly hash&encode an utf8 string', () => {
const buff = num.stringToSha256ToArrayBuff4('LedgerW');
Expand Down
17 changes: 0 additions & 17 deletions __tests__/utils/shortString.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
encodeShortString,
isDecimalString,
isShortString,
isString,
} from '../../src/utils/shortString';

describe('shortString', () => {
Expand Down Expand Up @@ -110,22 +109,6 @@ describe('shortString', () => {
).toBe('');
});

describe('isString', () => {
test('should return true for strings', () => {
expect(isString('test')).toBe(true);
expect(isString('')).toBe(true);
});

test('should return false for non-string values', () => {
expect(isString(10)).toBe(false);
expect(isString({})).toBe(false);
expect(isString(null)).toBe(false);
expect(isString(undefined)).toBe(false);
expect(isString([])).toBe(false);
expect(isString(true)).toBe(false);
});
});

describe('isShortString', () => {
test('should return true for short strings', () => {
const shortStr = '1234567890123456789012345678901';
Expand Down
100 changes: 100 additions & 0 deletions __tests__/utils/typed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {
isUndefined,
isBigInt,
isBoolean,
isNumber,
isString,
isObject,
} from '../../src/utils/typed';

describe('isUndefined', () => {
test('should return true if value is undefined', () => {
expect(isUndefined(undefined)).toBe(true);
});

test('should return false if value is not undefined', () => {
const value = 'existing value';
expect(isUndefined(value)).toBe(false);
});
});

describe('isNumber', () => {
test('should correctly determine if value is a number', () => {
expect(isNumber(0)).toBe(true);
expect(isNumber(123)).toBe(true);
expect(isNumber(-123)).toBe(true);

expect(isNumber(123n)).toBe(false);
expect(isNumber('')).toBe(false);
expect(isNumber('123')).toBe(false);
expect(isNumber(true)).toBe(false);
expect(isNumber(false)).toBe(false);
expect(isNumber(null)).toBe(false);
expect(isBoolean([])).toBe(false);
expect(isBoolean({})).toBe(false);
});
});

describe('isBoolean', () => {
test('should correctly determine if value is a boolean', () => {
expect(isBoolean(true)).toBe(true);
expect(isBoolean(false)).toBe(true);

expect(isBoolean(0)).toBe(false);
expect(isBoolean(1)).toBe(false);
expect(isBoolean('')).toBe(false);
expect(isBoolean('true')).toBe(false);
expect(isBoolean('false')).toBe(false);
expect(isBoolean(null)).toBe(false);
expect(isBoolean([])).toBe(false);
expect(isBoolean({})).toBe(false);
});
});

describe('isBigInt', () => {
test('should return true for big integers', () => {
expect(isBigInt(BigInt(10))).toBe(true);
expect(isBigInt(BigInt('9007199254740991'))).toBe(true);
});

test('should return false for non-big integers', () => {
expect(isBigInt(10)).toBe(false);
expect(isBigInt('10')).toBe(false);
expect(isBigInt(undefined)).toBe(false);
expect(isBigInt(null)).toBe(false);
expect(isBigInt({})).toBe(false);
expect(isBigInt([])).toBe(false);
expect(isBigInt(true)).toBe(false);
});
});

describe('isString', () => {
test('should return true for strings', () => {
expect(isString('test')).toBe(true);
expect(isString('')).toBe(true);
});

test('should return false for non-string values', () => {
expect(isString(10)).toBe(false);
expect(isString({})).toBe(false);
expect(isString(null)).toBe(false);
expect(isString(undefined)).toBe(false);
expect(isString([])).toBe(false);
expect(isString(true)).toBe(false);
});
});

describe('isObject', () => {
test('should return true if value is object', () => {
expect(isObject({ test: 'test' })).toEqual(true);
expect(isObject({})).toEqual(true);
});

test('should return false if value is not object', () => {
expect(isObject(10)).toBe(false);
expect(isObject(null)).toBe(false);
expect(isObject(undefined)).toBe(false);
expect(isObject([])).toBe(false);
expect(isObject(true)).toBe(false);
});
});
17 changes: 1 addition & 16 deletions package-lock.json

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

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"@types/jest": "^29.5.0",
"@types/jest-json-schema": "^6.1.1",
"@types/pako": "^2.0.0",
"@types/url-join": "^4.0.1",
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"ajv": "^8.12.0",
Expand Down Expand Up @@ -105,8 +104,7 @@
"lossless-json": "^4.0.1",
"pako": "^2.0.4",
"starknet-types-07": "npm:@starknet-io/types-js@^0.7.7",
"ts-mixer": "^6.0.3",
"url-join": "^4.0.1"
"ts-mixer": "^6.0.3"
},
"lint-staged": {
"*.ts": "eslint --cache --fix",
Expand Down
Loading