Skip to content

Commit

Permalink
support divide (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pistonight authored Nov 30, 2024
1 parent 8be188f commit ef243ef
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 92 deletions.
4 changes: 0 additions & 4 deletions .prettierrc.cjs

This file was deleted.

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Michael

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 10 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
# addtime
Adder of Time https://pistonite.github.io/addtime
Adder of Time https://addtime.pistonite.dev


## To use in Google Sheet Apps Script
In your Google Sheet, go to `Extensions > Apps Script`, and paste
in [this script](https://pistonite.github.io/addtime/appscript.txt)
in [this script](https://addtime.pistonite.dev/appscript.txt)

You will have access to the `ADDTIME` function which adds a range of cells,
and the `SUBTIME` function that subtracts 2 cells
The following functions are available:
- `ADDTIME` - Adds a range of cells of time expressions
- `SUBTIME` - Subtracts 2 cells of time expressions
- `DIVTIME` - Divides 2 cells of time expressions

## To consume as TypeScript library
Download the source to your project
Simply copy the TS source to your project
```
curl https://pistonite.github.io/addtime/time.ts > somewhere/addtime.ts
curl https://pistonite.github.io/addtime/dist.ts > somewhere/addtime.ts
```
Then map the path in your `tsconfig.json`
```json
{
"compilerOptions": {
"paths": {
"addtime": ["base/rel/path/to/somewhere/addtime.ts"]
}
},
"include": ["path/to/somewhere"]
}
```
Finally import and use
Then import and use
```typescript
import { calc } from "addtime";
import { calc, ratio } from "addtime.ts";
```
70 changes: 48 additions & 22 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ tasks:
install:
desc: Install dependencies
cmds:
- bun install
- bun install

ci:
cmds:
- bun install --frozen-lockfile
- bun install --frozen-lockfile

test:
desc: Run tests
cmds:
- bun test
- bun test
- task: test:code

dev:
desc: Run tests in watch mode
cmds:
- bun test --watch
- bun test --watch

check:
desc: Check for issues
Expand All @@ -38,28 +39,53 @@ tasks:

prettier:
cmds:
- bunx --bun prettier "./**/*.{html,ts,tsx,json,js,jsx,cjs,css}" {{.PRETTIER_ARGS}}
- bunx --bun prettier "./**/*.{html,ts,tsx,json,js,jsx,cjs,css}" {{.PRETTIER_ARGS}}

build:
desc: Build the distributable web app
desc: Build the distributables
cmds:
- mkdir -p dist temp
- rm -rf dist/*
- cp src/index.html dist/index.html
- bun build ./src/main.ts --outfile=dist/main.js --minify
- bun build ./src/code.ts --outfile=temp/code.gs --minify
- sed -i -e '1s/^/var calc=(function(){ /' temp/code.gs
- sed -i -e 's/;window\.__calc=/;return /' temp/code.gs
- cp src/code.gs dist/appscript.txt
- cat temp/code.gs >> dist/appscript.txt
- echo '})()' >> dist/appscript.txt
- echo '/* eslint-disable */' > dist/time.ts
- echo '// @ts-nocheck' >> dist/time.ts
- echo '//! https://github.com/Pistonite/addtime' >> dist/time.ts
- echo '' >> dist/time.ts
- cat src/time.ts >> dist/time.ts
- rm -rf dist/*
- task: build:main
- task: build:dist
- task: build:code

build:main:
desc: Build the main web app
cmds:
- mkdir -p dist
- cp src/index.html dist/index.html
- bun build ./src/main.ts --outfile=dist/main.js --minify

build:dist:
desc: Build the distributable TS source
cmds:
- mkdir -p dist
- echo '/* eslint-disable */' > dist/dist.ts
- echo '// @ts-nocheck' >> dist/dist.ts
- echo '//! https://github.com/Pistonite/addtime' >> dist/dist.ts
- echo '' >> dist/dist.ts
- cat src/time.ts >> dist/dist.ts

build:code:
desc: Build the distributable Google Apps Script
cmds:
- mkdir -p dist temp
- bun build ./src/code.ts --outfile=temp/code.ts --minify
- sed -i -e '1s/^/var AdderOfTime=(function(){ /' temp/code.ts
- sed -i -e 's/window\.__export=/;return /' temp/code.ts
- cp src/code.gs dist/appscript.txt
- cat temp/code.ts >> dist/appscript.txt
- echo '})()' >> dist/appscript.txt

test:code:
desc: Test the Google Apps Script
cmds:
- task: build:code
- cp dist/appscript.txt temp/appscript.e2e.js
- cat src/code.e2e.js >> temp/appscript.e2e.js
- bun temp/appscript.e2e.js

serve:
desc: Serve /dist
cmds:
- bunx --bun serve dist
- bunx --bun serve dist
24 changes: 0 additions & 24 deletions UNLICENSE

This file was deleted.

Binary file modified bun.lockb
Binary file not shown.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
"name": "addtime",
"devDependencies": {
"@types/bun": "latest",
"prettier": "^3.2.5",
"prettier": "^3.3.3",
"serve": "^14.2.3",
"typescript": "^5.0.0"
"typescript": "^5.7.2"
},
"prettier": {
"tabWidth": 4,
"endOfLine": "auto"
}
}
13 changes: 13 additions & 0 deletions src/code.e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// E2E testing for app script

function testEq(a, b) {
if (a !== b) {
throw new Error(`Expected ${a} to equal ${b}`);
}
}

testEq(ADDTIME([["1m20s", "2m40s"]]), "04m00s");
testEq(SUBTIME("1m20s", "30s"), "50s");
testEq(DIVTIME("1m", "01m20s"), 0.75);

console.log("All tests passed!");
14 changes: 11 additions & 3 deletions src/code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// Usage: =ADDTIME(range)
/// Example: =ADDTIME(A1:A36)
function ADDTIME(times) {
return calc(
return AdderOfTime.calc(
times.map(t=>t.filter(t=>t).join("+")).filter(t=>t).join("+"),
BigInt(30)
).answers.join(",\n");
Expand All @@ -12,8 +12,16 @@ function ADDTIME(times) {
/// Usage: =SUBTIME(cell1, cell2)
/// Example: =SUBTIME(A1, B2)
function SUBTIME(time1, time2){
var r =calc(time1+"-"+time2, BigInt(30));
return [r.answers.join(",\n"), r.errors.join(",\n")];
var r = AdderOfTime.calc(time1+"-"+time2, BigInt(30));
return r.answers.join(",\n");
}

/// DIVTIME function: divide 2 times
/// Usage: =DIVTIME(cell, divisor)
/// Example: =DIVTIME(A1, B2)
function DIVTIME(time1, time2){
var r = AdderOfTime.ratio(time1, time2, BigInt(30));
return r;
}

/// https://github.com/Pistonite/addtime
4 changes: 2 additions & 2 deletions src/code.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { calc } from "./time.ts";
import { calc, ratio } from "./time.ts";

// @ts-ignore
window.__calc = calc;
window.__export = { calc, ratio };
16 changes: 15 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
<meta property="og:site_name" content="Pistonite" />
<meta property="og:title" content="Adder of Time" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://pistonite.github.io/addtime" />
<meta property="og:url" content="https://addtime.pistonite.dev" />
<meta
property="og:description"
content="A simple but correct time adder"
/>
<style>
:root {
color-scheme: light dark;
}
</style>
</head>

<body>
Expand Down Expand Up @@ -59,6 +64,15 @@ <h1>Adder of Time</h1>
oninput="__addtime_update();"
></textarea>
<br />
<input
type="checkbox"
id="ratio_mode_checkbox"
onchange="__addtime_update();"
/>
<label for="ratio_mode_checkbox"
>Ratio Mode: Output will be Line 1 divided by Line 2 of Input</label
>
<br />
<label for="output">Output:</label>
<br />
<textarea
Expand Down
50 changes: 37 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
import { calc } from "./time";
import { calc, ratio } from "./time";

function isChecked(id: string) {
return (document.getElementById(id) as HTMLInputElement).checked;
}

function valueOf(id: string) {
return (document.getElementById(id) as HTMLInputElement).value;
}

function setTextArea(id: string, value: string) {
(document.getElementById(id) as HTMLTextAreaElement).value = value;
}

/// Update the output and errors
function update() {
const input = (document.getElementById("input") as HTMLInputElement).value;
const ratioMode = isChecked("ratio_mode_checkbox");
const input = valueOf("input");

var unit = BigInt(1000);
if (
(document.getElementById("radio_unit_30") as HTMLInputElement).checked
) {
if (isChecked("radio_unit_30")) {
unit = BigInt(30);
} else if (
(document.getElementById("radio_unit_60") as HTMLInputElement).checked
) {
} else if (isChecked("radio_unit_60")) {
unit = BigInt(60);
}
const { answers, errors } = calc(input, unit);
(document.getElementById("output") as HTMLTextAreaElement).value =
answers.join(",\n");
(document.getElementById("error") as HTMLTextAreaElement).value =
errors.join(",\n");

if (ratioMode) {
const [a, b] = input.split("\n");
if (!a || !b) {
setTextArea("error", "Ratio mode requires two lines in input");
}
const answer = ratio(a, b, unit);
if (typeof answer === "string") {
setTextArea("error", answer);
setTextArea("output", "There is an error");
} else {
setTextArea("output", answer * 100 + "%");
setTextArea("error", "");
}
} else {
const { answers, errors } = calc(input, unit);
setTextArea("output", answers.join(",\n"));
setTextArea("error", errors.join(",\n"));
}
}

(window as any).__addtime_update = update;
9 changes: 9 additions & 0 deletions src/time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,13 @@ describe("tokenize", () => {
"f",
]);
});
test("operator first", () => {
expect(tokenize("-30h-")).toEqual([
"0", // extra operand added by tokenizer
"-",
"30",
"h",
"-",
]);
});
});
Loading

0 comments on commit ef243ef

Please sign in to comment.