to-camel-case.js 470 B

1234567891011121314151617181920
  1. import {Errorf} from '@e22m4u/js-format';
  2. /**
  3. * To camel case.
  4. *
  5. * @param {string} input
  6. * @returns {string}
  7. */
  8. export function toCamelCase(input) {
  9. if (typeof input !== 'string')
  10. throw new Errorf(
  11. 'The first argument of "toCamelCase" ' +
  12. 'should be a String, but %v was given.',
  13. input,
  14. );
  15. return input
  16. .replace(/(^\w|[A-Z]|\b\w)/g, c => c.toUpperCase())
  17. .replace(/\W+/g, '')
  18. .replace(/(^\w)/g, c => c.toLowerCase());
  19. }