Skip to content

Commit

Permalink
Enumerate basis, add various inputs
Browse files Browse the repository at this point in the history
This makes progress on #12.
  • Loading branch information
reergymerej committed Sep 2, 2020
1 parent 2f68c35 commit 97b7895
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 47 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
"prettier",
],
rules: {
'@typescript-eslint/no-empty-function': 0,
"@typescript-eslint/no-explicit-any": 2,
"@typescript-eslint/explicit-module-boundary-types": 0,
complexity: [2, 4],
Expand Down
28 changes: 24 additions & 4 deletions src/getAddition.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
export const getAddition = (basis: unknown): number | string => {
const type = typeof basis
import { getType } from "./util"
import { error } from "./visualize"
import { SideLabel, TruthyError } from "./types"

// eslint-disable-next-line complexity
export const getAddition = (
side: SideLabel,
basis: unknown,
): number | string => {
const type = getType(basis)
switch (type) {
case "bigint":
return 9
case "boolean":
return 1
case "function":
return 1
case "null":
return 1
case "number":
return basis ? 0 : 1
case "object":
return 1
case "string":
return "anything"
default:
throw new Error(`unhandled case "${type}"`)
case "symbol":
throw error(side, "+", basis, TruthyError.AdditionSymbol)
case "undefined":
return ""
}
}
13 changes: 4 additions & 9 deletions src/getDivision.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TruthyError, SideLabel, StringType } from "./types"
import { error } from "./visualize"
import { getStringType } from "./util"
import { getStringType, getType } from "./util"

const divisionError = (side: SideLabel) => (
basis: unknown,
Expand Down Expand Up @@ -31,8 +31,6 @@ const solveForLeftString = (basis: string): ReturnType<typeof getDivision> => {
throw leftError(basis, TruthyError.DivisionString)
case StringType.Numeric:
return getDivision(SideLabel.left, Number(basis))
default:
throw new Error(`unhandled case "${getStringType(basis)}"`)
}
}

Expand All @@ -44,8 +42,6 @@ const solveForRightString = (basis: string): ReturnType<typeof getDivision> => {
throw rightError(basis, TruthyError.DivisionString)
case StringType.Numeric:
return getDivision(SideLabel.right, Number(basis))
default:
throw new Error(`unhandled case "${getStringType(basis)}"`)
}
}

Expand All @@ -59,14 +55,13 @@ export const getDivision = (
side: SideLabel,
basis: unknown,
): number | string => {
const type = typeof basis

const type = getType(basis)
switch (type) {
case "number":
return solveForNumber(side, basis as number)
case "string":
return solveForString(side, basis as string)
default:
throw new Error(`unhandled case "${type}"`)
case "symbol":
throw error(side, "/", basis, TruthyError.DivisionSymbol)
}
}
21 changes: 14 additions & 7 deletions src/getExponentiation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SideLabel, TruthyError, StringType } from "./types"
import { error } from "./visualize"
import { getStringType } from "./util"
import { getStringType, getType } from "./util"

const expoError = (side: SideLabel) => (
basis: string,
Expand All @@ -18,27 +18,34 @@ const solveForStringOnLeft = (
return 1
case StringType.Numeric:
return getExponentiation(SideLabel.left, Number(basis))
default:
throw new Error(`unhandled case "${getStringType(basis)}"`)
}
}

const solveForLeft = (basis: unknown): number | null => {
const type = typeof basis
const type = getType(basis)
switch (type) {
case "number":
return 1
case "string":
return solveForStringOnLeft(basis as string)
default:
throw new Error(`unhandled case "${type}"`)
case "symbol":
throw error(SideLabel.left, "**", basis, TruthyError.ExpoSymbol)
}
}

const solveForRight = (basis: unknown): null => {
const type = getType(basis)
switch (type) {
case "symbol":
throw error(SideLabel.right, "**", basis, TruthyError.ExpoSymbol)
}
return null
}

export const getExponentiation = (
side: SideLabel,
basis: unknown,
): ReturnType<typeof solveForLeft> | null =>
side === SideLabel.left
? solveForLeft(basis) //
: null
: solveForRight(basis)
14 changes: 7 additions & 7 deletions src/getModulo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SideLabel, TruthyError, StringType } from "./types"
import { error } from "./visualize"
import { getStringType } from "./util"
import { getStringType, getType } from "./util"

const modError = (side: SideLabel) => (
basis: unknown,
Expand Down Expand Up @@ -31,8 +31,6 @@ const getLeftString = (basis: string) => {
throw rightError(basis, TruthyError.ModuloLeftStringWord)
case StringType.Numeric:
return getModulo(SideLabel.left, Number(basis))
default:
throw new Error(`unhandled case "${getStringType(basis)}"`)
}
}

