Skip to content

Commit

Permalink
fix: preset types
Browse files Browse the repository at this point in the history
  • Loading branch information
JiLiZART committed Apr 28, 2024
1 parent 0e7ca36 commit 55e4d3c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 35 deletions.
6 changes: 4 additions & 2 deletions packages/bbob-preset-react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import presetHTML5 from '@bbob/preset-html5';
import type { PresetTagsDefinition } from '@bbob/preset';
import type { PresetTagsDefinition } from '@bbob/preset-html5';

const tagAttr = (style: Record<string, string>) => ({
attrs: {
style,
},
});

export default presetHTML5.extend((tags: PresetTagsDefinition<'b' | 'i' | 'u' | 's'>) => ({
const presetReact = presetHTML5.extend((tags: PresetTagsDefinition<'b' | 'i' | 'u' | 's'>) => ({
...tags,

b: (...args) => ({
Expand All @@ -30,3 +30,5 @@ export default presetHTML5.extend((tags: PresetTagsDefinition<'b' | 'i' | 'u' |
...tagAttr({ textDecoration: 'line-through' }),
}),
}));

export default presetReact;
3 changes: 1 addition & 2 deletions packages/bbob-preset-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"vue"
],
"dependencies": {
"@bbob/preset-html5": "*",
"@bbob/preset": "*"
"@bbob/preset-html5": "*"
},
"devDependencies": {
"@bbob/core": "*"
Expand Down
56 changes: 33 additions & 23 deletions packages/bbob-preset-vue/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
import presetHTML5 from '@bbob/preset-html5';
import type { PresetTagsDefinition } from '@bbob/preset';
import type { PresetTagsDefinition } from '@bbob/preset-html5';

export const tagAttr = (style: Record<string, string>) => ({
attrs: {
style,
},
});

export const createTags = <Tags extends PresetTagsDefinition = PresetTagsDefinition>(tags: Tags) => ({
b: (...args) => ({
...tags.b?.(...args),
...tagAttr({ fontWeight: 'bold' }),
}),

i: (...args) => ({
...tags.i?.(...args),
...tagAttr({ fontStyle: 'italic' }),
}),

u: (...args) => ({
...tags.u?.(...args),
...tagAttr({ textDecoration: 'underline' }),
}),

s: (...args) => ({
...tags.s?.(...args),
...tagAttr({ textDecoration: 'line-through' }),
}),
});
export const createTags = (tags: PresetTagsDefinition<string>) => {
const newTags: PresetTagsDefinition<string> = {
b: (...args) => ({
...tags.b?.(...args),
...tagAttr({ fontWeight: 'bold' }),
tag: 'b',
}),

i: (...args) => ({
...tags.i?.(...args),
...tagAttr({ fontStyle: 'italic' }),
tag: 'i',
}),

u: (...args) => ({
...tags.u?.(...args),
...tagAttr({ textDecoration: 'underline' }),
tag: 'u',
}),

export default presetHTML5.extend((tags) => ({
s: (...args) => ({
...tags.s?.(...args),
...tagAttr({ textDecoration: 'line-through' }),
tag: 's',
}),
}

return newTags
};

const presetVue = presetHTML5.extend((tags: PresetTagsDefinition<string>) => ({
...tags,
...createTags(tags),
}));

export default presetVue;
9 changes: 5 additions & 4 deletions packages/bbob-preset-vue/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ describe('@bbob/preset-vue', () => {
s: defFn,
}
const tags = createTags(defTags)
const args = [{tag: 'test'}]

expect(tags.b()).toEqual(tagAttr({ fontWeight: 'bold' }))
expect(tags.i()).toEqual(tagAttr({ fontStyle: 'italic' }))
expect(tags.u()).toEqual(tagAttr({ textDecoration: 'underline' }))
expect(tags.s()).toEqual(tagAttr({ textDecoration: 'line-through' }))
expect(tags.b?.({tag: 'b'}, ...args)).toEqual(tagAttr({ fontWeight: 'bold' }))
expect(tags.i?.({tag: 'i'}, ...args)).toEqual(tagAttr({ fontStyle: 'italic' }))
expect(tags.u?.({tag: 'u'}, ...args)).toEqual(tagAttr({ textDecoration: 'underline' }))
expect(tags.s?.({tag: 's'}, ...args)).toEqual(tagAttr({ textDecoration: 'line-through' }))
})
});
3 changes: 3 additions & 0 deletions packages/bbob-preset/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import type { process } from "./preset";

type PartialRecord<K extends keyof any, T> = Partial<Record<K, T>>

export type { TagNodeObject } from "@bbob/plugin-helper";
export type { BBobCoreTagNodeTree, BBobPluginFunction, BBobPluginOptions } from "@bbob/core";

export type PresetTagsDefinition<Key extends string = string> = PartialRecord<Key, PresetTagFunction>

export type PresetOptions = Record<string, unknown>
Expand Down
8 changes: 4 additions & 4 deletions packages/bbob-preset/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ describe('@bbob/preset', () => {
expect(processor.mock.calls.length).toBe(1);
});
test('extend', () => {
const defTags = { foo: () => 'foo' }
const extendedTags = { bar: () => 'bar' }
const defTags = { foo: () => ({ tag: 'foo'}) }
const extendedTags = { bar: () =>({tag: 'bar'}) }
const options = { foo: 'bar' }
const tree = createTree([], {})
const { preset, processor, core } = presetFactory(defTags);
Expand All @@ -48,8 +48,8 @@ describe('@bbob/preset', () => {
expect(processor.mock.calls.length).toBe(1);
});
test('pass options', () => {
const { preset, processor } = presetFactory({ test: () => 'test' });
const newPreset = preset.extend((tags, options) => ({ bar: () => 'bar' }));
const { preset } = presetFactory({ test: () => ({tag: 'test'}) });
const newPreset = preset.extend((tags, options) => ({ bar: () => ({tag: 'bar'}) }));

const instance = preset({ foo: 'bar' });
const instance2 = newPreset({ some: 'some' });
Expand Down

0 comments on commit 55e4d3c

Please sign in to comment.