Skip to content

Commit

Permalink
Mention JSONC in docs & tests (#159)
Browse files Browse the repository at this point in the history
* Fix whitespace

* Mention JSONC in docs & tests

While we don't handle JSON5 out of the box, it is mentioned in the docs
and tested against, which provides discoverability for consumers wanting
to use it.

Doing the same for JSONC (JSON with Comments) similarly helps consumers
by ensuring there are results when searching the repo for "jsonc".
  • Loading branch information
sambostock authored Nov 21, 2024
1 parent 64e4a2e commit 1c233f1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ i18next.use(i18nextHttpBackend).init(i18nextOptions);
// path to post missing resources, or a function
// function(lng, namespace) { return customPath; }
// the returned path will interpolate lng, ns if provided like giving a static path
//
//
// note that this only works when initialized with { saveMissing: true }
// (see https://www.i18next.com/overview/configuration-options)
addPath: '/locales/add/{{lng}}/{{ns}}',

// parse data after it has been fetched
// in example use https://www.npmjs.com/package/json5
// in example use https://www.npmjs.com/package/json5 or https://www.npmjs.com/package/jsonc-parser
// here it removes the letter a from the json (bad idea)
parse: function(data) { return data.replace(/a/g, ''); },

Expand Down
10 changes: 5 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ type FetchFunction = (input: string, init: RequestInit) => Promise<Response> | v
export interface HttpBackendOptions {
/**
* Use an alternative fetch function that acts like an interecept, (usefull for low level mocks/simulations)
*
*
* This option is not called if:
*
*
* 1. There is an custom value set for the "request" property in this options object.
* 2. The backend selected xmlHttpRequest over fetch
*
*
* If the function is called and it returns anything BUT a promise the fetch or xmlHttpRequest will be subsequentially called
*
*
*/
alternateFetch?: FetchFunction;
/**
Expand All @@ -38,7 +38,7 @@ export interface HttpBackendOptions {
addPath?: AddPathOption;
/**
* parse data after it has been fetched
* in example use https://www.npmjs.com/package/json5
* in example use https://www.npmjs.com/package/json5 or https://www.npmjs.com/package/jsonc-parser
* here it removes the letter a from the json (bad idea)
*/
parse?(
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"i18next": "23.16.6",
"json-server": "0.17.4",
"json5": "2.2.3",
"jsonc-parser": "3.3.1",
"mocha": "10.8.2",
"tslint": "5.20.1",
"tsd": "0.31.2",
Expand Down
5 changes: 5 additions & 0 deletions test/deno/fixtures/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const server = async () => {
key: "passing" // keys can be without ""
}`
})
app.get('/locales/en/testc', (c) => {
return `{ // this is jsonc, comments is stripped
"key": "passing"
}`
})
app.post('/locales/missing/en/test', (c) => {
assertNotEquals(c.request.body, {})
return {}
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ const server = (done) => {
key: "passing" // keys can be without ""
}`)
})
js.get('/locales/en/testc', (req, res) => {
res.send(`{ // this is jsonc, comments is stripped
"key": "passing"
}`)
})
js.post('/locales/missing/en/test', (req, res) => {
expect(req.body).not.to.eql({})
res.jsonp()
Expand Down
19 changes: 19 additions & 0 deletions test/http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import expect from 'expect.js'
import Http from '../index.js'
import i18next from 'i18next'
import JSON5 from 'json5'
import { parse as parseJSONC } from 'jsonc-parser'
import server from './fixtures/server.js'
import { hasXMLHttpRequest } from '../lib/utils.js'

Expand Down Expand Up @@ -128,6 +129,24 @@ describe(`http backend using ${hasXMLHttpRequest() ? 'XMLHttpRequest' : 'fetch'}
done()
})
})

it('should load jsonc data', (done) => {
backend = new Http(
{
interpolator: i18next.services.interpolator
},
{
loadPath: 'http://localhost:5001/locales/{{lng}}/{{ns}}',
parse: parseJSONC
}
)
backend.read('en', 'testc', function (err, data) {
expect(err).not.to.be.ok()
expect(data).to.eql({ key: 'passing' })
done()
})
})

it('should load custom parser data', (done) => {
backend = new Http(
{
Expand Down

0 comments on commit 1c233f1

Please sign in to comment.