Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wrap.js 717B

12345678910111213141516
  1. 'use strict';
  2. /**
  3. * @param {string} msg The message to wrap
  4. * @param {object} opts
  5. * @param {number|string} [opts.margin] Left margin
  6. * @param {number} opts.width Maximum characters per line including the margin
  7. */
  8. module.exports = (msg, opts = {}) => {
  9. const tab = Number.isSafeInteger(parseInt(opts.margin)) ? new Array(parseInt(opts.margin)).fill(' ').join('') : opts.margin || '';
  10. const width = opts.width;
  11. return (msg || '').split(/\r?\n/g).map(line => line.split(/\s+/g).reduce((arr, w) => {
  12. if (w.length + tab.length >= width || arr[arr.length - 1].length + w.length + 1 < width) arr[arr.length - 1] += ` ${w}`;else arr.push(`${tab}${w}`);
  13. return arr;
  14. }, [tab]).join('\n')).join('\n');
  15. };