UNPKG

fix-excel-sheet-name

Version:

This package fixes MS-Excel sheet name by limiting it to 31 characters, empty sheet name, and removing illegal characters such as :\/?*[] and more.

2 lines 1.73 kB
'use strict';var _typeof=typeof Symbol==='function'&&typeof Symbol.iterator==='symbol'?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==='function'&&obj.constructor===Symbol?'symbol':typeof obj};var _require=require('lodash');var assign=_require.assign;var escapeRegExp=_require.escapeRegExp;var truncate=_require.truncate;var invalidChars='\n\t\r:\\/?*[]';var escapedInvalidChars=escapeRegExp(invalidChars);var regex=new RegExp('['+escapedInvalidChars+']','gm');var checkNotString=function checkNotString(value,paramName){if(typeof value!=='string'){throw new TypeError('Invalid '+paramName+' type. Expecting a string. Actual type: '+(typeof value==='undefined'?'undefined':_typeof(value)))}};var checkInvalidChars=function checkInvalidChars(str,paramName){if(regex.test(str)){throw new SyntaxError(paramName+' contains forbidden characters for excel sheet name. Invalid characters are: '+invalidChars+' Given '+paramName+': '+str)}};var fixSheetName=function fixSheetName(sheetName,options){options=assign({empty:'<empty>',replacement:' ',omission:'...'},options);checkNotString(sheetName,'sheetName');checkNotString(options.empty,'options.empty');checkNotString(options.replacement,'options.replacement');checkNotString(options.omission,'options.omission');checkInvalidChars(options.empty,'options.empty');checkInvalidChars(options.replacement,'options.replacement');checkInvalidChars(options.omission,'options.omission');if(!sheetName){return options.empty}sheetName=sheetName.replace(regex,options.replacement);sheetName=truncate(sheetName,{length:31,omission:options.omission});return sheetName};module.exports=fixSheetName;module.exports.invalidChars=invalidChars; //# sourceMappingURL=index.js.map