From 1c233f192d9e578ad84cf82bc153e1da93d6e593 Mon Sep 17 00:00:00 2001 From: Sam Bostock Date: Thu, 21 Nov 2024 03:10:19 -0500 Subject: [PATCH] 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". --- README.md | 4 ++-- index.d.ts | 10 +++++----- package.json | 1 + test/deno/fixtures/server.js | 5 +++++ test/fixtures/server.js | 5 +++++ test/http.spec.js | 19 +++++++++++++++++++ 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6fca320..f0eabb7 100644 --- a/README.md +++ b/README.md @@ -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, ''); }, diff --git a/index.d.ts b/index.d.ts index ac5620c..e4ba89f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -14,14 +14,14 @@ type FetchFunction = (input: string, init: RequestInit) => Promise | 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; /** @@ -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?( diff --git a/package.json b/package.json index 1c7d0e3..48920b7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/deno/fixtures/server.js b/test/deno/fixtures/server.js index 24eca39..5469e06 100644 --- a/test/deno/fixtures/server.js +++ b/test/deno/fixtures/server.js @@ -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 {} diff --git a/test/fixtures/server.js b/test/fixtures/server.js index 3da0c6e..25752da 100644 --- a/test/fixtures/server.js +++ b/test/fixtures/server.js @@ -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() diff --git a/test/http.spec.js b/test/http.spec.js index b77be7a..1e68a5c 100644 --- a/test/http.spec.js +++ b/test/http.spec.js @@ -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' @@ -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( {