How to use string-length - 7 common examples

To help you get started, we’ve selected a few string-length examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vadimdemedes / ink / src / output.js View on Github external
write(x, y, text, {transformers}) {
		if (!text) {
			return;
		}

		const lines = text.split('\n');
		let offsetY = 0;

		for (let line of lines) {
			const length = stringLength(line);
			const currentLine = this.output[y + offsetY];

			// Line can be missing if `text` is taller than height of pre-initialized `this.output`
			if (!currentLine) {
				continue;
			}

			for (const transformer of transformers) {
				line = transformer(line);
			}

			this.output[y + offsetY] = sliceAnsi(currentLine, 0, x) + line + sliceAnsi(currentLine, x + length);

			offsetY++;
		}
	}
github vadimdemedes / ink / src / experimental / output.js View on Github external
}

		for (const write of this.writes) {
			const {x, y, text, transformers} = write;
			const lines = text.split('\n');
			let offsetY = 0;

			for (let line of lines) {
				const currentLine = output[y + offsetY];

				// Line can be missing if `text` is taller than height of pre-initialized `this.output`
				if (!currentLine) {
					continue;
				}

				const length = stringLength(line);

				for (const transformer of transformers) {
					line = transformer(line);
				}

				output[y + offsetY] = sliceAnsi(currentLine, 0, x) + line + sliceAnsi(currentLine, x + length);

				offsetY++;
			}
		}

		const generatedOutput = output
			.map(line => line.trimRight())
			.join('\n');

		return {
github danfuzz / bayou / local-modules / @bayou / see-all-server / HumanSink.js View on Github external
// Color the prefix depending on the event name / severity level.
    switch (logRecord.payload.name) {
      case 'error': { text = ck.red.bold(text);    break; }
      case 'warn':  { text = ck.yellow.bold(text); break; }
      default:      { text = ck.dim.bold(text);    break; }
    }

    // If there's context, color it and append it.
    if (logRecord.contextString !== null) {
      text += ` ${ck.hex('#44e').bold(logRecord.contextString)}`;
    }

    // **Note:** `stringLength()` takes into account the fact that ANSI color
    // escapes don't add to the visual-length of a string. `+1` to guarantee at
    // least one space of padding.
    const length = stringLength(text) + 1;

    // Update the prefix length instance variables. What we're doing here is
    // adjusting the prefix area to be wider when we discover a prefix which
    // would be longer than what we've seen before. At the same time, we record
    // a recently-observed maximum, and we reset to that from time to time. The
    // latter prevents brief "prefix blow-outs" from permanently messing with
    // the log output.
    const MIN    = MIN_PREFIX_LENGTH;
    const ADJUST = PREFIX_ADJUST_INCREMENT;
    const prefixLength = (length <= MIN)
      ? MIN
      : Math.ceil((length - MIN) / ADJUST) * ADJUST + MIN;
    this._prefixLength    = Math.max(this._prefixLength,    prefixLength);
    this._recentMaxPrefix = Math.max(this._recentMaxPrefix, prefixLength);
    this._recentLineCount++;
    if (this._recentLineCount >= 100) {
github jest-community / jest-watch-typeahead / src / file_name_plugin / prompt.js View on Github external
_printTypeahead(pattern: string, options: ScrollOptions) {
    const matchedTests = this._getMatchedTests(pattern);
    const total = matchedTests.length;
    const pipe = this._pipe;
    const prompt = this._prompt;

    printPatternCaret(pattern, pipe);

    pipe.write(ansiEscapes.cursorLeft);

    if (pattern) {
      printPatternMatches(total, 'file', pipe);

      const prefix = `  ${chalk.dim('\u203A')} `;
      const padding = stringLength(prefix) + 2;
      const width = getTerminalWidth(pipe);
      const { start, end, index } = scroll(total, options);

      prompt.setPromptLength(total);

      matchedTests
        .slice(start, end)
        .map(({ path, context }) => {
          const filePath = trimAndFormatPath(
            padding,
            context.config,
            path,
            width,
          );
          return highlight(path, filePath, pattern, context.config.rootDir);
        })
github pnpm / pnpm / packages / default-reporter / src / reporterForClient / reportStats.ts View on Github external
function padStep (s: string, step: number) {
  const sLength = stringLength(s)
  const placeholderLength = Math.ceil(sLength / step) * step
  if (sLength < placeholderLength) {
    return R.repeat(' ', placeholderLength - sLength).join('') + s
  }
  return s
}
github danfuzz / bayou / local-modules / @bayou / see-all-server / HumanSink.js View on Github external
      (prev, l) => { return Math.max(prev, stringLength(l)); },
      0);

string-length

Get the real length of a string - by correctly counting astral symbols and ignoring ansi escape codes

MIT
Latest version published 11 months ago

Package Health Score

79 / 100
Full package analysis

Popular string-length functions