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

v4.0.0-rc2 #74

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,42 @@ npx jsr add @ndaidong/txtgen
```

```ts
import { sentence } from '@ndaidong/txtgen';
sentence();
import { sentence } from "@ndaidong/txtgen";

for (let i = 0; i < 5; i++) {
console.log(sentence());
}
```

In Deno, you can optionally use JSR packages without an install step using `jsr:` specifiers:
In Deno, you can optionally use JSR packages without an install step using
`jsr:` specifiers:

```ts
import { sentence } from 'jsr:@ndaidong/txtgen';
sentence();
import { sentence } from "jsr:@ndaidong/txtgen";

for (let i = 0; i < 5; i++) {
console.log(sentence());
}
```

You can still use `npm:` specifiers as before:

```ts
import { sentence } from 'npm:@ndaidong/txtgen';
sentence();
import { sentence } from "npm:@ndaidong/txtgen";

for (let i = 0; i < 5; i++) {
console.log(sentence());
}
```

Or import from esm.sh

```ts
import { sentence } from 'https://esm.sh/@ndaidong/txtgen';
sentence();
import { sentence } from "https://esm.sh/@ndaidong/txtgen";

for (let i = 0; i < 5; i++) {
console.log(sentence());
}
```

### Node.js & Bun
Expand All @@ -63,20 +76,32 @@ bun add @ndaidong/txtgen
```

```js
import { sentence } from '@ndaidong/txtgen';
import { sentence } from "@ndaidong/txtgen";

// CommonJS environment
// const { sentence } = require('txtgen');
for (let i = 0; i < 5; i++) {
console.log(sentence());
}
```

sentence();
You can also use CJS style:

```js
const { sentence } = require("@ndaidong/txtgen");

for (let i = 0; i < 5; i++) {
console.log(sentence());
}
```

### Browsers:

```html
<script type="module">
import { sentence } from 'https://unpkg.com/@ndaidong/txtgen/esm/mod.js'
console.log(sentence())
import { sentence } from 'https://unpkg.com/@ndaidong/txtgen/esm/mod.js';

for (let i = 0; i < 5; i++) {
console.log(sentence());
}
</script>
```

Expand Down Expand Up @@ -161,7 +186,9 @@ console.log(phrase); // => nisi blandit feugiat tempus imperdiet etiam eu mus au

## Development

