Skip to content

Commit 1c233f1

Browse files
authored
Mention JSONC in docs & tests (#159)
* 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".
1 parent 64e4a2e commit 1c233f1

File tree

6 files changed

+37
-7
lines changed

6 files changed

+37
-7
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ i18next.use(i18nextHttpBackend).init(i18nextOptions);
114114
// path to post missing resources, or a function
115115
// function(lng, namespace) { return customPath; }
116116
// the returned path will interpolate lng, ns if provided like giving a static path
117-
//
117+
//
118118
// note that this only works when initialized with { saveMissing: true }
119119
// (see https://www.i18next.com/overview/configuration-options)
120120
addPath: '/locales/add/{{lng}}/{{ns}}',
121121

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

index.d.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ type FetchFunction = (input: string, init: RequestInit) => Promise<Response> | v
1414
export interface HttpBackendOptions {
1515
/**
1616
* Use an alternative fetch function that acts like an interecept, (usefull for low level mocks/simulations)
17-
*
17+
*
1818
* This option is not called if:
19-
*
19+
*
2020
* 1. There is an custom value set for the "request" property in this options object.
2121
* 2. The backend selected xmlHttpRequest over fetch
22-
*
22+
*
2323
* If the function is called and it returns anything BUT a promise the fetch or xmlHttpRequest will be subsequentially called
24-
*
24+
*
2525
*/
2626
alternateFetch?: FetchFunction;
2727
/**
@@ -38,7 +38,7 @@ export interface HttpBackendOptions {
3838
addPath?: AddPathOption;
3939
/**
4040
* parse data after it has been fetched
41-
* in example use https://www.npmjs.com/package/json5
41+
* in example use https://www.npmjs.com/package/json5 or https://www.npmjs.com/package/jsonc-parser
4242
* here it removes the letter a from the json (bad idea)
4343
*/
4444
parse?(

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"i18next": "23.16.6",
4949
"json-server": "0.17.4",
5050
"json5": "2.2.3",
51+
"jsonc-parser": "3.3.1",
5152
"mocha": "10.8.2",
5253
"tslint": "5.20.1",
5354
"tsd": "0.31.2",

test/deno/fixtures/server.js

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ const server = async () => {
1616
key: "passing" // keys can be without ""
1717
}`
1818
})
19+
app.get('/locales/en/testc', (c) => {
20+
return `{ // this is jsonc, comments is stripped
21+
"key": "passing"
22+
}`
23+
})
1924
app.post('/locales/missing/en/test', (c) => {
2025
assertNotEquals(c.request.body, {})
2126
return {}

test/fixtures/server.js

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ const server = (done) => {
4646
key: "passing" // keys can be without ""
4747
}`)
4848
})
49+
js.get('/locales/en/testc', (req, res) => {
50+
res.send(`{ // this is jsonc, comments is stripped
51+
"key": "passing"
52+
}`)
53+
})
4954
js.post('/locales/missing/en/test', (req, res) => {
5055
expect(req.body).not.to.eql({})
5156
res.jsonp()

test/http.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import expect from 'expect.js'
22
import Http from '../index.js'
33
import i18next from 'i18next'
44
import JSON5 from 'json5'
5+
import { parse as parseJSONC } from 'jsonc-parser'
56
import server from './fixtures/server.js'
67
import { hasXMLHttpRequest } from '../lib/utils.js'
78

@@ -128,6 +129,24 @@ describe(`http backend using ${hasXMLHttpRequest() ? 'XMLHttpRequest' : 'fetch'}
128129
done()
129130
})
130131
})
132+
133+
it('should load jsonc data', (done) => {
134+
backend = new Http(
135+
{
136+
interpolator: i18next.services.interpolator
137+
},
138+
{
139+
loadPath: 'http://localhost:5001/locales/{{lng}}/{{ns}}',
140+
parse: parseJSONC
141+
}
142+
)
143+
backend.read('en', 'testc', function (err, data) {
144+
expect(err).not.to.be.ok()
145+
expect(data).to.eql({ key: 'passing' })
146+
done()
147+
})
148+
})
149+
131150
it('should load custom parser data', (done) => {
132151
backend = new Http(
133152
{

0 commit comments

Comments
 (0)