Skip to content

Commit

Permalink
Merge pull request #25 from Psychopoulet/develop
Browse files Browse the repository at this point in the history
add ts support, pre-commit -> pre-push, improve validators & tests
  • Loading branch information
Psychopoulet committed Apr 25, 2018
2 parents 07767af + e3c974b commit f8a9be5
Show file tree
Hide file tree
Showing 33 changed files with 665 additions and 244 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language: node_js
node_js:
- "6"
- "8"
install:
- "npm install -g typescript"
- "npm install"
script: "npm run-script tests"
sudo: false
82 changes: 49 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,30 @@ $ npm install node-containerpattern

-- Attributes --

* ``` object documentations ``` used to save documentation by data's key
* ``` object limits ``` used to limit data's values possibilities
* ``` string recursionSeparator ``` used to parse recursive keys (default : '.')
* ``` object skeletons ``` used to force data type
* ``` documentations: object ``` used to save documentation by data's key
* ``` limits: object ``` used to limit data's values possibilities
* ``` recursionSeparator: string ``` used to parse recursive keys (default : '.')
* ``` skeletons: object ``` used to force data type

-- Constructor --

* ``` constructor([ string recursionSeparator = "." ]) ```
* ``` constructor(recursionSeparator: string = ".") ```

-- Methods --

* ``` clear() : return this ``` clearData & clearDocumentations & clearLimits & clearSkeletons
* ``` clearData() : return this ``` forget all the keys and there values and documentations (=> Map.clear)
* ``` clearDocumentations() : return this ``` forget all the skeletons
* ``` clearLimits() : return this ``` forget all the limits
* ``` clearSkeletons() : return this ``` forget all the skeletons
* ``` delete(string key) : return this ``` forget a key and its value
* ``` document(string key, string documentation) : return this ``` attach a documentation on the data. only visible if "set" method is applied with this key.
* ``` documentation() : return JSON object ``` generate a documentation for all the stored data
* ``` get(string key) : return mixed ``` return the value in association with this key (may be recursive)
* ``` has(string key) : return bool ``` check if a key is used (may be recursive)
* ``` limit(string key, array limit) : return this ``` associate a key with a limit
* ``` set(string key, mixed value) : return this ``` associate and remember a key with a value (may be recursive)
* ``` skeleton(string key, string skeleton) : return this ``` skeleton must be "array", "boolean", "email", "float", "integer", "ipv4", "ipv6", "number", "object", "string"
* ``` clear(): this ``` clearData & clearDocumentations & clearLimits & clearSkeletons
* ``` clearData() : this ``` forget all the keys and there values and documentations (=> Map.clear)
* ``` clearDocumentations() : this ``` forget all the skeletons
* ``` clearLimits() : this ``` forget all the limits
* ``` clearSkeletons() : this ``` forget all the skeletons
* ``` delete(key: string) : this ``` forget a key and its value
* ``` document(key: string, documentation: string) : this ``` attach a documentation on the data. only visible if "set" method is applied with this key.
* ``` documentation() : JSON object ``` generate a documentation for all the stored data
* ``` get(key: string) : mixed ``` the value in association with this key (may be recursive)
* ``` has(key: string) : bool ``` check if a key is used (may be recursive)
* ``` limit(key: string, array limit) : this ``` associate a key with a limit
* ``` set(key: string, mixed value) : this ``` associate and remember a key with a value (may be recursive)
* ``` skeleton(key: string, skeleton: string) : this ``` skeleton must be "array", "boolean", "email", "float", "integer", "ipv4", "ipv6", "number", "object", "string"

-- notes --

Expand All @@ -61,33 +61,35 @@ $ npm install node-containerpattern

## Examples

```js
const Container = require('node-containerpattern');
var container = new Container();
### Native

```javascript
const Container = require("node-containerpattern");
const container = new Container();

container
.skeleton("contact", "email").document("contact", "Contact address")
.skeleton("debug", "boolean").document("debug", "This is the debug module")
.skeleton("vat", "float")
.skeleton("heigth", "integer")

