before

  1. function before(n, func) {
  2. let result
  3. if (typeof func != 'function') {
  4. throw new TypeError('Expected a function')
  5. }
  6. return function(...args) {
  7. if (--n > 0) {
  8. result = func.apply(this, args)
  9. }
  10. if (n <= 1) {
  11. func = undefined
  12. }
  13. return result
  14. }
  15. }
  16. export default before

once

  1. import before from './before.js'
  2. function once(func) {
  3. return before(2, func)
  4. }
  5. export default once

———————————-

getTag

  1. const toString = Object.prototype.toString
  2. /**
  3. * Gets the `toStringTag` of `value`.
  4. *
  5. * @private
  6. * @param {*} value The value to query.
  7. * @returns {string} Returns the `toStringTag`.
  8. */
  9. function getTag(value) {
  10. if (value == null) {
  11. return value === undefined ? '[object Undefined]' : '[object Null]'
  12. }
  13. return toString.call(value)
  14. }
  15. export default getTag

isSymbol

  1. import getTag from './.internal/getTag.js'
  2. /**
  3. * Checks if `value` is classified as a `Symbol` primitive or object.
  4. *
  5. * @since 4.0.0
  6. * @category Lang
  7. * @param {*} value The value to check.
  8. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
  9. * @example
  10. *
  11. * isSymbol(Symbol.iterator)
  12. * // => true
  13. *
  14. * isSymbol('abc')
  15. * // => false
  16. */
  17. function isSymbol(value) {
  18. const type = typeof value
  19. return type == 'symbol' || (type == 'object' && value != null && getTag(value) == '[object Symbol]')
  20. }
  21. export default isSymbol

baseToString

  1. import isSymbol from '../isSymbol.js'
  2. /** Used as references for various `Number` constants. */
  3. const INFINITY = 1 / 0
  4. /** Used to convert symbols to primitives and strings. */
  5. const symbolToString = Symbol.prototype.toString
  6. /**
  7. * The base implementation of `toString` which doesn't convert nullish
  8. * values to empty strings.
  9. *
  10. * @private
  11. * @param {*} value The value to process.
  12. * @returns {string} Returns the string.
  13. */
  14. function baseToString(value) {
  15. // Exit early for strings to avoid a performance hit in some environments.
  16. if (typeof value == 'string') {
  17. return value
  18. }
  19. if (Array.isArray(value)) {
  20. // Recursively convert values (susceptible to call stack limits).
  21. return `${value.map(baseToString)}`
  22. }
  23. if (isSymbol(value)) {
  24. return symbolToString ? symbolToString.call(value) : ''
  25. }
  26. const result = `${value}`
  27. return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result
  28. }
  29. export default baseToString

baseToNumber

  1. import isSymbol from '../isSymbol.js'
  2. /** Used as references for various `Number` constants. */
  3. const NAN = 0 / 0
  4. /**
  5. * The base implementation of `toNumber` which doesn't ensure correct
  6. * conversions of binary, hexadecimal, or octal string values.
  7. *
  8. * @private
  9. * @param {*} value The value to process.
  10. * @returns {number} Returns the number.
  11. */
  12. function baseToNumber(value) {
  13. if (typeof value == 'number') {
  14. return value
  15. }
  16. if (isSymbol(value)) {
  17. return NAN
  18. }
  19. return +value
  20. }
  21. export default baseToNumber

createMathOperation

  1. import baseToNumber from './baseToNumber.js'
  2. import baseToString from './baseToString.js'
  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 (value, other) => {
  13. if (value === undefined && other === undefined) {
  14. return defaultValue
  15. }
  16. if (value !== undefined && other === undefined) {
  17. return value
  18. }
  19. if (other !== undefined && value === undefined) {
  20. return other
  21. }
  22. if (typeof value === 'string' || typeof other === 'string') {
  23. value = baseToString(value)
  24. other = baseToString(other)
  25. }
  26. else {
  27. value = baseToNumber(value)
  28. other = baseToNumber(other)
  29. }
  30. return operator(value, other)
  31. }
  32. }
  33. export default createMathOperation

add

  1. import createMathOperation from './.internal/createMathOperation.js'
  2. /**
  3. * Adds two numbers.
  4. *
  5. * @since 3.4.0
  6. * @category Math
  7. * @param {number} augend The first number in an addition.
  8. * @param {number} addend The second number in an addition.
  9. * @returns {number} Returns the total.
  10. * @example
  11. *
  12. * add(6, 4)
  13. * // => 10
  14. */
  15. const add = createMathOperation((augend, addend) => augend + addend, 0)
  16. export default add