JavaStrictStrict Type JavaScriptFast, simple and Enterprise Ready way to add arguments and / or return value type on one or more function, callback or method. Inspired on ActionScript 2.0 Strict Type behaviour. Why JavaStrict ?Every runtime compiled/executed program language requires hard debug instead of hard code and sometime we spend too much time, using big libraries, to find wrong objects, variables or methods during code execution. We have a lot of cool debuggers to add breakpoints, print on console each step but any automated check about each used function, its arguments and its expected return value type. JavaStrict tiny library allows developers to use a sort of Strict Type declaration for one or more function or method, respecting scope, isntances types and return type if it’s not Void (undefined). With a minimun performance impact that should be easyly removed declaring public Strict.debug value to false, every JavaScript developer could write strongly typed constructors or global functions, knowing every time when and where recieved arguments are not exactly expected one. JavaStrict is Enterprise Ready because code design need to be less scriptish and more strictly and at the same time it adds automatically informations about each method or function, showing return type and arguments type forcing users to respect them even removing documentation. Version| 1.1 | Now based on debug Boolean flag. Compatible with most JavaScript compressors. |
Browser CompatibilityEvery browser compatible with: - Function apply or call method
- try{ ... } catch(e){ ... }
- instanceof
AuthorAndrea Giammarchi, http://www.3site.eu LicenseCopyright (C) 2007 Andrea Giammarchi - www.3site.eu
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Source Code// (C) Andrea Giammarchi [www.3site.eu] - MIT Style License function Strict(returnValue, callback, arguments, name){ return Strict.debug ? Strict.returnValue(returnValue, Strict.apply(callback, arguments, name), name) : callback }; Strict.apply = function(callback, args, name){ return Strict.debug ? function(){ for(var i = 0; i < arguments.length ; i++){ if(!Strict.check(arguments[i], args[i])) throw new Error("[".concat(name || callback.name, "] Strict arguments ", i, " Error")); }; return callback.apply(this, arguments) } : callback }; Strict.call = function(){ var name = (arguments = [].slice.call(arguments)).length, callback = arguments.shift(); return Strict.debug ? Strict.apply(callback, arguments, Strict.check(arguments[name-2], "".constructor) ? arguments.pop() : Strict.Void) : callback }; Strict.check = function(arguments, constructor){ var returnValue = constructor === Strict.Void; return arguments === Strict.Void ? returnValue : (arguments === null ? !returnValue : (returnValue ? false : (Strict.object ? (typeof arguments === "object" ? arguments instanceof constructor : arguments.constructor === constructor) : (constructor === Object ? true : (arguments instanceof constructor || arguments.constructor === constructor))))) }; Strict.returnValue = function(args, callback, name){ return Strict.debug ? function(){ var returnValue = callback.apply(this, arguments); if(!Strict.check(returnValue, args)) throw new Error("[".concat(name || callback.name, "] Strict returnValue Error")); return returnValue } : callback }; Strict.debug = true; // set false on production enviroment to increase performances removing Strict checks Strict.object = false; // set true to check primitives too var Void = Strict.Void = Strict.Void;
Summary | Fast, simple and Enterprise Ready way to add arguments and / or return value type on one or more function, callback or method. | | Quick way to add both Strict returnValue and apply methods. | | | | Sets function or method accepted arguments types. | | Alias of Script.apply but accept N arguments. | | Checks Strict Type of a variable (primitive, instance or Void) and a generic constructor (or Void value). | | Sets function or method return value type. | | | | Public static Boolean value to enable/disable JavaStrict debug and increase performances too. | | Public static Boolean value to use strong Strict Type where primitive value are not usable as objects (“test” is not instanceof Object while new String(“test”) is). | | Alias of undefined value. |
Strict| function Strict( | returnValue, | | callback, | | arguments, | | name | ) |
|
Quick way to add both Strict returnValue and apply methods. Arguments| Function | constructor (instanceof) that function must return. | | Function | callback, function or method to modify setting JavaStrict returnValue and arguments check. | | Array | constructors list to verify each (instanceof) argument durig function excecution. | | [String] | optional callback name to show while throwing new Error |
Returns| Function | new JavaStrict enabled anonymous callback |
Methods - Public StaticSummary | Sets function or method accepted arguments types. | | Alias of Script.apply but accept N arguments. | | Checks Strict Type of a variable (primitive, instance or Void) and a generic constructor (or Void value). | | Sets function or method return value type. |
apply| Strict.apply = function( | callback, | | args, | | name | ) |
|
Sets function or method accepted arguments types. Arguments| Function | callback, function, method or Void to modify setting JavaStrict arguments check. | | Array | constructors list to verify each (instanceof) argument durig function excecution. | | [String] | optional callback name to show while throwing new Error |
Returns| Function | new JavaStrict enabled anonymous callback |
Examplefunction noArgs(){ return "hello"; }; noArgs = Strict.apply(noArgs, [Void]);
noArgs(); // "hello" noArgs(null); // new Error("[noArgs] Strict arguments 0 Error")
function substring(str, len){ return str.substring(len); }; substring = JavaStrict.apply(substring, [String, Number]);
substring("test", 1); // "est" substring("test", "me"); // new Error("[substring] Strict arguments 1 Error")
NoteActionScript 2.0 parser accepts arguments even when a function that have Void value (in this case, Void is used as arguments[0] referer). With JavaStrict, if You use Void as argument, function doesn’t accept any kind of defined argument.
callAlias of Script.apply but accept N arguments. Arguments| Function | callback, function or method to modify setting JavaStrict arguments check. | | Function | constructor (or Void) to verify (instanceof) first argument durig function excecution. | | [Function] | constructor to verify optional (instanceof) second argument durig function excecution. | | [Function] | constructor to verify optional (instanceof) third argument durig function excecution. | | [Function] | constructor to verify optional (instanceof) N argument durig function excecution. | | [String] | last optional callback name to show while throwing new Error |
Returns| Function | new JavaStrict enabled anonymous callback |
Examplefunction substring(str, len){ return str.substring(len); }; substring = Strict.call(substring, String, Number);
substring("test", 1); // "est" substring("test", "me"); // new Error("[substring] Strict arguments 1 Error")
check| Strict.check = function( | arguments, | | constructor | ) |
|
Checks Strict Type of a variable (primitive, instance or Void) and a generic constructor (or Void value). Arguments| Object | generic variable or Void - undefined value | | Function | constructor or Void -undefined value |
Returns| Boolean | true if Strict is valid |
Examplealert([ // Correct behaviour with default false JavaScript.object value
// false values example (don't respect Strict Type) Strict.check(Void, Object), Strict.check(null, Void), Strict.check(String, String), Strict.check({}, Void),
// true values example (respect Strict Type) Strict.check({}, Object), Strict.check(new String, Object), Strict.check(new String, String), Strict.check("", String), Strict.check(String, Function), Strict.check(null, Object), Strict.check(new Date, Date), Strict.check(Void, Void)
// Correct behaviour with true JavaScript.object value
// false values example (don't respect Strict Type) Strict.check("object", Object), Strict.check(1, Object),
// true values example (respect Strict Type) Strict.check("object", String), Strict.check(new String("object"), String), Strict.check(new String("object"), Object) ].join("\n"));
Note about Void and nullVoid is an undefined alias, please don’t use them as a global variable of your own script. null is a special value that should be used with every type of constructor but it can’t be used as undefined value. Note about Strict.objectIf setted as true, primitive variables (string, number or boolean) are not Objects. In these case return type or argument one need to be exactely expected type (String, Number or Boolean). Note about Strict.debug valueThis method is the only one that works with public static debug parameter setted to false too so You can use them for your own goal inside your library.
returnValue| Strict.returnValue = function( | args, | | callback, | | name | ) |
|
Sets function or method return value type. Arguments| Function | constructor (instanceof) that function must return. | | Function | callback, function or method to modify setting JavaStrict returnValue check. | | [String] | optional callback name to show while throwing new Error |
Returns| Function | new JavaStrict enabled anonymous callback |
Examplefunction getString(str){ return str; }; getString = Strict.returnValue(String, getString);
getString("test"); // "test" getString(true); // new Error("[getString] Strict returnValue Error")
NoteIf You specify Void as returnValue, function can’t return any type of value (only undefined one). If You need to return a generic variable type use Object, when Strict.object is false, and be sure that return value is atleast null.
Params - Public StaticSummary | Public static Boolean value to enable/disable JavaStrict debug and increase performances too. | | Public static Boolean value to use strong Strict Type where primitive value are not usable as objects (“test” is not instanceof Object while new String(“test”) is). | | Alias of undefined value. |
debugPublic static Boolean value to enable/disable JavaStrict debug and increase performances too. Switch to false on production enviroment. Defalut value: true
objectPublic static Boolean value to use strong Strict Type where primitive value are not usable as objects (“test” is not instanceof Object while new String(“test”) is). Defalut value: false
VoidAlias of undefined value. You can use just Void insteadof Strict.Void but please be sure that global Void variable is not re-defined in another piece of code.
|