Browse Source

Add modules and imports.

Frederic G. MARAND 5 years ago
parent
commit
80bf1131ae
9 changed files with 55 additions and 5 deletions
  1. 7 1
      __tests__/base.spec.ts
  2. 1 1
      jest.config.js
  3. 15 0
      src/core/functions.ts
  4. 2 0
      src/core/index.ts
  5. 15 0
      src/core/types.ts
  6. 4 1
      src/index.ts
  7. 6 0
      src/utils/checks.ts
  8. 1 0
      src/utils/index.ts
  9. 4 2
      tslint.json

+ 7 - 1
__tests__/base.spec.ts

@@ -1,4 +1,4 @@
-import {sampleFunction } from "../src";
+import {evaluate, sampleFunction} from "../src";
 
 describe("This is a simple test", () => {
   test("Check the sampleFunction function", () => {
@@ -7,3 +7,9 @@ describe("This is a simple test", () => {
     expect(actual).toEqual(expected);
   });
 });
+
+describe("Simple expression tests", () => {
+  test("Check literal value", () => {
+    expect(evaluate({ type: "literal", value: 5 })).toBeCloseTo(5);
+  });
+});

+ 1 - 1
jest.config.js

@@ -1,5 +1,5 @@
 module.exports = {
-  collectCoverage: true,
+  collectCoverage: false,
   moduleFileExtensions: [
     "ts",
     "tsx",

+ 15 - 0
src/core/functions.ts

@@ -0,0 +1,15 @@
+import {assertNever} from "../utils";
+import {Expression} from "./types";
+
+export function evaluate(expr: Expression): number {
+  switch (expr.type) {
+    case "literal": {
+      return expr.value;
+    }
+
+    /* istanbul ignore next */
+    default: {
+      return assertNever("Unexpected expression type", expr);
+    }
+  }
+}

+ 2 - 0
src/core/index.ts

@@ -0,0 +1,2 @@
+export * from "./types";
+export * from "./functions";

+ 15 - 0
src/core/types.ts

@@ -0,0 +1,15 @@
+export interface Literal {
+  type: "literal";
+  value: number;
+}
+
+export type BinaryOperators = "+" | "-" | "*" | "/";
+
+export interface BinaryOperation {
+  type: "binary";
+  operator: BinaryOperators;
+  left: Expression;
+  right: Expression;
+}
+
+export type Expression = Literal | BinaryOperation;

+ 4 - 1
src/index.ts

@@ -1,3 +1,6 @@
 export function sampleFunction(x: string): string {
-    return x + x;
+  return x + x;
 }
+
+export * from "./core";
+export * from "./utils";

+ 6 - 0
src/utils/checks.ts

@@ -0,0 +1,6 @@
+/* istanbul ignore next */
+import {Error} from "tslint/lib/error";
+
+export function assertNever(msg: string, value: never): never {
+  throw new Error(msg + ": " + JSON.stringify(value));
+}

+ 1 - 0
src/utils/index.ts

@@ -0,0 +1 @@
+export * from "./checks";

+ 4 - 2
tslint.json

@@ -4,6 +4,8 @@
     "tslint:recommended"
   ],
   "jsRules": {},
-  "rules": {},
+  "rules": {
+    "interface-name": [true, "never-prefix"]
+  },
   "rulesDirectory": []
-}
+}