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

Next #10

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft

Next #10

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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# carol-js
A small tool for building regular expressions.
A small tool for building regular expressions.

We build a RegExp object by combining functions.
We only support ES modules.
We only support ES modules.

## Example
```js
Expand Down
7 changes: 0 additions & 7 deletions doc/spec.md

This file was deleted.

File renamed without changes.
11 changes: 11 additions & 0 deletions doc/technical/spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# manyメソッド
1つも引数が与えられなかった場合はmin=0として処理する。
1つ以上の引数を与えられている時、lengthまたはminのどちらかが指定されていない場合はエラーを発生させる。

# 記号付加系のメソッドではパターンを囲む
パターンの後ろに記号を付加するメソッドでは、基本的に内側のパターンを`(?:)`で囲まなくてはならない。
また、内側のパターンが1文字かつ記号文字ではない場合は`(?:)`で囲まなくてもよい。

**対象メソッド:**
- many
- option
2 changes: 1 addition & 1 deletion examples/helloworld/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"start": "node ."
},
"dependencies": {
"carol-js": "file:../.."
"carol-js": "1.0.0"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "carol-js",
"version": "1.0.0",
"version": "1.1.0",
"license": "MIT",
"description": "A small tool for building regular expressions.",
"keywords": [
Expand Down
2 changes: 2 additions & 0 deletions src/carol.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ declare module 'carol-js' {
toRegex(flags?: Flag | Flag[]): RegExp;
}

export default carol;

export {
carol,
Flag,
Expand Down
77 changes: 60 additions & 17 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,65 @@
/// <reference path="../dist/carol.d.ts" />

import test, { describe } from 'node:test';
import assert from 'node:assert';
import _default, { carol, Pattern, alt, seq } from 'carol-js';

assert.ok(typeof carol === 'function');
assert.ok(Pattern != null);
assert.ok(alt != null);
assert.ok(seq != null);

assert.ok(typeof _default === 'function');
assert.ok(_default.carol != null);
assert.ok(_default.Pattern != null);
assert.ok(_default.alt != null);
assert.ok(_default.seq != null);

const p = carol(/abc/);
assert.ok(p.many != null);
assert.ok(p.option != null);
assert.ok(p.capture != null);
assert.ok(p.toRegex != null);
assert.ok(p.source != null);
describe('api', () => {
test('carol', () => {
assert.strictEqual(carol(/abc/).source, 'abc');
});

test('Pattern', () => {
assert.strictEqual(new Pattern('abc').source, 'abc');
});

test('seq', () => {
assert.strictEqual(seq([carol(/abc/), carol(/xyz/)]).source, 'abcxyz');
});

test('alt', () => {
assert.strictEqual(alt([carol(/abc/), carol(/xyz/)]).source, '(?:abc|xyz)');
});

test('Pattern.many', () => {
const p = carol(/abc/);
assert.strictEqual(p.many().source, '(?:abc)*');
});

test('Pattern.option', () => {
const p = carol(/abc/);
assert.strictEqual(p.option().source, '(?:abc)?');
});

test('Pattern.capture', () => {
const p = carol(/abc/);
assert.strictEqual(p.capture().source, '(abc)');
});

test('Pattern.toRegex', () => {
const p = carol(/abc/);
assert.ok(p.toRegex() instanceof RegExp);
});
});

describe('api (default export)', () => {
test('default', () => {
assert.strictEqual(_default(/abc/).source, 'abc');
});

test('default.carol', () => {
assert.strictEqual(_default.carol(/abc/).source, 'abc');
});

test('default.Pattern', () => {
assert.strictEqual(new _default.Pattern('abc').source, 'abc');
});

test('default.seq', () => {
assert.strictEqual(_default.seq([_default.carol(/abc/), _default.carol(/xyz/)]).source, 'abcxyz');
});

test('default.alt', () => {
assert.strictEqual(_default.alt([_default.carol(/abc/), _default.carol(/xyz/)]).source, '(?:abc|xyz)');
});
});
126 changes: 65 additions & 61 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,80 @@
/// <reference path="../dist/carol.d.ts" />

import test, { describe } from 'node:test';
import assert from 'node:assert';
import test from 'node:test';
import carol from '../dist/carol.js';
import carol from 'carol-js';

test('carol()', () => {
assert.strictEqual(carol(/abc/).toRegex().source, 'abc');
assert.strictEqual(carol('abc').toRegex().source, 'abc');
});
describe('main', () => {
test('carol()', () => {
assert.strictEqual(carol(/abc/).toRegex().source, 'abc');
assert.strictEqual(carol('abc').toRegex().source, 'abc');
});

test('seq()', () => {
assert.strictEqual(carol.seq([carol(/abc/), carol(/xyz/)]).toRegex().source, 'abcxyz');
assert.strictEqual(carol.seq([carol(/abc/).many(), carol(/xyz/)]).toRegex().source, '(?:abc)*xyz');
});
test('seq()', () => {
assert.strictEqual(carol.seq([carol(/abc/), carol(/xyz/)]).toRegex().source, 'abcxyz');
assert.strictEqual(carol.seq([carol(/abc/).many(), carol(/xyz/)]).toRegex().source, '(?:abc)*xyz');
});

test('alt()', () => {
assert.strictEqual(carol.alt([carol(/abc/), carol(/xyz/)]).toRegex().source, '(?:abc|xyz)');
});
test('alt()', () => {
assert.strictEqual(carol.alt([carol(/abc/), carol(/xyz/)]).toRegex().source, '(?:abc|xyz)');
});

test('Pattern.many()', () => {
assert.strictEqual(carol(/abc/).many().toRegex().source, '(?:abc)*');
assert.strictEqual(carol(/abc/).many(0).toRegex().source, '(?:abc)*');
assert.strictEqual(carol(/abc/).many(1).toRegex().source, '(?:abc)+');
assert.strictEqual(carol(/abc/).many(2).toRegex().source, '(?:abc){2,}');
assert.strictEqual(carol(/abc/).many(3, 5).toRegex().source, '(?:abc){3,5}');
assert.strictEqual(carol(/abc/).many({length: 2}).toRegex().source, '(?:abc){2}');
assert.strictEqual(carol(/abc/).many(2, 2).toRegex().source, '(?:abc){2}');
});
test('Pattern.many()', () => {
assert.strictEqual(carol(/abc/).many().toRegex().source, '(?:abc)*');
assert.strictEqual(carol(/abc/).many(0).toRegex().source, '(?:abc)*');
assert.strictEqual(carol(/abc/).many(1).toRegex().source, '(?:abc)+');
assert.strictEqual(carol(/abc/).many(2).toRegex().source, '(?:abc){2,}');
assert.strictEqual(carol(/abc/).many(3, 5).toRegex().source, '(?:abc){3,5}');
assert.strictEqual(carol(/abc/).many({length: 2}).toRegex().source, '(?:abc){2}');
assert.strictEqual(carol(/abc/).many(2, 2).toRegex().source, '(?:abc){2}');
});

test('Pattern.many() greedy', () => {
assert.strictEqual(carol(/abc/).many({min: 0, greedy: true}).toRegex().source, '(?:abc)*');
assert.strictEqual(carol(/abc/).many({min: 1, greedy: true}).toRegex().source, '(?:abc)+');
assert.strictEqual(carol(/abc/).many({min: 2, greedy: true}).toRegex().source, '(?:abc){2,}');
assert.strictEqual(carol(/abc/).many({min: 3, max: 5, greedy: true}).toRegex().source, '(?:abc){3,5}');
assert.strictEqual(carol(/abc/).many({length: 2, greedy: true}).toRegex().source, '(?:abc){2}');
assert.strictEqual(carol(/abc/).many({min: 2, max: 2, greedy: true}).toRegex().source, '(?:abc){2}');
test('Pattern.many() greedy', () => {
assert.strictEqual(carol(/abc/).many({min: 0, greedy: true}).toRegex().source, '(?:abc)*');
assert.strictEqual(carol(/abc/).many({min: 1, greedy: true}).toRegex().source, '(?:abc)+');
assert.strictEqual(carol(/abc/).many({min: 2, greedy: true}).toRegex().source, '(?:abc){2,}');
assert.strictEqual(carol(/abc/).many({min: 3, max: 5, greedy: true}).toRegex().source, '(?:abc){3,5}');
assert.strictEqual(carol(/abc/).many({length: 2, greedy: true}).toRegex().source, '(?:abc){2}');
assert.strictEqual(carol(/abc/).many({min: 2, max: 2, greedy: true}).toRegex().source, '(?:abc){2}');

assert.strictEqual(carol(/abc/).many({min: 0, greedy: false}).toRegex().source, '(?:abc)*?');
assert.strictEqual(carol(/abc/).many({min: 1, greedy: false}).toRegex().source, '(?:abc)+?');
assert.strictEqual(carol(/abc/).many({min: 2, greedy: false}).toRegex().source, '(?:abc){2,}?');
assert.strictEqual(carol(/abc/).many({min: 3, max: 5, greedy: false}).toRegex().source, '(?:abc){3,5}?');
assert.strictEqual(carol(/abc/).many({length: 2, greedy: false}).toRegex().source, '(?:abc){2}?');
assert.strictEqual(carol(/abc/).many({min: 2, max: 2, greedy: false}).toRegex().source, '(?:abc){2}?');
});
assert.strictEqual(carol(/abc/).many({min: 0, greedy: false}).toRegex().source, '(?:abc)*?');
assert.strictEqual(carol(/abc/).many({min: 1, greedy: false}).toRegex().source, '(?:abc)+?');
assert.strictEqual(carol(/abc/).many({min: 2, greedy: false}).toRegex().source, '(?:abc){2,}?');
assert.strictEqual(carol(/abc/).many({min: 3, max: 5, greedy: false}).toRegex().source, '(?:abc){3,5}?');
assert.strictEqual(carol(/abc/).many({length: 2, greedy: false}).toRegex().source, '(?:abc){2}?');
assert.strictEqual(carol(/abc/).many({min: 2, max: 2, greedy: false}).toRegex().source, '(?:abc){2}?');
});

test('Pattern.many() simple char', () => {
assert.strictEqual(carol(/a/).many().toRegex().source, 'a*');
assert.strictEqual(carol(/a/).many(1).toRegex().source, 'a+');
});
test('Pattern.many() simple char', () => {
assert.strictEqual(carol(/a/).many().toRegex().source, 'a*');
assert.strictEqual(carol(/a/).many(1).toRegex().source, 'a+');
});

test('Pattern.option()', () => {
assert.strictEqual(carol(/abc/).option().toRegex().source, '(?:abc)?');
});
test('Pattern.option()', () => {
assert.strictEqual(carol(/abc/).option().toRegex().source, '(?:abc)?');
});

test('Pattern.option() greedy', () => {
assert.strictEqual(carol(/abc/).option({greedy: true}).toRegex().source, '(?:abc)?');
assert.strictEqual(carol(/abc/).option({greedy: false}).toRegex().source, '(?:abc)??');
});
test('Pattern.option() greedy', () => {
assert.strictEqual(carol(/abc/).option({greedy: true}).toRegex().source, '(?:abc)?');
assert.strictEqual(carol(/abc/).option({greedy: false}).toRegex().source, '(?:abc)??');
});

test('Pattern.option() simple char', () => {
assert.strictEqual(carol(/a/).option().toRegex().source, 'a?');
});
test('Pattern.option() simple char', () => {
assert.strictEqual(carol(/a/).option().toRegex().source, 'a?');
});

test('Pattern.capture()', () => {
assert.strictEqual(carol(/abc/).capture().toRegex().source, '(abc)');
});
test('Pattern.capture()', () => {
assert.strictEqual(carol(/abc/).capture().toRegex().source, '(abc)');
});

test('hello world', () => {
const regex = carol.seq([
carol(/hello/),
carol(/ /),
carol(/world/),
carol(/!/).many(1),
]).many().toRegex();
test('hello world', () => {
const regex = carol.seq([
carol(/hello/),
carol(/ /),
carol(/world/),
carol(/!/).many(1),
]).many().toRegex();

assert.strictEqual(regex.source, '(?:hello world(?:!)+)*');
assert.strictEqual(regex.source, '(?:hello world(?:!)+)*');
});
});