index.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. /**
  2. * Sets a constant default value for the property when it is undefined.
  3. * @template T
  4. * @template {keyof T} Property
  5. * @param {T} object An object.
  6. * @param {Property} property A property of the provided object.
  7. * @param {T[Property]} [defaultValue] The default value to set for the property.
  8. * @returns {T[Property]} The defaulted property value.
  9. */
  10. const d = (object, property, defaultValue) => {
  11. if (typeof object[property] === 'undefined' && typeof defaultValue !== 'undefined') {
  12. object[property] = defaultValue;
  13. }
  14. return object[property];
  15. };
  16. /**
  17. * Resolves the value for a nested object option.
  18. * @template T
  19. * @template {keyof T} Property
  20. * @template Result
  21. * @param {T} object An object.
  22. * @param {Property} property A property of the provided object.
  23. * @param {function(T | undefined): Result} fn The handler to resolve the property's value.
  24. * @returns {Result} The resolved option value.
  25. */
  26. const n = (object, property, fn) => {
  27. object[property] = fn(object[property]);
  28. return object[property];
  29. };
  30. module.exports = { d, n };