What is platform?
The 'platform' npm package is a platform detection library that can be used to identify the operating system, browser, and engine of the platform on which the current script is running. It can be used in both browser and Node.js environments.
What are platform's main functionalities?
Operating System Detection
This feature allows you to detect the operating system of the user's device. The code sample demonstrates how to import the platform module and log the operating system information to the console.
const platform = require('platform');
console.log(platform.os);
Browser Detection
This feature enables you to detect the browser name and version. The code sample shows how to log the browser name and version to the console.
const platform = require('platform');
console.log(platform.name + ' ' + platform.version);
Engine Detection
This feature is used to detect the layout engine of the browser. The code sample illustrates how to log the layout engine information to the console.
const platform = require('platform');
console.log(platform.layout);
Device Detection
This feature allows you to detect the specific device (e.g., 'iPad', 'iPhone'). The code sample demonstrates how to log the device information to the console.
const platform = require('platform');
console.log(platform.product);
User-Agent Parsing
This feature provides a method to parse a user-agent string and extract detailed information about the platform. The code sample shows how to parse a user-agent string and log the resulting object.
const platform = require('platform');
const info = platform.parse('Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
console.log(info);
Other packages similar to platform
ua-parser-js
ua-parser-js is a user-agent string parser that can detect browser, engine, OS, CPU, and device type/model from the user-agent string. It offers similar functionality to 'platform' but with a focus on user-agent string parsing and additional details like CPU architecture.
detect-browser
detect-browser is a simple package for detecting a browser. It is smaller and more lightweight than 'platform' but does not provide as much detailed information about the operating system or device.
bowser
bowser is a browser detection library that is more feature-rich than 'platform', providing detailed information about the browser, engine, platform, OS, and whether certain features are supported in the browser.
Platform.js v1.3.1
A platform detection library that works on nearly all JavaScript platforms.
Disclaimer
Platform.js is for informational purposes only & not intended as a substitution for feature detection/inference checks.
Documentation
Installation
In a browser:
<script src="platform.js"></script>
In an AMD loader:
require(['platform'], function(platform) {});
Using npm:
$ npm i --save platform
In Node.js:
var platform = require('platform');
Usage example:
platform.name;
platform.version;
platform.layout;
platform.os;
platform.description;
platform.name;
platform.version;
platform.product;
platform.manufacturer;
platform.layout;
platform.os;
platform.description;
var info = platform.parse('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7.2; en; rv:2.0) Gecko/20100101 Firefox/4.0 Opera 11.52');
info.name;
info.version;
info.layout;
info.os;
info.description;
Support
Tested in Chrome 46-47, Firefox 42-43, IE 8-11, MS Edge 13, Safari 8-9, Node.js 0.8.28, 0.10.41, 0.12.9, 4.2.4, & 5.3.0, & PhantomJS 1.9.8.
BestieJS
Platform.js is part of the BestieJS “Best in Class” module collection. This means we promote solid browser/environment support, ES5+ precedents, unit testing, & plenty of documentation.