jmhobbs / jsTodoTxt

JavaScript parser for todo.txt formatted text files.
https://jstodotxt.velvetcache.org
MIT License
67 stars 14 forks source link

Possible extensions system for 1.0.0 #36

Open jmhobbs opened 1 year ago

jmhobbs commented 1 year ago

With the rewrite, I'm looking to support #10.

This is roughly the proposed API. Due to the check and cast when using, I'm uncertain this is any better than having users manage extensions outside of the core.

Example Usage

import { Item, Extension, RegisterExtension } from './src/index';

class HiddenExtension {
    #item: Item
    #hidden: boolean

    constructor(item: Item, value: string) {
        this.#hidden = value === '1';
        this.#item = item;
    }

    isHidden():boolean {
        return this.#hidden;
    }

    hide() {
        this.#hidden = true;
        this.#item.setExtension('h', '1');
    }

    unhide() {
        this.#hidden = false;
        this.#item.setExtension('h', '0');
    }
}

RegisterExtension(
    ['h'],
    (item: Item, _: string, value: string):Extension => { 
        return new HiddenExtension(item, value);
    }
);

const item = new Item('This is hidden. h:1');

console.log(item.extensions());
// [ { key: 'h', value: '1' } ]

const annotated = item.toAnnotatedString();
console.log(annotated);
/*
{
  string: 'This is hidden. h:1',
  contexts: [],
  projects: [],
  extensions: [
    {
      string: 'h:1',
      object: HiddenExtension {},
      parsed: [Object],
      span: [Object]
    }
  ]
}
*/

annotated.extensions.forEach(ext => {
    if(ext.parsed.key === 'h' && ext.object !== null)  {
        (ext.object as HiddenExtension).unhide();
    }
})
console.log(item.extensions());
// [ { key: 'h', value: '0' } ]

console.log(item.toString());
// This is hidden. h:0
netlify[bot] commented 1 year ago

Deploy Preview for grand-bunny-8f4598 ready!

Name Link
Latest commit ffafdd0dae7d14f9364db9940d0fd301811a4985
Latest deploy log https://app.netlify.com/sites/grand-bunny-8f4598/deploys/63522345d655270009dd1d53
Deploy Preview https://deploy-preview-36--grand-bunny-8f4598.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

codecov[bot] commented 1 year ago

Codecov Report

Base: 98.55% // Head: 96.74% // Decreases project coverage by -1.81% :warning:

Coverage data is based on head (ffafdd0) compared to base (1f7883e). Patch coverage: 64.51% of modified lines in pull request are covered.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## next #36 +/- ## ========================================== - Coverage 98.55% 96.74% -1.82% ========================================== Files 1 2 +1 Lines 555 584 +29 Branches 47 48 +1 ========================================== + Hits 547 565 +18 - Misses 6 17 +11 Partials 2 2 ``` | [Impacted Files](https://codecov.io/gh/jmhobbs/jsTodoTxt/pull/36?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=John+Hobbs) | Coverage Δ | | |---|---|---| | [src/Extensions.ts](https://codecov.io/gh/jmhobbs/jsTodoTxt/pull/36/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=John+Hobbs#diff-c3JjL0V4dGVuc2lvbnMudHM=) | `54.16% <54.16%> (ø)` | | | [src/Item.ts](https://codecov.io/gh/jmhobbs/jsTodoTxt/pull/36/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=John+Hobbs#diff-c3JjL0l0ZW0udHM=) | `98.57% <100.00%> (+0.01%)` | :arrow_up: | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=John+Hobbs). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=John+Hobbs)

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

jmhobbs commented 1 year ago

I think I prefer to move parsing and initialization outside of the core. My thought is to publish scoped packages like @jstodotxt/hidden-extension and opt in that way.