refactor: typescript support

This commit is contained in:
liyasthomas
2021-09-15 17:30:04 +05:30
parent 96bcbc80f8
commit 4e8a4e8914
21 changed files with 98 additions and 176 deletions

View File

@@ -8,9 +8,9 @@ import { map } from "rxjs/operators"
*
* @returns The constructed object observable
*/
export function constructFromStreams<T>(
streamObj: { [key in keyof T]: Observable<T[key]> }
): Observable<T> {
export function constructFromStreams<T>(streamObj: {
[key in keyof T]: Observable<T[key]>
}): Observable<T> {
return combineLatest(Object.values<Observable<T[keyof T]>>(streamObj)).pipe(
map((streams) => {
const keys = Object.keys(streamObj) as (keyof T)[]

View File

@@ -1,38 +0,0 @@
import debounce from "../debounce"
describe("debounce", () => {
test("doesn't call function right after calling", () => {
const fn = jest.fn()
const debFunc = debounce(fn, 100)
debFunc()
expect(fn).not.toHaveBeenCalled()
})
test("calls the function after the given timeout", () => {
const fn = jest.fn()
jest.useFakeTimers()
const debFunc = debounce(fn, 100)
debFunc()
jest.runAllTimers()
expect(fn).toHaveBeenCalled()
// expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 100)
})
test("calls the function only one time within the timeframe", () => {
const fn = jest.fn()
const debFunc = debounce(fn, 1000)
for (let i = 0; i < 100; i++) debFunc()
jest.runAllTimers()
expect(fn).toHaveBeenCalledTimes(1)
})
})

View File

@@ -1,21 +0,0 @@
import { parseUrlAndPath } from "../uri"
describe("parseUrlAndPath", () => {
test("has url and path fields", () => {
const result = parseUrlAndPath("https://hoppscotch.io/")
expect(result).toHaveProperty("url")
expect(result).toHaveProperty("path")
})
test("parses out URL correctly", () => {
const result = parseUrlAndPath("https://hoppscotch.io/test/page")
expect(result.url).toBe("https://hoppscotch.io")
})
test("parses out Path correctly", () => {
const result = parseUrlAndPath("https://hoppscotch.io/test/page")
expect(result.path).toBe("/test/page")
})
})

View File

@@ -1,4 +1,4 @@
export const decodeB64StringToArrayBuffer = (input) => {
export function decodeB64StringToArrayBuffer(input: string): ArrayBuffer {
const bytes = Math.floor((input.length / 4) * 3)
const ab = new ArrayBuffer(bytes)
const uarray = new Uint8Array(ab)

View File

@@ -1,15 +0,0 @@
// Debounce is a higher order function which makes its enclosed function be executed
// only if the function wasn't called again till 'delay' time has passed, this helps reduce impact of heavy working
// functions which might be called frequently
// NOTE : Don't use lambda functions as this doesn't get bound properly in them, use the 'function (args) {}' format
const debounce = (func, delay) => {
let inDebounce
return function () {
const context = this
const args = arguments
clearTimeout(inDebounce)
inDebounce = setTimeout(() => func.apply(context, args), delay)
}
}
export default debounce

View File

@@ -1,12 +0,0 @@
export function getSourcePrefix(source) {
const sourceEmojis = {
// Source used for info messages.
info: "\t [INFO]:\t",
// Source used for client to server messages.
client: "\t⬅ [SENT]:\t",
// Source used for server to client messages.
server: "\t➡ [RECEIVED]:\t",
}
if (Object.keys(sourceEmojis).includes(source)) return sourceEmojis[source]
return ""
}

12
helpers/utils/string.ts Normal file
View File

@@ -0,0 +1,12 @@
const sourceEmojis = {
// Source used for info messages.
info: "\t [INFO]:\t",
// Source used for client to server messages.
client: "\t⬅ [SENT]:\t",
// Source used for server to client messages.
server: "\t➡ [RECEIVED]:\t",
}
export function getSourcePrefix(source: keyof typeof sourceEmojis) {
return sourceEmojis[source]
}

View File

@@ -1,15 +0,0 @@
export function parseUrlAndPath(value) {
const result = {}
try {
const url = new URL(value)
result.url = url.origin
result.path = url.pathname
} catch (e) {
const uriRegex = value.match(
/^((http[s]?:\/\/)?(<<[^/]+>>)?[^/]*|)(\/?.*)$/
)
result.url = uriRegex[1]
result.path = uriRegex[4]
}
return result
}