Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: CI

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: yarn install --frozen-lockfile
- run: yarn test
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "browser-lang",
"version": "0.1.0",
"version": "0.2.0",
"license": "MIT",
"description": "",
"repository": "github:wiziple/browser-lang",
Expand All @@ -17,7 +17,8 @@
"main": "./dist/index.js",
"scripts": {
"clean": "rimraf dist",
"test": "jest test --watch",
"test": "jest test",
"test:watch": "jest test --watch",
"build": "babel src --out-dir dist",
"format": "prettier --write {src,test}/**/*.{js,jsx}"
},
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function getPreferredLanguage(options) {

// en == en_US
const matchCodeOnly = languages.filter(lang =>
startsWith(browserLanguage, lang)
startsWith(browserLanguage, lang) || startsWith(lang, browserLanguage)
)
return matchCodeOnly[0] || fallback
}
Expand Down
20 changes: 20 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,24 @@ describe("browserLang", () => {
}
expect(browserLang(options)).toBe("zh")
})

it('should return "de-de" when navigator.languages is ["de"] and "de-de" is one item in the list.', () => {
mockNavigator({
languages: ["de"],
})
const options = {
languages: ["en-us", "de-de"],
}
expect(browserLang(options)).toBe("de-de")
})

it('should return "de-de" when navigator.languages is ["de"] and "de-de" is first item in the list.', () => {
mockNavigator({
languages: ["de"],
})
const options = {
languages: ["en-us", "de-de", "de-as"],
}
expect(browserLang(options)).toBe("de-de")
})
})