Since v4.x.x, we switched to [Deno](https://docs.deno.com/runtime/manual/) platform, and use [DNT](https://github.com/denoland/dnt) to build Node.js packages.
Since v4.x.x, we switched to [Deno](https://docs.deno.com/runtime/manual/)
platform, and use [DNT](https://github.com/denoland/dnt) to build Node.js
packages.

```bash
git clone https://github.com/ndaidong/txtgen.git
Expand Down
3 changes: 1 addition & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ndaidong/txtgen",
"version": "4.0.0-rc1",
"version": "4.0.0-rc2",
"description": "Util for generating random sentences, paragraphs and articles in English",
"homepage": "https://github.com/ndaidong/txtgen",
"repository": {
Expand All @@ -10,7 +10,6 @@
"author": "@ndaidong",
"license": "MIT",
"tasks": {
"watch": "deno run --watch mod.ts",
"build": "deno run -A ./scripts/build_npm.ts"
},
"imports": {
Expand Down
19 changes: 19 additions & 0 deletions tests/helper_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { assertEquals } from "assert";

import { adjectives, nouns } from "../utils/sample.ts";

import { generator, normalize } from "../utils/helper.ts";

Deno.test("check if normalize() works correctly", () => {
assertEquals(normalize("dog"), "a dog");
assertEquals(normalize("car"), "a car");
assertEquals(normalize("hour"), "an hour");
assertEquals(normalize("egg"), "an egg");
});

Deno.test("check if generator() works correctly", () => {
assertEquals(nouns.includes(generator.noun()), true);
assertEquals(generator.a_noun().startsWith("a"), true);
assertEquals(adjectives.includes(generator.adjective()), true);
assertEquals(generator.an_adjective().startsWith("a"), true);
});
File renamed without changes.
96 changes: 96 additions & 0 deletions tests/sample_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { assertEquals, assertNotEquals } from "assert";

import {
addAdjectives,
addNouns,
addTemplates,
adjectives,
getAdjectives,
getNouns,
getTemplates,
nouns,
sentenceTemplates,
setAdjectives,
setNouns,
setTemplates,
} from "../utils/sample.ts";

Deno.test("check if getNouns() return same as pre-defined nouns", () => {
const val = getNouns();
const lastIndex = val.length - 1;
assertEquals(val.length, nouns.length);
assertEquals(val[0], nouns[0]);
assertEquals(val[9], nouns[9]);
assertEquals(val[lastIndex], nouns[lastIndex]);
});

Deno.test("check if addNouns()/setNouns() affects to pre-defined nouns", () => {
const preDefined = getNouns();
setNouns(["one", "two", "three"]);
const valAfterSetCall = getNouns();
assertNotEquals(valAfterSetCall.length, preDefined.length);
assertEquals(valAfterSetCall.length, 3);
assertEquals(valAfterSetCall[1], "two");

addNouns(["four", "five", "six"]);
const valAfterAddCall = getNouns();
assertEquals(valAfterAddCall.length > valAfterSetCall.length, true);
assertEquals(valAfterAddCall.includes("four"), true);
assertEquals(valAfterAddCall.includes("six"), true);
});

Deno.test("check if getAdjectives() return same as pre-defined adjectives", () => {
const val = getAdjectives();
const lastIndex = val.length - 1;
assertEquals(val.length, adjectives.length);
assertEquals(val[0], adjectives[0]);
assertEquals(val[9], adjectives[9]);
assertEquals(val[lastIndex], adjectives[lastIndex]);
});

Deno.test("check if addAdjectives()/setAdjectives() affects to pre-defined adjectives", () => {
const preDefined = getAdjectives();
setAdjectives(["black", "white", "yellow"]);
const valAfterSetCall = getAdjectives();
assertNotEquals(valAfterSetCall.length, preDefined.length);
assertEquals(valAfterSetCall.length, 3);
assertEquals(valAfterSetCall[1], "white");

addAdjectives(["blue", "red", "green"]);
const valAfterAddCall = getAdjectives();
assertEquals(valAfterAddCall.length > valAfterSetCall.length, true);
assertEquals(valAfterAddCall.includes("blue"), true);
assertEquals(valAfterAddCall.includes("green"), true);
});

Deno.test("check if getTemplates() return same as pre-defined sentenceTemplates", () => {
const val = getTemplates();
const lastIndex = val.length - 1;
assertEquals(val.length, sentenceTemplates.length);
assertEquals(val[0], sentenceTemplates[0]);
assertEquals(val[9], sentenceTemplates[9]);
assertEquals(val[lastIndex], sentenceTemplates[lastIndex]);
});

Deno.test("check if addTemplates()/setTemplates() affects to pre-defined sentenceTemplates", () => {
const preDefined = getTemplates();
setTemplates([
"first sentence template",
"second sentence template",
"third sentence template",
]);
const valAfterSetCall = getTemplates();
assertNotEquals(valAfterSetCall.length, preDefined.length);
assertEquals(valAfterSetCall.length, 3);
assertEquals(valAfterSetCall[1], "second sentence template");

addTemplates([
"fourth sentence template",
"fifth sentence template",
"sixth sentence template",
]);
const valAfterAddCall = getTemplates();
assertEquals(valAfterAddCall.length > valAfterSetCall.length, true);
assertEquals(valAfterAddCall.includes("fourth sentence template"), true);
assertEquals(valAfterAddCall.includes("sixth sentence template"), true);
});
30 changes: 30 additions & 0 deletions utils/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export let nouns: string[] = [
"tangerine",
"watermelon",
];

export let adjectives: string[] = [
"adaptable",
"adventurous",
Expand Down Expand Up @@ -326,6 +327,35 @@ export const vowels: string[] = [

export let sentenceTemplates: string[] = [
"however, {{nouns}} have begun to rent {{nouns}} over the past few months, specifically for {{nouns}} associated with their {{nouns}}",
"the {{noun}} is {{a_noun}}",
"{{a_noun}} is {{an_adjective}} {{noun}}",
"the first {{adjective}} {{noun}} is, in its own way, {{a_noun}}",
"their {{noun}} was, in this moment, {{an_adjective}} {{noun}}",
"{{a_noun}} is {{a_noun}} from the right perspective",
"the literature would have us believe that {{an_adjective}} {{noun}} is not but {{a_noun}}",
"{{an_adjective}} {{noun}} is {{a_noun}} of the mind",
"the {{adjective}} {{noun}} reveals itself as {{an_adjective}} {{noun}} to those who look",
"authors often misinterpret the {{noun}} as {{an_adjective}} {{noun}}, when in actuality it feels more like {{an_adjective}} {{noun}}",
"we can assume that any instance of {{a_noun}} can be construed as {{an_adjective}} {{noun}}",
"they were lost without the {{adjective}} {{noun}} that composed their {{noun}}",
"the {{adjective}} {{noun}} comes from {{an_adjective}} {{noun}}",
"{{a_noun}} can hardly be considered {{an_adjective}} {{noun}} without also being {{a_noun}}",
"few can name {{an_adjective}} {{noun}} that isn't {{an_adjective}} {{noun}}",
"some posit the {{adjective}} {{noun}} to be less than {{adjective}}",
"{{a_noun}} of the {{noun}} is assumed to be {{an_adjective}} {{noun}}",
"{{a_noun}} sees {{a_noun}} as {{an_adjective}} {{noun}}",
"the {{noun}} of {{a_noun}} becomes {{an_adjective}} {{noun}}",
"{{a_noun}} is {{a_noun}}'s {{noun}}",
"{{a_noun}} is the {{noun}} of {{a_noun}}",
"{{an_adjective}} {{noun}}'s {{noun}} comes with it the thought that the {{adjective}} {{noun}} is {{a_noun}}",
"{{nouns}} are {{adjective}} {{nouns}}",
"{{adjective}} {{nouns}} show us how {{nouns}} can be {{nouns}}",
"before {{nouns}}, {{nouns}} were only {{nouns}}",
"those {{nouns}} are nothing more than {{nouns}}",
"some {{adjective}} {{nouns}} are thought of simply as {{nouns}}",
"one cannot separate {{nouns}} from {{adjective}} {{nouns}}",
"the {{nouns}} could be said to resemble {{adjective}} {{nouns}}",
"{{an_adjective}} {{noun}} without {{nouns}} is truly a {{noun}} of {{adjective}} {{nouns}}",
];

export const phrases: string[] = [
Expand Down
Loading