Skip to content

Commit

Permalink
Add union type DeviceType for more concrete device.type usage (#120)
Browse files Browse the repository at this point in the history
* Add union type `DeviceType` for more concrete `device.type` usage from `GenericDeviceResult`.

* version 2.2.9

Co-authored-by: Etienne Martin <[email protected]>
  • Loading branch information
KODerFunk and etienne-martin authored Apr 9, 2021
1 parent 90a62be commit 73ba5b9
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "device-detector-js",
"version": "2.2.8",
"version": "2.2.9",
"description": "A javascript port of Matomo device-detector",
"homepage": "https://github.com/etienne-martin/device-detector-js",
"keywords": [
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import BrowserParser from "./parsers/client/browser";
import BotParser = require("./parsers/bot");
import { userAgentParser } from "./utils/user-agent";
import { versionCompare } from "./utils/version-compare";
import { GenericDeviceResult } from "./typings/device";

namespace DeviceDetector {
export interface DeviceDetectorResult {
Expand Down Expand Up @@ -147,8 +148,8 @@ class DeviceDetector {
/**
* All detected feature phones running android are more likely smartphones
*/
if (result.device && result.device?.type === "feature phone" && osFamily === "Android") {
result.device.type = "smartphone";
if ((result.device?.type as string) === "feature phone" && osFamily === "Android") {
result.device!.type = "smartphone";
}

/**
Expand Down Expand Up @@ -243,7 +244,7 @@ class DeviceDetector {
return userAgentParser("Touch", userAgent);
};

private createDeviceObject = () => ({
private createDeviceObject = (): GenericDeviceResult => ({
type: "",
brand: "",
model: ""
Expand Down
4 changes: 2 additions & 2 deletions src/parsers/device/cameras.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export default class CameraParser {
result.type = "camera";
result.brand = brand;

if (camera.model) {
if ("model" in camera && camera.model) {
result.model = variableReplacement(camera.model, match).trim();
} else if (camera.models) {
} else if ("models" in camera && camera.models) {
for (const model of camera.models) {
const modelMatch = userAgentParser(model.regex, userAgent);

Expand Down
8 changes: 4 additions & 4 deletions src/parsers/device/consoles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import consoles from "../../fixtures/regexes/device/consoles.json";
import { GenericDeviceResult } from "../../typings/device";
import { DeviceType, GenericDeviceResult } from "../../typings/device";
import { variableReplacement } from "../../utils/variable-replacement";
import { userAgentParser } from "../../utils/user-agent";

Expand All @@ -16,12 +16,12 @@ export default class ConsoleParser {

if (!match) continue;

result.type = gameConsole.device;
result.type = gameConsole.device as DeviceType;
result.brand = brand;

if (gameConsole.model) {
if ("model" in gameConsole && gameConsole.model) {
result.model = variableReplacement(gameConsole.model, match).trim();
} else if (gameConsole.models) {
} else if ("models" in gameConsole && gameConsole.models) {
for (const model of gameConsole.models) {
const modelMatch = userAgentParser(model.regex, userAgent);

Expand Down
21 changes: 11 additions & 10 deletions src/parsers/device/mobiles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mobiles from "../../fixtures/regexes/device/mobiles.json";
import { GenericDeviceResult } from "../../typings/device";
import { DeviceType, GenericDeviceResult } from "../../typings/device"
import { variableReplacement } from "../../utils/variable-replacement";
import { userAgentParser } from "../../utils/user-agent";
import { buildModel } from "../../utils/model";
Expand All @@ -11,27 +11,28 @@ export default class MobileParser {
brand: "",
model: ""
};
let resultType = "";

for (const [brand, mobile] of Object.entries(mobiles)) {
const match = userAgentParser(mobile.regex, userAgent);

if (!match) continue;

result.type = mobile.device || "";
resultType = "device" in mobile && mobile.device || "";
result.brand = brand;

if (mobile.model) {
if ("model" in mobile && mobile.model) {
result.model = buildModel(variableReplacement(mobile.model, match)).trim();
} else if (mobile.models) {
} else if ("models" in mobile && mobile.models) {
for (const model of mobile.models) {
const modelMatch = userAgentParser(model.regex, userAgent);

if (!modelMatch) continue;

result.model = buildModel(variableReplacement(model.model, modelMatch)).trim();

if (model.device) {
result.type = model.device;
if ("device" in model && model.device) {
resultType = model.device;
}

if ("brand" in model) {
Expand All @@ -44,12 +45,12 @@ export default class MobileParser {
}

// Sanitize device type
if (result.type === "tv") {
if (resultType === "tv") {
result.type = "television";
}

if (result.type === "car browser") {
} else if (resultType === "car browser") {
result.type = "car";
} else {
result.type = resultType as DeviceType;
}

// Sanitize device brand
Expand Down
8 changes: 4 additions & 4 deletions src/parsers/device/portable-media-players.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import portableMediaPlayers from "../../fixtures/regexes/device/portable_media_player.json";
import { GenericDeviceResult } from "../../typings/device";
import { DeviceType, GenericDeviceResult } from "../../typings/device";
import { variableReplacement } from "../../utils/variable-replacement";
import { userAgentParser } from "../../utils/user-agent";

Expand All @@ -16,12 +16,12 @@ export default class PortableMediaPlayersParser {

if (!match) continue;

result.type = portableMediaPlayer.device;
result.type = portableMediaPlayer.device as DeviceType;
result.brand = brand;

if (portableMediaPlayer.model) {
if ("model" in portableMediaPlayer && portableMediaPlayer.model) {
result.model = variableReplacement(portableMediaPlayer.model, match).trim();
} else if (portableMediaPlayer.models) {
} else if ("models" in portableMediaPlayer && portableMediaPlayer.models) {
for (const model of portableMediaPlayer.models) {
const modelMatch = userAgentParser(model.regex, userAgent);

Expand Down
4 changes: 2 additions & 2 deletions src/parsers/device/televisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export default class TelevisionParser {

result.brand = brand;

if (television.model) {
if ("model" in television && television.model) {
result.model = buildModel(variableReplacement(television.model, match)).trim();
} else if (television.models) {
} else if ("models" in television && television.models) {
for (const model of television.models) {
const modelMatch = userAgentParser(model.regex, userAgent);

Expand Down
14 changes: 13 additions & 1 deletion src/typings/device.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
export type DeviceType =
| ""
| "desktop"
| "smartphone"
| "tablet"
| "television"
| "smart display"
| "camera"
| "car"
| "console"
| "portable media player"

export interface GenericDeviceResult {
type: string;
type: DeviceType;
brand: string;
model: string;
}

0 comments on commit 73ba5b9

Please sign in to comment.