diff --git a/lib/svgo-node.js b/lib/svgo-node.js index 1614fc001..a80991f92 100644 --- a/lib/svgo-node.js +++ b/lib/svgo-node.js @@ -54,6 +54,9 @@ const loadConfig = async (configFile, cwd = process.cwd()) => { exports.loadConfig = loadConfig; const optimize = (input, config) => { + if (config == null) { + config = {}; + } if (typeof config !== 'object') { throw Error('Config should be an object'); } @@ -62,7 +65,7 @@ const optimize = (input, config) => { js2svg: { // platform specific default for end of line eol: os.EOL === '\r\n' ? 'crlf' : 'lf', - ...(config == null ? null : config.js2svg), + ...config.js2svg, }, }); }; diff --git a/lib/svgo-node.test.js b/lib/svgo-node.test.js new file mode 100644 index 000000000..c80cd33b3 --- /dev/null +++ b/lib/svgo-node.test.js @@ -0,0 +1,125 @@ +'use strict'; + +/** + * @typedef {import('../lib/types').Plugin} Plugin + */ + +const os = require('os'); +const { optimize } = require('./svgo-node.js'); + +const describeLF = os.EOL === '\r\n' ? describe.skip : describe; +const describeCRLF = os.EOL === '\r\n' ? describe : describe.skip; + +describeLF('with LF line-endings', () => { + test('should work', () => { + const svg = ` + + + + Not standard description + + + + `; + const { data } = optimize(svg); + // using toEqual because line endings matter in these tests + expect(data).toEqual( + '' + ); + }); + + test('should respect config', () => { + const svg = ` + + + + Not standard description + + + + `; + const { data } = optimize(svg, { + js2svg: { pretty: true, indent: 2 }, + }); + // using toEqual because line endings matter in these tests + expect(data).toEqual( + '\n \n\n' + ); + }); + + test('should respect line-ending config', () => { + const svg = ` + + + + Not standard description + + + + `; + const { data } = optimize(svg, { + js2svg: { eol: 'crlf', pretty: true, indent: 2 }, + }); + // using toEqual because line endings matter in these tests + expect(data).toEqual( + '\r\n \r\n\r\n' + ); + }); +}); + +describeCRLF('with CRLF line-endings', () => { + test('should work', () => { + const svg = ` + + + + Not standard description + + + + `; + const { data } = optimize(svg); + // using toEqual because line endings matter in these tests + expect(data).toEqual( + '' + ); + }); + + test('should respect config', () => { + const svg = ` + + + + Not standard description + + + + `; + const { data } = optimize(svg, { + js2svg: { pretty: true, indent: 2 }, + }); + // using toEqual because line endings matter in these tests + expect(data).toEqual( + '\r\n \r\n\r\n' + ); + }); + + test('should respect line-ending config', () => { + const svg = ` + + + + Not standard description + + + + `; + const { data } = optimize(svg, { + js2svg: { eol: 'lf', pretty: true, indent: 2 }, + }); + // using toEqual because line endings matter in these tests + expect(data).toEqual( + '\n \n\n' + ); + }); +});