.set('contact', "myaddress@provider.com")
.set('debug', true) // '"yes"', '"y"', '"1"', '1', '"true"' or 'true' => get = true, else => get = false
.set('vat', '5.5').set('vat', 5.5)
.set('conf', { usr : { login : 'login', pwd : 'pwd' } })
.set('conf.usr.login', "login2")
.set('object', new Object())
.set("contact", "myaddress@provider.com")
.set("debug", true) // '"yes"', '"y"', '"1"', '1', '"true"' or 'true' => get = true, else => get = false
.set("vat", '5.5').set('vat', 5.5)
.set("conf", { usr : { login : 'login', pwd : 'pwd' } })
.set("conf.usr.login", "login2")
.set("object", new Object())

.limit("debug", [true, false]); // debug is now limited to 'true' or 'false'

console.log(container.get('conf').usr.login);
console.log(container.get('conf.usr.login'));
console.log(container.get('object'));
console.log(container.get('conf'));
console.log(container.get("conf").usr.login);
console.log(container.get("conf.usr.login"));
console.log(container.get("object"));
console.log(container.get("conf"));

container.delete('object');
container.delete("object");

console.log(container.has('object'));
console.log(container.has("object"));
console.log(container.size);

for (let key of container.keys()) {
Expand All @@ -106,6 +108,20 @@ console.log(container.documentation());
container.clear();
```

### Typescript

```typescript
import Container = require("node-containerpattern");
const container = new Container();

container
.skeleton("contact", "email").document("contact", "Contact address")
.set("contact", "myaddress@provider.com");

console.log(container.get("contact"));

```

## Tests

```bash
Expand Down
9 changes: 0 additions & 9 deletions lib/IPV6Pattern.js

This file was deleted.

54 changes: 27 additions & 27 deletions lib/ensureData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,45 @@

const { join } = require("path");

const { isDefined, isArray, inArray } = require(join(__dirname, "validators.js"));
const ensureDataArray = require(join(__dirname, "ensureDataArray.js"));
const ensureDataObject = require(join(__dirname, "ensureDataObject.js"));
const ensureDataSpecific = require(join(__dirname, "ensureDataSpecific.js"));
const ensureDataBasic = require(join(__dirname, "ensureDataBasic.js"));

// module

module.exports = (key, limit, skeleton, value) => {
module.exports = (key, limits, skeleton, value) => {

let result = value;
// check existance
if (!isDefined(value)) {
throw new ReferenceError("The \"" + key + "\" value is undefined");
}

// check existance
if ("undefined" === typeof value) {
throw new ReferenceError("The \"" + key + "\" value is undefined");
}

// check limits
else if (limit && -1 >= limit.indexOf(value)) {
throw new Error("The \"" + key + "\" data does not correspond to the limits (" + JSON.stringify(limit) + ")");
}
// check limits
else if (isArray(limits) && !inArray(limits, value)) {
throw new Error("The \"" + key + "\" data does not correspond to the limits (" + JSON.stringify(limits) + ")");
}

// check skeleton
else if (skeleton) {

if ("object" === skeleton) {
result = ensureDataObject(key, skeleton, result);
}
else if ("array" === skeleton) {
result = ensureDataArray(key, skeleton, result);
}
else if ([ "email", "ipv4", "ipv6" ].includes(skeleton)) {
result = ensureDataSpecific(key, skeleton, result);
}
else {
result = ensureDataBasic(key, skeleton, result);
}
// check skeleton
else if (skeleton) {

if ("object" === skeleton) {
return ensureDataObject(key, skeleton, value);
}
else if ("array" === skeleton) {
return ensureDataArray(key, skeleton, value);
}
else if (inArray([ "email", "ipv4", "ipv6" ], skeleton)) {
return ensureDataSpecific(key, skeleton, value);
}
else {
return ensureDataBasic(key, skeleton, value);
}

return result;
}
else {
return value;
}

};
4 changes: 2 additions & 2 deletions lib/ensureDataArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// deps

const isArray = require(require("path").join(__dirname, "isArray.js"));
const { isArray, isString } = require(require("path").join(__dirname, "validators"));

// module

Expand All @@ -13,7 +13,7 @@ module.exports = (key, skeleton, value) => {

if ("array" === skeleton && !isArray(value)) {

if ("string" !== typeof value || "[" !== value[0] || "]" !== value[value.length - 1]) {
if (!isString(value) || "[" !== value[0] || "]" !== value[value.length - 1]) {
throw new TypeError("The \"" + key + "\" data does not correspond to the skeleton");
}
else {
Expand Down
24 changes: 17 additions & 7 deletions lib/ensureDataBasic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

"use strict";

// deps

const { isNumber, isInteger } = require(require("path").join(__dirname, "validators"));

// module

module.exports = (key, skeleton, value) => {
Expand All @@ -13,17 +16,24 @@ module.exports = (key, skeleton, value) => {
}
else if ("float" === skeleton || "number" === skeleton) {

const parsed = parseFloat(value);

if (isNaN(parsed)) {
throw new TypeError("The \"" + key + "\" data does not correspond to the skeleton");
if (isNumber(value)) {
return value;
}
else {
return parsed;

const parsed = parseFloat(value);

if (isNaN(parsed)) {
throw new TypeError("The \"" + key + "\" data does not correspond to the skeleton");
}
else {
return parsed;
}

}

}
else if ("integer" === skeleton) {
else if ("integer" === skeleton && !isInteger(value)) {

const parsed = parseInt(value, 10);

Expand Down
4 changes: 2 additions & 2 deletions lib/ensureDataObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// deps

const isPlainObject = require(require("path").join(__dirname, "isPlainObject.js"));
const { isPlainObject, isString } = require(require("path").join(__dirname, "validators"));

// module

Expand All @@ -13,7 +13,7 @@ module.exports = (key, skeleton, value) => {

if ("object" === skeleton && !isPlainObject(value)) {

if ("string" !== typeof value || "{" !== value[0] || "}" !== value[value.length - 1]) {
if (!isString(value) || "{" !== value[0] || "}" !== value[value.length - 1]) {
throw new TypeError("The \"" + key + "\" data does not correspond to the skeleton");
}
else {
Expand Down
56 changes: 12 additions & 44 deletions lib/ensureDataSpecific.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,26 @@
/*
eslint no-extra-parens: 0
*/

"use strict";

// deps

const { join } = require("path");

// consts

const EMAIL_PATTERN = require(join(__dirname, "emailPattern.js"));
const IPV4_PATTERN = require(join(__dirname, "IPV4Pattern.js"));
const IPV6_PATTERN = require(join(__dirname, "IPV6Pattern.js"));
const { isEmail, isEmptyString, isIPV4, isIPV6 } = require(require("path").join(__dirname, "validators"));

// module

module.exports = (key, skeleton, value) => {

if ("email" === skeleton) {

if ("string" !== typeof value) {
throw new TypeError("The \"" + key + "\" data does not correspond to the skeleton");
}
else if ("" !== value && !EMAIL_PATTERN.test(value)) {
throw new TypeError("The \"" + key + "\" data does not correspond to the skeleton");
}
else {
return value;
}

if (isEmptyString(value)) {
return value.trim();
}
else if ("ipv4" === skeleton) {

if ("string" !== typeof value) {
throw new TypeError("The \"" + key + "\" data does not correspond to the skeleton");
}
else if ("" !== value && !IPV4_PATTERN.test(value)) {
throw new TypeError("The \"" + key + "\" data does not correspond to the skeleton");
}
else {
return value;
}

}
else if ("ipv6" === skeleton) {

if ("string" !== typeof value) {
throw new TypeError("The \"" + key + "\" data does not correspond to the skeleton");
}
else if ("" !== value && !IPV6_PATTERN.test(value)) {
throw new TypeError("The \"" + key + "\" data does not correspond to the skeleton");
}
else {
return value;
}

else if (
("email" === skeleton && !isEmail(value)) ||
("ipv4" === skeleton && !isIPV4(value)) ||
("ipv6" === skeleton && !isIPV6(value))
) {
throw new TypeError("The \"" + key + "\" data does not correspond to the skeleton");
}
else {
return value;
Expand Down
Loading

0 comments on commit f8a9be5

Please sign in to comment.