Skip to content

Commit

Permalink
improve controls & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Psychopoulet committed Oct 5, 2018
1 parent 560c4ab commit 7ed335c
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 114 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ $ npm install split-frames
```typescript
type ControlBits = "none" | "end+1" | "end+2";
type Tag: number | Buffer | Array<number | Buffer>
type Tag: number | Buffer | Array< number | Buffer >
```

* ```javascript "startWith": Tag ```
* ```javascript "startTimeout": integer ``` (default: 200)
* ```javascript "endWith": Tag ```
* ```javascript "escapeWith": Tag ```
* ```javascript "escapeWith": number ```
* ```javascript "escaped": Array<Tag> ```
* ```javascript "specifics": object ```
* ```javascript "controlBits": ControlBits ``` (default: "none")
Expand Down
2 changes: 1 addition & 1 deletion lib/checkers/checkTagsValidity.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = (tags) => {

for (let i = 0; i < tags.length; ++i) {

if ("number" !== typeof tags[i]) {
if ("number" !== typeof tags[i] && "object" !== typeof tags[i] && !(tags[i] instanceof Buffer)) {
result = false; break;
}

Expand Down
102 changes: 45 additions & 57 deletions lib/checkers/initData.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

// consts

const ALLOWED_OPTIONS = [ "startWith", "startTimeout", "endWith", "escapeWith", "escaped", "specifics", "controlBits" ];
const ALLOWED_CONTROL_BITS_OPTIONS = [ "none", "end+1", "end+2" ];

// module
Expand All @@ -35,85 +34,74 @@ module.exports = (options) => {
if ("object" !== typeof options) {
throw new TypeError("parameter sended is not an object");
}
else {

Object.keys(options).forEach((key) => {

if (!ALLOWED_OPTIONS.includes(key)) {
throw new Error("\"" + key + "\" option sended is not in [ \"" + ALLOWED_OPTIONS.join("\", \"") + "\" ]");
}

});
else if ("undefined" !== typeof options.startWith && !_checkTagsValidity(options.startWith)) {
throw new TypeError("startWith parameter sended is not a number, Buffer, or an Array of numbers");
}
else if ("undefined" !== typeof options.startTimeout && "number" !== typeof options.startTimeout) {
throw new TypeError("startTimeout parameter sended is not a number");
}
else if ("undefined" !== typeof options.endWith && !_checkTagsValidity(options.endWith)) {
throw new TypeError("endWith parameter sended is not a number, Buffer, or an Array of numbers");
}

if ("undefined" !== typeof options.startWith && !_checkTagsValidity(options.startWith)) {
throw new TypeError("startWith parameter sended is not a number, Buffer, or an Array of numbers");
}
else if ("undefined" !== typeof options.startTimeout && "number" !== typeof options.startTimeout) {
throw new TypeError("startTimeout parameter sended is not a number");
}
else if ("undefined" !== typeof options.endWith && !_checkTagsValidity(options.endWith)) {
throw new TypeError("endWith parameter sended is not a number, Buffer, or an Array of numbers");
}
else if ("undefined" !== typeof options.escapeWith && "number" !== typeof options.escapeWith) {
throw new TypeError("escapeWith parameter sended is not a number");
}

else if ("undefined" !== typeof options.escapeWith && "number" !== typeof options.escapeWith) {
throw new TypeError("escapeWith parameter sended is not a number");
}
else if ("undefined" !== typeof options.specifics && "object" !== typeof options.specifics) {
throw new TypeError("escapeWith parameter sended is not an object");
}

else if ("undefined" !== typeof options.specifics && "object" !== typeof options.specifics) {
throw new TypeError("escapeWith parameter sended is not an object");
}
else if ("undefined" !== typeof options.controlBits && "string" !== typeof options.controlBits) {
throw new TypeError("controlBits parameter sended is not a string");
}

else if ("undefined" !== typeof options.controlBits && "string" !== typeof options.controlBits) {
throw new TypeError("controlBits parameter sended is not a string");
else if ("undefined" !== typeof options.controlBits && !ALLOWED_CONTROL_BITS_OPTIONS.includes(options.controlBits)) {
throw new Error("controlBits option sended is not in [ \"" + ALLOWED_CONTROL_BITS_OPTIONS.join("\", \"") + "\" ]");
}

else if ("undefined" !== typeof options.controlBits && !ALLOWED_CONTROL_BITS_OPTIONS.includes(options.controlBits)) {
throw new Error("controlBits option sended is not in [ \"" + ALLOWED_CONTROL_BITS_OPTIONS.join("\", \"") + "\" ]");
}

else {

result.startWith = "undefined" !== typeof options.startWith ? options.startWith : null;
result.startTimeout = "undefined" !== typeof options.startTimeout ? options.startTimeout : 200;
result.endWith = "undefined" !== typeof options.endWith ? options.endWith : null;
else {

result.escapeWith = "undefined" !== typeof options.escapeWith ? options.escapeWith : null;
result.escaped = "undefined" !== typeof options.escaped ? options.escaped : [];
result.startWith = "undefined" !== typeof options.startWith ? options.startWith : null;
result.startTimeout = "undefined" !== typeof options.startTimeout ? options.startTimeout : 200;
result.endWith = "undefined" !== typeof options.endWith ? options.endWith : null;

if ("object" === typeof options.specifics) {
result.escapeWith = "undefined" !== typeof options.escapeWith ? options.escapeWith : null;
result.escaped = "undefined" !== typeof options.escaped ? options.escaped : [];

Object.keys(options.specifics).forEach((key) => {
if ("object" === typeof options.specifics) {

if ("undefined" !== typeof options.specifics[key]) {
Object.keys(options.specifics).forEach((key) => {

if (!_checkTagsValidity(options.specifics[key])) {
if ("undefined" !== typeof options.specifics[key]) {

throw new Error(
"\"" + options.specifics[key] + "\" specifics option" +
" sended is not a number, or an Array of numbers"
);
if (!_checkTagsValidity(options.specifics[key])) {

}
else if (!_checkTagsCompatibility(result.startWith, result.endWith, options.specifics[key])) {
throw new Error(
"\"" + options.specifics[key] + "\" specifics option" +
" sended is not a number, or an Array of numbers"
);

throw new Error(
"if you want use \"" + options.specifics[key] + "\" specifics option" +
", you have to use both \"startWith\" AND \"endWith\" tags (... or none)"
);
}
else if (!_checkTagsCompatibility(result.startWith, result.endWith, options.specifics[key])) {

}
throw new Error(
"if you want use \"" + options.specifics[key] + "\" specifics option" +
", you have to use both \"startWith\" AND \"endWith\" tags (... or none)"
);

}

});
}

}

result.specifics = "undefined" !== typeof options.specifics ? options.specifics : {};
result.controlBits = "undefined" !== typeof options.controlBits ? options.controlBits : "none";
});

}

result.specifics = "undefined" !== typeof options.specifics ? options.specifics : {};
result.controlBits = "undefined" !== typeof options.controlBits ? options.controlBits : "none";

}

}
Expand Down
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "split-frames",
"version": "2.1.0",
"version": "2.2.0",
"description": "Split Buffer frames from streams",
"main": "lib/main.js",
"typings": "lib/index.d.ts",
Expand All @@ -9,6 +9,12 @@
"precommit": "gulp eslint",
"prepush": "gulp mocha"
},
"husky": {
"hooks": {
"pre-commit": "gulp eslint",
"pre-push": "gulp mocha"
}
},
"repository": {
"type": "git",
"url": "git://github.com/Psychopoulet/split-frames"
Expand Down Expand Up @@ -44,14 +50,14 @@
},
"dependencies": {},
"devDependencies": {
"@types/node": "10.9.4",
"@types/node": "10.11.4",
"gulp": "4.0.0",
"gulp-coveralls": "0.1.4",
"gulp-eslint": "5.0.0",
"gulp-istanbul": "1.1.3",
"gulp-mocha": "3.0.1",
"gulp-plumber": "1.2.0",
"husky": "0.14.3"
"husky": "1.1.1"
},
"homepage": "https://github.com/Psychopoulet/split-frames#readme",
"engines": {
Expand Down
Loading

0 comments on commit 7ed335c

Please sign in to comment.