Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: changing the api to return more info #26

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ npm install --save @readme/ua-platformer

```javascript
import uaPlatformer from '@readme/ua-platformer';
// const { default: uaPlatformer } = require('@readme/ua-platformer');
erunion marked this conversation as resolved.
Show resolved Hide resolved

const useragent = 'Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.1.22000.832'

console.log(uaPlatformer(useragent)); // PowerShell
console.log(uaPlatformer(useragent)); // { name: 'PowerShell', browser: false }
```

## Examples
Expand Down
34 changes: 26 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function getPlatformName(platform: string): string {
* platform is unable to be determined, `false` is returned.
*
*/
export default function uaPlatformer(useragent: string): false | string {
export default function uaPlatformer(useragent: string): false | { browser: boolean; name: string } {
if (!useragent) {
return false;
}
Expand All @@ -74,31 +74,49 @@ export default function uaPlatformer(useragent: string): false | string {
// Is this user agent from some service at Amazon, Google, or Microsoft (like Amazon Cloudfront,
// Google Cloud Tasks, or Azure Logic Apps).
if (ualower.startsWith('amazon') || ualower === 'fulfillment-gateway') {
return 'Amazon';
return {
name: 'Amazon',
browser: false,
};
} else if (ualower.startsWith('google')) {
return 'Google';
return {
name: 'Google',
browser: false,
};
} else if (ualower.startsWith('azure')) {
return 'Azure';
return {
name: 'Azure',
browser: false,
};
}

// Does this user agent contain platform or client that we know about?
const rgx = new RegExp(`(^|[(| ])(?<agent>${Object.keys(PLATFORMS).join('|')})([/| ]?v?[\\d.]+)?`, 'i');
const platform = useragent.match(rgx);
if (platform) {
return getPlatformName(platform.groups.agent);
return {
name: getPlatformName(platform.groups.agent),
browser: false,
};
}

// Is this a browser?
const parser = new UAParser(useragent).getResult();
if (parser.browser.name) {
let browserName: string;
// We don't need to differentiate here between `Chrome`, `Chromium` or `Chrome WebView`.
if (parser.browser.name.startsWith('Chrome')) {
return 'Chrome';
browserName = 'Chrome';
} else if (parser.browser.name === 'Mobile Safari') {
return 'Safari';
browserName = 'Safari';
} else {
browserName = parser.browser.name;
}

return parser.browser.name;
return {
name: browserName,
browser: true,
};
}

return false;
Expand Down
15 changes: 12 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ describe('ua-platformer', () => {
}),
)('%s', (platform, useragents) => {
it.each(useragents)('%s', useragent => {
expect(uaPlatformer(useragent)).to.equal(platform);
expect(uaPlatformer(useragent)).toStrictEqual({
name: platform,
browser: false,
});
});
});
});
Expand Down Expand Up @@ -112,7 +115,10 @@ describe('ua-platformer', () => {
}),
)('%s', (platform, useragents) => {
it.each(useragents)('%s', useragent => {
expect(uaPlatformer(useragent)).to.equal(platform);
expect(uaPlatformer(useragent)).toStrictEqual({
name: platform,
browser: false,
});
});
});
});
Expand Down Expand Up @@ -140,7 +146,10 @@ describe('ua-platformer', () => {
}),
)('%s', (platform, useragents) => {
it.each(useragents)('%s', useragent => {
expect(uaPlatformer(useragent)).to.equal(platform);
expect(uaPlatformer(useragent)).toStrictEqual({
name: platform,
browser: true,
});
});
});
});
Expand Down