Skip to content

Commit

Permalink
fix: add support for now (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoehnelt authored Jul 9, 2020
1 parent 4bc28d3 commit 53ef591
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
5 changes: 2 additions & 3 deletions e2e/directions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ test("directions should get correct result", async () => {
"McLaren+Vale,SA",
],
optimize: true,
departure_time: "now" as const,
key: process.env.GOOGLE_MAPS_API_KEY,
};
const r = await directions({ params: params });
expect(r.request.path).toMatch(
"waypoints=optimize%3Atrue|Barossa"
);
expect(r.request.path).toMatch("waypoints=optimize%3Atrue|Barossa");
expect(r.data.status).toEqual(Status.OK);
expect(r.data.routes[0].legs[0].distance.value).toBeGreaterThan(0);
expect(
Expand Down
35 changes: 17 additions & 18 deletions src/serialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
objectToString,
serializer,
toLatLngLiteral,
toTimestamp
toTimestamp,
} from "./serialize";

test("latLngToString is correct", () => {
Expand All @@ -41,22 +41,22 @@ test("latLngBoundsToString is correct", () => {
expect(
latLngBoundsToString({
southwest: { lat: 1, lng: 2 },
northeast: { lat: 3, lng: 4 }
northeast: { lat: 3, lng: 4 },
})
).toBe("1,2|3,4");
});

test("serializer", () => {
expect(serializer({ quz: o => o })({ foo: ["bar"] })).toBe("foo=bar");
expect(serializer({ quz: (o) => o })({ foo: ["bar"] })).toBe("foo=bar");
expect(
serializer({ foo: o => o.map((latLng: LatLng) => latLngToString(latLng)) })(
{
foo: [
[0, 1],
[2, 3]
]
}
)
serializer({
foo: (o) => o.map((latLng: LatLng) => latLngToString(latLng)),
})({
foo: [
[0, 1],
[2, 3],
],
})
).toBe("foo=0%2C1|2%2C3");
});

Expand All @@ -75,7 +75,7 @@ test("latLngArrayToStringMaybeEncoded", () => {
expect(
latLngArrayToStringMaybeEncoded([
[40.714728, -73.998672],
[-34.397, 150.644]
[-34.397, 150.644],
])
).toEqual("enc:abowFtzsbMhgmiMuobzi@");
});
Expand All @@ -85,11 +85,11 @@ test("toLatLngLiteral", () => {
expect(toLatLngLiteral([0, 1])).toEqual({ lat: 0, lng: 1 });
expect(toLatLngLiteral({ lat: 0, lng: 1 })).toEqual({
lat: 0,
lng: 1
lng: 1,
});
expect(toLatLngLiteral({ latitude: 0, longitude: 1 })).toEqual({
lat: 0,
lng: 1
lng: 1,
});
expect(() => {
toLatLngLiteral({} as LatLngLiteral);
Expand All @@ -98,10 +98,9 @@ test("toLatLngLiteral", () => {

test("toTimestamp", () => {
expect(toTimestamp(100)).toEqual(100);

const dt = new Date();
const seconds = Number(dt) / 1000
const seconds = Number(dt) / 1000;
expect(toTimestamp(dt)).toEqual(seconds);

expect(toTimestamp("now")).toEqual("now");
});

13 changes: 8 additions & 5 deletions src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function latLngToString(o: LatLng) {
}

return o
.map(x => {
.map((x) => {
return x.toString();
})
.join(",");
Expand All @@ -47,7 +47,7 @@ export function objectToString(o: string | object): string {
} else {
let keys = Object.keys(o);
keys.sort();
return keys.map(k => k + ":" + o[k]).join(separator);
return keys.map((k) => k + ":" + o[k]).join(separator);
}
}

Expand Down Expand Up @@ -101,7 +101,7 @@ export function serializer(
format: serializerFormat,
queryStringOptions: object = {
arrayFormat: "separator",
arrayFormatSeparator: separator
arrayFormatSeparator: separator,
}
) {
return (params: { [key: string]: any }) => {
Expand All @@ -114,9 +114,12 @@ export function serializer(
};
}

export function toTimestamp(o: number | Date) {
export function toTimestamp(o: "now" | number | Date): number | "now" {
if (o === "now") {
return o;
}
if (o instanceof Date) {
return Number(o) / 1000;
}
return o
return o;
}

0 comments on commit 53ef591

Please sign in to comment.