From 8f5d10dac4dc4b33a3be98b5413021733d4479f7 Mon Sep 17 00:00:00 2001 From: calvintwr Date: Sat, 6 Jun 2020 21:53:14 +0800 Subject: [PATCH] [minor] replace deprecated __proto__ usage --- dist/not.js | 12 ++++++------ dist/not.min.js | 2 +- package.json | 2 +- src/index.js | 12 ++++++------ 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/dist/not.js b/dist/not.js index 6f1c99b..93e1a78 100644 --- a/dist/not.js +++ b/dist/not.js @@ -1,6 +1,6 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Not = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i-1){if(this._are(this[r].primitive,e,this.customNameReplace(r))){if("function"==typeof this[r].pass){if(this[r].pass(e)){o=!0;break}continue}o=!0;break}}else if(-1!==n.indexOf(r)){o=!0;break}}return o},e.prepareExpect=function(t){var e=this;if("string"==typeof t)t=[t];else if(!Array.isArray(t)){console.log(t);var n=TypeError("Internal error: Say what you expect to check as a string or array of strings. Found ".concat(this.list(this.type(t),"as"),"."));throw console.log(n.stack),n}return t.reduce(function(t,n){if("string"!=typeof n)throw TypeError("Internal error: Say what you expect to check as a string. Found ".concat(e.list(e.type(n),"as"),"."));return n=n.toLowerCase(),e.mapExpect(t,n)},[])},e.mapExpect=function(t,e){if(-1===this._primitives.indexOf(e)){if(void 0!==this["$$custom_".concat(e)])return t.push("$$custom_".concat(e)),t;throw TypeError("Internal error: `".concat(e,"` is not a valid type to check for."))}return t.push(e),t},e.msg=function(t,e,n,o){var i="Wrong Type";return i+=n?" (".concat(n,")"):"",(i+=": Expecting type ".concat(this.list(t)," but got ").concat(this.list(e),"."))+(o?" Note: ".concat(o,"."):"")},e.list=function(t,e){if(e||(e="or"),"string"==typeof t&&(t=[t]),1===(t=t.map(function(t){return"`".concat(t.toLowerCase(),"`")})).length)return this.customNameReplace(t[0]);if(2===t.length)return this.customNameReplace(t.join(" ".concat(e," ")));var n="".concat(t.slice(0,-1).join(", ")," ").concat(e," ").concat(t.slice(-1));return this.customNameReplace(n)},e.customNameReplace=function(t){return t.replace("$$custom_optional","optional(null or undefined)").replace("$$custom_","custom:")},e.type=function(e){return"object"!=t(e)?"number"==typeof e&&isNaN(e)?this.opinionatedOnNaN?"nan":["nan","number"]:t(e):Array.isArray(e)?this.opinionatedOnArray?"array":["array","object"]:null===e?this.opinionatedOnNull?"null":["null","object"]:e instanceof String?this.opinionatedOnString?"string":["string","object"]:e instanceof Number?this.opinionatedOnNumber?isNaN(e.valueOf())?"nan":"number":isNaN(e.valueOf())?["number","nan","object"]:["number","object"]:e instanceof Boolean?this.opinionatedOnBoolean?"boolean":["boolean","object"]:"object"},e.lodge=function(t,e,n,o){this._lodged||(this._lodged=[]);var i=!1;try{i=this.areNot(t,e,n,o)}catch(r){i=r.message}return i&&this._lodged.push(i),i},e.resolve=function(t,e){if(void 0===this._lodged||0===this._lodged.length)return"function"==typeof t&&t(!1,e);if("function"==typeof t)return t(this._lodged,e);if(this.willThrowError){var n=TypeError("Wrong types provided. See `trace`.");throw n.trace=this._lodged,n}return this._lodged},e.checkObject=function(e,n,o,i){this.areNot("string",e),this.areNot("object",n),this.areNot("object",o),this.areNot(["function","object","optional"],i);var r=Object.create(this);if("function"==typeof i)return r.walkObject(e,n,o),r.resolve(i);if("object"==t(i)){var a=null;return!0===i.returnPayload&&"$$empty$$"===(a=r.walkObject(e,n,o,!0))&&(a={}),i="function"==typeof i.callback?i.callback:function(t,e){return t||e},r.resolve(i,a)}return r.walkObject(e,n,o),r.resolve()},e.walkObject=function(e,n,o,i){if(i)var r={};for(var a=0,c=Object.keys(n);a-1&&t.indexOf(e)===t.length-e.length},f=c[a],l=n[f],p=!1;u(f,"?")?p=s("?"):u(f,"__optional")&&(p=s("__optional"));var h=p?p(f):f,y=o[h];if("object"!=t(l)||null===l||Array.isArray(l)){p&&(Array.isArray(l)?l.push("optional"):l=[l,"optional"]);var d=this.lodge(l,y,"".concat(e,".").concat(h));i&&!d&&y&&(r[h]=y)}else{if("object"==t(y)&&null!==y&&!Array.isArray(l)){if("$$empty$$"===this.walkObject("".concat(e,".").concat(h),l,y,i))continue;i&&(r[h]=y);continue}if(p)continue;this.lodge("object",y,"".concat(e,".").concat(h))}}if(i)return Object.keys(r).length<1?"$$empty$$":r},e.defineType=function(t){var e=this,n=this.__proto__.checkObject("defineType",{primitive:["string","array"],type:"string",pass:["function","optional"]},t,{returnPayload:!0});if(Array.isArray(n))throw n.forEach(function(t){console.error(t)}),TypeError("Wrong inputs for #defineType.");"string"==typeof n.primitive&&(n.primitive=[n.primitive]),n.primitive.forEach(function(t){if(-1===e._primitives.indexOf(t))throw TypeError("Internal error: `".concat(t,"` is not a valid primitive."))});var o="$$custom_".concat(n.type);this[o]={primitive:n.primitive},n.pass&&(this[o].pass=n.pass)},e.create=function(t){var e=Object.create(this);return this._applyOptions(e,t),e.areNot.bind(e)},e.createNot=function(t){return this.create(t)},e.createIs=function(t){var e=Object.create(this);return this._applyOptions(e,t),e.are.bind(e)},e._applyOptions=function(t,e){var n=this;if(this._are("object",e)){if(this._are("boolean",e.willThrowError)&&(t.willThrowError=e.willThrowError),this._are("boolean",e.isOpinionated))return void(t.isOpinionated=e.isOpinionated);this._opinions.forEach(function(o){n._are("boolean",e[o])&&(t[o]=e[o])})}},e.$$custom_optional={primitive:["null","undefined"]},Object.create(e)}); \ No newline at end of file +!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).Not=t()}}(function(){function t(e){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(e)}var e={willThrowError:!0};return Object.defineProperty(e,"_opinions",{value:["opinionatedOnNaN","opinionatedOnArray","opinionatedOnNull","opinionatedOnString","opinionatedOnNumber","opinionatedOnBoolean"]}),Object.defineProperty(e,"isOpinionated",{get:function(){return this._isOpinionated},set:function(t){var e=this;this._isOpinionated=t,this._opinions.forEach(function(n){e[n]=t})},enumerable:!0}),e.isOpinionated=!0,Object.defineProperty(e,"areNot",{value:function(t,e,n,o){t=this.prepareExpect(t);var i=this.type(e);if(this.found(t,e,i))return!1;var r=this.msg(t,i,n,o);if(this.willThrowError)throw TypeError(r);return r}}),Object.defineProperties(e,{are:{value:function(t,e,n,o){return!this.areNot(t,e)}},_are:{value:function(t,e,n,o){try{return"string"!=typeof this.areNot(t,e,n,o)}catch(i){return!1}}}}),Object.defineProperty(e,"_primitives",{value:["string","number","array","object","function","boolean","null","undefined","symbol","nan"],enumerable:!0}),e.isNot=function(t,e,n,o){return this.areNot(t,e,n,o)},e.not=function(t,e,n,o){return this.areNot(t,e,n,o)},e.is=function(t,e,n,o){return this.are(t,e,n,o)},e.found=function(t,e,n){"string"==typeof n&&(n=[n]);for(var o=!1,i=0;i-1){if(this._are(this[r].primitive,e,this.customNameReplace(r))){if("function"==typeof this[r].pass){if(this[r].pass(e)){o=!0;break}continue}o=!0;break}}else if(-1!==n.indexOf(r)){o=!0;break}}return o},e.prepareExpect=function(t){var e=this;if("string"==typeof t)t=[t];else if(!Array.isArray(t)){console.log(t);var n=TypeError("Internal error: Say what you expect to check as a string or array of strings. Found ".concat(this.list(this.type(t),"as"),"."));throw console.log(n.stack),n}return t.reduce(function(t,n){if("string"!=typeof n)throw TypeError("Internal error: Say what you expect to check as a string. Found ".concat(e.list(e.type(n),"as"),"."));return n=n.toLowerCase(),e.mapExpect(t,n)},[])},e.mapExpect=function(t,e){if(-1===this._primitives.indexOf(e)){if(void 0!==this["$$custom_".concat(e)])return t.push("$$custom_".concat(e)),t;throw TypeError("Internal error: `".concat(e,"` is not a valid type to check for."))}return t.push(e),t},e.msg=function(t,e,n,o){var i="Wrong Type";return i+=n?" (".concat(n,")"):"",(i+=": Expecting type ".concat(this.list(t)," but got ").concat(this.list(e),"."))+(o?" Note: ".concat(o,"."):"")},e.list=function(t,e){if(e||(e="or"),"string"==typeof t&&(t=[t]),1===(t=t.map(function(t){return"`".concat(t.toLowerCase(),"`")})).length)return this.customNameReplace(t[0]);if(2===t.length)return this.customNameReplace(t.join(" ".concat(e," ")));var n="".concat(t.slice(0,-1).join(", ")," ").concat(e," ").concat(t.slice(-1));return this.customNameReplace(n)},e.customNameReplace=function(t){return t.replace("$$custom_optional","optional(null or undefined)").replace("$$custom_","custom:")},e.type=function(e){return"object"!=t(e)?"number"==typeof e&&isNaN(e)?this.opinionatedOnNaN?"nan":["nan","number"]:t(e):Array.isArray(e)?this.opinionatedOnArray?"array":["array","object"]:null===e?this.opinionatedOnNull?"null":["null","object"]:e instanceof String?this.opinionatedOnString?"string":["string","object"]:e instanceof Number?this.opinionatedOnNumber?isNaN(e.valueOf())?"nan":"number":isNaN(e.valueOf())?["number","nan","object"]:["number","object"]:e instanceof Boolean?this.opinionatedOnBoolean?"boolean":["boolean","object"]:"object"},e.lodge=function(t,e,n,o){this._lodged||(this._lodged=[]);var i=!1;try{i=this.areNot(t,e,n,o)}catch(r){i=r.message}return i&&this._lodged.push(i),i},e.resolve=function(t,e){if(void 0===this._lodged||0===this._lodged.length)return"function"==typeof t&&t(!1,e);if("function"==typeof t)return t(this._lodged,e);if(this.willThrowError){var n=TypeError("Wrong types provided. See `trace`.");throw n.trace=this._lodged,n}return this._lodged},e.checkObject=function(e,n,o,i){this.areNot("string",e),this.areNot("object",n),this.areNot("object",o),this.areNot(["function","object","optional"],i);var r=Object.create(this);if("function"==typeof i)return r.walkObject(e,n,o),r.resolve(i);if("object"==t(i)){var a=null;return!0===i.returnPayload&&"$$empty$$"===(a=r.walkObject(e,n,o,!0))&&(a={}),i="function"==typeof i.callback?i.callback:function(t,e){return t||e},r.resolve(i,a)}return r.walkObject(e,n,o),r.resolve()},e.walkObject=function(e,n,o,i){if(i)var r={};for(var a=0,c=Object.keys(n);a-1&&t.indexOf(e)===t.length-e.length},f=c[a],l=n[f],p=!1;u(f,"?")?p=s("?"):u(f,"__optional")&&(p=s("__optional"));var h=p?p(f):f,y=o[h];if("object"!=t(l)||null===l||Array.isArray(l)){p&&(Array.isArray(l)?l.push("optional"):l=[l,"optional"]);var d=this.lodge(l,y,"".concat(e,".").concat(h));i&&!d&&y&&(r[h]=y)}else{if("object"==t(y)&&null!==y&&!Array.isArray(l)){if("$$empty$$"===this.walkObject("".concat(e,".").concat(h),l,y,i))continue;i&&(r[h]=y);continue}if(p)continue;this.lodge("object",y,"".concat(e,".").concat(h))}}if(i)return Object.keys(r).length<1?"$$empty$$":r},e.defineType=function(t){var e=this,n=Object.getPrototypeOf(this).checkObject("defineType",{primitive:["string","array"],type:"string",pass:["function","optional"]},t,{returnPayload:!0});if(Array.isArray(n))throw n.forEach(function(t){console.error(t)}),TypeError("Wrong inputs for #defineType.");"string"==typeof n.primitive&&(n.primitive=[n.primitive]),n.primitive.forEach(function(t){if(-1===e._primitives.indexOf(t))throw TypeError("Internal error: `".concat(t,"` is not a valid primitive."))});var o="$$custom_".concat(n.type);this[o]={primitive:n.primitive},n.pass&&(this[o].pass=n.pass)},e.create=function(t){var e=Object.create(this);return this._applyOptions(e,t),e.areNot.bind(e)},e.createNot=function(t){return this.create(t)},e.createIs=function(t){var e=Object.create(this);return this._applyOptions(e,t),e.are.bind(e)},e._applyOptions=function(t,e){var n=this;if(this._are("object",e)){if(this._are("boolean",e.willThrowError)&&(t.willThrowError=e.willThrowError),this._are("boolean",e.isOpinionated))return void(t.isOpinionated=e.isOpinionated);this._opinions.forEach(function(o){n._are("boolean",e[o])&&(t[o]=e[o])})}},e.$$custom_optional={primitive:["null","undefined"]},Object.create(e)}); \ No newline at end of file diff --git a/package.json b/package.json index b4d5076..f5fc097 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "you-are-not", - "version": "0.5.2", + "version": "0.5.3", "description": "Write accurate code. Meet deadlines. Maintain type flexibility. No compiling. A minimal, blazing fast, intuitive, and customisable type-checking helper that you are missing.", "main": "index.js", "browser": "dist/not.min.js", diff --git a/src/index.js b/src/index.js index bf32f9a..f6aa59f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,5 @@ /*! - * You-Are-Not v0.5.2 + * You-Are-Not v0.5.3 * (c) 2020 Calvin Tan * Released under the MIT License. */ @@ -361,7 +361,7 @@ You.walkObject = function (name, expectObject, gotObject, returnPayload) { } You.defineType = function(payload) { - let sanitised = this.__proto__.checkObject('defineType', { + let sanitised = Object.getPrototypeOf(this).checkObject('defineType', { primitive: ['string', 'array'], type: 'string', pass: ['function', 'optional'] @@ -397,17 +397,17 @@ You.createIs = function(options) { return you.are.bind(you) } -You._applyOptions = function (instance, options) { +You._applyOptions = function (descendant, options) { //using #_are because it's not writable and configurable if(this._are('object', options)) { - if(this._are('boolean', options.willThrowError)) instance.willThrowError = options.willThrowError + if(this._are('boolean', options.willThrowError)) descendant.willThrowError = options.willThrowError if(this._are('boolean', options.isOpinionated)) { - instance.isOpinionated = options.isOpinionated + descendant.isOpinionated = options.isOpinionated return } this._opinions.forEach(optionKey => { - if (this._are('boolean', options[optionKey])) instance[optionKey] = options[optionKey] + if (this._are('boolean', options[optionKey])) descendant[optionKey] = options[optionKey] }) } }