Comparing version 1.0.0-alpha.5 to 1.0.0-alpha.6
@@ -5,12 +5,9 @@ | ||
/// Test if the value is valid | ||
var isValid = Assert | ||
.required() | ||
.string() | ||
.regex("^M") | ||
.max(10) | ||
.min(5) | ||
var isValid = Assert.required() | ||
.string() | ||
.regex("^M") | ||
.max(10) | ||
.min(5) | ||
.isValid("Mary"); | ||
/// test if the rules above are valid | ||
.isValid("Mary"); | ||
expect(isValid) | ||
@@ -21,4 +18,3 @@ .toBe(false); | ||
/// Valid assertion: | ||
var obj = Assert | ||
.required() | ||
var obj = Assert.required() | ||
.array() | ||
@@ -25,0 +21,0 @@ .max(5) |
@@ -14,3 +14,4 @@ | ||
/// Exception instance is an error! | ||
expect(error instanceof Error).equals(true); | ||
expect(error instanceof Error) | ||
.equals(true); | ||
@@ -17,0 +18,0 @@ /// Exception message |
@@ -9,3 +9,4 @@ | ||
/// is an instance of Error | ||
expect(errors instanceof Error).toEqual(true); | ||
expect(errors instanceof Error) | ||
.toEqual(true); | ||
@@ -18,3 +19,4 @@ /// adding errors to ExceptionList ( you can also merge other ExceptionList! ) | ||
/// get length of the list | ||
expect(errors.length).toEqual(3); | ||
expect(errors.length) | ||
.toEqual(3); | ||
@@ -25,1 +27,5 @@ /// get an error from the list | ||
/// get the translated error | ||
expect(errors.toString({ "Error1": "Error 1", "Error2": "Error 2", "Error3": "Error 3" })) | ||
.toEqual("Error 1, Error 2, Error 3"); | ||
@@ -14,6 +14,8 @@ | ||
var en = message.toString(); | ||
expect(en).toBe("hello oscar"); | ||
expect(en) | ||
.toBe("hello oscar"); | ||
/// Gets the Portuguese message | ||
var en = message.toString(Portuguese); | ||
expect(en).toBe("olá oscar"); | ||
expect(en) | ||
.toBe("olá oscar"); |
## Install | ||
## ![alt text](https://raw.githubusercontent.com/divhide/divhide-core/master/.readme/assets/nodejs-logo.jpg "Node.js") | ||
## Node.js | ||
@@ -23,3 +23,3 @@ | ||
## ![alt text](https://raw.githubusercontent.com/divhide/divhide-core/master/.readme/assets/bower-logo.jpg "Bower") | ||
## Bower | ||
@@ -43,1 +43,19 @@ ``` | ||
``` | ||
## Titanium | ||
``` | ||
bower install divhide | ||
``` | ||
Copy the file (`bower_components/divhide/dist/divhide.js`) to your titanium project (e.g. `app/lib/vendor`). | ||
``` html | ||
var Divhide = require("/vendor/divhide"); | ||
.... | ||
``` |
@@ -6,2 +6,4 @@ | ||
{%= _.badge("nodeio") %} | ||
{%= _.badge("donate") %} | ||
@@ -12,3 +14,8 @@ | ||
The intent of this library is to expose some building blocks that you can use when building a library, a mobile application, a web page, a web server, a command line utility... | ||
Please note that all examples are integrated on the CI build. That's the reason why the `expect` statements are visible here. | ||
![alt text](https://raw.githubusercontent.com/divhide/divhide-core/master/.readme/assets/nodejs-logo.jpg) ![alt text](https://raw.githubusercontent.com/divhide/divhide-core/master/.readme/assets/bower-logo.jpg) ![alt text](https://raw.githubusercontent.com/divhide/divhide-core/master/.readme/assets/titanium-logo.png) | ||
{%= _.include("Install.md") %} | ||
@@ -21,14 +28,2 @@ | ||
### Type | ||
{%= _.doc("Common/Type.md") %} | ||
### Safe | ||
{%= _.doc("Common/Safe.md") %} | ||
### Obj | ||
{%= _.doc("Common/Obj.md") %} | ||
### Arr | ||
{%= _.doc("Common/Arr.md") %} | ||
### I18N | ||
@@ -40,5 +35,2 @@ {%= _.doc("Common/I18N.md") %} | ||
### Chain | ||
{%= _.doc("Common/Chain.md") %} | ||
### Assert | ||
@@ -50,5 +42,24 @@ {%= _.doc("Common/Assert.md") %} | ||
### Chain | ||
{%= _.doc("Common/Chain.md") %} | ||
### Schema | ||
{%= _.doc("Common/Schema.md") %} | ||
### CustomSchema | ||
{%= _.doc("Common/CustomSchema.md") %} | ||
### Type | ||
{%= _.doc("Common/Type.md") %} | ||
### Safe | ||
{%= _.doc("Common/Safe.md") %} | ||
### Obj | ||
{%= _.doc("Common/Obj.md") %} | ||
### Arr | ||
{%= _.doc("Common/Arr.md") %} | ||
## Contribute | ||
@@ -55,0 +66,0 @@ {%= _.include("Contribute.md") %} |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "1.0.0-alpha.5", | ||
"version": "1.0.0-alpha.6", | ||
@@ -9,0 +9,0 @@ "authors": [ |
{ | ||
"name": "divhide", | ||
"description": "Divhide javascript core components for NodeJs, Browser, Titanium, ... and everything that run javascript.", | ||
"version": "1.0.0-alpha.5", | ||
"description": "Divhide javascript core components for NodeJs, Browser, Titanium, ... and everything that runs javascript.", | ||
"version": "1.0.0-alpha.6", | ||
"author": { | ||
@@ -28,3 +28,3 @@ "name": "Oscar Brito <aetheon@gmail.com>", | ||
"devDependencies": { | ||
"browserify": "^8.0.2", | ||
"browserify": "8.1.0", | ||
"expect.js": ">=0.2.0", | ||
@@ -31,0 +31,0 @@ "grunt": "~0.4.5", |
1354
README.md
@@ -5,2 +5,4 @@ # [![Divhide](http://blog.divhide.com/assets/images/divhide_128px.png)](http://divhide.com/) divhide-core | ||
[![NPM Stats](https://nodei.co/npm/divhide.png?downloads=true)](https://www.npmjs.com/package/divhide) | ||
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=NYVPSL7GBYD6A&lc=US&item_name=Oscar%20Brito¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted) | ||
@@ -11,7 +13,12 @@ | ||
The intent of this library is to expose some building blocks that you can use when building a library, a mobile application, a web page, a web server, a command line utility... | ||
Please note that all examples are integrated on the CI build. That's the reason why the `expect` statements are visible here. | ||
![alt text](https://raw.githubusercontent.com/divhide/divhide-core/master/.readme/assets/nodejs-logo.jpg) ![alt text](https://raw.githubusercontent.com/divhide/divhide-core/master/.readme/assets/bower-logo.jpg) ![alt text](https://raw.githubusercontent.com/divhide/divhide-core/master/.readme/assets/titanium-logo.png) | ||
### Install | ||
### ![alt text](https://raw.githubusercontent.com/divhide/divhide-core/master/.readme/assets/nodejs-logo.jpg "Node.js") | ||
### Node.js | ||
@@ -35,3 +42,3 @@ | ||
### ![alt text](https://raw.githubusercontent.com/divhide/divhide-core/master/.readme/assets/bower-logo.jpg "Bower") | ||
### Bower | ||
@@ -56,18 +63,37 @@ ``` | ||
### Titanium | ||
``` | ||
bower install divhide | ||
``` | ||
Copy the file (`bower_components/divhide/dist/divhide.js`) to your titanium project (e.g. `app/lib/vendor`). | ||
``` html | ||
var Divhide = require("/vendor/divhide"); | ||
.... | ||
``` | ||
## API | ||
* [API](#api) | ||
* [Type](#type) | ||
* [Safe](#safe) | ||
* [Obj](#obj) | ||
* [Arr](#arr) | ||
* [I18N](#i18n) | ||
* [Exception](#exception) | ||
* [Chain](#chain) | ||
* [Assert](#assert) | ||
* [Assertion](#assertion) | ||
* [Chain](#chain) | ||
* [Schema](#schema) | ||
* [CustomSchema](#customschema) | ||
* [Type](#type) | ||
* [Safe](#safe) | ||
* [Obj](#obj) | ||
* [Arr](#arr) | ||
* [Contribute](#contribute) | ||
@@ -78,487 +104,360 @@ * [Authors](#authors) | ||
### Type | ||
### I18N | ||
Type facility provides an API that can help you with typical operations using the javascript | ||
data Types. | ||
The Internationalization package. This package provides you with some utilities that will help you | ||
on your internationalization tasks. | ||
```js | ||
There's no intention for this library to contain translations for other languages. | ||
#### I18NString | ||
var Type = Divhide.Type; | ||
The I18NString is a String representation that can be translated. This string can be a plain String or | ||
a lodash template. | ||
var type = Type.of({}); | ||
expect(type).toBe("object"); | ||
This implementation creates a clear separation between the translation mechanism and it's internal logic. This package is used across the library in order to provide I18N out of the box! | ||
var type = Type.of([]); | ||
expect(type).toBe("array"); | ||
**Constructor** | ||
var type = Type.of(1); | ||
expect(type).toBe("number"); | ||
* `new I18N.String(message, data?, translations?)` | ||
<br /> | ||
Creates a String instance with the given *message*, associated *data*, and an object which contain the *translation*. | ||
var type = Type.of("name"); | ||
expect(type).toBe("string"); | ||
**Methods** | ||
var type = Type.of(true); | ||
expect(type).toBe("boolean"); | ||
* `.toString(translations?)` | ||
<br /> | ||
Gets the string representation of the error. If _translations_ object is provided the message Error message will try to be translated. | ||
var isArray = Type.isArray([]); | ||
expect(isArray).toBe(true); | ||
**Example** | ||
var isBoolean = Type.isBoolean(true); | ||
expect(isBoolean).toBe(true); | ||
var isFunction = Type.isFunction(function(){}); | ||
expect(isFunction).toBe(true); | ||
var isString = Type.isString(""); | ||
expect(isString).toBe(true); | ||
var isObject = Type.isObject({}); | ||
expect(isObject).toBe(true); | ||
var isObject = Type.isObject(null); | ||
expect(isObject).toBe(false); | ||
var isRegExp = Type.isRegExp(/reg/); | ||
expect(isRegExp).toBe(true); | ||
var isNumber = Type.isNumber(1); | ||
expect(isNumber).toBe(true); | ||
var isNumber = Type.isNumber("1.1"); | ||
expect(isNumber).toBe(true); | ||
var isDefined = Type.isDefined(null); | ||
expect(isDefined).toBe(false); | ||
var isDefined = Type.isDefined(undefined); | ||
expect(isDefined).toBe(false); | ||
var isEmpty = Type.isEmpty(""); | ||
expect(isEmpty).toBe(true); | ||
var isEmpty = Type.isEmpty([]); | ||
expect(isEmpty).toBe(true); | ||
var isEmpty = Type.isEmpty({}); | ||
expect(isEmpty).toBe(true); | ||
var isEmpty = Type.isEmpty(null); | ||
expect(isEmpty).toBe(true); | ||
``` | ||
### Safe | ||
Safe facility provides an API that can helps you safelly working with javascript data types. | ||
```js | ||
var Safe = Divhide.Safe; | ||
var I18NString = Divhide.I18N.String; | ||
var value = Safe.array(1); | ||
expect(value) | ||
.equals([1]); | ||
/// the external translation data | ||
var Portuguese = { | ||
"hello <%= username %>": "olá <%= username %>" | ||
}; | ||
var value = Safe.array(1); | ||
expect(value) | ||
.equals([1]); | ||
/// create a I18NString (message + data) | ||
var message = new I18NString("hello <%= username %>", { username: "oscar" }); | ||
var value = Safe.array([1, 2]); | ||
expect(value) | ||
.equals([1, 2]); | ||
/// Gets the English message | ||
var en = message.toString(); | ||
expect(en) | ||
.toBe("hello oscar"); | ||
var value = Safe.array(null, [ 1, 2 ]); | ||
expect(value) | ||
.equals([1, 2]); | ||
/// Gets the Portuguese message | ||
var en = message.toString(Portuguese); | ||
expect(en) | ||
.toBe("olá oscar"); | ||
var value = Safe.array("1", [1, 2]); | ||
expect(value) | ||
.equals(["1"]); | ||
``` | ||
```js | ||
### Exception | ||
var Safe = Divhide.Safe; | ||
The Exception package provides some utilities created in order to normalize the Error handling. | ||
var value = Safe.boolean(true); | ||
expect(value).toBe(true); | ||
All the Exceptions classes inherit from Error. Also, these classes are using the I18N | ||
package. | ||
var value = Safe.boolean(false); | ||
expect(value).toBe(false); | ||
var value = Safe.boolean(1); | ||
expect(value).toBe(true); | ||
#### Exception | ||
var value = Safe.boolean("1"); | ||
expect(value).toBe(true); | ||
The Exception class inherits from Error. This class is integrated with I18N package. | ||
var value = Safe.boolean("0"); | ||
expect(value).toBe(false); | ||
**Constructor** | ||
var value = Safe.boolean({}); | ||
expect(value).toBe(false); | ||
* `new Exception.Exception(message, data?)` | ||
<br /> | ||
Creates an instance of an exception with the given _message_ and _data_. | ||
var value = Safe.boolean({}, true); | ||
expect(value).toBe(true); | ||
**Methods** | ||
var value = Safe.boolean([]); | ||
expect(value).toBe(false); | ||
* `.message` | ||
<br /> | ||
Gets the string representation of the error. | ||
var value = Safe.boolean(null); | ||
expect(value).toBe(false); | ||
* `.toString(translations?)` | ||
<br /> | ||
Gets the string representation of the I18NString. You can provide a *translation* object. | ||
**Example** | ||
``` | ||
```js | ||
var Safe = Divhide.Safe; | ||
var Exception = Divhide.Exception.Exception; | ||
var fn = Safe.function(function(){}); | ||
expect(fn()) | ||
.toBe(undefined); | ||
/// the external translation data | ||
var Portuguese = { | ||
"The maximum value allowed is <%= value %>.": "Valor máximo é <%= value %>." | ||
}; | ||
var fn = Safe.function(""); | ||
expect(fn()) | ||
.toBe(undefined); | ||
var error = new Exception( | ||
"The maximum value allowed is <%= value %>.", | ||
{ value: 10 }); | ||
var fn = Safe.function("", function(){ return 1; }); | ||
expect(fn()) | ||
.toBe(1); | ||
/// Exception instance is an error! | ||
expect(error instanceof Error) | ||
.equals(true); | ||
/// Exception message | ||
expect(error.toString()) | ||
.equals("The maximum value allowed is 10."); | ||
/// I18N Exception message | ||
expect(error.toString(Portuguese)) | ||
.equals("Valor máximo é 10."); | ||
``` | ||
```js | ||
var Safe = Divhide.Safe; | ||
var value = Safe.length([1, 2]); | ||
expect(value).toBe(2); | ||
var value = Safe.length({ one: 1, two: 2}); | ||
expect(value).toBe(2); | ||
var value = Safe.length(2); | ||
expect(value).toBe(2); | ||
var value = Safe.length("hello"); | ||
expect(value).toBe(5); | ||
``` | ||
```js | ||
#### ExceptionList | ||
This allows you to create an Exception that contains inner exceptions. | ||
var Safe = Divhide.Safe; | ||
The ExceptionList is also an instance of Error and compatible with the I18N package. | ||
var value = Safe.number(1); | ||
expect(value).equals(1); | ||
**Constructor** | ||
var value = Safe.number(""); | ||
expect(value).equals(0); | ||
* `new Exception.ExceptionList()` | ||
var value = Safe.number("1"); | ||
expect(value).equals(1); | ||
**Methods** | ||
var value = Safe.number({}); | ||
expect(value).equals(0); | ||
* `.message` | ||
<br /> | ||
Gets the string representation of the ExceptionList. | ||
var value = Safe.number("", 1); | ||
expect(value).equals(1); | ||
* `.items` | ||
<br /> | ||
Gets the list of inner Error. | ||
* `.length` | ||
<br /> | ||
Gets length of the ExceptionList | ||
* `.clear()` | ||
<br /> | ||
Clear the ExceptionList | ||
``` | ||
* `.push(Error)` | ||
<br /> | ||
Push an Error instance to the ExceptionList | ||
```js | ||
* `.push(ExceptionList)` | ||
<br /> | ||
Merge the given ExceptionList into the instance | ||
* `.toString(translations?)` | ||
<br /> | ||
Gets the string representation of the I18NString. You can provide a *translation* object. | ||
var Safe = Divhide.Safe; | ||
var value = Safe.object({ one: 1 }); | ||
expect(value).equals({ one: 1 }); | ||
**Example** | ||
var value = Safe.object([]); | ||
expect(value).equals({}); | ||
var value = Safe.object([], { one: 1 }); | ||
expect(value).equals({ one: 1 }); | ||
``` | ||
```js | ||
var Safe = Divhide.Safe; | ||
var Exception = Divhide.Exception.Exception, | ||
ExceptionList = Divhide.Exception.ExceptionList; | ||
var value = Safe.regexp(/regexp/); | ||
expect(value) | ||
.toEqual(/regexp/); | ||
var value = Safe.regexp("/regexp/"); | ||
expect(value) | ||
.toEqual(/regexp/); | ||
var errors = new ExceptionList(); | ||
var value = Safe.regexp(""); | ||
expect(value) | ||
.toEqual(/^$/); | ||
/// is an instance of Error | ||
expect(errors instanceof Error) | ||
.toEqual(true); | ||
var value = Safe.regexp("name"); | ||
expect(value) | ||
.toEqual(/^name$/); | ||
/// adding errors to ExceptionList ( you can also merge other ExceptionList! ) | ||
errors.push( new Exception("Error1") ); | ||
errors.push( new Exception("Error2") ); | ||
errors.push( new Exception("Error3") ); | ||
var value = Safe.regexp({}, /regexp/); | ||
expect(value) | ||
.toEqual(/regexp/); | ||
/// get length of the list | ||
expect(errors.length) | ||
.toEqual(3); | ||
/// get an error from the list | ||
expect(errors.items[0].toString()) | ||
.toEqual("Error1"); | ||
/// get the translated error | ||
expect(errors.toString({ "Error1": "Error 1", "Error2": "Error 2", "Error3": "Error 3" })) | ||
.toEqual("Error 1, Error 2, Error 3"); | ||
``` | ||
```js | ||
var Safe = Divhide.Safe; | ||
var value = Safe.string(""); | ||
expect(value).toBe(""); | ||
var value = Safe.string({}); | ||
expect(value).toBe(""); | ||
var value = Safe.string({}, "default"); | ||
expect(value).toBe("default"); | ||
``` | ||
```js | ||
### Assert | ||
var Safe = Divhide.Safe; | ||
The Assert facility provides an assertion expression builder with some pre-built functions. | ||
var value = Safe.value(1); | ||
expect(value).toBe(1); | ||
**Methods** | ||
var value = Safe.value("1"); | ||
expect(value).toBe("1"); | ||
* `.required()` | ||
<br /> | ||
Set the expected value to be defined | ||
var value = Safe.value(null); | ||
expect(value).toBe(null); | ||
* `.string()` | ||
<br /> | ||
Set the expected value to be a string | ||
var value = Safe.value(undefined); | ||
expect(value).toBe(null); | ||
* `.array()` | ||
<br /> | ||
Set the expected value to be an array | ||
var value = Safe.value(null, 1); | ||
expect(value).toBe(1); | ||
* `.object()` | ||
<br /> | ||
Set the expected value to be an object | ||
* `.number()` | ||
<br /> | ||
Set the expected value to be a number | ||
* `.max(number)` | ||
<br /> | ||
Set the expected value to have a specified maximum. In the case of String or Array the context will be the value length. In the case of an object will be the number of keys | ||
* `.min(number)` | ||
<br /> | ||
Set the expected value to have a specified minimum. In the case of String or Array the context will be the value length. In the case of an object will be the number of keys | ||
``` | ||
* `.regex(value)` | ||
<br /> | ||
Set the expected value to pass the regex | ||
* `.isValid(value)` | ||
<br /> | ||
Test if the given value is valid within the current assertion | ||
* `.assert(value)` | ||
<br /> | ||
Return the given value if valid; otherwise will throw an Exception | ||
### Obj | ||
**Example** | ||
Object facility provides some utility function to use on Objects. | ||
```js | ||
var Obj = Divhide.Obj; | ||
var Assert = Divhide.Assert; | ||
var results = Obj.filter({ "one": 1, "two": 2 }); | ||
expect(results) | ||
.toEqual(["one", "two"]); | ||
/// Test if the value is valid | ||
var isValid = Assert.required() | ||
.string() | ||
.regex("^M") | ||
.max(10) | ||
.min(5) | ||
.isValid("Mary"); | ||
var results = Obj.filter({ "one": 1, "two": 2 }, "one"); | ||
expect(results) | ||
.toEqual(["one"]); | ||
expect(isValid) | ||
.toBe(false); | ||
var results = Obj.filter({ "one": 1, "two": 2 }, "three"); | ||
expect(results) | ||
.toEqual([]); | ||
/// Valid assertion: | ||
var obj = Assert.required() | ||
.array() | ||
.max(5) | ||
.assert([1, 2, 4, 5]); | ||
expect(obj) | ||
.equals([1, 2, 4, 5]); | ||
``` | ||
/// Invalid assertion: | ||
var fn = function(){ | ||
### Arr | ||
Assert.required() | ||
.array() | ||
.max(1) /// will be on array context | ||
.assert(["first", "second"]); | ||
Array facility provides an API to easily manage array references. | ||
}; | ||
```js | ||
expect(fn) | ||
.toThrow(); | ||
var Arr = Divhide.Arr; | ||
var value = Arr.index([1 ,2, 3], 0); | ||
expect(value).toBe(1); | ||
var value = Arr.index([1 ,2, 3], 10); | ||
expect(value).toBeNull(); | ||
var first = Arr.first([1 ,2, 3]); | ||
expect(first).toBe(first); | ||
var last = Arr.last([1 ,2, 3]); | ||
expect(last).toBe(3); | ||
var length = Arr.length([1 ,2, 3]); | ||
expect(last).toBe(3); | ||
var array = [1, 2, 3]; | ||
Arr.insert(array, 4); | ||
expect(array).toEqual([1, 2, 3, 4]); | ||
var array = Arr.insert([1 ,2, 3], [4, 5]); | ||
expect(array).toEqual([1, 2, 3, 4, 5]); | ||
var array = Arr.insert([1 ,2, 3], -1, 0); | ||
expect(array).toEqual([-1, 1, 2, 3]); | ||
var array = [1, 2, 3]; | ||
Arr.remove(array, 0); | ||
expect(array).toEqual([2, 3]); | ||
var array = [1, 2, 3]; | ||
Arr.remove(array, 0, 2); | ||
expect(array).toEqual([3]); | ||
``` | ||
### I18N | ||
Internationalization package. This package provides you with some utilities that will help you | ||
on your internationalization tasks. | ||
### Assertion | ||
There's no intention for this library to contain translations for other languages. | ||
Assertion facility provides a way to build custom Asserts. You can create your own assertion functions | ||
and integrate them with the Assert facility. | ||
##### I18NString | ||
**Constructor** | ||
The I18NString is a String representation that can be translation. This string can be a plain String or | ||
a lodash template that can be binded to some data. | ||
* `new Assertion(methods)` | ||
<br /> | ||
Creates a custom assertion instance that contains the given methods plus the default methods (see Assert). | ||
When getting the result a translation dictionary can be provided. If the string/template exists on the | ||
translation dictionary the translation is returned. | ||
This implementation creates a clear separation between the translation mechanism and it's internal logic. | ||
```js | ||
/// Assertion builder | ||
var Assertion = Divhide.Assertion; | ||
``` | ||
/// Create the custom Assert facility | ||
var Assert = new Assertion({ | ||
/** | ||
* | ||
* Tests if the string starts with the given value | ||
* | ||
* @param {String} val | ||
* @param {String} str | ||
* @return {String} | ||
*/ | ||
startsWith: function(val, str){ | ||
### Exception | ||
if(val.indexOf(str) !== 0){ | ||
throw new Error("Does not starts with " + str); | ||
} | ||
The Exception package provides you with some utilities created in order to | ||
normalize the Error handling. | ||
} | ||
All the Exceptions classes inherit from Error. This means that you can throw | ||
Errors with these! | ||
}); | ||
##### Exception | ||
/// Test if the value is valid | ||
var isValid = Assert | ||
.required() | ||
.string() | ||
.startsWith("Mary") | ||
.isValid("Mary and Peter"); | ||
The Exception class inherits from Error. This class is integrated with I18N package. | ||
expect(isValid) | ||
.toBe(true) | ||
```js | ||
/// Assert value | ||
var value = Assert | ||
.required() | ||
.string() | ||
.startsWith("Mary") | ||
.assert("Mary and Peter"); | ||
var Exception = Divhide.Exception.Exception; | ||
expect(value) | ||
.equals("Mary and Peter"); | ||
/// the external translation data | ||
var Portuguese = { | ||
"The maximum value allowed is <%= value %>.": "Valor máximo é <%= value %>." | ||
}; | ||
var error = new Exception( | ||
"The maximum value allowed is <%= value %>.", | ||
{ value: 10 }); | ||
/// Exception instance is an error! | ||
expect(error instanceof Error).equals(true); | ||
/// Exception message | ||
expect(error.toString()) | ||
.equals("The maximum value allowed is 10."); | ||
/// I18N Exception message | ||
expect(error.toString(Portuguese)) | ||
.equals("Valor máximo é 10."); | ||
``` | ||
##### ExceptionList | ||
The ExceptionList is also an instance of Error. This allows you to create an Exception that contains | ||
multiple sub Exceptions. | ||
```js | ||
var Exception = Divhide.Exception.Exception, | ||
ExceptionList = Divhide.Exception.ExceptionList; | ||
var errors = new ExceptionList(); | ||
/// is an instance of Error | ||
expect(errors instanceof Error).toEqual(true); | ||
/// adding errors to ExceptionList ( you can also merge other ExceptionList! ) | ||
errors.push( new Exception("Error1") ); | ||
errors.push( new Exception("Error2") ); | ||
errors.push( new Exception("Error3") ); | ||
/// get length of the list | ||
expect(errors.length).toEqual(3); | ||
/// get an error from the list | ||
expect(errors.items[0].toString()) | ||
.toEqual("Error1"); | ||
``` | ||
### Chain | ||
@@ -570,2 +469,13 @@ | ||
**Constructor** | ||
* `new Chain(chainableFns, evaluationFns, options)` | ||
<br /> | ||
Creates a Chain instance that allows you to execute chainable methods (__chainableFns__). The chain is only executed | ||
when an evaluation method (__evaluationFns__) is called. | ||
<br /> | ||
* chainableFns | ||
* evaluationFns | ||
* options - `{ pipe: Boolean, type: Function, argument: *, scope: Object }`. | ||
```js | ||
@@ -620,162 +530,84 @@ | ||
Chainable functions are functions which can be chained. Each function can have as many arguments as you want but the first arguments will be | ||
injected by the evalutaion function or the previous executed function ( if pipe: true is defined on options ). | ||
A Evaluation function will execute the list of chainable functions previous defined. This function receives as argument the result of the | ||
chain execution and the if exists the execution error. The evaluation function can have several parameters; the ''err'' parameter is injected | ||
in the end. | ||
The specified functions share the same scope. This means that you can share values between execution by using the function context. | ||
### Schema | ||
The Schema facility provide an easy way to write validation rules. Using a chainable API you can | ||
compile and/or evaluate the rules. | ||
**Methods** | ||
`.any()` | ||
<br /> | ||
Set the expected type as any object. | ||
### Assert | ||
`.string()` | ||
<br /> | ||
Set the expected type as a string | ||
Assert facility provides an API to build expressions that can be evaluated with | ||
.isValid() and .assert(). | ||
`.number()` | ||
<br /> | ||
Set the expected type as a number | ||
```js | ||
`.object(value)` | ||
<br /> | ||
Set the expected type as an object. The **value** is an object with rules. | ||
`.array(value)` | ||
<br /> | ||
Set the expected type as an object. The **value** is an array with rules. | ||
var Assert = Divhide.Assert; | ||
`.required()` | ||
<br /> | ||
Set as required. | ||
/// Test if the value is valid | ||
var isValid = Assert | ||
.required() | ||
.string() | ||
.regex("^M") | ||
.max(10) | ||
.min(5) | ||
`.optional()` | ||
<br /> | ||
Set as optional | ||
/// test if the rules above are valid | ||
.isValid("Mary"); | ||
`.default(value)` | ||
<br /> | ||
Set the default value. This is used when the schema its required and the provided value is null. | ||
expect(isValid) | ||
.toBe(false); | ||
`.repeatable()` | ||
<br /> | ||
Set the type as repeatable. This repeats the schema through the structure (array only!). | ||
`.min(value)` | ||
<br /> | ||
Set the min value expected. If in number context the value is used. If in string context the length is used. | ||
If in array context the length is used. If in object context the number of keys is used. | ||
/// Valid assertion: | ||
var obj = Assert | ||
.required() | ||
.array() | ||
.max(5) | ||
.assert([1, 2, 4, 5]); | ||
`.max(value)` | ||
<br /> | ||
Set the max value expected. If in number context the value is used. If in string context the length is used. | ||
If in array context the length is used. If in object context the number of keys is used. | ||
expect(obj) | ||
.equals([1, 2, 4, 5]); | ||
`.regex(value)` | ||
<br /> | ||
Sets a regexp condition ( only use on string context! ) | ||
`.compile()` | ||
<br /> | ||
Compiles the schema. If you are using the same Schema multiple time you can compile it for performance reasons. | ||
This avoid compiling the Schema before every usage. | ||
/// Invalid assertion: | ||
var fn = function(){ | ||
`.value(value)` | ||
<br /> | ||
Test the schema returning the normalized value if valid. Throws an Error if invalid. | ||
Assert.required() | ||
.array() | ||
.max(1) /// will be on array context | ||
.assert(["first", "second"]); | ||
`.isValid(value)` | ||
<br /> | ||
Test the schema return if its valid or not. | ||
}; | ||
`.errors(value)` | ||
<br /> | ||
Test the schema returning an Array with the errors | ||
expect(fn) | ||
.toThrow(); | ||
**Example** overview | ||
``` | ||
The following methods are provided by default. | ||
given value. | ||
```js | ||
.required() /// the value should not be null or undifined | ||
.string() /// the value should be a string | ||
.array() /// the value should be an array | ||
.object() /// the value should be an object | ||
.number() /// the value should be a number | ||
.max(10) /// the maximum value applied to the context -> string: length, object: keys, array: items, number: value | ||
.min(5) /// the minimum value applied to the context -> string: length, object: keys, array: items, number: value | ||
.regex("^M") /// the value should match the regex | ||
``` | ||
### Assertion | ||
Assertion facility provides a way to build custom Asserts. You can create your own assertion functions | ||
and integrate them with the Assert facility. | ||
```js | ||
/// Assertion builder | ||
var Assertion = Divhide.Assertion; | ||
/// Create the custom Assert facility | ||
var Assert = new Assertion({ | ||
/** | ||
* | ||
* Tests if the string starts with the given value | ||
* | ||
* @param {String} val | ||
* @param {String} str | ||
* @return {String} | ||
*/ | ||
startsWith: function(val, str){ | ||
if(val.indexOf(str) !== 0){ | ||
throw new Error("Does not starts with " + str); | ||
} | ||
} | ||
}); | ||
/// Test if the value is valid | ||
var isValid = Assert | ||
.required() | ||
.string() | ||
.startsWith("Mary") | ||
.isValid("Mary and Peter"); | ||
expect(isValid) | ||
.toBe(true) | ||
/// Assert value | ||
var value = Assert | ||
.required() | ||
.string() | ||
.startsWith("Mary") | ||
.assert("Mary and Peter"); | ||
expect(value) | ||
.equals("Mary and Peter"); | ||
``` | ||
### Schema | ||
The Schema facility provide an easy way to write validation rules. Using a chainable API you can | ||
compile and/or evaluate the rules. | ||
**Overview** | ||
```js | ||
var Schema = Divhide.Schema; | ||
@@ -815,45 +647,4 @@ | ||
**Example** String Schema | ||
**Schema chainable API:** | ||
`.any()` Set the expected type as any object. | ||
`.string()` Set the expected type as a string | ||
`.number()` Set the expected type as a number | ||
`.object(value)` Set the expected type as an object. The **value** is an object with rules. | ||
`.array(value)` Set the expected type as an object. The **value** is an array with rules. | ||
`.required()` Set as required. **By default every rule is required!** | ||
`.optional()` Set as optional | ||
`.default(value)` Set the default value. This is used when the schema its required and the provided value is null. | ||
`.repeatable()` Set the type as repeatable. This repeats the schema through the structure (array only!). | ||
`.min(value)` Set the min value expected. If in number context the value is used. If in string context the length is used. | ||
If in array context the length is used. If in object context the number of keys is used. | ||
`.max(value)` Set the max value expected. If in number context the value is used. If in string context the length is used. | ||
If in array context the length is used. If in object context the number of keys is used. | ||
`.regex(value)` Sets a regexp condition ( only use on string context! ) | ||
**Schema evaluation API:** | ||
`.compile()` Compiles the schema. If you are using the same Schema multiple time you can compile it for performance reasons. | ||
This avoid compiling the Schema before every usage. | ||
`.value(value)` Test the schema returning the normalized value if valid. Throws an Error if invalid. | ||
`.isValid(value)` Test the schema return if its valid or not. | ||
`.errors(value)` Test the schema returning an Array with the errors | ||
**Example 1:** String Schema | ||
```js | ||
@@ -913,3 +704,3 @@ | ||
**Example 2:** Number Schema | ||
**Example** Number Schema | ||
@@ -976,3 +767,3 @@ ```js | ||
**Example 3:** Any Schema | ||
**Example** Any Schema | ||
@@ -1013,3 +804,3 @@ ```js | ||
**Example 4:** Object Schema | ||
**Example** Object Schema | ||
@@ -1062,3 +853,3 @@ Objects schema is set by applying rules to each property of the object. You can also use regular expressions on | ||
``` | ||
**Example 5:** Array Schema | ||
**Example** Array Schema | ||
@@ -1145,2 +936,505 @@ The following example describe an array rule that is optional and its expecting three items. | ||
### CustomSchema | ||
### Type | ||
Type facility provides an API that can help you with typical operations using the javascript | ||
data Types. | ||
**Methods** | ||
* `Type.of(value)` | ||
<br /> | ||
Gets the string representation of the given value. | ||
* `Type.isArray(value)` | ||
* `Type.isBoolean(value)` | ||
* `Type.isFunction(value)` | ||
* `Type.isString(value)` | ||
* `Type.isObject(value)` | ||
* `Type.isBoolean(value)` | ||
* `Type.isRegExp(value)` | ||
* `Type.isRegExpStr(value)` | ||
* `Type.isNumber(value)` | ||
* `Type.isDefined(value)` | ||
<br /> | ||
Checks if the value is defined. | ||
* `Type.isEmpty(value)` | ||
<br /> | ||
Checks if the value is empty (executed in string, list and object context ). | ||
```js | ||
var Type = Divhide.Type; | ||
var type = Type.of({}); | ||
expect(type).toBe("object"); | ||
var type = Type.of([]); | ||
expect(type).toBe("array"); | ||
var type = Type.of(1); | ||
expect(type).toBe("number"); | ||
var type = Type.of("name"); | ||
expect(type).toBe("string"); | ||
var type = Type.of(true); | ||
expect(type).toBe("boolean"); | ||
var isArray = Type.isArray([]); | ||
expect(isArray).toBe(true); | ||
var isBoolean = Type.isBoolean(true); | ||
expect(isBoolean).toBe(true); | ||
var isFunction = Type.isFunction(function(){}); | ||
expect(isFunction).toBe(true); | ||
var isString = Type.isString(""); | ||
expect(isString).toBe(true); | ||
var isObject = Type.isObject({}); | ||
expect(isObject).toBe(true); | ||
var isObject = Type.isObject(null); | ||
expect(isObject).toBe(false); | ||
var isRegExp = Type.isRegExp(/reg/); | ||
expect(isRegExp).toBe(true); | ||
var isNumber = Type.isNumber(1); | ||
expect(isNumber).toBe(true); | ||
var isNumber = Type.isNumber("1.1"); | ||
expect(isNumber).toBe(true); | ||
var isDefined = Type.isDefined(null); | ||
expect(isDefined).toBe(false); | ||
var isDefined = Type.isDefined(undefined); | ||
expect(isDefined).toBe(false); | ||
var isEmpty = Type.isEmpty(""); | ||
expect(isEmpty).toBe(true); | ||
var isEmpty = Type.isEmpty([]); | ||
expect(isEmpty).toBe(true); | ||
var isEmpty = Type.isEmpty({}); | ||
expect(isEmpty).toBe(true); | ||
var isEmpty = Type.isEmpty(null); | ||
expect(isEmpty).toBe(true); | ||
``` | ||
### Safe | ||
Safe facility provides an API that can helps you safelly working with javascript data types. This methods | ||
are supposed to work with different value types. | ||
**Methods** | ||
* `Safe.array(value, defaultValue?)` | ||
<br /> | ||
Gets the value in the array representation. __defaultValue__ is returned if defined and if value is not | ||
an array. | ||
* `Safe.boolean(value, defaultValue?)` | ||
* `Safe.string(value, defaultValue?)` | ||
* `Safe.object(value, defaultValue?)` | ||
* `Safe.number(value, defaultValue?)` | ||
* `Safe.function(value, defaultValue?)` | ||
* `Safe.value(value, defaultValue?)` | ||
* `Safe.regexp(value, defaultValue)` | ||
* `Safe.instanceOf(value, Class)` | ||
<br /> | ||
Gets an instance of the given value if is an instance of the given Class, otherwise it will | ||
create an instance. | ||
* `Safe.length(value)` | ||
<br /> | ||
Gets the length of the value. | ||
* `Safe.coerce(value, expected)` | ||
Gets the value coerced by the expected value type. | ||
**Example** Array | ||
```js | ||
var Safe = Divhide.Safe; | ||
var value = Safe.array(1); | ||
expect(value) | ||
.equals([1]); | ||
var value = Safe.array(1); | ||
expect(value) | ||
.equals([1]); | ||
var value = Safe.array([1, 2]); | ||
expect(value) | ||
.equals([1, 2]); | ||
var value = Safe.array(null, [ 1, 2 ]); | ||
expect(value) | ||
.equals([1, 2]); | ||
var value = Safe.array("1", [1, 2]); | ||
expect(value) | ||
.equals(["1"]); | ||
``` | ||
**Example** Boolean | ||
```js | ||
var Safe = Divhide.Safe; | ||
var value = Safe.boolean(true); | ||
expect(value).toBe(true); | ||
var value = Safe.boolean(false); | ||
expect(value).toBe(false); | ||
var value = Safe.boolean(1); | ||
expect(value).toBe(true); | ||
var value = Safe.boolean("1"); | ||
expect(value).toBe(true); | ||
var value = Safe.boolean("0"); | ||
expect(value).toBe(false); | ||
var value = Safe.boolean({}); | ||
expect(value).toBe(false); | ||
var value = Safe.boolean({}, true); | ||
expect(value).toBe(true); | ||
var value = Safe.boolean([]); | ||
expect(value).toBe(false); | ||
var value = Safe.boolean(null); | ||
expect(value).toBe(false); | ||
``` | ||
**Example** Function | ||
```js | ||
var Safe = Divhide.Safe; | ||
var fn = Safe.function(function(){}); | ||
expect(fn()) | ||
.toBe(undefined); | ||
var fn = Safe.function(""); | ||
expect(fn()) | ||
.toBe(undefined); | ||
var fn = Safe.function("", function(){ return 1; }); | ||
expect(fn()) | ||
.toBe(1); | ||
``` | ||
**Example** Length | ||
```js | ||
var Safe = Divhide.Safe; | ||
var value = Safe.length([1, 2]); | ||
expect(value).toBe(2); | ||
var value = Safe.length({ one: 1, two: 2}); | ||
expect(value).toBe(2); | ||
var value = Safe.length(2); | ||
expect(value).toBe(2); | ||
var value = Safe.length("hello"); | ||
expect(value).toBe(5); | ||
``` | ||
**Example** Number | ||
```js | ||
var Safe = Divhide.Safe; | ||
var value = Safe.number(1); | ||
expect(value).equals(1); | ||
var value = Safe.number(""); | ||
expect(value).equals(0); | ||
var value = Safe.number("1"); | ||
expect(value).equals(1); | ||
var value = Safe.number({}); | ||
expect(value).equals(0); | ||
var value = Safe.number("", 1); | ||
expect(value).equals(1); | ||
``` | ||
**Example** Object | ||
```js | ||
var Safe = Divhide.Safe; | ||
var value = Safe.object({ one: 1 }); | ||
expect(value).equals({ one: 1 }); | ||
var value = Safe.object([]); | ||
expect(value).equals({}); | ||
var value = Safe.object([], { one: 1 }); | ||
expect(value).equals({ one: 1 }); | ||
``` | ||
**Example** Regex | ||
```js | ||
var Safe = Divhide.Safe; | ||
var value = Safe.regexp(/regexp/); | ||
expect(value) | ||
.toEqual(/regexp/); | ||
var value = Safe.regexp("/regexp/"); | ||
expect(value) | ||
.toEqual(/regexp/); | ||
var value = Safe.regexp(""); | ||
expect(value) | ||
.toEqual(/^$/); | ||
var value = Safe.regexp("name"); | ||
expect(value) | ||
.toEqual(/^name$/); | ||
var value = Safe.regexp({}, /regexp/); | ||
expect(value) | ||
.toEqual(/regexp/); | ||
``` | ||
**Example** String | ||
```js | ||
var Safe = Divhide.Safe; | ||
var value = Safe.string(""); | ||
expect(value).toBe(""); | ||
var value = Safe.string({}); | ||
expect(value).toBe(""); | ||
var value = Safe.string({}, "default"); | ||
expect(value).toBe("default"); | ||
``` | ||
**Example** Value | ||
```js | ||
var Safe = Divhide.Safe; | ||
var value = Safe.value(1); | ||
expect(value).toBe(1); | ||
var value = Safe.value("1"); | ||
expect(value).toBe("1"); | ||
var value = Safe.value(null); | ||
expect(value).toBe(null); | ||
var value = Safe.value(undefined); | ||
expect(value).toBe(null); | ||
var value = Safe.value(null, 1); | ||
expect(value).toBe(1); | ||
``` | ||
### Obj | ||
Object facility provides some utility function to use on Objects. | ||
**Methods** | ||
* `filter(value, filter)` | ||
<br /> | ||
Returns an array with all the keys that match the __filter__. | ||
**Example** | ||
```js | ||
var Obj = Divhide.Obj; | ||
var results = Obj.filter({ "one": 1, "two": 2 }); | ||
expect(results) | ||
.toEqual(["one", "two"]); | ||
var results = Obj.filter({ "one": 1, "two": 2 }, "one"); | ||
expect(results) | ||
.toEqual(["one"]); | ||
var results = Obj.filter({ "one": 1, "two": 2 }, "three"); | ||
expect(results) | ||
.toEqual([]); | ||
``` | ||
### Arr | ||
Array facility provides an API to easily manage array references. | ||
**Methods** | ||
* `index(value, index)` | ||
<br /> | ||
Gets the value of the index. | ||
* `first(value)` | ||
* `last(value)` | ||
* `length(value)` | ||
* `insert(array, value, index?)` | ||
Inserts the given value(s) on the given index of the __array__. | ||
* `remove(array, index, n?)` | ||
Removes the given __n__ elements from the __index__ from the __array__. | ||
**Example** | ||
```js | ||
var Arr = Divhide.Arr; | ||
var value = Arr.index([1 ,2, 3], 0); | ||
expect(value).toBe(1); | ||
var value = Arr.index([1 ,2, 3], 10); | ||
expect(value).toBeNull(); | ||
var first = Arr.first([1 ,2, 3]); | ||
expect(first).toBe(first); | ||
var last = Arr.last([1 ,2, 3]); | ||
expect(last).toBe(3); | ||
var length = Arr.length([1 ,2, 3]); | ||
expect(last).toBe(3); | ||
var array = [1, 2, 3]; | ||
Arr.insert(array, 4); | ||
expect(array).toEqual([1, 2, 3, 4]); | ||
var array = Arr.insert([1 ,2, 3], [4, 5]); | ||
expect(array).toEqual([1, 2, 3, 4, 5]); | ||
var array = Arr.insert([1 ,2, 3], -1, 0); | ||
expect(array).toEqual([-1, 1, 2, 3]); | ||
var array = [1, 2, 3]; | ||
Arr.remove(array, 0); | ||
expect(array).toEqual([2, 3]); | ||
var array = [1, 2, 3]; | ||
Arr.remove(array, 0, 2); | ||
expect(array).toEqual([3]); | ||
``` | ||
## Contribute | ||
@@ -1210,4 +1504,4 @@ | ||
## License | ||
Copyright (c) 2014 Oscar Brito <aetheon@gmail.com>, contributors. | ||
Copyright (c) 2015 Oscar Brito <aetheon@gmail.com>, contributors. | ||
Released under the license | ||
Array facility provides an API to easily manage array references. | ||
**Methods** | ||
* `index(value, index)` | ||
<br /> | ||
Gets the value of the index. | ||
* `first(value)` | ||
* `last(value)` | ||
* `length(value)` | ||
* `insert(array, value, index?)` | ||
Inserts the given value(s) on the given index of the __array__. | ||
* `remove(array, index, n?)` | ||
Removes the given __n__ elements from the __index__ from the __array__. | ||
**Example** | ||
```js | ||
@@ -5,0 +26,0 @@ |
Assert facility provides an API to build expressions that can be evaluated with | ||
.isValid() and .assert(). | ||
The Assert facility provides an assertion expression builder with some pre-built functions. | ||
```js | ||
**Methods** | ||
{%= _.include("Assert/AssertExample.js") %} | ||
* `.required()` | ||
<br /> | ||
Set the expected value to be defined | ||
``` | ||
* `.string()` | ||
<br /> | ||
Set the expected value to be a string | ||
* `.array()` | ||
<br /> | ||
Set the expected value to be an array | ||
The following methods are provided by default. | ||
given value. | ||
* `.object()` | ||
<br /> | ||
Set the expected value to be an object | ||
```js | ||
* `.number()` | ||
<br /> | ||
Set the expected value to be a number | ||
* `.max(number)` | ||
<br /> | ||
Set the expected value to have a specified maximum. In the case of String or Array the context will be the value length. In the case of an object will be the number of keys | ||
.required() /// the value should not be null or undifined | ||
.string() /// the value should be a string | ||
.array() /// the value should be an array | ||
.object() /// the value should be an object | ||
.number() /// the value should be a number | ||
.max(10) /// the maximum value applied to the context -> string: length, object: keys, array: items, number: value | ||
.min(5) /// the minimum value applied to the context -> string: length, object: keys, array: items, number: value | ||
.regex("^M") /// the value should match the regex | ||
* `.min(number)` | ||
<br /> | ||
Set the expected value to have a specified minimum. In the case of String or Array the context will be the value length. In the case of an object will be the number of keys | ||
* `.regex(value)` | ||
<br /> | ||
Set the expected value to pass the regex | ||
``` | ||
* `.isValid(value)` | ||
<br /> | ||
Test if the given value is valid within the current assertion | ||
* `.assert(value)` | ||
<br /> | ||
Return the given value if valid; otherwise will throw an Exception | ||
**Example** | ||
```js | ||
{%= _.include("Assert/AssertExample.js") %} | ||
``` |
@@ -5,2 +5,9 @@ | ||
**Constructor** | ||
* `new Assertion(methods)` | ||
<br /> | ||
Creates a custom assertion instance that contains the given methods plus the default methods (see Assert). | ||
```js | ||
@@ -7,0 +14,0 @@ |
@@ -214,8 +214,11 @@ 'use strict'; | ||
* { | ||
* type : {}, | ||
* pipe : [true|false], | ||
* instanceof : Function, | ||
* context : {}, | ||
* scope : Function, | ||
* argument : undefined | ||
* } | ||
* }, | ||
* | ||
* /// context | ||
* {} | ||
* | ||
* ); | ||
@@ -222,0 +225,0 @@ * |
@@ -6,2 +6,13 @@ | ||
**Constructor** | ||
* `new Chain(chainableFns, evaluationFns, options)` | ||
<br /> | ||
Creates a Chain instance that allows you to execute chainable methods (__chainableFns__). The chain is only executed | ||
when an evaluation method (__evaluationFns__) is called. | ||
<br /> | ||
* chainableFns | ||
* evaluationFns | ||
* options - `{ pipe: Boolean, type: Function, argument: *, scope: Object }`. | ||
```js | ||
@@ -13,11 +24,1 @@ | ||
Chainable functions are functions which can be chained. Each function can have as many arguments as you want but the first arguments will be | ||
injected by the evalutaion function or the previous executed function ( if pipe: true is defined on options ). | ||
A Evaluation function will execute the list of chainable functions previous defined. This function receives as argument the result of the | ||
chain execution and the if exists the execution error. The evaluation function can have several parameters; the ''err'' parameter is injected | ||
in the end. | ||
The specified functions share the same scope. This means that you can share values between execution by using the function context. | ||
The Exception package provides you with some utilities created in order to | ||
normalize the Error handling. | ||
The Exception package provides some utilities created in order to normalize the Error handling. | ||
All the Exceptions classes inherit from Error. This means that you can throw | ||
Errors with these! | ||
All the Exceptions classes inherit from Error. Also, these classes are using the I18N | ||
package. | ||
#### Exception | ||
### Exception | ||
The Exception class inherits from Error. This class is integrated with I18N package. | ||
**Constructor** | ||
* `new Exception.Exception(message, data?)` | ||
<br /> | ||
Creates an instance of an exception with the given _message_ and _data_. | ||
**Methods** | ||
* `.message` | ||
<br /> | ||
Gets the string representation of the error. | ||
* `.toString(translations?)` | ||
<br /> | ||
Gets the string representation of the I18NString. You can provide a *translation* object. | ||
**Example** | ||
```js | ||
@@ -18,7 +36,45 @@ | ||
#### ExceptionList | ||
### ExceptionList | ||
The ExceptionList is also an instance of Error. This allows you to create an Exception that contains | ||
multiple sub Exceptions. | ||
This allows you to create an Exception that contains inner exceptions. | ||
The ExceptionList is also an instance of Error and compatible with the I18N package. | ||
**Constructor** | ||
* `new Exception.ExceptionList()` | ||
**Methods** | ||
* `.message` | ||
<br /> | ||
Gets the string representation of the ExceptionList. | ||
* `.items` | ||
<br /> | ||
Gets the list of inner Error. | ||
* `.length` | ||
<br /> | ||
Gets length of the ExceptionList | ||
* `.clear()` | ||
<br /> | ||
Clear the ExceptionList | ||
* `.push(Error)` | ||
<br /> | ||
Push an Error instance to the ExceptionList | ||
* `.push(ExceptionList)` | ||
<br /> | ||
Merge the given ExceptionList into the instance | ||
* `.toString(translations?)` | ||
<br /> | ||
Gets the string representation of the I18NString. You can provide a *translation* object. | ||
**Example** | ||
```js | ||
@@ -25,0 +81,0 @@ |
@@ -28,4 +28,6 @@ "use strict"; | ||
*/ | ||
Internal.getMessage = function(message, messageData){ | ||
Internal.getI18NMessage = function(message, messageData){ | ||
messageData = Safe.object(messageData); | ||
if( message instanceof I18NString ){ | ||
@@ -53,34 +55,8 @@ return message; | ||
var _i18nMessage = "", | ||
_i18nMessageData = messageData; | ||
message = Internal.getI18NMessage(message, messageData); | ||
messageData = Safe.object(messageData); | ||
Object.defineProperty( | ||
this, | ||
"i18nMessage", | ||
{ | ||
get: function(){ | ||
return _i18nMessage; | ||
}, | ||
set: function(message){ | ||
_i18nMessage = Internal.getMessage(message, _i18nMessageData); | ||
}, | ||
enumerable: false, | ||
configurable: false | ||
} | ||
); | ||
/// initialize the Error.message | ||
this.message = message.toString(); | ||
Object.defineProperty( | ||
this, | ||
"message", | ||
{ | ||
get: function(i18nMessages){ | ||
var message = _i18nMessage.toString(i18nMessages); | ||
return message; | ||
}, | ||
set: function(val){}, | ||
enumerable: false, | ||
configurable: false | ||
} | ||
); | ||
/** | ||
@@ -90,3 +66,3 @@ * | ||
* | ||
* @param {Object} messages | ||
* @param {Object} translations | ||
* | ||
@@ -96,10 +72,6 @@ * @return {String} | ||
*/ | ||
this.toString = function(messages){ | ||
var message = _i18nMessage.toString(messages); | ||
return message; | ||
this.toString = function(translations){ | ||
return message.toString(translations); | ||
}; | ||
/// initalize message | ||
this.i18nMessage = message; | ||
}; | ||
@@ -106,0 +78,0 @@ |
@@ -12,2 +12,3 @@ "use strict"; | ||
* Error class that can contain multiple errors | ||
* @class | ||
* | ||
@@ -108,13 +109,31 @@ */ | ||
* | ||
* @param {Object} translations | ||
* | ||
* @return {String} | ||
* | ||
*/ | ||
this.toString = function(){ | ||
this.toString = function(translations){ | ||
var str = ""; | ||
var str = "", | ||
length = _items.length, | ||
separator = ", "; | ||
_.each(_items, function(error){ | ||
str += error.toString(); | ||
}); | ||
_.each( | ||
_items, | ||
function(error, index){ | ||
if(error instanceof Exception){ | ||
str += error.toString(translations); | ||
} | ||
else { | ||
str += error.toString(); | ||
} | ||
/// add the separator if its not the last element | ||
if(index != length-1){ | ||
str += separator; | ||
} | ||
}); | ||
return str; | ||
@@ -129,17 +148,3 @@ | ||
/** | ||
* | ||
* ToString method with i18n support | ||
* | ||
* @param {Object} messages | ||
* | ||
* @return {String} | ||
* | ||
*/ | ||
ExceptionList.prototype.toString = function(messages){ | ||
/// TODO iterate over error list | ||
}; | ||
module.exports = ExceptionList; |
Internationalization package. This package provides you with some utilities that will help you | ||
The Internationalization package. This package provides you with some utilities that will help you | ||
on your internationalization tasks. | ||
@@ -7,16 +7,27 @@ | ||
#### I18NString | ||
### I18NString | ||
The I18NString is a String representation that can be translation. This string can be a plain String or | ||
a lodash template that can be binded to some data. | ||
The I18NString is a String representation that can be translated. This string can be a plain String or | ||
a lodash template. | ||
When getting the result a translation dictionary can be provided. If the string/template exists on the | ||
translation dictionary the translation is returned. | ||
This implementation creates a clear separation between the translation mechanism and it's internal logic. This package is used across the library in order to provide I18N out of the box! | ||
This implementation creates a clear separation between the translation mechanism and it's internal logic. | ||
**Constructor** | ||
* `new I18N.String(message, data?, translations?)` | ||
<br /> | ||
Creates a String instance with the given *message*, associated *data*, and an object which contain the *translation*. | ||
**Methods** | ||
* `.toString(translations?)` | ||
<br /> | ||
Gets the string representation of the error. If _translations_ object is provided the message Error message will try to be translated. | ||
**Example** | ||
```js | ||
{%= _.include("I18N/StringDocExample.js") %} | ||
{%= _.include("I18N/I18NStringDocExample.js") %} | ||
``` |
@@ -22,13 +22,21 @@ "use strict"; | ||
* | ||
* @param {String} messages | ||
* @param {String} name | ||
* @param {Object} data | ||
* @param {Object} messages | ||
* @param {Object} customMessages | ||
* | ||
* @return {String} | ||
* | ||
*/ | ||
Internal.applyTemplate = function(messages){ | ||
Internal.applyTemplate = function(name, data, messages, customMessages){ | ||
var data = Safe.object(this.data); | ||
messages = Safe.object(messages, this.messages); | ||
name = Safe.string(name); | ||
data = Safe.object(data); | ||
messages = Safe.object(messages); | ||
customMessages = Safe.object(customMessages); | ||
var tmplStr = Safe.string(messages[this.name]) || this.name; | ||
messages = _.extend({}, messages, customMessages); | ||
var tmplStr = Safe.string(messages[name]) || name; | ||
return _.template(tmplStr, data); | ||
@@ -53,32 +61,37 @@ | ||
Object.defineProperty( | ||
this, | ||
"name", | ||
{ | ||
value: Safe.string(name), | ||
writable: false, | ||
configurable: false | ||
} | ||
); | ||
name = Safe.string(name); | ||
data = Safe.object(data); | ||
messages = Safe.object(messages); | ||
Object.defineProperty( | ||
this, | ||
"messages", | ||
{ | ||
value: Safe.object(messages), | ||
writable: false, | ||
configurable: false | ||
} | ||
); | ||
/** | ||
* | ||
* toString() | ||
* | ||
* @throws {Error} If template is not correct | ||
* | ||
* @param {String} messages | ||
* @param {Object} data | ||
* @return {String} | ||
* | ||
*/ | ||
this.toString = function(customMessages){ | ||
Object.defineProperty( | ||
this, | ||
"data", | ||
{ | ||
value: Safe.object(data), | ||
writable: false, | ||
configurable: false | ||
try{ | ||
return Internal.applyTemplate.apply(this, [ name, data, messages, customMessages ]); | ||
} catch(e){ | ||
var error = _.template( | ||
"Error on template '<%= name %>': <%= error %>", { | ||
name: name, | ||
error: e.message | ||
}); | ||
throw new Error(error); | ||
} | ||
); | ||
}; | ||
/// Initialize string by calling .super() | ||
@@ -94,34 +107,2 @@ String.call( this, this.toString() ); | ||
/** | ||
* | ||
* toString() | ||
* | ||
* @throws {Error} If template is not correct | ||
* | ||
* @param {String} messages | ||
* @param {Object} data | ||
* @return {String} | ||
* | ||
*/ | ||
I18NString.prototype.toString = function(messages){ | ||
try{ | ||
return Internal.applyTemplate.apply(this, [ messages ]); | ||
}catch(e){ | ||
var error = _.template( | ||
"Error on template '<%= name %>': <%= error %>", { | ||
name: this.name, | ||
error: e.message | ||
}); | ||
throw new Error(error); | ||
} | ||
}; | ||
module.exports = I18NString; |
Object facility provides some utility function to use on Objects. | ||
**Methods** | ||
* `filter(value, filter)` | ||
<br /> | ||
Returns an array with all the keys that match the __filter__. | ||
**Example** | ||
```js | ||
@@ -5,0 +14,0 @@ |
Safe facility provides an API that can helps you safelly working with javascript data types. | ||
Safe facility provides an API that can helps you safelly working with javascript data types. This methods | ||
are supposed to work with different value types. | ||
**Methods** | ||
* `Safe.array(value, defaultValue?)` | ||
<br /> | ||
Gets the value in the array representation. __defaultValue__ is returned if defined and if value is not | ||
an array. | ||
* `Safe.boolean(value, defaultValue?)` | ||
* `Safe.string(value, defaultValue?)` | ||
* `Safe.object(value, defaultValue?)` | ||
* `Safe.number(value, defaultValue?)` | ||
* `Safe.function(value, defaultValue?)` | ||
* `Safe.value(value, defaultValue?)` | ||
* `Safe.regexp(value, defaultValue)` | ||
* `Safe.instanceOf(value, Class)` | ||
<br /> | ||
Gets an instance of the given value if is an instance of the given Class, otherwise it will | ||
create an instance. | ||
* `Safe.length(value)` | ||
<br /> | ||
Gets the length of the value. | ||
* `Safe.coerce(value, expected)` | ||
Gets the value coerced by the expected value type. | ||
**Example** Array | ||
```js | ||
@@ -12,2 +47,4 @@ | ||
**Example** Boolean | ||
```js | ||
@@ -19,2 +56,4 @@ | ||
**Example** Function | ||
```js | ||
@@ -26,2 +65,4 @@ | ||
**Example** Length | ||
```js | ||
@@ -33,2 +74,4 @@ | ||
**Example** Number | ||
```js | ||
@@ -40,2 +83,4 @@ | ||
**Example** Object | ||
```js | ||
@@ -47,2 +92,4 @@ | ||
**Example** Regex | ||
```js | ||
@@ -54,2 +101,4 @@ | ||
**Example** String | ||
```js | ||
@@ -61,2 +110,4 @@ | ||
**Example** Value | ||
```js | ||
@@ -63,0 +114,0 @@ |
@@ -5,56 +5,83 @@ | ||
**Methods** | ||
`.any()` | ||
<br /> | ||
Set the expected type as any object. | ||
**Overview** | ||
`.string()` | ||
<br /> | ||
Set the expected type as a string | ||
```js | ||
`.number()` | ||
<br /> | ||
Set the expected type as a number | ||
{%= _.include("Schema/SchemaOverview.js") %} | ||
`.object(value)` | ||
<br /> | ||
Set the expected type as an object. The **value** is an object with rules. | ||
`.array(value)` | ||
<br /> | ||
Set the expected type as an object. The **value** is an array with rules. | ||
``` | ||
`.required()` | ||
<br /> | ||
Set as required. | ||
`.optional()` | ||
<br /> | ||
Set as optional | ||
**Schema chainable API:** | ||
`.default(value)` | ||
<br /> | ||
Set the default value. This is used when the schema its required and the provided value is null. | ||
`.any()` Set the expected type as any object. | ||
`.repeatable()` | ||
<br /> | ||
Set the type as repeatable. This repeats the schema through the structure (array only!). | ||
`.string()` Set the expected type as a string | ||
`.min(value)` | ||
<br /> | ||
Set the min value expected. If in number context the value is used. If in string context the length is used. | ||
If in array context the length is used. If in object context the number of keys is used. | ||
`.number()` Set the expected type as a number | ||
`.max(value)` | ||
<br /> | ||
Set the max value expected. If in number context the value is used. If in string context the length is used. | ||
If in array context the length is used. If in object context the number of keys is used. | ||
`.object(value)` Set the expected type as an object. The **value** is an object with rules. | ||
`.regex(value)` | ||
<br /> | ||
Sets a regexp condition ( only use on string context! ) | ||
`.array(value)` Set the expected type as an object. The **value** is an array with rules. | ||
`.compile()` | ||
<br /> | ||
Compiles the schema. If you are using the same Schema multiple time you can compile it for performance reasons. | ||
This avoid compiling the Schema before every usage. | ||
`.required()` Set as required. **By default every rule is required!** | ||
`.value(value)` | ||
<br /> | ||
Test the schema returning the normalized value if valid. Throws an Error if invalid. | ||
`.optional()` Set as optional | ||
`.isValid(value)` | ||
<br /> | ||
Test the schema return if its valid or not. | ||
`.default(value)` Set the default value. This is used when the schema its required and the provided value is null. | ||
`.errors(value)` | ||
<br /> | ||
Test the schema returning an Array with the errors | ||
`.repeatable()` Set the type as repeatable. This repeats the schema through the structure (array only!). | ||
`.min(value)` Set the min value expected. If in number context the value is used. If in string context the length is used. | ||
If in array context the length is used. If in object context the number of keys is used. | ||
**Example** overview | ||
`.max(value)` Set the max value expected. If in number context the value is used. If in string context the length is used. | ||
If in array context the length is used. If in object context the number of keys is used. | ||
```js | ||
`.regex(value)` Sets a regexp condition ( only use on string context! ) | ||
{%= _.include("Schema/SchemaOverview.js") %} | ||
**Schema evaluation API:** | ||
`.compile()` Compiles the schema. If you are using the same Schema multiple time you can compile it for performance reasons. | ||
This avoid compiling the Schema before every usage. | ||
``` | ||
`.value(value)` Test the schema returning the normalized value if valid. Throws an Error if invalid. | ||
**Example** String Schema | ||
`.isValid(value)` Test the schema return if its valid or not. | ||
`.errors(value)` Test the schema returning an Array with the errors | ||
**Example 1:** String Schema | ||
```js | ||
@@ -69,3 +96,3 @@ | ||
**Example 2:** Number Schema | ||
**Example** Number Schema | ||
@@ -79,3 +106,3 @@ ```js | ||
**Example 3:** Any Schema | ||
**Example** Any Schema | ||
@@ -89,3 +116,3 @@ ```js | ||
**Example 4:** Object Schema | ||
**Example** Object Schema | ||
@@ -104,3 +131,3 @@ Objects schema is set by applying rules to each property of the object. You can also use regular expressions on | ||
``` | ||
**Example 5:** Array Schema | ||
**Example** Array Schema | ||
@@ -107,0 +134,0 @@ The following example describe an array rule that is optional and its expecting three items. |
@@ -5,2 +5,35 @@ | ||
**Methods** | ||
* `Type.of(value)` | ||
<br /> | ||
Gets the string representation of the given value. | ||
* `Type.isArray(value)` | ||
* `Type.isBoolean(value)` | ||
* `Type.isFunction(value)` | ||
* `Type.isString(value)` | ||
* `Type.isObject(value)` | ||
* `Type.isBoolean(value)` | ||
* `Type.isRegExp(value)` | ||
* `Type.isRegExpStr(value)` | ||
* `Type.isNumber(value)` | ||
* `Type.isDefined(value)` | ||
<br /> | ||
Checks if the value is defined. | ||
* `Type.isEmpty(value)` | ||
<br /> | ||
Checks if the value is empty (executed in string, list and object context ). | ||
```js | ||
@@ -7,0 +40,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
1598757
91
1494
24832