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.

31 lines
817 B

4 years ago
  1. import {isFunction} from './checks'
  2. // does simply nothing
  3. export function noop() {
  4. return this
  5. }
  6. /**
  7. * Autobind the methods of a source object to itself
  8. * @param {Object} source - probably a riot tag instance
  9. * @param {Array<string>} methods - list of the methods to autobind
  10. * @returns {Object} the original object received
  11. */
  12. export function autobindMethods(source, methods) {
  13. methods.forEach(method => {
  14. source[method] = source[method].bind(source)
  15. })
  16. return source
  17. }
  18. /**
  19. * Call the first argument received only if it's a function otherwise return it as it is
  20. * @param {*} source - anything
  21. * @returns {*} anything
  22. */
  23. export function callOrAssign(source) {
  24. return isFunction(source) ? (source.prototype && source.prototype.constructor ?
  25. new source() : source()
  26. ) : source
  27. }