formattingwhatsapp 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # Formatting WhatsApp
2
+
3
+ A fluent, type-safe TypeScript library for formatting WhatsApp messages.
4
+
5
+ ## Features
6
+
7
+ - **Basic Formatting**: Bold, Italic, Strikethrough, Monospace, Quote.
8
+ - **Lists**: Bullet, Numbered, and Indented lists.
9
+ - **Fluent API**: `MessageBuilder` for chaining commands.
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ bun add formattingwhatsapp
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ### Basic Formatting
20
+
21
+ ```typescript
22
+ import { bold, italic, strike, monospace } from 'formattingwhatsapp'
23
+
24
+ // Example: Sending a simple formatted message
25
+ const message = `${bold('Welcome!')} This is ${italic('my bot')}.`
26
+
27
+ await sock.sendMessage(chatId, { text: message })
28
+ ```
29
+
30
+ ### Combination Formatting
31
+
32
+ You can combine formatters by nesting functions. The library automatically trims whitespace to ensure WhatsApp recognizes the formatting options.
33
+
34
+ ```typescript
35
+ import { bold, italic, strike } from 'formattingwhatsapp'
36
+
37
+ // Bold + Italic
38
+ // Output: *_Text_*
39
+ const bi = bold(italic('Text'))
40
+
41
+ // Bold + Italic + Strikethrough
42
+ // Output: ~*_Text_*~
43
+ const bis = strike(bold(italic('Text')))
44
+
45
+ await sock.sendMessage(chatId, { text: bis })
46
+ ```
47
+
48
+ ### Fluent Message Builder
49
+
50
+ ```typescript
51
+ import { MessageBuilder } from 'formattingwhatsapp'
52
+
53
+ const message = new MessageBuilder()
54
+ .bold('Welcome to the Bot!')
55
+ .newline()
56
+ .text('Here are your options:')
57
+ .newline()
58
+ .bulletList(['Option 1', 'Option 2', 'Option 3'])
59
+ .newline()
60
+ .quote('Reply with your choice.')
61
+ .build()
62
+
63
+ // Sending the built message
64
+ await sock.sendMessage(chatId, { text: message })
65
+ ```
66
+
67
+ ### Fluent Message Builder
68
+
69
+ ```typescript
70
+ import { MessageBuilder, bold, italic } from 'formattingwhatsapp'
71
+
72
+ const message = new MessageBuilder()
73
+ .text('Standard text ')
74
+ .bold(italic('Bold and Italic')) // Nesting works here too!
75
+ .newline()
76
+ .text('Standard text again')
77
+ .build()
78
+
79
+ await sock.sendMessage(chatId, { text: message })
80
+ ```
81
+
82
+ ## API Reference
83
+
84
+ ### Text Formatters
85
+
86
+ - `bold(text: string): string`
87
+ - `italic(text: string): string`
88
+ - `strike(text: string): string`
89
+ - `monospace(text: string): string`
90
+ - `code(text: string): string`
91
+ - `quote(text: string): string`
92
+
93
+ ### List Formatters
94
+
95
+ - `bulletList(items: string[], symbol?: string): string`
96
+ - `numberedList(items: string[]): string`
97
+ - `indentedList(items: string[], indent?: string, symbol?: string): string`
98
+
99
+ ### MessageBuilder Methods
100
+
101
+ - `text(text: string)`
102
+ - `bold(text: string)`
103
+ - `italic(text: string)`
104
+ - `strike(text: string)`
105
+ - `monospace(text: string)`
106
+ - `code(text: string)`
107
+ - `quote(text: string)`
108
+ - `bulletList(items: string[], symbol?: string)`
109
+ - `numberedList(items: string[])`
110
+ - `newline(count?: number)`
111
+ - `build(): string`
112
+
113
+ ## License
114
+
115
+ ISC
@@ -0,0 +1,56 @@
1
+ export declare class MessageBuilder {
2
+ private parts;
3
+ constructor(initialText?: string);
4
+ /**
5
+ * Appends raw text to the message.
6
+ */
7
+ text(text: string): this;
8
+ /**
9
+ * Appends bold text.
10
+ */
11
+ bold(text: string): this;
12
+ /**
13
+ * Appends italic text.
14
+ */
15
+ italic(text: string): this;
16
+ /**
17
+ * Appends strikethrough text.
18
+ */
19
+ strike(text: string): this;
20
+ /**
21
+ * Appends monospace text block.
22
+ */
23
+ monospace(text: string): this;
24
+ /**
25
+ * Appends an inline code snippet.
26
+ */
27
+ code(text: string): this;
28
+ /**
29
+ * Appends a quoted block.
30
+ */
31
+ quote(text: string): this;
32
+ /**
33
+ * Appends a bullet list.
34
+ */
35
+ bulletList(items: string[], symbol?: string): this;
36
+ /**
37
+ * Appends a numbered list.
38
+ */
39
+ numberedList(items: string[]): this;
40
+ /**
41
+ * Adds a new line.
42
+ * @param count Number of new lines to add (default: 1).
43
+ */
44
+ newline(count?: number): this;
45
+ /**
46
+ * Builds the final message string.
47
+ */
48
+ build(): string;
49
+ /**
50
+ * Builds the message joined by a specific separator (e.g., newline).
51
+ * Useful if you built parts separately but want them joined by newlines.
52
+ * Note: The standard 'text' method just pushes to an array, so often you want to manage newlines manually or use this.
53
+ * However, standard behavior for this builder is just concatenation.
54
+ */
55
+ toString(): string;
56
+ }
@@ -0,0 +1,96 @@
1
+ import { bold, italic, strike, monospace, code, quote } from '../formatters/text.js';
2
+ import { bulletList, numberedList } from '../formatters/list.js';
3
+ export class MessageBuilder {
4
+ parts = [];
5
+ constructor(initialText = '') {
6
+ if (initialText) {
7
+ this.parts.push(initialText);
8
+ }
9
+ }
10
+ /**
11
+ * Appends raw text to the message.
12
+ */
13
+ text(text) {
14
+ this.parts.push(text);
15
+ return this;
16
+ }
17
+ /**
18
+ * Appends bold text.
19
+ */
20
+ bold(text) {
21
+ this.parts.push(bold(text));
22
+ return this;
23
+ }
24
+ /**
25
+ * Appends italic text.
26
+ */
27
+ italic(text) {
28
+ this.parts.push(italic(text));
29
+ return this;
30
+ }
31
+ /**
32
+ * Appends strikethrough text.
33
+ */
34
+ strike(text) {
35
+ this.parts.push(strike(text));
36
+ return this;
37
+ }
38
+ /**
39
+ * Appends monospace text block.
40
+ */
41
+ monospace(text) {
42
+ this.parts.push(monospace(text));
43
+ return this;
44
+ }
45
+ /**
46
+ * Appends an inline code snippet.
47
+ */
48
+ code(text) {
49
+ this.parts.push(code(text));
50
+ return this;
51
+ }
52
+ /**
53
+ * Appends a quoted block.
54
+ */
55
+ quote(text) {
56
+ this.parts.push(quote(text));
57
+ return this;
58
+ }
59
+ /**
60
+ * Appends a bullet list.
61
+ */
62
+ bulletList(items, symbol) {
63
+ this.parts.push(bulletList(items, symbol));
64
+ return this;
65
+ }
66
+ /**
67
+ * Appends a numbered list.
68
+ */
69
+ numberedList(items) {
70
+ this.parts.push(numberedList(items));
71
+ return this;
72
+ }
73
+ /**
74
+ * Adds a new line.
75
+ * @param count Number of new lines to add (default: 1).
76
+ */
77
+ newline(count = 1) {
78
+ this.parts.push('\n'.repeat(count));
79
+ return this;
80
+ }
81
+ /**
82
+ * Builds the final message string.
83
+ */
84
+ build() {
85
+ return this.parts.join('');
86
+ }
87
+ /**
88
+ * Builds the message joined by a specific separator (e.g., newline).
89
+ * Useful if you built parts separately but want them joined by newlines.
90
+ * Note: The standard 'text' method just pushes to an array, so often you want to manage newlines manually or use this.
91
+ * However, standard behavior for this builder is just concatenation.
92
+ */
93
+ toString() {
94
+ return this.build();
95
+ }
96
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Creates an indented list (useful for sub-lists or specific formatting).
3
+ * @param items The items to list.
4
+ * @param indent The indentation string (default: ' ').
5
+ * @param symbol The bullet symbol (default: '-').
6
+ */
7
+ export declare function indentedList(items: string[], indent?: string, symbol?: string): string;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Creates an indented list (useful for sub-lists or specific formatting).
3
+ * @param items The items to list.
4
+ * @param indent The indentation string (default: ' ').
5
+ * @param symbol The bullet symbol (default: '-').
6
+ */
7
+ export function indentedList(items, indent = ' ', symbol = '-') {
8
+ if (items.length === 0)
9
+ return '';
10
+ return items.map(item => `${indent}${symbol} ${item}`).join('\n');
11
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Creates a bulleted list.
3
+ * @param items The items to list.
4
+ * @param symbol The bullet symbol to use (default: '-').
5
+ */
6
+ export declare function bulletList(items: string[], symbol?: string): string;
7
+ /**
8
+ * Creates a numbered list.
9
+ * @param items The items to list.
10
+ */
11
+ export declare function numberedList(items: string[]): string;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Creates a bulleted list.
3
+ * @param items The items to list.
4
+ * @param symbol The bullet symbol to use (default: '-').
5
+ */
6
+ export function bulletList(items, symbol = '-') {
7
+ if (items.length === 0)
8
+ return '';
9
+ return items.map(item => `${symbol} ${item}`).join('\n');
10
+ }
11
+ /**
12
+ * Creates a numbered list.
13
+ * @param items The items to list.
14
+ */
15
+ export function numberedList(items) {
16
+ if (items.length === 0)
17
+ return '';
18
+ return items.map((item, index) => `${index + 1}. ${item}`).join('\n');
19
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Formats text as bold.
3
+ * @param text The text to bold.
4
+ */
5
+ export declare function bold(text: string): string;
6
+ /**
7
+ * Formats text as italic.
8
+ * @param text The text to italicize.
9
+ */
10
+ export declare function italic(text: string): string;
11
+ /**
12
+ * Formats text as strikethrough.
13
+ * @param text The text to strikethrough.
14
+ */
15
+ export declare function strike(text: string): string;
16
+ /**
17
+ * Formats text as monospace (inline code).
18
+ * @param text The text to format as monospace.
19
+ */
20
+ export declare function monospace(text: string): string;
21
+ /**
22
+ * Formats text as inline code.
23
+ * @param text The text to format as inline code.
24
+ */
25
+ export declare function code(text: string): string;
26
+ /**
27
+ * Formats text as a quote or blockquote.
28
+ * @param text The text to quote.
29
+ */
30
+ export declare function quote(text: string): string;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Formats text as bold.
3
+ * @param text The text to bold.
4
+ */
5
+ export function bold(text) {
6
+ return `*${text.trim()}*`;
7
+ }
8
+ /**
9
+ * Formats text as italic.
10
+ * @param text The text to italicize.
11
+ */
12
+ export function italic(text) {
13
+ return `_${text.trim()}_`;
14
+ }
15
+ /**
16
+ * Formats text as strikethrough.
17
+ * @param text The text to strikethrough.
18
+ */
19
+ export function strike(text) {
20
+ return `~${text.trim()}~`;
21
+ }
22
+ /**
23
+ * Formats text as monospace (inline code).
24
+ * @param text The text to format as monospace.
25
+ */
26
+ export function monospace(text) {
27
+ return `\`\`\`${text}\`\`\``;
28
+ }
29
+ /**
30
+ * Formats text as inline code.
31
+ * @param text The text to format as inline code.
32
+ */
33
+ export function code(text) {
34
+ return `\`${text}\``;
35
+ }
36
+ /**
37
+ * Formats text as a quote or blockquote.
38
+ * @param text The text to quote.
39
+ */
40
+ export function quote(text) {
41
+ return `> ${text}`;
42
+ }
@@ -0,0 +1,4 @@
1
+ export * from './formatters/text.js';
2
+ export * from './formatters/list.js';
3
+ export * from './formatters/indent.js';
4
+ export * from './builders/message.js';
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ // Export basic formatters
2
+ export * from './formatters/text.js';
3
+ export * from './formatters/list.js';
4
+ export * from './formatters/indent.js';
5
+ // Export builders
6
+ export * from './builders/message.js';
7
+ // Export utils
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "formattingwhatsapp",
3
+ "version": "1.0.0",
4
+ "description": "A fluent TypeScript library for formatting WhatsApp messages.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "type": "module",
11
+ "scripts": {
12
+ "test": "bun test",
13
+ "build": "tsc",
14
+ "prepublishOnly": "bun run build"
15
+ },
16
+ "keywords": [
17
+ "whatsapp",
18
+ "formatting",
19
+ "bot",
20
+ "typescript"
21
+ ],
22
+ "author": "Zaidan",
23
+ "license": "ISC",
24
+ "devDependencies": {
25
+ "@types/bun": "latest",
26
+ "typescript": "^5"
27
+ }
28
+ }