Skip to content

Commit

Permalink
Working
Browse files Browse the repository at this point in the history
  • Loading branch information
mvasilkov committed Jan 18, 2020
1 parent c12559a commit a665c41
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
38 changes: 38 additions & 0 deletions javascript/outdent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
// Newlines
const CR_LF = '\r\n';
const LF = '\n';
const ENDING = RegExp(`${CR_LF}|${LF}`);
// Indentation
const WS = '[ \t]';
const NOT_WS = '[^ \t]';
const PREFIX = RegExp(`^${WS}*(?=${NOT_WS})`);
function indentLevel(a) {
const b = a.match(PREFIX);
if (b)
return b[0].length;
return Infinity; // Empty lines don't contribute to the indentation level
}
function clean(a) {
const start = a[0] ? 0 : 1;
const end = a[a.length - 1] ? a.length : -1;
return a.slice(start, end);
}
function outdentLines(a, options) {
var _a;
if ((_a = options) === null || _a === void 0 ? void 0 : _a.strict) {
const level = Math.min(...a.map(indentLevel));
if (isFinite(level) && level != 0)
return a.map(b => b.slice(level));
return a;
}
if (a.length < 2)
return clean(a);
return clean([a[0], ...outdentLines(a.slice(1), Object.assign({}, options, { strict: true }))]);
}
exports.outdentLines = outdentLines;
function outdent(a, options) {
return outdentLines(a.split(ENDING), options).join('\n');
}
exports.outdent = outdent;
20 changes: 18 additions & 2 deletions typescript/outdent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,24 @@ function indentLevel(a: string): number {
return Infinity // Empty lines don't contribute to the indentation level
}

export function outdentLines(a: string[], options: IOptions) {
function clean(a: string[]): string[] {
const start = a[0] ? 0 : 1
const end = a[a.length - 1] ? a.length : -1
return a.slice(start, end)
}

export function outdent(a: string, options: IOptions) {
export function outdentLines(a: string[], options?: IOptions): string[] {
if (options?.strict) {
const level = Math.min(...a.map(indentLevel))
if (isFinite(level) && level != 0)
return a.map(b => b.slice(level))
return a
}
if (a.length < 2) return clean(a)
return clean([a[0], ...outdentLines(a.slice(1),
Object.assign({}, options, { strict: true }))])
}

export function outdent(a: string, options?: IOptions): string {
return outdentLines(a.split(ENDING), options).join('\n')
}
20 changes: 0 additions & 20 deletions unindent.js

This file was deleted.

0 comments on commit a665c41

Please sign in to comment.