Skip to content

Commit

Permalink
Merge pull request #8 from Codevendor/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Codevendor authored Mar 19, 2023
2 parents adb85f1 + a0e76b9 commit 04baf9b
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Add both to your webpage for type checking. You only need to recompile with the

Below are examples for using [**InferJS-Library**][inferjs-library] in your code.

#### Brower ESModule Import Example - Client side
#### Browser ESModule Import Example - Client side
```html
<script type="module">
import { InferObject } from "./inferobject.js";
Expand Down Expand Up @@ -214,6 +214,7 @@ Below is a list of `standard` and `extended types` that can be checked:
| [symbol]() | A symbol type: `Symbol` |
| [function]() | A function type: `function` |
| **Extended InferJS Types** | **Description** |
| [any]() | Allows Any type: `any` or `*` |
| [array]() | An array type: `[]` |
| [infinity]() | An infinity number type: `Infinity` |
| [nan]() | Not a number type: `NaN` |
Expand Down
5 changes: 4 additions & 1 deletion bin/build-test1-inferobject.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ for (let i = 0; i < keys.length; i++) {

// Set command to exec for compiling InferObject
const compiler = `node ${paths['compiler']}`;
const cmd = `inferjs-compiler -f ${paths['input']} --output-options-flags='w' --output-options-module='esm' -o ${paths['output']}`;
const cmd = `${compiler} -f ${paths['input']} --output-options-flags='w' --output-options-module='esm' -o ${paths['output']}`;
//const cmd = `inferjs-compiler -f ${paths['input']} --output-options-flags='w' --output-options-module='esm' -o ${paths['output']}`;

//console.log(cmd);
//process.exit();

// Execute compiler to build inferobject for test1
exec(cmd, (error, stdout, stderr) => {
Expand Down
1 change: 0 additions & 1 deletion dist/latest/ver-0-0-6/commonjs/inferjs-0.0.6.min.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/latest/ver-0-0-6/esmodule/inferjs-0.0.6.min.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/latest/ver-0-0-6/script/inferjs-0.0.6.min.js

This file was deleted.

1 change: 1 addition & 0 deletions dist/latest/ver-0-0-7/commonjs/inferjs-0.0.7.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/latest/ver-0-0-7/esmodule/inferjs-0.0.7.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/latest/ver-0-0-7/script/inferjs-0.0.7.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/versions/ver-0-0-7/commonjs/inferjs-0.0.7.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/versions/ver-0-0-7/esmodule/inferjs-0.0.7.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/versions/ver-0-0-7/script/inferjs-0.0.7.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "inferjs-library",
"version": "0.0.6",
"version": "0.0.7",
"description": "A runtime library that allows you to infer rules for extended type checking in JavaScript.",
"main": "index.js",
"homepage": "https://inferjs.com",
"type": "module",
"devDependencies": {
"inferjs-compiler": "^0.0.8",
"inferjs-compiler": "^0.0.9",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1"
},
Expand Down
9 changes: 6 additions & 3 deletions src/core/inferjs-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,18 @@ export class InferJS {
#checkMethod(inferId, args, returnException = false) {

// Check if inferid exists or through error
if (type_of(this.#inferObject) !== 'object' || type_of(this.#inferObject.infers) !== 'object' || !this.#inferObject.infers.hasOwnProperty(inferId)) {
if (type_of(this.#inferObject) !== 'object' || type_of(this.#inferObject.methods) !== 'object' || type_of(this.#inferObject.methods.infers) !== 'object' || !this.#inferObject.methods.infers.hasOwnProperty(inferId)) {

const err = new TypeError(`Cannot find infer with inferid: ${inferId}`);
if (!returnException) throw err;
return err;
}

// Convert infer to inf for shorthand
const inf = this.#inferObject.infers[inferId];
const inf = this.#inferObject.methods.infers[inferId];

// Set inferid
inf["@inferid"] = inferId;

// Check if has @param
if (inf.hasOwnProperty('@param')) {
Expand Down Expand Up @@ -134,7 +137,7 @@ export class InferJS {

if (allowedTypes.hasOwnProperty(actualType)) {

const infers = allowedTypes[actualType].infers;
const infers = allowedTypes[actualType].expects;
const infersArray = Object.keys(infers);

for (let i3 = 0; i3 < infersArray.length; i3++) {
Expand Down
12 changes: 11 additions & 1 deletion src/errors/infer-expect-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class InferExpectError extends Error {
#expectedType = '';
#paramPositionRepresent = '';
#method = '';
#methodName = '';
#inferId = '';
#methodSignature = '';
#param = '';
Expand All @@ -41,6 +42,9 @@ export class InferExpectError extends Error {
/** Gets the method name from @function. */
get method() { return this.#method; }

/** Gets the method name from actual code. */
get methodName() { return this.#methodName; }

/** Gets the inferId from @inferid. */
get inferId() { return this.#inferId; }

Expand Down Expand Up @@ -105,6 +109,7 @@ export class InferExpectError extends Error {
this.#inferId = (inferObject.hasOwnProperty('@inferid')) ? inferObject['@inferid'] : 'Unknown';
this.#paramPositionRepresent = numberRepresent(this.#paramPosition);
this.#method = (inferObject.hasOwnProperty('@function')) ? inferObject['@function'] : '';
this.#methodName = (inferObject.hasOwnProperty('name')) ? inferObject['name'] : '';
this.#methodSignature = this.#getMethodSignature(inferObject);
this.#param = Object.keys(inferObject['@param'])[paramIndex];
this.#expectedType = this.#getExpectedType(inferObject, this.#param, true);
Expand Down Expand Up @@ -195,7 +200,12 @@ export class InferExpectError extends Error {
#getMethodSignature(inferObject) {

// Variables
let sig = (inferObject.hasOwnProperty('@function')) ? inferObject['@function'] + '( ' : '( ';
//let sig = (inferObject.hasOwnProperty('@function')) ? inferObject['@function'] + '( ' : '( ';

let sig = '';
if (inferObject.hasOwnProperty('name')) { sig = inferObject['name'] + '( ' };
if (inferObject.hasOwnProperty('@function')) { sig = inferObject['@function'] + '( ' };
if (sig === '') { sig = '( '; }

if (inferObject.hasOwnProperty('@param')) {

Expand Down
10 changes: 9 additions & 1 deletion src/errors/infer-type-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class InferTypeError extends TypeError {
#expectedType = '';
#paramPositionRepresent = '';
#method = '';
#methodName = '';
#inferId = '';
#methodSignature = '';
#param = '';
Expand All @@ -36,6 +37,9 @@ export class InferTypeError extends TypeError {
/** Gets the method name from @function. */
get method() { return this.#method; }

/** Gets the method name from actual code. */
get methodName() { return this.#methodName; }

/** Gets the inferId from @inferid. */
get inferId() { return this.#inferId; }

Expand Down Expand Up @@ -76,6 +80,7 @@ export class InferTypeError extends TypeError {
this.#inferId = (inferObject.hasOwnProperty('@inferid')) ? inferObject['@inferid'] : 'Unknown';
this.#paramPositionRepresent = numberRepresent(this.#paramPosition);
this.#method = (inferObject.hasOwnProperty('@function')) ? inferObject['@function'] : '';
this.#methodName = (inferObject.hasOwnProperty('name')) ? inferObject['name'] : '';
this.#methodSignature = this.#getMethodSignature(inferObject);
this.#param = Object.keys(inferObject['@param'])[paramIndex];
this.#expectedType = this.#getExpectedType(inferObject, this.#param, true);
Expand Down Expand Up @@ -142,7 +147,10 @@ export class InferTypeError extends TypeError {
#getMethodSignature(inferObject) {

// Variables
let sig = (inferObject.hasOwnProperty('@function')) ? inferObject['@function'] + '( ' : '( ';
let sig = '';
if (inferObject.hasOwnProperty('name')) { sig = inferObject['name'] + '( ' };
if (inferObject.hasOwnProperty('@function')) { sig = inferObject['@function'] + '( ' };
if (sig === '') { sig = '( '; }

if (inferObject.hasOwnProperty('@param')) {

Expand Down
4 changes: 2 additions & 2 deletions src/helpers/type-of.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ export function type_of(src, extended = false) {

case 'arguments': return 'arguments';
case 'array': return 'array';
case 'error': return (src.constructor.name) ? src.constructor.name : 'error';
case 'error': return (src.constructor && src.constructor.name) ? src.constructor.name : 'error';
default:

// Check if constructor
if (src.constructor.name) {
if (src.constructor && src.constructor.name) {
if(src.constructor.name.toLowerCase() === 'object') return 'object';
return src.constructor.name;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/inferobjects/test1.io.js

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

4 changes: 2 additions & 2 deletions tests/test1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InferObject } from "./inferobjects/test1.io.js";
import { InferJS } from "../dist/latest/ver-0-0-4/esmodule/inferjs-0.0.4.min.js";
import { InferJS } from "../src/core/inferjs-library.js";
const inferjs = new InferJS(InferObject);

/**
Expand Down Expand Up @@ -28,4 +28,4 @@ function foo(msg, id, send) {
}

// Example Normal Call
console.log(foo('test', '-1234', true));
console.log(foo('test', -1234, true));

0 comments on commit 04baf9b

Please sign in to comment.