Add checks for type declarations (#2448)

* Add overloads for Matrix add and subtract

* Add check for types/index.ts

* Fix type errors in types/index.ts

* Fix a couple execution errors

* Run test:types as part of test:all

* Fix remaining errors

* Replace types/index.ts comments with asserts

* Add tests for narrowed type inference

* Add dual-purpose comment at top of types/index.ts

* Update AUTHORS

* Use Glen's alternate test:types suggestion
This commit is contained in:
Sam Estep 2022-03-08 09:59:35 -05:00 committed by GitHub
parent 9ac5a67c71
commit bb9eb4e0ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 369 additions and 69 deletions

View File

@ -181,6 +181,7 @@ Glen Whitney <glen@studioinfinity.org>
Divya Yeruva <divyareddy.y019@gmail.com>
Yeruva <Divya.Yeruva@pepsico.com>
Eternal-Rise <my_dawn@ukr.net>
Sam Estep <sam@samestep.com>
yifanwww <yifanw1101@gmail.com>
NattapongSiri <38780353+NattapongSiri@users.noreply.github.com>
Hjortur Jonasson <hjorturjonasson@gmail.com>

274
package-lock.json generated
View File

@ -28,6 +28,7 @@
"@babel/plugin-transform-runtime": "7.17.0",
"@babel/preset-env": "7.16.11",
"@babel/register": "7.17.0",
"@types/assert": "^1.5.6",
"assert": "2.0.0",
"babel-loader": "8.2.3",
"benchmark": "2.1.4",
@ -63,6 +64,7 @@
"process": "0.11.10",
"standard": "16.0.4",
"sylvester": "0.0.21",
"ts-node": "10.5.0",
"typescript": "4.5.5",
"webpack": "4.46.0",
"zeros": "1.0.0"
@ -1737,6 +1739,27 @@
"node": ">=6.9.0"
}
},
"node_modules/@cspotcode/source-map-consumer": {
"version": "0.8.0",
"resolved": "https://registry.npmjs.org/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz",
"integrity": "sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==",
"dev": true,
"engines": {
"node": ">= 12"
}
},
"node_modules/@cspotcode/source-map-support": {
"version": "0.7.0",
"resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.7.0.tgz",
"integrity": "sha512-X4xqRHqN8ACt2aHVe51OxeA2HjbcL4MqFqXkrmQszJ1NOUuUu5u6Vqx/0lZSVNku7velL5FC/s5uEAj1lsBMhA==",
"dev": true,
"dependencies": {
"@cspotcode/source-map-consumer": "0.8.0"
},
"engines": {
"node": ">=12"
}
},
"node_modules/@definitelytyped/header-parser": {
"version": "0.0.85",
"resolved": "https://registry.npmjs.org/@definitelytyped/header-parser/-/header-parser-0.0.85.tgz",
@ -1978,6 +2001,36 @@
"node": ">= 6"
}
},
"node_modules/@tsconfig/node10": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.8.tgz",
"integrity": "sha512-6XFfSQmMgq0CFLY1MslA/CPUfhIL919M1rMsa5lP2P097N2Wd1sSX0tx1u4olM16fLNhtHZpRhedZJphNJqmZg==",
"dev": true
},
"node_modules/@tsconfig/node12": {
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.9.tgz",
"integrity": "sha512-/yBMcem+fbvhSREH+s14YJi18sp7J9jpuhYByADT2rypfajMZZN4WQ6zBGgBKp53NKmqI36wFYDb3yaMPurITw==",
"dev": true
},
"node_modules/@tsconfig/node14": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.1.tgz",
"integrity": "sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==",
"dev": true
},
"node_modules/@tsconfig/node16": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.2.tgz",
"integrity": "sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==",
"dev": true
},
"node_modules/@types/assert": {
"version": "1.5.6",
"resolved": "https://registry.npmjs.org/@types/assert/-/assert-1.5.6.tgz",
"integrity": "sha512-Y7gDJiIqb9qKUHfBQYOWGngUpLORtirAVPuj/CWJrU2C6ZM4/y3XLwuwfGMF8s7QzW746LQZx23m0+1FSgjfug==",
"dev": true
},
"node_modules/@types/component-emitter": {
"version": "1.2.11",
"resolved": "https://registry.npmjs.org/@types/component-emitter/-/component-emitter-1.2.11.tgz",
@ -2253,6 +2306,15 @@
"acorn": "^6.0.0 || ^7.0.0 || ^8.0.0"
}
},
"node_modules/acorn-walk": {
"version": "8.2.0",
"resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.2.0.tgz",
"integrity": "sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==",
"dev": true,
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/agent-base": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz",
@ -2566,6 +2628,12 @@
"readable-stream": "^2.0.6"
}
},
"node_modules/arg": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
"integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
"dev": true
},
"node_modules/argparse": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
@ -4609,6 +4677,12 @@
"sha.js": "^2.4.8"
}
},
"node_modules/create-require": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz",
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
"dev": true
},
"node_modules/cross-spawn": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
@ -10348,6 +10422,12 @@
"node": ">=6"
}
},
"node_modules/make-error": {
"version": "1.3.6",
"resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
"integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
"dev": true
},
"node_modules/make-iterator": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/make-iterator/-/make-iterator-1.0.1.tgz",
@ -15251,6 +15331,69 @@
"node": ">=0.8"
}
},
"node_modules/ts-node": {
"version": "10.5.0",
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.5.0.tgz",
"integrity": "sha512-6kEJKwVxAJ35W4akuiysfKwKmjkbYxwQMTBaAxo9KKAx/Yd26mPUyhGz3ji+EsJoAgrLqVsYHNuuYwQe22lbtw==",
"dev": true,
"dependencies": {
"@cspotcode/source-map-support": "0.7.0",
"@tsconfig/node10": "^1.0.7",
"@tsconfig/node12": "^1.0.7",
"@tsconfig/node14": "^1.0.0",
"@tsconfig/node16": "^1.0.2",
"acorn": "^8.4.1",
"acorn-walk": "^8.1.1",
"arg": "^4.1.0",
"create-require": "^1.1.0",
"diff": "^4.0.1",
"make-error": "^1.1.1",
"v8-compile-cache-lib": "^3.0.0",
"yn": "3.1.1"
},
"bin": {
"ts-node": "dist/bin.js",
"ts-node-cwd": "dist/bin-cwd.js",
"ts-node-script": "dist/bin-script.js",
"ts-node-transpile-only": "dist/bin-transpile.js",
"ts-script": "dist/bin-script-deprecated.js"
},
"peerDependencies": {
"@swc/core": ">=1.2.50",
"@swc/wasm": ">=1.2.50",
"@types/node": "*",
"typescript": ">=2.7"
},
"peerDependenciesMeta": {
"@swc/core": {
"optional": true
},
"@swc/wasm": {
"optional": true
}
}
},
"node_modules/ts-node/node_modules/acorn": {
"version": "8.7.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.7.0.tgz",
"integrity": "sha512-V/LGr1APy+PXIwKebEWrkZPwoeoF+w1jiOBUmuxuiUIaOHtob8Qc9BTrYo7VuI5fR8tqsy+buA2WFooR5olqvQ==",
"dev": true,
"bin": {
"acorn": "bin/acorn"
},
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/ts-node/node_modules/diff": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
"integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
"dev": true,
"engines": {
"node": ">=0.3.1"
}
},
"node_modules/tsconfig-paths": {
"version": "3.11.0",
"resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.11.0.tgz",
@ -15800,6 +15943,12 @@
"integrity": "sha512-gTpR5XQNKFwOd4clxfnhaqvfqMpqEwr4tOtCyz4MtYZX2JYhfr1JvBFKdS+7K/9rfpZR3VLX+YWBbKoxCgS43Q==",
"dev": true
},
"node_modules/v8-compile-cache-lib": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.0.tgz",
"integrity": "sha512-mpSYqfsFvASnSn5qMiwrr4VKfumbPyONLCOPmsR3A6pTY/r0+tSaVbgPWSAIuzbk3lCTa+FForeTiO+wBQGkjA==",
"dev": true
},
"node_modules/v8flags": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/v8flags/-/v8flags-3.2.0.tgz",
@ -16625,6 +16774,15 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/yn": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
"integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
"dev": true,
"engines": {
"node": ">=6"
}
},
"node_modules/yocto-queue": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz",
@ -17813,6 +17971,21 @@
"to-fast-properties": "^2.0.0"
}
},
"@cspotcode/source-map-consumer": {
"version": "0.8.0",
"resolved": "https://registry.npmjs.org/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz",
"integrity": "sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==",
"dev": true
},
"@cspotcode/source-map-support": {
"version": "0.7.0",
"resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.7.0.tgz",
"integrity": "sha512-X4xqRHqN8ACt2aHVe51OxeA2HjbcL4MqFqXkrmQszJ1NOUuUu5u6Vqx/0lZSVNku7velL5FC/s5uEAj1lsBMhA==",
"dev": true,
"requires": {
"@cspotcode/source-map-consumer": "0.8.0"
}
},
"@definitelytyped/header-parser": {
"version": "0.0.85",
"resolved": "https://registry.npmjs.org/@definitelytyped/header-parser/-/header-parser-0.0.85.tgz",
@ -18009,6 +18182,36 @@
"integrity": "sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==",
"dev": true
},
"@tsconfig/node10": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.8.tgz",
"integrity": "sha512-6XFfSQmMgq0CFLY1MslA/CPUfhIL919M1rMsa5lP2P097N2Wd1sSX0tx1u4olM16fLNhtHZpRhedZJphNJqmZg==",
"dev": true
},
"@tsconfig/node12": {
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.9.tgz",
"integrity": "sha512-/yBMcem+fbvhSREH+s14YJi18sp7J9jpuhYByADT2rypfajMZZN4WQ6zBGgBKp53NKmqI36wFYDb3yaMPurITw==",
"dev": true
},
"@tsconfig/node14": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.1.tgz",
"integrity": "sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==",
"dev": true
},
"@tsconfig/node16": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.2.tgz",
"integrity": "sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==",
"dev": true
},
"@types/assert": {
"version": "1.5.6",
"resolved": "https://registry.npmjs.org/@types/assert/-/assert-1.5.6.tgz",
"integrity": "sha512-Y7gDJiIqb9qKUHfBQYOWGngUpLORtirAVPuj/CWJrU2C6ZM4/y3XLwuwfGMF8s7QzW746LQZx23m0+1FSgjfug==",
"dev": true
},
"@types/component-emitter": {
"version": "1.2.11",
"resolved": "https://registry.npmjs.org/@types/component-emitter/-/component-emitter-1.2.11.tgz",
@ -18273,6 +18476,12 @@
"dev": true,
"requires": {}
},
"acorn-walk": {
"version": "8.2.0",
"resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.2.0.tgz",
"integrity": "sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==",
"dev": true
},
"agent-base": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz",
@ -18529,6 +18738,12 @@
"readable-stream": "^2.0.6"
}
},
"arg": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
"integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
"dev": true
},
"argparse": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
@ -20190,6 +20405,12 @@
"sha.js": "^2.4.8"
}
},
"create-require": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz",
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
"dev": true
},
"cross-spawn": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
@ -24662,6 +24883,12 @@
"semver": "^5.6.0"
}
},
"make-error": {
"version": "1.3.6",
"resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
"integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
"dev": true
},
"make-iterator": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/make-iterator/-/make-iterator-1.0.1.tgz",
@ -28563,6 +28790,41 @@
"punycode": "^2.1.1"
}
},
"ts-node": {
"version": "10.5.0",
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.5.0.tgz",
"integrity": "sha512-6kEJKwVxAJ35W4akuiysfKwKmjkbYxwQMTBaAxo9KKAx/Yd26mPUyhGz3ji+EsJoAgrLqVsYHNuuYwQe22lbtw==",
"dev": true,
"requires": {
"@cspotcode/source-map-support": "0.7.0",
"@tsconfig/node10": "^1.0.7",
"@tsconfig/node12": "^1.0.7",
"@tsconfig/node14": "^1.0.0",
"@tsconfig/node16": "^1.0.2",
"acorn": "^8.4.1",
"acorn-walk": "^8.1.1",
"arg": "^4.1.0",
"create-require": "^1.1.0",
"diff": "^4.0.1",
"make-error": "^1.1.1",
"v8-compile-cache-lib": "^3.0.0",
"yn": "3.1.1"
},
"dependencies": {
"acorn": {
"version": "8.7.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.7.0.tgz",
"integrity": "sha512-V/LGr1APy+PXIwKebEWrkZPwoeoF+w1jiOBUmuxuiUIaOHtob8Qc9BTrYo7VuI5fR8tqsy+buA2WFooR5olqvQ==",
"dev": true
},
"diff": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
"integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
"dev": true
}
}
},
"tsconfig-paths": {
"version": "3.11.0",
"resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.11.0.tgz",
@ -29000,6 +29262,12 @@
"integrity": "sha512-gTpR5XQNKFwOd4clxfnhaqvfqMpqEwr4tOtCyz4MtYZX2JYhfr1JvBFKdS+7K/9rfpZR3VLX+YWBbKoxCgS43Q==",
"dev": true
},
"v8-compile-cache-lib": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.0.tgz",
"integrity": "sha512-mpSYqfsFvASnSn5qMiwrr4VKfumbPyONLCOPmsR3A6pTY/r0+tSaVbgPWSAIuzbk3lCTa+FForeTiO+wBQGkjA==",
"dev": true
},
"v8flags": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/v8flags/-/v8flags-3.2.0.tgz",
@ -29655,6 +29923,12 @@
}
}
},
"yn": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
"integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
"dev": true
},
"yocto-queue": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz",

View File

@ -41,6 +41,7 @@
"@babel/plugin-transform-runtime": "7.17.0",
"@babel/preset-env": "7.16.11",
"@babel/register": "7.17.0",
"@types/assert": "^1.5.6",
"assert": "2.0.0",
"babel-loader": "8.2.3",
"benchmark": "2.1.4",
@ -76,6 +77,7 @@
"process": "0.11.10",
"standard": "16.0.4",
"sylvester": "0.0.21",
"ts-node": "10.5.0",
"typescript": "4.5.5",
"webpack": "4.46.0",
"zeros": "1.0.0"
@ -139,9 +141,10 @@
"test:src": "mocha test/unit-tests",
"test:generated": "mocha test/generated-code-tests",
"test:node": "mocha test/node-tests/*.test.js test/node-tests/**/*.test.js",
"test:all": "npm run test:src && npm run test:generated && npm run test:node",
"test:all": "npm run test:src && npm run test:generated && npm run test:node && npm run test:types",
"test:browser": "karma start test/browser-test-config/local-karma.js",
"test:browserstack": "karma start test/browser-test-config/browserstack-karma.js",
"test:types": "cd types && node --loader ts-node/esm ./index.ts",
"coverage": "nyc --reporter=lcov --reporter=text-summary mocha test/unit-tests && echo \"\nDetailed coverage report is available at ./coverage/lcov-report/index.html\"",
"prepublishOnly": "npm run test:all && npm run lint",
"prepare": "npm run build",

