add util.ts for sharing common functions between frontend and backend

This commit is contained in:
LouisLam
2021-07-30 11:23:04 +08:00
parent 71af902a4e
commit 081abcb6a1
12 changed files with 120 additions and 49 deletions

View File

@@ -3,9 +3,9 @@
<span v-else>{{ value }}</span>
</template>
<script>
<script lang="ts">
import { sleep } from "../util-frontend"
import { sleep } from "../util.ts"
export default {

View File

@@ -345,9 +345,9 @@
</Confirm>
</template>
<script>
<script lang="ts">
import { Modal } from "bootstrap"
import { ucfirst } from "../util-frontend"
import { ucfirst } from "../util.ts"
import axios from "axios";
import { useToast } from "vue-toastification"
import Confirm from "./Confirm.vue";

View File

@@ -5,19 +5,6 @@ import utc from "dayjs/plugin/utc";
dayjs.extend(utc)
dayjs.extend(timezone)
export function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export function ucfirst(str) {
if (! str) {
return str;
}
const firstLetter = str.substr(0, 1);
return firstLetter.toUpperCase() + str.substr(1);
}
function getTimezoneOffset(timeZone) {
const now = new Date();
const tzString = now.toLocaleString("en-US", {

24
src/util.js Normal file
View File

@@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.debug = exports.ucfirst = exports.sleep = exports.PENDING = exports.UP = exports.DOWN = void 0;
exports.DOWN = 0;
exports.UP = 1;
exports.PENDING = 2;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
exports.sleep = sleep;
function ucfirst(str) {
if (!str) {
return str;
}
const firstLetter = str.substr(0, 1);
return firstLetter.toUpperCase() + str.substr(1);
}
exports.ucfirst = ucfirst;
function debug(msg) {
if (process.env.NODE_ENV === "development") {
console.log(msg);
}
}
exports.debug = debug;

31
src/util.ts Normal file
View File

@@ -0,0 +1,31 @@
// Common Util for frontend and backend
// Backend uses the compiled file util.js
// Frontend uses util.ts
// Need to run "tsc" to compile if there are any changes.
export const DOWN = 0;
export const UP = 1;
export const PENDING = 2;
export function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* PHP's ucfirst
* @param str
*/
export function ucfirst(str) {
if (! str) {
return str;
}
const firstLetter = str.substr(0, 1);
return firstLetter.toUpperCase() + str.substr(1);
}
export function debug(msg) {
if (process.env.NODE_ENV === "development") {
console.log(msg)
}
}