2.1.0 • Published 1 month ago

locter v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Locter 🔥

npm version CI codecov Known Vulnerabilities Conventional Commits

Locter is a library to locate and load a file/modules regarding specific criteria.

Table of Contents

Installation

npm install locter --save

Usage

The following examples are based on some shared assumptions:

  • A folder named files exists in the root directory.
  • The folder files contains the following files:
    • example.js
    • example.json
    • example.ts
    • example-long.ts

Locator

Multiple

Locating multiple files will return information about all files matching the pattern.

import { locateMany } from 'locter';

(async () => {
    let files = await locateMany(
        'files/example.{js,.ts}'
    );

    console.log(files);
    /*
    [
        { path: 'files', name: 'example', extension: '.js'},
        { path: 'files', name: 'example', extension: '.ts'}
    ]
     */

    files = await locateMany(
        'files/*.{js,ts}'
    );

    console.log(files);
    /*
    [
        { path: 'files', name: 'example', extension: '.js'},
        { path: 'files', name: 'example', extension: '.ts'},
        { path: 'files', name: 'example-long', extension: '.ts'},
    ]
     */
})

A synchronous variant is also available: locateManySync

Single

Locating a single file will return information about the first file matching the pattern.

import { locate } from 'locter';

(async () => {
    let file = await locate(
        'files/example.{js,.ts}'
    );

    console.log(file);
    /*
    { path: 'files', name: 'example', extension: '.js'}
     */
})

A synchronous variant is also available: locateSync

Loader

The load method can be used to load a file/module in an asynchronous fashion. Either a string or the output of the locate/locateSync method can be passed as argument.

import { load, locate } from 'locter';

(async () => {
    const file = await locate(
        'files/example.{js,.ts}'
    );

    let content = await load(file);
    console.log(content);
    // ...

    content = await load('...');
    console.log(content);
    // ...
})

There is also a synchronous method called loadSync to load files.

import { loadSync, locateSync } from 'locter';

(async () => {
    const file = await locateSync(
        'files/example.{js,.ts}'
    );

    let content = await loadSync(file);
    console.log(content);
    // ...

    content = await loadSync('...');
    console.log(content);
    // ...
})

Two loaders are predefined from scratch and already registered:

  • ConfLoader: This loader allows to load .conf files.
  • JSONLoader: This loader allows to load .json files.
  • YAMLLoader: This loader allows to load .yml files.
  • ModuleLoader: This loader allows to load modules with .js, .mjs, .mts, .cjs, .cts, .ts file extensions independent of the environment (cjs or esm).

To register loader for other file types, the function registerLoader can be used.

import { registerLoader } from 'locter';

registerLoader(['.ext'], {
    execute(input: string) {

    },
    executeSync(input: string) {

    }
})

License

Made with 💚

Published under MIT License.

2.1.0

1 month ago

2.0.2

4 months ago

2.0.1

4 months ago

2.0.0

4 months ago

1.3.0

4 months ago

1.2.3

5 months ago

1.2.2

8 months ago

1.2.0

10 months ago

1.1.1

11 months ago

1.1.3

10 months ago

1.2.1

9 months ago

1.1.2

11 months ago

1.0.2

1 year ago

1.1.0

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.11

1 year ago

0.8.1

1 year ago

1.0.10

1 year ago

0.8.0

1 year ago

0.7.1

1 year ago

0.8.2

1 year ago

0.7.0

1 year ago

0.6.2

1 year ago

0.5.3

1 year ago

0.5.0

1 year ago

0.3.2

2 years ago

0.4.0

1 year ago

0.6.1

1 year ago

0.5.2

1 year ago

0.6.0

1 year ago

0.5.1

1 year ago

0.3.1

2 years ago

0.2.1

2 years ago

0.1.2

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.2.2

2 years ago

0.0.1

2 years ago