Add package updates, car updates, and vehicle screens (#25)
This commit is contained in:
13
src/utils/dates.js
Normal file
13
src/utils/dates.js
Normal file
@@ -0,0 +1,13 @@
|
||||
export const ts2DateTime = (timestamp) => {
|
||||
return new Date(timestamp * 1000);
|
||||
}
|
||||
|
||||
export const tsLocalDateTimeString = (timestamp) => {
|
||||
const date = ts2DateTime(timestamp);
|
||||
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`
|
||||
}
|
||||
|
||||
export const LocalDateTimeString = (datestring) => {
|
||||
const date = new Date(datestring.replace(' ', 'T'));
|
||||
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`
|
||||
}
|
||||
@@ -11,4 +11,14 @@ export const fetchRespHandler = (response) => {
|
||||
error: response.statusText,
|
||||
message: `${response.status} ${response.statusText}`,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
export const addQueryParams = (url, params) => {
|
||||
if (!params) return url;
|
||||
|
||||
var u = new URL(url);
|
||||
|
||||
Object.keys(params).forEach(key => u.searchParams.append(key, params[key]))
|
||||
|
||||
return u.toString();
|
||||
}
|
||||
|
||||
42
src/utils/http.test.js
Normal file
42
src/utils/http.test.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import { getAuthHeaderOptions, addQueryParams } from "./http";
|
||||
|
||||
describe("HTTP Helper", () => {
|
||||
it("Authorization header", () => {
|
||||
const result = getAuthHeaderOptions("TEST_TOKEN");
|
||||
|
||||
expect(result).toBeTruthy();
|
||||
expect(result.Authorization).toBeTruthy();
|
||||
expect(result.Authorization).toEqual("Bearer TEST_TOKEN");
|
||||
});
|
||||
|
||||
describe("addQueryParams", () => {
|
||||
it("String and number params", () => {
|
||||
const params = {
|
||||
a: "a",
|
||||
b: "b",
|
||||
c: 1,
|
||||
d: 3.14,
|
||||
}
|
||||
const result = addQueryParams("http://example.com", params);
|
||||
|
||||
expect(result).toEqual("http://example.com/?a=a&b=b&c=1&d=3.14");
|
||||
});
|
||||
|
||||
it("Null params", () => {
|
||||
const params = null;
|
||||
const result = addQueryParams("http://example.com", params);
|
||||
|
||||
expect(result).toEqual("http://example.com");
|
||||
});
|
||||
|
||||
it("Undefined params", () => {
|
||||
const result = addQueryParams("http://example.com");
|
||||
expect(result).toEqual("http://example.com");
|
||||
});
|
||||
|
||||
it("Empty params", () => {
|
||||
const result = addQueryParams("http://example.com", {});
|
||||
expect(result).toEqual("http://example.com/");
|
||||
});
|
||||
})
|
||||
});
|
||||
@@ -12,4 +12,3 @@ describe("JWT Helper", () => {
|
||||
expect(v.exp).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user