Skip to content

Commit 6a5af6b

Browse files
Add missing packages and other stuff
1 parent d0b44d7 commit 6a5af6b

9 files changed

+208
-15
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# Default:
4+
# https://github.com/editorconfig/editorconfig-defaults/blob/master/editorconfig-defaults.json
5+
6+
# top-most EditorConfig file
7+
root = true
8+
9+
# Unix-style newlines with a newline ending every file
10+
[*]
11+
end_of_line = lf
12+
insert_final_newline = true
13+
trim_trailing_whitespace = true
14+
indent_style = space
15+
indent_size = 4

package-lock.json

+35-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
"version": "1.0.0",
44
"description": "TensorFlow record (.tfrecord) API for Node.JS and Browsers",
55
"main": "lib/index.js",
6-
"types": "lib/index.d.ts", "scripts": {
6+
"dependencies": {
7+
"buffer-reverse": "^1.0.1",
8+
"crypto-js": "^3.1.9-1",
9+
"google-protobuf": "^3.6.1",
10+
"node-int64": "^0.4.0",
11+
"shortid": "^2.2.14"
12+
},
13+
"types": "lib/index.d.ts",
14+
"scripts": {
715
"test": "jest --config jestconfig.json",
816
"build": "tsc",
917
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
@@ -18,14 +26,18 @@
1826
"type": "git",
1927
"url": "git+https://github.com/JacopoMangiavacchi/TFRecords.git"
2028
},
21-
"keywords": ["TFRecords", "TensorFlow"],
29+
"keywords": [
30+
"TFRecords",
31+
"TensorFlow"
32+
],
2233
"author": "Jacopo Mangiavacchi",
2334
"license": "MIT",
2435
"bugs": {
2536
"url": "https://github.com/JacopoMangiavacchi/TFRecords/issues"
2637
},
2738
"homepage": "https://github.com/JacopoMangiavacchi/TFRecords#readme",
2839
"devDependencies": {
40+
"@types/node": "10.12.7",
2941
"@types/jest": "^24.0.6",
3042
"jest": "^24.1.0",
3143
"prettier": "^1.16.4",

src/guard.test.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import Guard from "./guard";
2+
3+
describe("Guard", () => {
4+
function methodWithRequiredName(name: string) {
5+
Guard.emtpy(name);
6+
}
7+
8+
function methodWithRequiredNameWithParam(name: string) {
9+
Guard.emtpy(name, "name", "Name is required");
10+
}
11+
12+
function methodWithRequiredObject(options: any) {
13+
Guard.null(options);
14+
}
15+
16+
function methodWithRequiredExpression(value: number) {
17+
Guard.expression(value, (num) => num > 0 && num < 100);
18+
}
19+
20+
describe("empty", () => {
21+
it("throws error on null value", () => {
22+
expect(() => methodWithRequiredName(null)).toThrowError();
23+
});
24+
25+
it("throws error on empty value", () => {
26+
expect(() => methodWithRequiredName("")).toThrowError();
27+
});
28+
29+
it("throw error on whitespace", () => {
30+
expect(() => methodWithRequiredName(" ")).toThrowError();
31+
});
32+
33+
it("does not throw error on valid value", () => {
34+
expect(() => methodWithRequiredName("valid")).not.toThrowError();
35+
});
36+
37+
it("throws specific error message", () => {
38+
expect(() => methodWithRequiredNameWithParam(null)).toThrowError("Name is required");
39+
});
40+
});
41+
42+
describe("null", () => {
43+
it("throws error on null value", () => {
44+
expect(() => methodWithRequiredObject(null)).toThrowError();
45+
});
46+
47+
it("does not throw error on valid value", () => {
48+
expect(() => methodWithRequiredObject({})).not.toThrowError();
49+
});
50+
});
51+
52+
describe("expression", () => {
53+
it("throws error on invalide value", () => {
54+
expect(() => methodWithRequiredExpression(0)).toThrowError();
55+
});
56+
57+
it("does not throw error on valid value", () => {
58+
expect(() => methodWithRequiredExpression(1)).not.toThrowError();
59+
});
60+
});
61+
});

src/guard.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export default class Guard {
2+
/**
3+
* Validates the string express is not null or empty, otherwise throws an exception
4+
* @param value - The value to validate
5+
* @param paramName - The name of the parameter to validate
6+
* @param message - The error message to return on invalid value
7+
*/
8+
public static emtpy(value: string, paramName?: string, message?: string) {
9+
if ((!!value === false || value.trim().length === 0)) {
10+
message = message || (`'${paramName || "value"}' cannot be null or empty`);
11+
throw new Error(message);
12+
}
13+
}
14+
15+
/**
16+
* Validates the value is not null, otherwise throw an exception
17+
* @param value - The value to validate
18+
* @param paramName - The name of the parameter to validate
19+
* @param message - The error message to return on invalid value
20+
*/
21+
public static null(value: any, paramName?: string, message?: string) {
22+
if ((!!value === false)) {
23+
message = message || (`'${paramName || "value"}' cannot be null or undefined`);
24+
throw new Error(message);
25+
}
26+
}
27+
28+
/**
29+
* Validates the value meets the specified expectation, otherwise throws an exception
30+
* @param value - The value to validate
31+
* @param predicate - The predicate used for validation
32+
* @param paramName - The name of the parameter to validate
33+
* @param message - The error message to return on invalid value
34+
*/
35+
public static expression<T>(value: T, predicate: (value: T) => boolean, paramName?: string, message?: string) {
36+
if (!!value === false || !predicate(value)) {
37+
message = message || (`'${paramName || "value"}' is not a valid value`);
38+
throw new Error(message);
39+
}
40+
}
41+
}

src/tensorFlowHelpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import Int64 from "node-int64";
2424
import reverse from "buffer-reverse";
25+
import Guard from "./guard";
2526

2627
/**
2728
* @buffer - Buffer input

src/tensorFlowReader.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Guard from "./guard";
12
import { FeatureType } from "./tensorFlowBuilder";
23
import { crc32c, maskCrc, readInt64, textDecode } from "./tensorFlowHelpers";
34
import { TFRecordsImageMessage } from "./tensorFlowRecordsProtoBuf_pb";

tsconfig.json

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
{
2-
"compilerOptions": {
3-
"target": "es5",
4-
"module": "commonjs",
5-
"declaration": true,
6-
"outDir": "./lib",
7-
"strict": true
8-
},
9-
"include": ["src"],
10-
"exclude": ["node_modules", "**/__tests__/*"]
11-
}
2+
"compilerOptions": {
3+
"target": "es6",
4+
"allowJs": false,
5+
"skipLibCheck": false,
6+
"esModuleInterop": true,
7+
"allowSyntheticDefaultImports": true,
8+
"strict": false,
9+
"forceConsistentCasingInFileNames": true,
10+
"module": "esnext",
11+
"moduleResolution": "node",
12+
"resolveJsonModule": true,
13+
"isolatedModules": true,
14+
"noEmit": true,
15+
"jsx": "preserve",
16+
"experimentalDecorators": true,
17+
"lib": [
18+
"es2015",
19+
"dom"
20+
]
21+
},
22+
"include": [
23+
"src",
24+
".storybook"
25+
]
26+
}

tslint.json

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
{
2-
"extends": ["tslint:recommended", "tslint-config-prettier"]
2+
"defaultSeverity": "error",
3+
"extends": [
4+
"tslint:recommended"
5+
],
6+
"jsRules": {},
7+
"rules": {
8+
"object-literal-sort-keys": false,
9+
"no-console": false,
10+
"no-shadowed-variable": false,
11+
"ordered-imports": false,
12+
"no-string-literal": false,
13+
"no-bitwise": false,
14+
"function-constructor": false
15+
},
16+
"rulesDirectory": []
317
}

0 commit comments

Comments
 (0)