25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.1 KiB

4 년 전
  1. var baseToNumber = require('./_baseToNumber'),
  2. baseToString = require('./_baseToString');
  3. /**
  4. * Creates a function that performs a mathematical operation on two values.
  5. *
  6. * @private
  7. * @param {Function} operator The function to perform the operation.
  8. * @param {number} [defaultValue] The value used for `undefined` arguments.
  9. * @returns {Function} Returns the new mathematical operation function.
  10. */
  11. function createMathOperation(operator, defaultValue) {
  12. return function(value, other) {
  13. var result;
  14. if (value === undefined && other === undefined) {
  15. return defaultValue;
  16. }
  17. if (value !== undefined) {
  18. result = value;
  19. }
  20. if (other !== undefined) {
  21. if (result === undefined) {
  22. return other;
  23. }
  24. if (typeof value == 'string' || typeof other == 'string') {
  25. value = baseToString(value);
  26. other = baseToString(other);
  27. } else {
  28. value = baseToNumber(value);
  29. other = baseToNumber(other);
  30. }
  31. result = operator(value, other);
  32. }
  33. return result;
  34. };
  35. }
  36. module.exports = createMathOperation;