Skip to content

Commit

Permalink
lib: add navigator.language & navigator.languages
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Oct 29, 2023
1 parent 14af167 commit 5bdd487
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
35 changes: 35 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,41 @@ logical processors available to the current Node.js instance.
console.log(`This process is running on ${navigator.hardwareConcurrency} logical processors`);
```


### `navigator.language`

<!-- YAML
added: REPLACEME
-->

* {string}

The `navigator.language` read-only property returns a string representing the
preferred language of the Node.js instance.

The value is representing the language version as defined in RFC <5646>.
The default value is `'en-US'`.

```js
console.log(`The preferred language of the Node.js instance has the tag '${navigator.language}'`);
```

### `navigator.languages`

<!-- YAML
added: REPLACEME
-->

* {Array<string>}

The `navigator.languages` read-only property returns an array of strings
representing the preferred languages of the Node.js instance.
The default value is `['en-US']`.

```js
console.log(`The preferred languages are '${navigator.language}'`);
```

### `navigator.userAgent`

<!-- YAML
Expand Down
15 changes: 15 additions & 0 deletions lib/internal/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
ObjectDefineProperties,
ObjectFreeze,
Symbol,
} = primordials;

Expand Down Expand Up @@ -40,6 +41,20 @@ class Navigator {
return this.#availableParallelism;
}

/**
* @return {string}
*/
get language() {
return 'en-US';
}

/**
* @return {Array<string>}
*/
get languages() {
return ObjectFreeze([this.language]);
}

/**
* @return {string}
*/
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,19 @@ is.number(navigator.hardwareConcurrency, 'hardwareConcurrency');
assert.ok(navigator.hardwareConcurrency > 0);
assert.strictEqual(typeof navigator.userAgent, 'string');
assert.match(navigator.userAgent, /^Node\.js\/\d+$/);

assert.strictEqual(typeof navigator.language, 'string');
assert.strictEqual(navigator.language, 'en-US');

assert.ok(Array.isArray(navigator.languages));
assert.strictEqual(navigator.languages.length, 1);
assert.strictEqual(typeof navigator.languages[0], 'string');

assert.throws(() => {navigator.languages[0] = 'foo'}, new TypeError("Cannot assign to read only property '0' of object '[object Array]'"));

Check failure on line 26 in test/parallel/test-navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

This line has a length of 139. Maximum allowed is 120

Check failure on line 26 in test/parallel/test-navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Requires a space after '{'

Check failure on line 26 in test/parallel/test-navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Requires a space before '}'

Check failure on line 26 in test/parallel/test-navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
assert.notStrictEqual(navigator.languages[0], 'foo');
assert.strictEqual(navigator.languages[0], 'en-US');

Object.defineProperty(navigator, 'language', {value: 'de-DE'});

Check failure on line 30 in test/parallel/test-navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

A space is required after '{'

Check failure on line 30 in test/parallel/test-navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

A space is required before '}'
assert.strictEqual(navigator.language, 'de-DE');
assert.strictEqual(navigator.languages.length, 1);
assert.strictEqual(navigator.languages[0], 'de-DE');

0 comments on commit 5bdd487

Please sign in to comment.