From 37ed65b5897e12410a9f60803e020b9ec4c8ceac Mon Sep 17 00:00:00 2001 From: mshanemc Date: Wed, 25 Oct 2023 11:34:15 -0500 Subject: [PATCH] chore: remove unused unexported code --- src/util/jsonXmlTools.ts | 55 ------------------ test/unit/util/jsonXmlToolsTest.ts | 90 ------------------------------ 2 files changed, 145 deletions(-) delete mode 100644 src/util/jsonXmlTools.ts delete mode 100644 test/unit/util/jsonXmlToolsTest.ts diff --git a/src/util/jsonXmlTools.ts b/src/util/jsonXmlTools.ts deleted file mode 100644 index 52d02ec402..0000000000 --- a/src/util/jsonXmlTools.ts +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2021, salesforce.com, inc. - * All rights reserved. - * Licensed under the BSD 3-Clause license. - * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause - */ - -import { promises as fs } from 'fs'; -import * as jsToXml from 'js2xmlparser'; -import { JsonMap } from '@salesforce/ts-types'; -import { IOptions } from 'js2xmlparser/lib/options'; - -export interface JSONasXML { - json: JsonMap; - type: string; - options?: IOptions; -} - -export interface WriteJSONasXMLInputs extends JSONasXML { - path: string; -} - -export const standardOptions: IOptions = { - declaration: { - include: true, - encoding: 'UTF-8', - version: '1.0', - }, - format: { - doubleQuotes: true, - }, -}; - -export const writeJSONasXML = async ({ - path, - json, - type, - options = standardOptions, -}: WriteJSONasXMLInputs): Promise => { - const xml = jsToXml.parse(type, fixExistingDollarSign(json), options); - return fs.writeFile(path, xml); -}; - -export const JsonAsXml = ({ json, type, options = standardOptions }: JSONasXML): string => - jsToXml.parse(type, fixExistingDollarSign(json), options); - -export const fixExistingDollarSign = (existing: WriteJSONasXMLInputs['json']): Record => { - const existingCopy = { ...existing } as Record; - if (existingCopy.$) { - const temp = existingCopy.$; - delete existingCopy.$; - existingCopy['@'] = temp; - } - return existingCopy; -}; diff --git a/test/unit/util/jsonXmlToolsTest.ts b/test/unit/util/jsonXmlToolsTest.ts deleted file mode 100644 index d30bbae6ba..0000000000 --- a/test/unit/util/jsonXmlToolsTest.ts +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (c) 2021, salesforce.com, inc. - * All rights reserved. - * Licensed under the BSD 3-Clause license. - * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause - */ - -import { promises as fs } from 'fs'; -import { expect } from 'chai'; -import { SinonStub } from 'sinon'; -import { stubMethod } from '@salesforce/ts-sinon'; -import { shouldThrow, TestContext } from '../../../src/testSetup'; -import { writeJSONasXML, fixExistingDollarSign } from '../../../src/util/jsonXmlTools'; - -const XML_PATH = '/tmp/myXml.xml'; -const TEST_JSON = { - name: 'Anna', - address: 'Ocean Drive 101', - phone: 123456789, - alias: ['Anita', 'Anny'], - cars: { - primary: { - brand: 'Honda', - model: 'Civic', - year: '2016', - }, - }, -}; -const TEST_XML = - '\n\n Anna\n
Ocean Drive 101
\n 123456789\n Anita\n Anny\n \n \n Honda\n Civic\n 2016\n \n \n
'; -const DOLLARSIGN_OBJECT = { - $: 'value', -}; - -describe('jsonXmlTools', () => { - const $$ = new TestContext(); - - let fsWriteFileStub: SinonStub; - beforeEach(() => { - fsWriteFileStub = stubMethod($$.SANDBOX, fs, 'writeFile').callsFake((fsPath, xml) => { - expect(fsPath).to.be.a('string').and.to.have.length.greaterThan(0).and.to.be.equal(XML_PATH); - expect(xml).to.be.a('string').and.to.have.length.greaterThan(0); - return Promise.resolve(); - }); - }); - - afterEach(() => { - $$.SANDBOX.restore(); - }); - - it('writes json as xml', async () => { - const result = await writeJSONasXML({ - path: XML_PATH, - type: 'RecordType', - json: TEST_JSON, - }); - expect(fsWriteFileStub.callCount).to.be.equal(1); - expect(fsWriteFileStub.firstCall.args[1]).to.equal(TEST_XML); - // undefined means write operation succeeded https://nodejs.org/api/fs.html#fs_fspromises_writefile_file_data_options - expect(result).to.be.undefined; - }); - - it('fails to write json as xml but fails', async () => { - fsWriteFileStub.restore(); - fsWriteFileStub = stubMethod($$.SANDBOX, fs, 'writeFile').rejects(); - try { - // create() calls read() which calls schemaValidate() - await shouldThrow( - writeJSONasXML({ - path: XML_PATH, - type: 'RecordType', - json: TEST_JSON, - }) - ); - } catch (e) { - const error = e as Error; - expect(error.name).to.equal('Error'); - expect(error.message).to.equal('Error'); - } - }); -}); - -describe('fixExistingDollarSign', () => { - it('fixes existing dollar sing key in object', () => { - const result = fixExistingDollarSign(DOLLARSIGN_OBJECT); - expect(result).to.deep.equal({ - '@': 'value', - }); - }); -});