Expand All @@ -44,29 +42,31 @@ const getRightString = (basis: string) => {
throw rightError(basis, TruthyError.ModuloRightStringWord)
case StringType.Numeric:
return getModulo(SideLabel.right, Number(basis))
default:
throw new Error(`unhandled case "${getStringType(basis)}"`)
}
}

const getLeft = (basis: unknown) => {
const type = typeof basis
const type = getType(basis)
switch (type) {
case "number":
return getLeftNumber(basis as number)
case "string":
return getLeftString(basis as string)
case "symbol":
throw error(SideLabel.left, "%", basis, TruthyError.ModSymbol)
default:
throw new Error(`unhandled case "${type}"`)
}
}
const getRight = (basis: unknown) => {
const type = typeof basis
const type = getType(basis)
switch (type) {
case "number":
return getRightNumber(basis as number)
case "string":
return getRightString(basis as string)
case "symbol":
throw error(SideLabel.right, "%", basis, TruthyError.ModSymbol)
default:
throw new Error(`unhandled case "${type}"`)
}
Expand Down
10 changes: 4 additions & 6 deletions src/getMultiplication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SideLabel, TruthyError, StringType } from "./types"
import { error } from "./visualize"
import { getStringType } from "./util"
import { getStringType, getType } from "./util"

const solveForNumber = (side: SideLabel, basis: number): number => {
if (basis === 0) {
Expand All @@ -20,22 +20,20 @@ const solveForString = (
throw new Error(error(side, "*", basis, TruthyError.MultiplyStringWord))
case StringType.Numeric:
return getMultiplication(side, Number(basis))
default:
throw new Error(`unhandled case "${getStringType(basis)}"`)
}
}

export const getMultiplication = (
side: SideLabel,
basis: unknown,
): number | string => {
const type = typeof basis
const type = getType(basis)
switch (type) {
case "number":
return solveForNumber(side, basis as number)
case "string":
return solveForString(side, basis as string)
default:
throw new Error(`unhandled case "${type}"`)
case "symbol":
throw error(side, "*", basis, TruthyError.MultiplySymbol)
}
}
10 changes: 7 additions & 3 deletions src/getSubtraction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SideLabel, TruthyError } from "./types"
import { error } from "./visualize"
import { getType } from "./util"

const solveForString = (
side: SideLabel,
Expand All @@ -12,17 +13,20 @@ const solveForString = (
return getSubtraction(side, parsed)
}

// eslint-disable-next-line complexity
export const getSubtraction = (
side: SideLabel,
basis: unknown,
): number | string => {
const type = typeof basis
const type = getType(basis)
switch (type) {
case "number":
return basis ? 0 : 1
case "string":
return solveForString(side, basis as string)
default:
throw new Error(`unhandled case "${type}"`)
case "bigint":
return 1
case "symbol":
throw error(side, "-", basis, TruthyError.SubtractionSymbol)
}
}
11 changes: 11 additions & 0 deletions src/greaterOrLessThan.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { getCodePoint } from "./greaterOrLessThan"

describe("getCodePoint", () => {
describe("with an empty string", () => {
it("should throw", () => {
expect(() => {
getCodePoint("")
}).toThrow(/empty/)
})
})
})
18 changes: 13 additions & 5 deletions src/greaterOrLessThan.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { Operator, SideLabel, TruthyError } from "./types"
import { error } from "./visualize"
import { getType } from "./util"

export const getCodePoint = (basis: string): number => {
if (!basis) {
throw new Error(`Unable to determine next when the input string is empty.`)
}
return basis.codePointAt(0) as number
}

const getNextString = (change: number, basis: string): string => {
const codePoint = basis.codePointAt(0)
const nextString = String.fromCodePoint((codePoint || 0) + change)
const codePoint = getCodePoint(basis)
const nextString = String.fromCodePoint(codePoint + change)
const result = nextString + basis.substring(1)
return result
}
Expand All @@ -29,14 +37,14 @@ const getGreaterOrLessThan = (change: number) => (
side: SideLabel,
basis: unknown,
): number | string => {
const type = typeof basis
const type = getType(basis)
switch (type) {
case "number":
return (basis as number) + change
case "string":
return getString(change)(operator, basis as string, side)
default:
throw new Error(`unhandled case "${type}"`)
case "symbol":
throw error(side, operator, basis, TruthyError.GreaterThanLessThanSymbol)
}
}

Expand Down

0 comments on commit 97b7895

Please sign in to comment.