Dbc project
ES6 classes, which offer the several possibilities:
- the Design by Contract (DbC) approach. You may also set the exception throwing policy, if some contract has failed
- working with the DOM-model, there are methods for fetching/appending/updating/removing the HTML-nodes
- deep working with the HTML-attributes, if you plan to create the possible bindings for the MVC/MVP/MVVM pattern implementation
- loading the resources (the HTML/CSS/JavaScript content) asynchronously via the BLOB-urls, which were created on the fly
- other useful stuff, which isn't supported by the web-browsers from the box
How to use library in your own project:
For the browsers:
const contract = Dbc.Contract;
contract.throwException = true;
class Foo {
constructor( value, data ) {
contract.isNotEmptyString(value);
contract.isArray(data);
}
}
For the NodeJS context:
node
> const dbc = require("dbc-code-contracts");
undefined
> dbc.Contract.isString("");
true
> const data = [ 1, 2, 3 ];
undefined
> dbc.Contract.isPrototypeOf(data, Array);
true
How to install/test library:
Project may be used both for the web-browser (window object) and nodejs contexts. Run the following commands at the library source path:
// for restoring the dependencies
npm install
// for the project build
npm run build
// for testing the project
npm run test
Supported contracts:
- isFunction
- isObject
- isSymbol
- isBoolean
- isTrue
- isFalse
- isString
- isEmptyString
- isNotEmptyString
- areStringsEqual
- isNumber
- isNumberLess
- isNumberBigger
- areNumbersEqual
- isValueNaN
- isDefined
- isUndefined
- isNull
- isArray
- isEmptyArray
- isNotEmptyArray
- isObjectImmutable
- isPromise
- isPrototypeOf
DOM manipulation methods:
- loadResource
- loadSourceCode
- createBlobContent
- getElementById
- getElementsByClass
- setId
- appendItemToHead
- appendItemToBody
- loadModule
- loadStyle
- loadTemplate
- parseStringAsHtmlObject
- changeHtmlById
- changeHtmlByClass
- removeElement
- removeById
- removeByClass
- removeByIdAndClass
- removeChildsById
- removeChildsByClass
- setLocationHash
DOM-attributes manipulation methods:
- exists
- getNodeByName
- getValueByName
- create
- linkWithDomObject
- append
- appendById
- update
- updateById
- remove
- removeById
Useful stuff from the base class:
- 'emptyString' getter
- function 'equal', which compares the two given values for the equality and provides the strict check type between them
- function 'assert', which provides the assertion check and the possibility to throw/bypass an exception with the custom string message
- function 'delay', which provides the possibility to delay the function execution using the timer (in milliseconds)
- function 'capitalize', which helps to capitalize some string
- function 'raise', provides the possibility to throw an exception with the custom string message
- function 'immute', which creates the immutable object with the type-check
- function 'enum', it allows you to declare the enumeration in custom context with the desired name, e.g.:
> const dbc = require("dbc-code-contracts");
undefined
> const enumerate = dbc.Base.enum;
undefined
> enumerate({
Red : 0,
Green : 1,
Blue : 2 },
{name: "Color", context: this});
{ Red: 0, Green: 1, Blue: 2 }
> this.Color
{ Red: 0, Blue: 1 }
> delete this.Color.Red
false
Used tools:
- Karma/Jasmine is used for the unit-tests
- UglifyES is used for the build/minification