How to use string-width - 9 common examples

To help you get started, we’ve selected a few string-width 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 gajus / table / src / wrapWord.js View on Github external
subject = input;

  const chunks = [];

  // https://regex101.com/r/gY5kZ1/1
  const re = new RegExp('(^.{1,' + size + '}(\\s+|$))|(^.{1,' + (size - 1) + '}(\\\\|/|_|\\.|,|;|-))');

  do {
    let chunk;

    chunk = subject.match(re);

    if (chunk) {
      chunk = chunk[0];

      subject = slice(subject, stringWidth(chunk));

      chunk = chunk.trim();
    } else {
      chunk = slice(subject, 0, size);
      subject = slice(subject, size);
    }

    chunks.push(chunk);
  } while (stringWidth(subject));

  return chunks;
};
github d2-projects / folder-explorer / src / store.js View on Github external
function bridgeAuto ({ element, note }, max) {
        if (note !== '' || setting.BRIDGE_ALWAYS) {
          let length = setting.BRIDGE_MIN
          if (setting.FLOAT_RIGHT) length += (max - width(`${element}${note}`))
          else length += (max - width(element))
          return setting.BRIDGE_CELL.repeat(length)
        }
        return ''
      }
      // 第一步 转换 element 和 note
github sishuguojixuefu / react-native-form / src / components / SsAmount.tsx View on Github external
render() {
    const { placeholder, label, required, form, id, textAlign, upper } = this.props
    const omitProps = omit(this.props, ['error', 'labelNumber'])
    return (
      
        {this.fieldDecorator(
          
            <label label="{label}" required="{required}">
          </label>
        )}
        {upper &amp;&amp; (
          
        )}
      
    )
  }
github gajus / table / src / wrapString.js View on Github external
export default (subject, size) => {
  let subjectSlice;

  subjectSlice = subject;

  const chunks = [];

  do {
    chunks.push(slice(subjectSlice, 0, size));

    subjectSlice = slice(subjectSlice, size).trim();
  } while (stringWidth(subjectSlice));

  return chunks;
};
github egoist / lamu / src / index.js View on Github external
const maxLabelWidth = this.messages.reduce((current, next) => {
      if (getWidth(next[0]) > current) return getWidth(next[0])
      return current
    }, 0)
    const data = this.messages.map(message => {
github d2-projects / folder-explorer / src / store.js View on Github external
return result.reduce((max, { element }) => {
            const length = width(element)
            return length > max ? length : max
          }, 0)
        }
github prisma / prisma2 / cli / ink-components / src / components / drawBox.tsx View on Github external
.map(l => {
      const lineWidth = Math.min(stringWidth(l || ''), width)
      const paddingRight = Math.max(width - lineWidth - 2, 0)
      return `${chalk.grey(chars.vertical)}${chalk.reset(cliTruncate(l, width - 2))}${' '.repeat(
        paddingRight,
      )}${chalk.grey(chars.vertical)}`
    })
    .join('\n')
github tommy351 / kosko / packages / cli / src / cli / help.ts View on Github external
private async printEntries(header: string, entries: Entry[]) {
    if (!entries.length) return;

    await this.printHeader(header);

    const keySize = Math.max(...entries.map(e =&gt; stringWidth(e.key)));

    entries.sort((a, b) =&gt; {
      if (a.key &gt; b.key) return 1;
      if (a.key &lt; b.key) return -1;
      return 0;
    });

    for (const entry of entries) {
      const pad = Array(keySize - stringWidth(entry.key))
        .fill(" ")
        .join("");

      await this.write(`  ${entry.key}${pad}  ${entry.description}\n`);
    }
  }
}
github d2-projects / folder-explorer / src / store.js View on Github external
          const elementLengthMax = result.reduce((max, { element }) => width(element) > max ? width(element) : max, 0)
          const noteLengthMax = result.reduce((max, { note }) => width(note) > max ? width(note) : max, 0)

string-width

Get the visual width of a string - the number of columns required to display it

MIT
Latest version published 5 months ago

Package Health Score

78 / 100
Full package analysis

Popular string-width functions