UNPKG

feeles-ide

Version:

The hackable and serializable IDE to make learning material

36 lines (27 loc) 1.28 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = preserveTrailingSpaceBeautify; var _jsBeautify = _interopRequireDefault(require("js-beautify")); /** * * @param {String} source * @param {Object} options * @param {String} uniqueLineEnd スペースだけの行に付け加えるユニークな文字列. これがソースに含まれているといけない */ function preserveTrailingSpaceBeautify(source, options) { var uniqueLineEnd = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '///DtpOmVZptu'; if (source.includes(uniqueLineEnd + '\n')) { return preserveTrailingSpaceBeautify(source, options, uniqueLineEnd + 'MrhbRewZAi'); // ユニークになるまで... } source = source.replace(/^([ \t]+)$/gm, '$1' + uniqueLineEnd); // 行をトリムされないようにする if (source.endsWith(uniqueLineEnd)) { // ファイル末尾のインデントは trim する source = source.substr(0, source.length - uniqueLineEnd.length); } source = (0, _jsBeautify.default)(source, options); source = source.split(uniqueLineEnd + '\n').join('\n'); // 元に戻す return source; }