Skip to content

Commit

Permalink
fix(vue-gtag.d.ts): typescript types & deduping utils.js (#315)
Browse files Browse the repository at this point in the history
closes #314
  • Loading branch information
based-ghost committed May 21, 2021
1 parent 8788664 commit 8c2e395
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 38 deletions.
6 changes: 3 additions & 3 deletions src/add-routes-tracker.js
@@ -1,5 +1,5 @@
import { nextTick } from "vue";
import { isFn } from "@/utils";
import { isFunction } from "@vue/shared";
import { getRouter } from "@/router";
import { getOptions } from "@/options";
import addConfiguration from "@/add-configuration";
Expand Down Expand Up @@ -33,13 +33,13 @@ export default () => {
return;
}

if (isFn(onBeforeTrack)) {
if (isFunction(onBeforeTrack)) {
onBeforeTrack(to, from);
}

track(to, from);

if (isFn(onAfterTrack)) {
if (isFunction(onAfterTrack)) {
onAfterTrack(to, from);
}
});
Expand Down
5 changes: 3 additions & 2 deletions src/track.js
@@ -1,5 +1,6 @@
import { isFunction } from "@vue/shared";
import { getOptions } from "@/options";
import { validateScreenviewShape, isFn } from "@/utils";
import { validateScreenviewShape } from "@/utils";
import * as api from "@/api";

export default (to = {}, from = {}) => {
Expand All @@ -16,7 +17,7 @@ export default (to = {}, from = {}) => {

let template = to;

if (isFn(proxy)) {
if (isFunction(proxy)) {
template = proxy(to, from);
} else if (useScreenview) {
template = validateScreenviewShape({
Expand Down
12 changes: 4 additions & 8 deletions src/utils.js
@@ -1,3 +1,5 @@
import { isPlainObject } from "@vue/shared";

export const load = (url, options = {}) => {
return new Promise((resolve, reject) => {
if (typeof document === "undefined") {
Expand Down Expand Up @@ -27,25 +29,19 @@ export const load = (url, options = {}) => {
});
};

export const isFn = (fn) => typeof fn === "function";

export const isObject = (item) => {
return item && typeof item === "object" && !Array.isArray(item);
};

export const mergeDeep = (target, ...sources) => {
if (!sources.length) {
return target;
}

const source = sources.shift();

if (!isObject(target) || !isObject(source)) {
if (!isPlainObject(target) || !isPlainObject(source)) {
return;
}

for (const key in source) {
if (isObject(source[key])) {
if (isPlainObject(source[key])) {
if (!target[key]) {
Object.assign(target, { [key]: {} });
}
Expand Down
20 changes: 0 additions & 20 deletions test/utils.spec.js
Expand Up @@ -74,23 +74,3 @@ describe("warn", () => {
expect(console.warn).toHaveBeenCalledWith("[vue-gtag] foo");
});
});

describe("isFn", () => {
it("should return true", () => {
expect(util.isFn(() => {})).toBe(true);
});

it("should return false", () => {
expect(util.isFn("hello")).toBe(false);
});
});

describe("isObject", () => {
it("should return true", () => {
expect(util.isObject({})).toBe(true);
});
it("should return false", () => {
expect(util.isObject([])).toBe(false);
expect(util.isObject("aa")).toBe(false);
});
});
10 changes: 5 additions & 5 deletions vue-gtag.d.ts
@@ -1,6 +1,6 @@
declare module "vue-gtag" {
import VueRouter from "vue-router";
import _Vue from "vue";
import type { App } from "vue";
import type { Router } from "vue-router";

/**
* Types copied from @types/gtag.js.
Expand All @@ -12,7 +12,7 @@ declare module "vue-gtag" {
(command: 'config', targetId: string, config?: ControlParams | EventParams | CustomParams): void;
(command: 'set', config: CustomParams): void;
(command: 'js', config: Date): void;
(command: 'event', eventName: EventNames | string, eventParams?: ControlParams | EventParams | CustomParams): void;
(command: 'event', eventName: EventNames | string, eventParams?: ControlParams | EventParams | CustomParams): void;
}

interface CustomParams {
Expand Down Expand Up @@ -284,9 +284,9 @@ declare module "vue-gtag" {

export class VueGtagPlugin {
static install(
Vue: typeof _Vue,
app: App,
options: PluginOptions,
router?: VueRouter
router?: Router
): void;
}

Expand Down

0 comments on commit 8c2e395

Please sign in to comment.