Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Expand APIs with new customizable options and enhanced functionality #6

Merged
merged 7 commits into from
Apr 26, 2024

Conversation

mitsuki31
Copy link
Owner

@mitsuki31 mitsuki31 commented Apr 26, 2024

Overview

This pull request introduces several enhancements and additions to the LSFND package, focusing on enhancing APIs usage by introducing some additional options, and refining type declarations to more robust and compatible with these changes and future changes.

Notable Changes

  • Addition of several new API options, including absolute, rootDir, and basename option to LsOptions (i.e., the options parameter of ls* functions).
  • Introduced a new constant object represents the default options of LsOptions, this is publicly visible.
  • Improvements to type declarations, ensuring type safety, compatibility, and improving code robustness.

Details

APIs Enhancements

  • Introduced several new API options:
    • absolute: Determines whether to return absolute paths for all entries.
    • basename: Controls whether returned entries include only file and/or directory names.
    • rootDir: Specifies the root directory to resolve relative paths.

Note

These options have priority tiers, with the highest priority being at the top of the list. For example, if both rootDir and absolute are specified, absolute takes precedence.

The basename option implicitly includes any directories on the returned entries. If you want to only include the filenames, then combine this option with lsTypes.LS_F type if you are using ls function, or use this option with lsFiles function for better flexibility.

Warning

THIS MIGHT BREAKING CHANGES!

In previous release (prior to version 0.1.0) you can literally use an explicit method that makes the returned results as absolute paths entirely. That is by utilizing the path.resolve function, here is an example:

const absfiles = await lsFiles(path.resolve('foo'));

In the above code, the directory path is resolved to an absolute path before being passed to the lsFiles function. As a result, the function treats the specified directory path as a relative path and does not attempt to resolve it back to a relative path, thus returning absolute paths. This approach was considered unstable and problematic due to inconsistencies in the internal logic.
Therefore, the absolute option was introduced as a replacement and will default returns relative paths when this option are disabled (set to false or unspecified), they will relative to the path specified in the rootDir option.

  • Added support for file URLs in the rootDir option. Note that only URLs with the 'file:' protocol are supported, and attempting to use other protocols will result in an error. This option uses the same logic resolution as the dirpath parameter, which you can pass a relative path of file URL string like this:

    lsDirs('file:./a/b/c', { rootDir: 'file:./a' });
    // Output: b/c
  • Enhanced the resolution of the options parameter by defining a new internal function called resolveOptions. This improves flexibility and reduces code duplication while ensuring type strictness.

  • Introduced the defaultLsOptions constant object, containing all default options for ls* functions.

Types Declaration

  • Declared a new type called StringPath representing a path from a string.
  • Refactored and improved type declarations, including changes to LsOptionsInterface, LsTypesValues, ResolvedLsOptions, and DefaultLsOptions.

Documentation

  • Added TypeDoc documentation for the defaultLsOptions object and the resolveOptions internal function.

Summary

These changes significantly enhance the LSFND package by introducing new API options, improving options handling and documentation, and refining type declarations. These enhancements contribute to better usability, flexibility, and correctness of the package.

* Declared a new type called `StringPath` represents the path from a string
* Changed the value of `LsOptionsInterface`'s properties to their constant values instead of their types (number)
* Refactored the `LsTypesValues` type, ensuring values compatibility with changes of `LsTypesInterface` interface
* Declared a new type called `ResolvedLsOptions` and an interface called `DefaultLsOptions`
This constant object contains all default options for `ls*` functions and it is publicly visible. In addition, refactored some types representation from specific parameters, variables, and even on explicit conversion types to more precise and improve types correctiveness.
By defining a new internal function called `resolveOptions` with consume one argument which for the unresoved `options` itself we can definitely improve the `options` parameter resolution to more flexible and use less code. This also make improvisation of type strict to that parameter.
Here is the list new options that just being added in this change:
  - absolute?: { boolean } - Whether to return absolute paths for all entries.
  - basename? { boolean } - Whether to makes the returned entries only have their file and/or directories names.
  - rootDir?: { StringPath } - A string path represents the root directory to resolve the relative paths.

They all were have their own priority tier, in the list above their priority are sorted from the highest (top) to the lowest (bottom). So, if you specified the `rootDir` but you also enabled the `absolute` option, then -- as a result, -- the `absolute` will be used instead (it is because the `absolute` option have the highest priority) and the `rootDir` value will be ignored.

Please note, that the `basename` option are implicitly includes any directory names while listing a directory. If you want include the filenames only, then combining it with the `lsTypes.LS_F` is a good way if you are using `ls` function, or combine this option with `lsFiles` function for more flexible.
Please note, this only supports the URL with 'file:' protocol. Attempting to use other than 'file:' protocol will causing an error.
Added TypeDoc documentation to the `defaultLsOptions` object and the `resolveOptions` (internal) function.
@mitsuki31 mitsuki31 self-assigned this Apr 26, 2024
@mitsuki31 mitsuki31 added enhancement New feature or request major Major changes (BREAKING CHANGES) labels Apr 26, 2024
@mitsuki31 mitsuki31 merged commit a274276 into master Apr 26, 2024
3 checks passed
@mitsuki31 mitsuki31 deleted the feat/add-new-api-options branch April 26, 2024 18:25
@mitsuki31 mitsuki31 added this to the v1.0.0 milestone Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request major Major changes (BREAKING CHANGES)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant