From b9dd5c583c7a7e0a384f7918c912f453207578d8 Mon Sep 17 00:00:00 2001 From: "Angus.Fenying" Date: Tue, 27 Aug 2019 18:22:35 +0800 Subject: [PATCH] Fixed positive expression using `any` or `true`. --- CHANGES.md | 4 ++++ package-lock.json | 38 +++++++++++++++--------------- package.json | 2 +- src/lib/langs/JavaScript.ts | 14 +++++++++-- src/samples/quick-start.ts | 6 +++++ src/test/00-all.ts | 2 ++ src/test/09-exceptions.ts | 46 +++++++++++++++++++++++++++++++++++++ 7 files changed, 90 insertions(+), 22 deletions(-) create mode 100644 src/test/09-exceptions.ts diff --git a/CHANGES.md b/CHANGES.md index 1ae3562..7032296 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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. diff --git a/package-lock.json b/package-lock.json index 86b5d4d..0389d21 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@litert/typeguard", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -11,9 +11,9 @@ "dev": true }, "@types/node": { - "version": "11.13.15", - "resolved": "https://registry.npmjs.org/@types/node/-/node-11.13.15.tgz", - "integrity": "sha512-x6ypl5Uzly+j23hbxmMzf12Eb4lOhIEqQz0HuczpTUa1KIx1GpbN/o4E3aAED20UoEsdK0wvyY8QcffuWSLDkw==", + "version": "11.13.19", + "resolved": "https://registry.npmjs.org/@types/node/-/node-11.13.19.tgz", + "integrity": "sha512-tLRDU1hmcWamtgRT2iVRdraAQVGFQGgtcqracSo9XyMN1VeZLSVGb8RJJxVqab7UGbijoUijGPVFMjmqzyZIUw==", "dev": true }, "ansi-colors": { @@ -442,9 +442,9 @@ } }, "lodash": { - "version": "4.17.11", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", - "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==", + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", "dev": true }, "log-symbols": { @@ -507,9 +507,9 @@ } }, "mocha": { - "version": "6.1.4", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-6.1.4.tgz", - "integrity": "sha512-PN8CIy4RXsIoxoFJzS4QNnCH4psUCPWc4/rPrst/ecSJJbLBkubMiyGCP2Kj/9YnWbotFqAoeXyXMucj7gwCFg==", + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-6.2.0.tgz", + "integrity": "sha512-qwfFgY+7EKAAUAdv7VYMZQknI7YJSGesxHyhn6qD52DV8UcSZs5XwCifcZGMVIE4a5fbmhvbotxC0DLQ0oKohQ==", "dev": true, "requires": { "ansi-colors": "3.2.3", @@ -641,9 +641,9 @@ "dev": true }, "p-limit": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.0.tgz", - "integrity": "sha512-pZbTJpoUsCzV48Mc9Nh51VbwO0X9cuPFE8gYwx9BTCt9SF8/b7Zljd2fVgOxhIF/HDTKgpVzs+GPhyKfjLLFRQ==", + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.1.tgz", + "integrity": "sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==", "dev": true, "requires": { "p-try": "^2.0.0" @@ -705,9 +705,9 @@ "dev": true }, "semver": { - "version": "5.7.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", - "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==", + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", "dev": true }, "set-blocking": { @@ -784,9 +784,9 @@ } }, "typescript": { - "version": "3.5.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.5.2.tgz", - "integrity": "sha512-7KxJovlYhTX5RaRbUdkAXN1KUZ8PwWlTzQdHV6xNqvuFOs7+WBo10TQUqT19Q/Jz2hk5v9TQDIhyLhhJY4p5AA==", + "version": "3.5.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.5.3.tgz", + "integrity": "sha512-ACzBtm/PhXBDId6a6sDJfroT2pOWt/oOnk4/dElG5G33ZL776N3Y6/6bKZJBFpd+b05F3Ct9qDjMeJmRWtE2/g==", "dev": true }, "which": { diff --git a/package.json b/package.json index de2c3c5..b2cd85b 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/lib/langs/JavaScript.ts b/src/lib/langs/JavaScript.ts index 565fceb..8ab0fb8 100644 --- a/src/lib/langs/JavaScript.ts +++ b/src/lib/langs/JavaScript.ts @@ -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) { @@ -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) { diff --git a/src/samples/quick-start.ts b/src/samples/quick-start.ts index bb21b06..796bc41 100644 --- a/src/samples/quick-start.ts +++ b/src/samples/quick-start.ts @@ -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()); diff --git a/src/test/00-all.ts b/src/test/00-all.ts index b7d7b18..3921474 100644 --- a/src/test/00-all.ts +++ b/src/test/00-all.ts @@ -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(); @@ -31,3 +32,4 @@ testStringAssert(); testStructure(); testModifiers(); testMapAndDict(); +testExceptions(); diff --git a/src/test/09-exceptions.ts b/src/test/09-exceptions.ts new file mode 100644 index 0000000..e379625 --- /dev/null +++ b/src/test/09-exceptions.ts @@ -0,0 +1,46 @@ +/** + * Copyright 2019 Angus.Fenying + * + * 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);