Skip to content

Commit

Permalink
Fixed positive expression using any or true.
Browse files Browse the repository at this point in the history
  • Loading branch information
fenying committed Aug 27, 2019
1 parent 7d0e298 commit b9dd5c5
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes Logs

## v1.0.1

- Fixed lack of positive expression when `any` or `true` is used.

## v1.0.0

- A full refactor, with a better and simple code generation.
Expand Down
38 changes: 19 additions & 19 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@litert/typeguard",
"version": "1.0.0",
"version": "1.0.1",
"description": "An easy and powerful data validation code generator by JavaScript.",
"main": "lib/index.js",
"scripts": {
Expand Down
14 changes: 12 additions & 2 deletions src/lib/langs/JavaScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ implements C.ILanguageBuilder {
return conditions[0];
}

conditions = this._dereplicate(conditions.filter((x) => x !== "true"));
if (conditions.includes("true")) {

return "true";
}

conditions = this._dereplicate(conditions);

if (!conditions.length) {

Expand All @@ -122,7 +127,12 @@ implements C.ILanguageBuilder {
return conditions[0];
}

conditions = this._dereplicate(conditions.filter((x) => x !== "true"));
if (conditions.includes("false")) {

return "false";
}

conditions = this._dereplicate(conditions);

if (!conditions.length) {

Expand Down
6 changes: 6 additions & 0 deletions src/samples/quick-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ const check3 = tgc.compile({
console.log(check3("hello"));
console.log(check3("world"));
console.log(check3.toString());

console.log(tgc.compile({
rule: {
"test": "any"
}
}).toString());
2 changes: 2 additions & 0 deletions src/test/00-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import testStringAssert from "./05-string-asserts";
import testStructure from "./06-structure";
import testModifiers from "./07-modifiers";
import testMapAndDict from "./08-map-and-dict";
import testExceptions from "./09-exceptions";

testElementalTypes();
testArrayAndList();
Expand All @@ -31,3 +32,4 @@ testStringAssert();
testStructure();
testModifiers();
testMapAndDict();
testExceptions();
46 changes: 46 additions & 0 deletions src/test/09-exceptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright 2019 Angus.Fenying <fenying@litert.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
createTestDefinition,
ITestSuite
} from "./abstracts";

const testItems: ITestSuite = {

name: "When optional with any",
sections: [

{
"name": JSON.stringify({ "test?": "any" }),
"rule": { "test?": "any" },
"items": [
{
inputName: "When test === 'ffff'",
inputValue: {"test": "ffff"},
expect: true
},
{
inputName: "When test is omitted",
inputValue: {},
expect: true
}
]
},
]
};

export default createTestDefinition(testItems);

0 comments on commit b9dd5c5

Please sign in to comment.