Skip to content

Commit 6dcab2b

Browse files
init files + tests
1 parent 802d621 commit 6dcab2b

File tree

5 files changed

+116
-37
lines changed

5 files changed

+116
-37
lines changed

.gitignore

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,2 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
6-
# Runtime data
7-
pids
8-
*.pid
9-
*.seed
10-
11-
# Directory for instrumented libs generated by jscoverage/JSCover
12-
lib-cov
13-
14-
# Coverage directory used by tools like istanbul
15-
coverage
16-
17-
# nyc test coverage
18-
.nyc_output
19-
20-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21-
.grunt
22-
23-
# node-waf configuration
24-
.lock-wscript
25-
26-
# Compiled binary addons (http://nodejs.org/api/addons.html)
27-
build/Release
28-
29-
# Dependency directories
30-
node_modules
31-
jspm_packages
32-
33-
# Optional npm cache directory
34-
.npm
35-
36-
# Optional REPL history
37-
.node_repl_history
1+
coverage/
2+
node_modules/

circle.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
machine:
2+
node:
3+
version: 6.9
4+
5+
dependencies:
6+
cache_directories:
7+
- ~/.npm/
8+
9+
test:
10+
override:
11+
- npm test -- --coverage

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"description": "byte flag calculator",
3+
"main": "src/byte_flag_calculator.js",
4+
"version": "0.1.0",
5+
"repository": {
6+
"type": "git",
7+
"url": "git+https://github.com/explore-node-js/node.js-byte-flag-calculator.git"
8+
},
9+
"keywords": [
10+
"bitwise operations",
11+
"byte flag",
12+
"byte flag calculator",
13+
"byte mask",
14+
"byte mask calculator"
15+
],
16+
"author": "Eugene Matvejev <eugene.matvejev@gmail.com>",
17+
"license": "MIT",
18+
"dependencies": {},
19+
"devDependencies": {
20+
"jest": "^18.1.0"
21+
},
22+
"scripts": {
23+
"test": "node node_modules/jest/bin/jest.js --verbose"
24+
},
25+
"bugs": {
26+
"url": "https://github.com/explore-node-js/node.js-byte-flag-calculator/issues"
27+
},
28+
"homepage": "https://github.com/explore-node-js/node.js-byte-flag-calculator#readme"
29+
}

src/byte_flag_calculator.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = class ByteFlagCalculator {
2+
static hasBytes(flags, flag) {
3+
return (flags & flag) === flag;
4+
}
5+
6+
static addBytes(flags, flag) {
7+
return flags | flag;
8+
}
9+
10+
static removeBytes(flags, flag) {
11+
return ByteFlagCalculator.hasBytes(flags, flag)
12+
? flags & ~flag
13+
: flags;
14+
}
15+
};

tests/byte_flag_calculator.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const calc = require("../src/byte_flag_calculator");
2+
3+
describe(`byte flag calculator`, () => {
4+
describe(`dataProvider with HEX values`, () => {
5+
describe(`::hasBytes`, () => {
6+
[
7+
{ flags: 0x01, flag: 0x01 },
8+
{ flags: 0x03, flag: 0x01 },
9+
{ flags: 0x05, flag: 0x01 },
10+
{ flags: 0x07, flag: 0x02 },
11+
{ flags: 0x05, flag: 0x04 },
12+
].forEach(el => {
13+
it(` - expected true as ${el.flag} bytes are presented in ${el.flags}`, () => {
14+
expect(calc.hasBytes(el.flags, el.flag)).toBe(true);
15+
})
16+
});
17+
18+
[
19+
{ flags: 0x00, flag: 0x01 },
20+
{ flags: 0x00, flag: 0x02 },
21+
{ flags: 0x01, flag: 0x02 },
22+
{ flags: 0x03, flag: 0x04 },
23+
{ flags: 0x06, flag: 0x01 },
24+
].forEach(el => {
25+
it(` - expected false as ${el.flag} bytes aren't presented in ${el.flags}`, () => {
26+
expect(calc.hasBytes(el.flags, el.flag)).toBe(false);
27+
})
28+
});
29+
});
30+
31+
describe(`::addBytes`, () => {
32+
[
33+
{ flags: 0x00, flag: 0x01, expected: 0x01 },
34+
{ flags: 0x01, flag: 0x01, expected: 0x01 },
35+
{ flags: 0x01, flag: 0x02, expected: 0x03 },
36+
{ flags: 0x03, flag: 0x04, expected: 0x07 },
37+
{ flags: 0x06, flag: 0x01, expected: 0x07 },
38+
].forEach(el => {
39+
it(` - expected ${el.expected} by adding bytes ${el.flag} to ${el.flags}`, () => {
40+
expect(calc.addBytes(el.flags, el.flag)).toBe(el.expected);
41+
})
42+
});
43+
});
44+
45+
describe(`::removeBytes`, () => {
46+
[
47+
{ flags: 0x00, flag: 0x01, expected: 0x00 },
48+
{ flags: 0x02, flag: 0x01, expected: 0x02 },
49+
{ flags: 0x12, flag: 0x02, expected: 0x10 },
50+
{ flags: 0x04, flag: 0x04, expected: 0x00 },
51+
{ flags: 0x0F, flag: 0xFF, expected: 0x0F },
52+
].forEach(el => {
53+
it(` - expected ${el.expected} by removing bytes ${el.flag} from ${el.flags}`, () => {
54+
expect(calc.removeBytes(el.flags, el.flag)).toBe(el.expected);
55+
})
56+
});
57+
});
58+
});
59+
});

0 commit comments

Comments
 (0)