當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript diff.diffChars函數代碼示例

本文整理匯總了TypeScript中diff.diffChars函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript diffChars函數的具體用法?TypeScript diffChars怎麽用?TypeScript diffChars使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了diffChars函數的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: encodeDiff

export function encodeDiff(orig: string, modified: string): string {
  let diff = "";
  if (orig !== modified) {
    const results = diffChars(modified, orig);
    const last = results[results.length - 1];
    for (let ix = 0; ix < results.length; ++ix) {
      const result = results[ix];
      if (result.added === true) {
        diff += `p${stringToCodeSequence(result.value)}`;
      }
      else if (result.removed ===  true) {
        const next = results[ix + 1];
        if ((next !== undefined && next.added === true) &&
            (result.value.toUpperCase() === next.value)) {
          diff += `u${result.value.length}`;
          ix++;
        }
        else {
          diff += `m${result.value.length}`;
        }
      }
      else {
        // We don't output this if it is last, as it is implied.
        if (result !== last) {
          diff += `g${result.value.length}`;
        }
      }
    }
  }

  return diff;
}
開發者ID:lddubeau,項目名稱:wed,代碼行數:32,代碼來源:util.ts

示例2: pad

	res.diffSet.forEach((entry) => {
		var state = {
			'equal' : '==',
			'left' : '->',
			'right' : '<-',
			'distinct' : '<>'
		}[entry.state];
		var name1 = entry.name1 ? entry.name1 : '';
		var name2 = entry.name2 ? entry.name2 : '';
		var space = '                                ';
		console.log('        - ' + pad(space, name1 + '(' + entry.type1.cyan + ') ', true)
				+ state + ' ' + pad(space, name2 + '(' + entry.type2.cyan + ')', true));

		if (entry.state === 'distinct') {
			var diff = jsdiff.diffChars(
				io.readFileSync(path.join(entry.path1, entry.name1)),
				io.readFileSync(path.join(entry.path2, entry.name2)));

			diff.forEach(function(part) {
				var color = part.added
					? 'green'
					: part.removed
						? 'red'
						: 'magenta';
				process.stderr.write(part.value[color]);
			});
		}
	});
開發者ID:AsherBarak,項目名稱:tsd,代碼行數:28,代碼來源:cases-spec.ts

示例3: diff

 protected diff(text: string, other: string, before: string = '', after: string = '') {
     const diff = jsdiff.diffChars(text, other);
     let out: string[] = [];
     diff.forEach(function(part){
         let color = part.added ? 'green' : part.removed ? 'red' : 'black';
         out.push(part.value[color]);
     });
     return before['black'] + out.join('') + after['black'];
 }
開發者ID:kerbyfc,項目名稱:cucumber-tsflow-suite,代碼行數:9,代碼來源:stepset.ts

示例4: getWrappingLines

		//print string diff with indent and wrapping
		public getWrappingLines(actual:string, expected:string, maxWidth:number, rowPadLength:number, padFirst:string[], leadSymbols:boolean = false):string {
			var changes:StringDiffChange[] = stringDiff.diffChars(expected, actual);

			var escape = unfunk.escape;
			var style = this.diff.style;
			var sep = '\n';

			// should not happen
			if (changes.length === 0) {
				return [
					padFirst[0],
					padFirst[1] + style.warning('<no diff data>'),
					padFirst[1]
				].join(sep);
			}

			// checked if string has no whitespace or other fancy characters
			// RegExp should also pick up empty strings
			var isSimple:boolean = (unfunk.identAnyExp.test(actual) && unfunk.identAnyExp.test(expected));
			var delim:string = (isSimple ? '' : '"');
			var delimEmpty = repeatStr(' ', delim.length);

			// accumulate the 3 rows
			var top = padFirst[0];
			var middle = padFirst[1];
			var bottom = padFirst[2];

			// accumulate styles center
			var buffer = '';

			// sometimes first set of rows has symbols
			if (leadSymbols) {
				top += style.error(this.diff.markRemov);
				middle += style.plain(this.diff.markEmpty);
				bottom += style.success(this.diff.markAdded);

				rowPadLength += this.diff.markAdded.length;
			}

			// sanity check if we don't break availible space
			var dataLength = maxWidth - rowPadLength;
			if (rowPadLength + delim.length * 2 >= maxWidth) {
				return '<no space for padded diff: "' + (rowPadLength + ' >= ' + maxWidth) + '">';
			}

			var rowPad = repeatStr(' ', rowPadLength);

			// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

			var blocks = [];
			var charSame = '|';
			var charAdded = '+';
			var charMissing = '-';

			var charCounter = 0;

			// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

			// append wrapping quotes
			function delimLine() {
				top += delimEmpty;
				middle += delim;
				bottom += delimEmpty;
			}

			delimLine();

			// concat accumulated lined and add to block buffer
			function flushLine() {
				// close open lines
				flushStyle();
				delimLine();
				blocks.push(top + sep + middle + sep + bottom);

				// prepare new
				top = rowPad;
				middle = rowPad;
				bottom = rowPad;
				charCounter = 0;
				delimLine();
			}

			// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

			//TODO swap style/row accumulators with some kind of statemachine (not worth it

			// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

			function appendAdd(value) {
				for (var i = 0; i < value.length; i++) {
					top += ' ';
					buffer += charAdded;
				}
				bottom += value;
			}

			function flushAdd() {
				if (buffer.length > 0) {
					middle += style.success(buffer);
//.........這裏部分代碼省略.........
開發者ID:Bartvds,項目名稱:unfunk-diff,代碼行數:101,代碼來源:string.ts

示例5: examineChanges

import * as diff from 'diff';

const one = 'beep boop';
const other = 'beep boob blah';

let changes = diff.diffChars(one, other);
examineChanges(changes);

// $ExpectType void
diff.diffChars(one, other, {
    callback: (err, value) => {
        err; // $ExpectType undefined
        value; // $ExpectType Change[] | undefined
    },
});
// $ExpectType void
diff.diffChars(one, other, (err, value) => {
    err; // $ExpectType undefined
    value; // $ExpectType Change[] | undefined
});

const diffArraysResult = diff.diffArrays(['a', 'b', 'c'], ['a', 'c', 'd']);
diffArraysResult.forEach(result => {
    result.added; // $ExpectType boolean | undefined
    result.removed; // $ExpectType boolean | undefined
    result.value; // $ExpectType string[]
    result.count; // $ExpectType number | undefined
});

interface DiffObj {
    value: number;
開發者ID:frankwallis,項目名稱:DefinitelyTyped,代碼行數:31,代碼來源:diff-tests.ts


注:本文中的diff.diffChars函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。