Skip to content

Commit

Permalink
chore: add more JSDoc types (#2801)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmaj authored Jun 24, 2024
1 parent 5af35ec commit 9c2be94
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/fns/src/dropLast.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Drops the last element from an array.
*
* @template T
* @param {T[]} array the array to drop the last element from
* @returns {T[]} the new array with the last element dropped
*/
const dropLast = (array) => array.slice(0, array.length - 1);

export default dropLast;
12 changes: 11 additions & 1 deletion packages/fns/src/evolve.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Applies a set of transformations to an object and returns a new object with the transformed values.
*
* @template T
* @param {Record<string, (value: T) => T | Record<string, (value: T) => T>>} transformations - The transformations to apply.
* @param {T} object the object to transform.
* @returns {T} the transformed object.
*/
const evolve = (transformations, object) => {
const result = object instanceof Array ? [] : {};
const keys = Object.keys(object);
Expand All @@ -11,7 +19,9 @@ const evolve = (transformations, object) => {
result[key] = transformation(object[key]);
} else if (transformation && type === 'object') {
result[key] = evolve(transformation, object[key]);
} else result[key] = object[key];
} else {
result[key] = object[key];
}
}

return result;
Expand Down
8 changes: 8 additions & 0 deletions packages/fns/src/get.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import isNil from './isNil';
import castArray from './castArray';

/**
* Retrieves the value at a given path from an object.
*
* @param {object} target the object to retrieve the value from.
* @param {string | string[]} path the path of the value to retrieve.
* @param {*} defaultValue the default value to return if the path does not exist.
* @returns {*} the value at the given path, or the default value if the path does not exist.
*/
const get = (target, path, defaultValue) => {
if (isNil(target)) return defaultValue;

Expand Down
7 changes: 7 additions & 0 deletions packages/fns/src/isNil.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Checks if a value is null or undefined.
*
* @template {unknown} T
* @param {T} value the value to check
* @returns {T is null | undefined} true if the value is null or undefined, false otherwise
*/
const isNil = (value) => value === null || value === undefined;

export default isNil;
6 changes: 6 additions & 0 deletions packages/fns/src/last.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Returns the last character of a string.
*
* @param {string} value the input string
* @returns {string} the last character of the string
*/
const last = (value) => {
return value === '' ? '' : value[value.length - 1];
};
Expand Down
7 changes: 7 additions & 0 deletions packages/fns/src/mapValues.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Maps over the values of an object and applies a function to each value.
*
* @param {Object} object the object to map over
* @param {Function} fn the function to apply to each value
* @returns {Object} a new object with the mapped values
*/
const mapValues = (object, fn) => {
const entries = Object.entries(object);

Expand Down
7 changes: 7 additions & 0 deletions packages/fns/src/omit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import castArray from './castArray';

/**
* Creates a new object by omitting specified keys from the original object.
*
* @param {string|string[]} keys the key or keys to omit
* @param {object} object the original object
* @returns {object} the new object without the omitted keys
*/
const omit = (keys, object) => {
const _keys = castArray(keys);

Expand Down
7 changes: 7 additions & 0 deletions packages/fns/src/pick.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Picks the specified keys from an object and returns a new object with only those keys.
*
* @param {string[]} keys the keys to pick from the object
* @param {object} obj the object to pick the keys from
* @returns {object} a new object with only the picked keys
*/
const pick = (keys, obj) => {
const result = {};

Expand Down
12 changes: 10 additions & 2 deletions packages/fns/src/repeat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const repeat = (list, length = 0) => {
/**
* Repeats an element a specified number of times.
*
* @template {unknown} T
* @param {T} element element to be repeated
* @param {number} length number of times to repeat element
* @returns {T[]} repeated elements
*/
const repeat = (elem, length = 0) => {
const result = new Array(length);
for (let i = 0; i < length; i += 1) {
result[i] = list;
result[i] = elem;
}
return result;
};
Expand Down
8 changes: 8 additions & 0 deletions packages/fns/src/reverse.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Reverses the list
*
* @template {unknown} T
* @param {T[]} list list to be reversed
* @returns {T[]} reversed list
*/
const reverse = (list) => Array.prototype.slice.call(list, 0).reverse();

export default reverse;

2 changes: 1 addition & 1 deletion packages/textkit/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @property {string} [alignLastLine]
* @property {Attachment} [attachment]
* @property {string} [backgroundColor]
* @property {unknown} [bidiLevel]
* @property {number} [bidiLevel]
* @property {unknown} [bullet]
* @property {number} [characterSpacing]
* @property {string} [color]
Expand Down

0 comments on commit 9c2be94

Please sign in to comment.