17
types/index.d.ts vendored
View File

@ -365,7 +365,11 @@ declare namespace math {
version: string;
expression: MathNode;
json: MathJsJson;
/**
* Returns reviver function that can be used as reviver in JSON.parse function.
*/
reviver(): (key: any, value: any) => any;
/*************************************************************************
* Core functions
@ -767,6 +771,7 @@ declare namespace math {
* @param y Second value to add
* @returns Sum of x and y
*/
add<T extends MathType>(x: T, y: T): T;
add(x: MathType, y: MathType): MathType;
/**
@ -1116,8 +1121,7 @@ declare namespace math {
* @param y Value to subtract from x
* @returns Subtraction of x and y
*/
subtract(x: number, y: number): number;
subtract(x: Unit, y: Unit): Unit;
subtract<T extends MathType>(x: T, y: T): T;
subtract(x: MathType, y: MathType): MathType;
/**
@ -3522,13 +3526,6 @@ declare namespace math {
randomSeed?: string | null;
}
interface MathJsJson {
/**
* Returns reviver function that can be used as reviver in JSON.parse function.
*/
reviver(): (key: any, value: any) => any;
}
interface MathJsChain {
done(): any;

View File

@ -8,6 +8,11 @@ import {
divideDependencies,
formatDependencies,
} from 'mathjs';
import * as assert from 'assert';
// This file serves a dual purpose:
// 1) examples of how to use math.js in TypeScript
// 2) tests for the TypeScript declarations provided by math.js
/*
Basic usage examples
@ -29,13 +34,18 @@ Basic usage examples
math.evaluate('1.2 * (2 + 4.5)');
// chained operations
const a = math.chain(3).add(4).multiply(2).done(); // 14
const a = math.chain(3).add(4).multiply(2).done();
assert.strictEqual(a, 14);
// mixed use of different data types in functions
math.add(4, [5, 6]); // number + Array, [9, 10]
math.multiply(math.unit('5 mm'), 3); // Unit * number, 15 mm
math.subtract([2, 3, 4], 5); // Array - number, [-3, -2, -1]
math.add(math.matrix([2, 3]), [4, 5]); // Matrix + Array, [6, 8]
assert.deepStrictEqual(math.add(4, [5, 6]), [9, 10]); // number + Array
assert.deepStrictEqual(math.multiply(math.unit('5 mm'), 3), math.unit('15 mm')); // Unit * number
assert.deepStrictEqual(math.subtract([2, 3, 4], 5), [-3, -2, -1]); // Array - number
assert.deepStrictEqual(math.add(math.matrix([2, 3]), [4, 5]), math.matrix([6, 8])); // Matrix + Array
// narrowed type inference
const b: math.Matrix = math.add(math.matrix([2]), math.matrix([3]));
const c: math.Matrix = math.subtract(math.matrix([4]), math.matrix([5]));
}
/*
@ -49,8 +59,8 @@ Bignumbers examples
});
{
math.add(math.bignumber(0.1), math.bignumber(0.2)); // BigNumber, 0.3
math.divide(math.bignumber(0.3), math.bignumber(0.2)); // BigNumber, 1.5
assert.deepStrictEqual(math.add(math.bignumber(0.1), math.bignumber(0.2)), math.bignumber(0.3));
assert.deepStrictEqual(math.divide(math.bignumber(0.3), math.bignumber(0.2)), math.bignumber(1.5));
}
}
@ -59,14 +69,16 @@ Chaining examples
*/
{
const math = create(all, {});
const a = math.chain(3).add(4).multiply(2).done(); // 14
const a = math.chain(3).add(4).multiply(2).done();
assert.strictEqual(a, 14);
// Another example, calculate square(sin(pi / 4))
const b = math.chain(math.pi).divide(4).sin().square().done();
// toString will return a string representation of the chain's value
const chain = math.chain(2).divide(3);
const str: string = chain.toString(); // "0.6666666666666666"
const str: string = chain.toString();
assert.strictEqual(str, "0.6666666666666666");
chain.valueOf();
@ -75,17 +87,21 @@ Chaining examples
[1, 2],
[3, 4],
];
const v = math.chain(array).subset(math.index(1, 0)).done(); // 3
const v = math.chain(array).subset(math.index(1, 0)).done();
assert.strictEqual(v, 3);
const m = math.chain(array).subset(math.index(0, 0), 8).multiply(3).done();
// filtering
math
.chain([-1, 0, 1.1, 2, 3, 1000])
.filter(math.isPositive)
.filter(math.isInteger)
.filter((n) => n !== 1000)
.done(); // [2, 3]
assert.deepStrictEqual(
math
.chain([-1, 0, 1.1, 2, 3, 1000])
.filter(math.isPositive)
.filter(math.isInteger)
.filter((n) => n !== 1000)
.done(),
[2, 3]
);
}
/*
@ -164,13 +180,16 @@ Expressions examples
// get content of a parenthesis node
{
const node = math.parse('(1)');
if (node.type !== 'ParenthesisNode') {
throw Error(`expected ParenthesisNode, got ${node.type}`);
}
const innerNode = node.content;
}
// scope can contain both variables and functions
{
const scope = { hello: (name: string) => `hello, ${name}!` };
math.evaluate('hello("hero")', scope); // "hello, hero!"
assert.strictEqual(math.evaluate('hello("hero")', scope), "hello, hero!");
}
// define a function as an expression
@ -203,10 +222,10 @@ Expressions examples
// get and set variables and functions
{
parser.evaluate('x = 7 / 2'); // 3.5
parser.evaluate('x + 3'); // 6.5
assert.strictEqual(parser.evaluate('x = 7 / 2'), 3.5);
assert.strictEqual(parser.evaluate('x + 3'), 6.5);
parser.evaluate('f(x, y) = x^y'); // f(x, y)
parser.evaluate('f(2, 3)'); // 8
assert.strictEqual(parser.evaluate('f(2, 3)'), 8);
const x = parser.get('x');
const f = parser.get('f');
@ -277,7 +296,7 @@ Matrices examples
b.subset(math.index(1, [0, 1]), [[7, 8]]);
const c = math.multiply(a, b);
const f: math.Matrix = math.matrix([1, 0]);
const d: math.Matrix = f.subset(math.index(1, 0));
const d: math.Matrix = f.subset(math.index(1));
}
// get a sub matrix
@ -313,23 +332,29 @@ Matrices examples
// map matrix
{
math.map([1, 2, 3], function (value) {
return value * value;
}); // returns [1, 4, 9]
assert.deepStrictEqual(
math.map([1, 2, 3], function (value) {
return value * value;
}),
[1, 4, 9]
);
}
// filter matrix
{
math.filter([6, -2, -1, 4, 3], function (x) {
return x > 0;
}); // returns [6, 4, 3]
math.filter(['23', 'foo', '100', '55', 'bar'], /[0-9]+/); // returns ["23", "100", "55"]
assert.deepStrictEqual(
math.filter([6, -2, -1, 4, 3], function (x) {
return x > 0;
}),
[6, 4, 3]
)
assert.deepStrictEqual(math.filter(['23', 'foo', '100', '55', 'bar'], /[0-9]+/), ["23", "100", "55"]);
}
// concat matrix
{
math.concat([[0, 1, 2]], [[1, 2, 3]]); // returns [[ 0, 1, 2, 1, 2, 3 ]]
math.concat([[0, 1, 2]], [[1, 2, 3]], 0); // returns [[ 0, 1, 2 ], [ 1, 2, 3 ]]
assert.deepStrictEqual(math.concat([[0, 1, 2]], [[1, 2, 3]]), [[ 0, 1, 2, 1, 2, 3 ]]);
assert.deepStrictEqual(math.concat([[0, 1, 2]], [[1, 2, 3]], 0), [[ 0, 1, 2 ], [ 1, 2, 3 ]]);
}
}
@ -347,7 +372,7 @@ Sparse matrices examples
const c = math.multiply(b, math.complex(2, 2));
const d = math.matrix([0, 1]);
const e = math.transpose(d);
const f = math.multiply(e, a);
const f = math.multiply(e, d);
}
/*
@ -380,7 +405,7 @@ Units examples
);
math.createUnit(
{
foo_2: {
foo2: {
prefixes: 'long',
},
bar: '40 foo',
@ -427,7 +452,7 @@ Expression tree examples
// Filter an expression tree
const node: math.MathNode = math.parse('x^2 + x/4 + 3*y');
const filtered: math.MathNode[] = node.filter((node: math.MathNode) => node.isSymbolNode && node.name === 'x');
const filtered: math.MathNode[] = node.filter((node: math.MathNode) => node.type === 'SymbolNode' && node.name === 'x');
const arr: string[] = filtered.map((node: math.MathNode) => node.toString());
@ -442,7 +467,7 @@ Expression tree examples
case 'SymbolNode':
return node.type === 'SymbolNode';
default:
return node.type === 'any string at all';
return;
}
});
}
@ -454,22 +479,22 @@ Function floor examples
const math = create(all, {});
// number input
math.floor(3.2); // returns number 3
math.floor(-4.2); // returns number -5
assert.strictEqual(math.floor(3.2), 3);
assert.strictEqual(math.floor(-4.2), -5);
// number input
// roundoff result to 2 decimals
math.floor(3.212, 2); // returns number 3.21
math.floor(-4.212, 2); // returns number -4.22
assert.strictEqual(math.floor(3.212, 2), 3.21);
assert.strictEqual(math.floor(-4.212, 2), -4.22);
// Complex input
const c = math.complex(3.24, -2.71); // returns Complex 3 - 3i
math.floor(c); // returns Complex 3 - 3i
math.floor(c, 1); // returns Complex 3.2 - 2.8i
const c = math.complex(3.24, -2.71);
assert.deepStrictEqual(math.floor(c), math.complex(3, -3));
assert.deepStrictEqual(math.floor(c, 1), math.complex(3.2, -2.8));
//array input
math.floor([3.2, 3.8, -4.7]); // returns Array [3, 3, -5]
math.floor([3.21, 3.82, -4.71], 1); // returns Array [3.2, 3.8, -4.8]
assert.deepStrictEqual(math.floor([3.2, 3.8, -4.7]), [3, 3, -5]);
assert.deepStrictEqual(math.floor([3.21, 3.82, -4.71], 1), [3.2, 3.8, -4.8]);
}
@ -483,8 +508,8 @@ JSON serialization/deserialization
bigNumber: math.bignumber('1.5'),
};
const stringified = JSON.stringify(data);
const parsed = JSON.parse(stringified, math.json.reviver);
parsed.bigNumber === math.bignumber('1.5'); // true
const parsed = JSON.parse(stringified, math.reviver);
assert.deepStrictEqual(parsed.bigNumber, math.bignumber('1.5'));
}
/*
@ -576,8 +601,8 @@ Factory Test
const b = fraction(3, 7);
const c = add(a, b);
const d = divide(a, b);
console.log('c =', format(c)); // outputs "c = 16/21"
console.log('d =', format(d)); // outputs "d = 7/9"
assert.strictEqual(format(c), "16/21");
assert.strictEqual(format(d), "7/9");
}
/**
@ -592,8 +617,8 @@ Factory Test
};
// now we can use the \u260E (phone) character in expressions
const result = math.evaluate("\u260Efoo", { "\u260Efoo": 42 }); // returns 42
console.log(result);
const result = math.evaluate("\u260Efoo", { "\u260Efoo": 42 });
assert.strictEqual(result, 42);
}
/**
@ -604,13 +629,13 @@ Factory Test
const math = create(all, {});
// hasNumericValue function
math.hasNumericValue(2); // returns true
math.hasNumericValue('2'); // returns true
math.isNumeric('2'); // returns false
math.hasNumericValue(0); // returns true
math.hasNumericValue(math.bignumber(500)); // returns true
math.hasNumericValue([2.3, 'foo', false]); // returns [true, false, true]
math.hasNumericValue(math.fraction(4)); // returns true
math.hasNumericValue(math.complex('2-4i')); // returns false
assert. strictEqual(math.hasNumericValue(2), true);
assert. strictEqual(math.hasNumericValue('2'), true);
assert. strictEqual(math.isNumeric('2'), false);
assert. strictEqual(math.hasNumericValue(0), true);
assert. strictEqual(math.hasNumericValue(math.bignumber(500)), true);
assert.deepStrictEqual(math.hasNumericValue([2.3, 'foo', false]), [true, false, true]);
assert. strictEqual(math.hasNumericValue(math.fraction(4)), true);
assert. strictEqual(math.hasNumericValue(math.complex('2-4i')), false);
}

View File

@ -7,7 +7,7 @@
"typeRoots": [],
"types": [],
"lib": ["ES6", "DOM"],
"module": "commonjs",
"module": "ESNEXT",
"noEmit": true,
"noImplicitAny": true,
"noImplicitThis": true,