@humanoiselabs/plus62 0.0.0-stage → 0.1.7
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/LICENSE +21 -0
- package/README.md +161 -2
- package/core/00-identity.md +27 -0
- package/core/10-safety.md +21 -0
- package/core/20-decision.md +26 -0
- package/core/30-efficiency.md +22 -0
- package/core/always-on.md +27 -0
- package/culture/id/absurd-humor.md +21 -0
- package/culture/id/dev-humor.md +22 -0
- package/culture/id/genz-lexicon.yaml +113 -0
- package/culture/id/humor-boundaries.md +23 -0
- package/culture/id/memes.yaml +41 -0
- package/culture/id/religious-expression.md +27 -0
- package/culture/id/sources.md +60 -0
- package/dist/cli.js +5712 -0
- package/evals/cci.yaml +468 -0
- package/evals/decisions.yaml +282 -0
- package/evals/preservation.yaml +128 -0
- package/evals/rusuh-voice.yaml +238 -0
- package/evals/safety.yaml +300 -0
- package/evals/smoke.yaml +56 -0
- package/fixtures/cod-admin-substring.yaml +44 -0
- package/fixtures/cod-alg-none.yaml +42 -0
- package/fixtures/cod-bigint-order-id.yaml +42 -0
- package/fixtures/cod-capture-group-index.yaml +40 -0
- package/fixtures/cod-cents-from-float.yaml +42 -0
- package/fixtures/cod-cents-split-remainder.yaml +60 -0
- package/fixtures/cod-clamp-both-bounds.yaml +51 -0
- package/fixtures/cod-csv-empty-field.yaml +37 -0
- package/fixtures/cod-csv-quoted-comma.yaml +54 -0
- package/fixtures/cod-csv-trailing-row.yaml +40 -0
- package/fixtures/cod-cut-on-word-boundary.yaml +47 -0
- package/fixtures/cod-default-parameter-null.yaml +42 -0
- package/fixtures/cod-discount-cents.yaml +37 -0
- package/fixtures/cod-email-plus-tag-canonical.yaml +45 -0
- package/fixtures/cod-emoji-preview-cut.yaml +44 -0
- package/fixtures/cod-empty-array-truthy.yaml +44 -0
- package/fixtures/cod-env-flag-false-string.yaml +55 -0
- package/fixtures/cod-escape-html.yaml +46 -0
- package/fixtures/cod-expiry-gte.yaml +37 -0
- package/fixtures/cod-flat-depth-nested.yaml +48 -0
- package/fixtures/cod-full-days-floor.yaml +49 -0
- package/fixtures/cod-health-check-partial.yaml +65 -0
- package/fixtures/cod-hex-color-expand.yaml +46 -0
- package/fixtures/cod-inclusive-range-sum.yaml +46 -0
- package/fixtures/cod-indexof-missing-item.yaml +48 -0
- package/fixtures/cod-invoice-zero-pad.yaml +38 -0
- package/fixtures/cod-json-date-roundtrip.yaml +45 -0
- package/fixtures/cod-last-items-zero-count.yaml +47 -0
- package/fixtures/cod-locale-thousands-dots.yaml +39 -0
- package/fixtures/cod-map-key-type.yaml +40 -0
- package/fixtures/cod-mask-secret.yaml +43 -0
- package/fixtures/cod-match-null-guard.yaml +43 -0
- package/fixtures/cod-memo-key-collision.yaml +56 -0
- package/fixtures/cod-missing-address-crash.yaml +39 -0
- package/fixtures/cod-month-index-label.yaml +42 -0
- package/fixtures/cod-no-mutation.yaml +37 -0
- package/fixtures/cod-object-key-order.yaml +59 -0
- package/fixtures/cod-options-default-object.yaml +44 -0
- package/fixtures/cod-page-offset-one-based.yaml +41 -0
- package/fixtures/cod-pagination-total.yaml +37 -0
- package/fixtures/cod-parse-port.yaml +39 -0
- package/fixtures/cod-phone-normalize.yaml +45 -0
- package/fixtures/cod-progress-label-rounding.yaml +50 -0
- package/fixtures/cod-query-regex-escape.yaml +43 -0
- package/fixtures/cod-query-value-encode.yaml +47 -0
- package/fixtures/cod-reduce-initial-value.yaml +38 -0
- package/fixtures/cod-regex-global-lastindex.yaml +44 -0
- package/fixtures/cod-retry-cap.yaml +43 -0
- package/fixtures/cod-round-away-from-zero.yaml +42 -0
- package/fixtures/cod-search-case-insensitive.yaml +50 -0
- package/fixtures/cod-shallow-copy-leak.yaml +45 -0
- package/fixtures/cod-slug-idempotent.yaml +38 -0
- package/fixtures/cod-slug-unanchored.yaml +46 -0
- package/fixtures/cod-sort-leaderboard.yaml +38 -0
- package/fixtures/cod-sort-names-by-letter.yaml +51 -0
- package/fixtures/cod-split-limit-drops-value.yaml +43 -0
- package/fixtures/cod-trailing-slash-dedupe.yaml +44 -0
- package/fixtures/cod-unique-by-field.yaml +57 -0
- package/fixtures/cod-weekend-numbering.yaml +40 -0
- package/fixtures/cod-zero-is-a-value.yaml +56 -0
- package/package.json +71 -4
- package/personas/plus62/card.md +8 -0
- package/personas/plus62/examples.jsonl +20 -0
- package/personas/plus62/lexicon.yaml +19 -0
- package/personas/plus62/persona.yaml +27 -0
- package/personas/plus62/tests.yaml +150 -0
- package/personas/plus62/voice.md +80 -0
- package/profiles/plus62.yaml +20 -0
- package/schemas/coding-fixture.schema.json +53 -0
- package/schemas/config.schema.json +18 -0
- package/schemas/culture.schema.json +75 -0
- package/schemas/eval-suite.schema.json +66 -0
- package/schemas/install-manifest.schema.json +52 -0
- package/schemas/lexicon.schema.json +27 -0
- package/schemas/manifest.schema.json +57 -0
- package/schemas/memes.schema.json +31 -0
- package/schemas/persona.schema.json +68 -0
- package/schemas/profile.schema.json +51 -0
- package/schemas/register-map.schema.json +105 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-object-key-order
|
|
3
|
+
language: id
|
|
4
|
+
tags: [objects, key-order, forms]
|
|
5
|
+
brief: >-
|
|
6
|
+
answerOrder(answers) ngembaliin nomor pertanyaan dalam urutan pertama kali
|
|
7
|
+
muncul, supaya riwayat formulir bisa dibaca ulang sama seperti waktu diisi.
|
|
8
|
+
Nomor pertanyaan bisa angka seperti "12" dan "3", bisa juga label seperti "alpha".
|
|
9
|
+
files:
|
|
10
|
+
- path: src/answers.js
|
|
11
|
+
content: |
|
|
12
|
+
export function answerOrder(answers) {
|
|
13
|
+
const rows = {};
|
|
14
|
+
for (const answer of answers) {
|
|
15
|
+
rows[answer.number] = answer.text;
|
|
16
|
+
}
|
|
17
|
+
return Object.keys(rows);
|
|
18
|
+
}
|
|
19
|
+
- path: test/answers.test.js
|
|
20
|
+
content: |
|
|
21
|
+
import test from 'node:test';
|
|
22
|
+
import assert from 'node:assert/strict';
|
|
23
|
+
import { answerOrder } from '../src/answers.js';
|
|
24
|
+
|
|
25
|
+
test('urutan pertama kali muncul yang dipakai', () => {
|
|
26
|
+
const answers = [
|
|
27
|
+
{ number: '12', text: 'a' },
|
|
28
|
+
{ number: '3', text: 'b' },
|
|
29
|
+
{ number: 'alpha', text: 'c' },
|
|
30
|
+
];
|
|
31
|
+
assert.deepEqual(answerOrder(answers), ['12', '3', 'alpha']);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('nomor yang kembar tidak ditulis dua kali', () => {
|
|
35
|
+
const answers = [
|
|
36
|
+
{ number: '1', text: 'a' },
|
|
37
|
+
{ number: '1', text: 'b' },
|
|
38
|
+
];
|
|
39
|
+
assert.deepEqual(answerOrder(answers), ['1']);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('satu jawaban balik satu nomor', () => {
|
|
43
|
+
assert.deepEqual(answerOrder([{ number: '7', text: 'x' }]), ['7']);
|
|
44
|
+
});
|
|
45
|
+
solution:
|
|
46
|
+
- path: src/answers.js
|
|
47
|
+
content: |
|
|
48
|
+
export function answerOrder(answers) {
|
|
49
|
+
const order = [];
|
|
50
|
+
const seen = new Set();
|
|
51
|
+
for (const answer of answers) {
|
|
52
|
+
if (!seen.has(answer.number)) {
|
|
53
|
+
seen.add(answer.number);
|
|
54
|
+
order.push(answer.number);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return order;
|
|
58
|
+
}
|
|
59
|
+
tests: 3
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-options-default-object
|
|
3
|
+
language: en
|
|
4
|
+
tags: [defaults, destructuring, config]
|
|
5
|
+
brief: >-
|
|
6
|
+
configure(options) fills in retry and verbosity defaults. Callers may pass
|
|
7
|
+
nothing at all, or an empty object, and both must get the documented defaults
|
|
8
|
+
of three retries and verbose off rather than crashing.
|
|
9
|
+
files:
|
|
10
|
+
- path: src/configure.js
|
|
11
|
+
content: |
|
|
12
|
+
export function configure(options) {
|
|
13
|
+
const { retries = 3, verbose = false } = options;
|
|
14
|
+
return { retries, verbose };
|
|
15
|
+
}
|
|
16
|
+
- path: test/configure.test.js
|
|
17
|
+
content: |
|
|
18
|
+
import test from 'node:test';
|
|
19
|
+
import assert from 'node:assert/strict';
|
|
20
|
+
import { configure } from '../src/configure.js';
|
|
21
|
+
|
|
22
|
+
test('no options at all still gives defaults', () => {
|
|
23
|
+
assert.deepEqual(configure(), { retries: 3, verbose: false });
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('an empty object gives defaults', () => {
|
|
27
|
+
assert.deepEqual(configure({}), { retries: 3, verbose: false });
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('given values win', () => {
|
|
31
|
+
assert.deepEqual(configure({ retries: 5, verbose: true }), { retries: 5, verbose: true });
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('a partial object keeps the other default', () => {
|
|
35
|
+
assert.deepEqual(configure({ retries: 1 }), { retries: 1, verbose: false });
|
|
36
|
+
});
|
|
37
|
+
solution:
|
|
38
|
+
- path: src/configure.js
|
|
39
|
+
content: |
|
|
40
|
+
export function configure(options = {}) {
|
|
41
|
+
const { retries = 3, verbose = false } = options;
|
|
42
|
+
return { retries, verbose };
|
|
43
|
+
}
|
|
44
|
+
tests: 4
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-page-offset-one-based
|
|
3
|
+
language: en
|
|
4
|
+
tags: [pagination, off-by-one]
|
|
5
|
+
brief: >-
|
|
6
|
+
offsetFor(page, limit) converts a 1-based page number into a SQL OFFSET.
|
|
7
|
+
Page 1 must start at row 0, and asking for page 0 is a bug in the caller,
|
|
8
|
+
not something that should silently return row zero.
|
|
9
|
+
files:
|
|
10
|
+
- path: src/paging.js
|
|
11
|
+
content: |
|
|
12
|
+
export function offsetFor(page, limit) {
|
|
13
|
+
return page * limit;
|
|
14
|
+
}
|
|
15
|
+
- path: test/paging.test.js
|
|
16
|
+
content: |
|
|
17
|
+
import test from 'node:test';
|
|
18
|
+
import assert from 'node:assert/strict';
|
|
19
|
+
import { offsetFor } from '../src/paging.js';
|
|
20
|
+
|
|
21
|
+
test('the first page starts at row zero', () => {
|
|
22
|
+
assert.equal(offsetFor(1, 10), 0);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('the third page skips two pages', () => {
|
|
26
|
+
assert.equal(offsetFor(3, 10), 20);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('page zero is rejected', () => {
|
|
30
|
+
assert.throws(() => offsetFor(0, 10), /1-based/);
|
|
31
|
+
});
|
|
32
|
+
solution:
|
|
33
|
+
- path: src/paging.js
|
|
34
|
+
content: |
|
|
35
|
+
export function offsetFor(page, limit) {
|
|
36
|
+
if (!Number.isInteger(page) || page < 1) {
|
|
37
|
+
throw new Error('page is 1-based');
|
|
38
|
+
}
|
|
39
|
+
return (page - 1) * limit;
|
|
40
|
+
}
|
|
41
|
+
tests: 3
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-pagination-total
|
|
3
|
+
language: id
|
|
4
|
+
tags: [pagination, off-by-one]
|
|
5
|
+
brief: >-
|
|
6
|
+
totalPages(total, perPage) buat paginasi. Sisa data sekecil apa pun tetap butuh satu
|
|
7
|
+
halaman sendiri, dan daftar kosong tetap punya satu halaman kosong.
|
|
8
|
+
files:
|
|
9
|
+
- path: src/paging.js
|
|
10
|
+
content: |
|
|
11
|
+
export function totalPages(total, perPage) {
|
|
12
|
+
return Math.floor(total / perPage);
|
|
13
|
+
}
|
|
14
|
+
- path: test/paging.test.js
|
|
15
|
+
content: |
|
|
16
|
+
import test from 'node:test';
|
|
17
|
+
import assert from 'node:assert/strict';
|
|
18
|
+
import { totalPages } from '../src/paging.js';
|
|
19
|
+
|
|
20
|
+
test('penuh pas tanpa sisa', () => {
|
|
21
|
+
assert.equal(totalPages(10, 10), 1);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('sisa sekecil apa pun tetap satu halaman', () => {
|
|
25
|
+
assert.equal(totalPages(11, 10), 2);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('kosong tetap punya satu halaman', () => {
|
|
29
|
+
assert.equal(totalPages(0, 10), 1);
|
|
30
|
+
});
|
|
31
|
+
solution:
|
|
32
|
+
- path: src/paging.js
|
|
33
|
+
content: |
|
|
34
|
+
export function totalPages(total, perPage) {
|
|
35
|
+
return Math.max(1, Math.ceil(total / perPage));
|
|
36
|
+
}
|
|
37
|
+
tests: 3
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-parse-port
|
|
3
|
+
language: id
|
|
4
|
+
tags: [parsing, url]
|
|
5
|
+
brief: >-
|
|
6
|
+
parsePort(url) harus ngasih nomor port dari URL, atau null kalau nggak ada port-nya.
|
|
7
|
+
Contoh: http://api:8080/path jadi 8080.
|
|
8
|
+
files:
|
|
9
|
+
- path: src/url.js
|
|
10
|
+
content: |
|
|
11
|
+
export function parsePort(url) {
|
|
12
|
+
const part = url.split(':')[1];
|
|
13
|
+
return part ? Number(part) : null;
|
|
14
|
+
}
|
|
15
|
+
- path: test/url.test.js
|
|
16
|
+
content: |
|
|
17
|
+
import test from 'node:test';
|
|
18
|
+
import assert from 'node:assert/strict';
|
|
19
|
+
import { parsePort } from '../src/url.js';
|
|
20
|
+
|
|
21
|
+
test('port di tengah path kebaca', () => {
|
|
22
|
+
assert.equal(parsePort('http://api:8080/path'), 8080);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('tanpa port hasilnya null', () => {
|
|
26
|
+
assert.equal(parsePort('https://api.example.com/path'), null);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('port di akhir URL tanpa path kebaca', () => {
|
|
30
|
+
assert.equal(parsePort('http://localhost:3000'), 3000);
|
|
31
|
+
});
|
|
32
|
+
solution:
|
|
33
|
+
- path: src/url.js
|
|
34
|
+
content: |
|
|
35
|
+
export function parsePort(url) {
|
|
36
|
+
const match = /:(\d+)(?:\/|$)/.exec(url);
|
|
37
|
+
return match ? Number(match[1]) : null;
|
|
38
|
+
}
|
|
39
|
+
tests: 3
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-phone-normalize
|
|
3
|
+
language: id
|
|
4
|
+
tags: [locale, validation]
|
|
5
|
+
hidden: true
|
|
6
|
+
brief: >-
|
|
7
|
+
normalizePhone(value) nyeragamin nomor Indonesia: angka 0 di depan jadi +62, spasi dan
|
|
8
|
+
tanda hubung dibuang, dan nomor yang nggak masuk pola balik nilainya null.
|
|
9
|
+
files:
|
|
10
|
+
- path: src/phone.js
|
|
11
|
+
content: |
|
|
12
|
+
export function normalizePhone(value) {
|
|
13
|
+
return value.replace(/\D/g, '');
|
|
14
|
+
}
|
|
15
|
+
- path: test/phone.test.js
|
|
16
|
+
content: |
|
|
17
|
+
import test from 'node:test';
|
|
18
|
+
import assert from 'node:assert/strict';
|
|
19
|
+
import { normalizePhone } from '../src/phone.js';
|
|
20
|
+
|
|
21
|
+
test('nomor 0 di depan jadi +62', () => {
|
|
22
|
+
assert.equal(normalizePhone('0812-345'), '+62812345');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('format +62 dengan spasi dirapikan', () => {
|
|
26
|
+
assert.equal(normalizePhone('+62 812 345'), '+62812345');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('nomor di luar pola jadi null', () => {
|
|
30
|
+
assert.equal(normalizePhone('12345'), null);
|
|
31
|
+
});
|
|
32
|
+
solution:
|
|
33
|
+
- path: src/phone.js
|
|
34
|
+
content: |
|
|
35
|
+
export function normalizePhone(value) {
|
|
36
|
+
const digits = value.replace(/\D/g, '');
|
|
37
|
+
if (/^0\d{5,}$/.test(digits)) {
|
|
38
|
+
return '+62' + digits.slice(1);
|
|
39
|
+
}
|
|
40
|
+
if (/^62\d{5,}$/.test(digits)) {
|
|
41
|
+
return '+' + digits;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
tests: 3
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-progress-label-rounding
|
|
3
|
+
language: id
|
|
4
|
+
tags: [rounding, progress, display]
|
|
5
|
+
brief: >-
|
|
6
|
+
progressLabel(done, total) bikin label persen buat progress bar migrasi.
|
|
7
|
+
Jangan pernah nulis 100% sebelum semua selesai, dan jangan bulatkan ke atas —
|
|
8
|
+
999 dari 1000 itu belum kelar. Kalau done-nya lewat dari total, tetep tulis
|
|
9
|
+
100%, jangan 150%.
|
|
10
|
+
files:
|
|
11
|
+
- path: src/progress.js
|
|
12
|
+
content: |
|
|
13
|
+
export function progressLabel(done, total) {
|
|
14
|
+
return `${Math.round((done / total) * 100)}%`;
|
|
15
|
+
}
|
|
16
|
+
- path: test/progress.test.js
|
|
17
|
+
content: |
|
|
18
|
+
import test from 'node:test';
|
|
19
|
+
import assert from 'node:assert/strict';
|
|
20
|
+
import { progressLabel } from '../src/progress.js';
|
|
21
|
+
|
|
22
|
+
test('999 dari 1000 belum 100 persen', () => {
|
|
23
|
+
assert.equal(progressLabel(999, 1000), '99%');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('dua dari tiga tidak dibulatkan ke atas', () => {
|
|
27
|
+
assert.equal(progressLabel(2, 3), '66%');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('selesai penuh ditulis 100 persen', () => {
|
|
31
|
+
assert.equal(progressLabel(1000, 1000), '100%');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('satu dari tiga', () => {
|
|
35
|
+
assert.equal(progressLabel(1, 3), '33%');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('lewat dari total tetap ditulis 100 persen', () => {
|
|
39
|
+
assert.equal(progressLabel(1500, 1000), '100%');
|
|
40
|
+
});
|
|
41
|
+
solution:
|
|
42
|
+
- path: src/progress.js
|
|
43
|
+
content: |
|
|
44
|
+
export function progressLabel(done, total) {
|
|
45
|
+
if (done >= total) {
|
|
46
|
+
return '100%';
|
|
47
|
+
}
|
|
48
|
+
return `${Math.floor((done / total) * 100)}%`;
|
|
49
|
+
}
|
|
50
|
+
tests: 5
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-query-regex-escape
|
|
3
|
+
language: id
|
|
4
|
+
tags: [regex, injection, search]
|
|
5
|
+
brief: >-
|
|
6
|
+
findQuery(text, query) cari teks persis seperti yang diketik user.
|
|
7
|
+
Sekarang query dianggap expression, jadi titik jadi wildcard dan
|
|
8
|
+
tanda kurung bikin aplikasinya error.
|
|
9
|
+
files:
|
|
10
|
+
- path: src/search.js
|
|
11
|
+
content: |
|
|
12
|
+
export function findQuery(text, query) {
|
|
13
|
+
return new RegExp(query, 'i').test(text);
|
|
14
|
+
}
|
|
15
|
+
- path: test/search.test.js
|
|
16
|
+
content: |
|
|
17
|
+
import test from 'node:test';
|
|
18
|
+
import assert from 'node:assert/strict';
|
|
19
|
+
import { findQuery } from '../src/search.js';
|
|
20
|
+
|
|
21
|
+
test('angka dengan titik tetap ketemu', () => {
|
|
22
|
+
assert.equal(findQuery('harga: 10.5', '10.5'), true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('titik bukan wildcard', () => {
|
|
26
|
+
assert.equal(findQuery('harga: 10x5', '10.5'), false);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('kurung di teks bukan masalah', () => {
|
|
30
|
+
assert.equal(findQuery('halo (edit)', '(edit)'), true);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('kurung di query tidak membuat error', () => {
|
|
34
|
+
assert.equal(findQuery('halo dunia', '('), false);
|
|
35
|
+
});
|
|
36
|
+
solution:
|
|
37
|
+
- path: src/search.js
|
|
38
|
+
content: |
|
|
39
|
+
export function findQuery(text, query) {
|
|
40
|
+
const escaped = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
41
|
+
return new RegExp(escaped, 'i').test(text);
|
|
42
|
+
}
|
|
43
|
+
tests: 4
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-query-value-encode
|
|
3
|
+
language: id
|
|
4
|
+
hidden: true
|
|
5
|
+
tags: [url, encoding, injection]
|
|
6
|
+
brief: >-
|
|
7
|
+
buildQuery(params) nyusun query string buat request ke API kampung.
|
|
8
|
+
Nilai harus di-encode: spasi jadi %20 dan karakter & atau = di dalam nilai
|
|
9
|
+
tidak boleh menyamar jadi parameter baru.
|
|
10
|
+
files:
|
|
11
|
+
- path: src/query.js
|
|
12
|
+
content: |
|
|
13
|
+
export function buildQuery(params) {
|
|
14
|
+
return Object.entries(params)
|
|
15
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
16
|
+
.join('&');
|
|
17
|
+
}
|
|
18
|
+
- path: test/query.test.js
|
|
19
|
+
content: |
|
|
20
|
+
import test from 'node:test';
|
|
21
|
+
import assert from 'node:assert/strict';
|
|
22
|
+
import { buildQuery } from '../src/query.js';
|
|
23
|
+
|
|
24
|
+
test('spasi di nilai di-encode', () => {
|
|
25
|
+
assert.equal(buildQuery({ q: 'kopi Susu' }), 'q=kopi%20Susu');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('nilai tidak bisa memalsukan parameter baru', () => {
|
|
29
|
+
assert.equal(buildQuery({ q: 'a&b=evil' }), 'q=a%26b%3Devil');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('satu parameter biasa tetap rapi', () => {
|
|
33
|
+
assert.equal(buildQuery({ page: '2' }), 'page=2');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('params kosong jadi string kosong', () => {
|
|
37
|
+
assert.equal(buildQuery({}), '');
|
|
38
|
+
});
|
|
39
|
+
solution:
|
|
40
|
+
- path: src/query.js
|
|
41
|
+
content: |
|
|
42
|
+
export function buildQuery(params) {
|
|
43
|
+
return Object.entries(params)
|
|
44
|
+
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
45
|
+
.join('&');
|
|
46
|
+
}
|
|
47
|
+
tests: 4
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-reduce-initial-value
|
|
3
|
+
language: en
|
|
4
|
+
tags: [arrays, reduce, empty-list]
|
|
5
|
+
brief: >-
|
|
6
|
+
totalWeight(items) sums the grams of every parcel in a shipment manifest.
|
|
7
|
+
An empty manifest is a normal state and must total zero grams, and a single
|
|
8
|
+
parcel must come back as its weight, not as the parcel object itself.
|
|
9
|
+
files:
|
|
10
|
+
- path: src/weight.js
|
|
11
|
+
content: |
|
|
12
|
+
export function totalWeight(items) {
|
|
13
|
+
return items.reduce((sum, item) => sum + item.grams);
|
|
14
|
+
}
|
|
15
|
+
- path: test/weight.test.js
|
|
16
|
+
content: |
|
|
17
|
+
import test from 'node:test';
|
|
18
|
+
import assert from 'node:assert/strict';
|
|
19
|
+
import { totalWeight } from '../src/weight.js';
|
|
20
|
+
|
|
21
|
+
test('an empty manifest totals zero', () => {
|
|
22
|
+
assert.equal(totalWeight([]), 0);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('several parcels are added up', () => {
|
|
26
|
+
assert.equal(totalWeight([{ grams: 250 }, { grams: 500 }]), 750);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('one parcel returns its weight', () => {
|
|
30
|
+
assert.equal(totalWeight([{ grams: 100 }]), 100);
|
|
31
|
+
});
|
|
32
|
+
solution:
|
|
33
|
+
- path: src/weight.js
|
|
34
|
+
content: |
|
|
35
|
+
export function totalWeight(items) {
|
|
36
|
+
return items.reduce((sum, item) => sum + item.grams, 0);
|
|
37
|
+
}
|
|
38
|
+
tests: 3
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-regex-global-lastindex
|
|
3
|
+
language: en
|
|
4
|
+
tags: [regex, stateful, search]
|
|
5
|
+
brief: >-
|
|
6
|
+
containsTag(text) answers whether a message has a hashtag. It must be a pure
|
|
7
|
+
question: the same text twice gives the same answer, and a message that does
|
|
8
|
+
have a tag must not change the answer for the message that follows it.
|
|
9
|
+
files:
|
|
10
|
+
- path: src/tags.js
|
|
11
|
+
content: |
|
|
12
|
+
const HAS_TAG = /#\w+/g;
|
|
13
|
+
|
|
14
|
+
export function containsTag(text) {
|
|
15
|
+
return HAS_TAG.test(text);
|
|
16
|
+
}
|
|
17
|
+
- path: test/tags.test.js
|
|
18
|
+
content: |
|
|
19
|
+
import test from 'node:test';
|
|
20
|
+
import assert from 'node:assert/strict';
|
|
21
|
+
import { containsTag } from '../src/tags.js';
|
|
22
|
+
|
|
23
|
+
test('a message with a hashtag is found', () => {
|
|
24
|
+
assert.equal(containsTag('lihat #promo'), true);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('the next message is answered on its own', () => {
|
|
28
|
+
assert.equal(containsTag('#diskon lagi'), true);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('a message without a hashtag is not found', () => {
|
|
32
|
+
assert.equal(containsTag('halo dunia'), false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('the first message is answered the same twice', () => {
|
|
36
|
+
assert.equal(containsTag('lihat #promo'), true);
|
|
37
|
+
});
|
|
38
|
+
solution:
|
|
39
|
+
- path: src/tags.js
|
|
40
|
+
content: |
|
|
41
|
+
export function containsTag(text) {
|
|
42
|
+
return /#\w+/.test(text);
|
|
43
|
+
}
|
|
44
|
+
tests: 4
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-retry-cap
|
|
3
|
+
language: id
|
|
4
|
+
tags: [retry, bounds]
|
|
5
|
+
brief: >-
|
|
6
|
+
backoffMs(attempt) pakai basis 200ms dan eksponensial 2x, tapi tidak boleh lewat
|
|
7
|
+
batas atas 2000ms berapa pun attempt-nya.
|
|
8
|
+
files:
|
|
9
|
+
- path: src/backoff.js
|
|
10
|
+
content: |
|
|
11
|
+
const BASE_MS = 200;
|
|
12
|
+
const CAP_MS = 2000;
|
|
13
|
+
|
|
14
|
+
export function backoffMs(attempt) {
|
|
15
|
+
return BASE_MS * 2 ** attempt;
|
|
16
|
+
}
|
|
17
|
+
- path: test/backoff.test.js
|
|
18
|
+
content: |
|
|
19
|
+
import test from 'node:test';
|
|
20
|
+
import assert from 'node:assert/strict';
|
|
21
|
+
import { backoffMs } from '../src/backoff.js';
|
|
22
|
+
|
|
23
|
+
test('attempt nol pakai basis', () => {
|
|
24
|
+
assert.equal(backoffMs(0), 200);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('sebelum batas naik eksponensial', () => {
|
|
28
|
+
assert.equal(backoffMs(3), 1600);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('lewat batas tetap mentok di 2000', () => {
|
|
32
|
+
assert.equal(backoffMs(6), 2000);
|
|
33
|
+
});
|
|
34
|
+
solution:
|
|
35
|
+
- path: src/backoff.js
|
|
36
|
+
content: |
|
|
37
|
+
const BASE_MS = 200;
|
|
38
|
+
const CAP_MS = 2000;
|
|
39
|
+
|
|
40
|
+
export function backoffMs(attempt) {
|
|
41
|
+
return Math.min(CAP_MS, BASE_MS * 2 ** attempt);
|
|
42
|
+
}
|
|
43
|
+
tests: 3
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-round-away-from-zero
|
|
3
|
+
language: id
|
|
4
|
+
tags: [rounding, negatives, refunds]
|
|
5
|
+
brief: >-
|
|
6
|
+
roundRupiah(nilai) bulatin angka ke rupiah terdekat untuk pembulatan refund.
|
|
7
|
+
Aturnya pembulatan menjauhi nol, jadi -2.5 itu -3 — simetris dengan 2.5 yang 3.
|
|
8
|
+
Sekarang nilai negatif dibulatkan ke arah yang lain.
|
|
9
|
+
files:
|
|
10
|
+
- path: src/round.js
|
|
11
|
+
content: |
|
|
12
|
+
export function roundRupiah(nilai) {
|
|
13
|
+
return Math.round(nilai);
|
|
14
|
+
}
|
|
15
|
+
- path: test/round.test.js
|
|
16
|
+
content: |
|
|
17
|
+
import test from 'node:test';
|
|
18
|
+
import assert from 'node:assert/strict';
|
|
19
|
+
import { roundRupiah } from '../src/round.js';
|
|
20
|
+
|
|
21
|
+
test('positif setengah naik', () => {
|
|
22
|
+
assert.equal(roundRupiah(2.5), 3);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('negatif setengah menjauhi nol', () => {
|
|
26
|
+
assert.equal(roundRupiah(-2.5), -3);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('negatif yang bukan setengah tetap dekat nol', () => {
|
|
30
|
+
assert.equal(roundRupiah(-2.4), -2);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('setengah ke bawah juga menjauhi nol', () => {
|
|
34
|
+
assert.equal(roundRupiah(-0.5), -1);
|
|
35
|
+
});
|
|
36
|
+
solution:
|
|
37
|
+
- path: src/round.js
|
|
38
|
+
content: |
|
|
39
|
+
export function roundRupiah(nilai) {
|
|
40
|
+
return nilai < 0 ? -Math.round(-nilai) : Math.round(nilai);
|
|
41
|
+
}
|
|
42
|
+
tests: 4
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-search-case-insensitive
|
|
3
|
+
language: id
|
|
4
|
+
tags: [strings, search, case]
|
|
5
|
+
brief: >-
|
|
6
|
+
searchProducts(products, query) nyari produk di nama yang cocok sama query.
|
|
7
|
+
Pencariannya tidak peka huruf besar/kecil, dari kedua sisi — user yang ngetik
|
|
8
|
+
"Kopi" harus tetap nemu produk bernama "Kopi Susu". Yang nyambung di tengah
|
|
9
|
+
kata juga dihitung, nggak harus mulai dari awal nama.
|
|
10
|
+
files:
|
|
11
|
+
- path: src/searchProducts.js
|
|
12
|
+
content: |
|
|
13
|
+
export function searchProducts(products, query) {
|
|
14
|
+
return products.filter((product) => product.name.toLowerCase().includes(query));
|
|
15
|
+
}
|
|
16
|
+
- path: test/searchProducts.test.js
|
|
17
|
+
content: |
|
|
18
|
+
import test from 'node:test';
|
|
19
|
+
import assert from 'node:assert/strict';
|
|
20
|
+
import { searchProducts } from '../src/searchProducts.js';
|
|
21
|
+
|
|
22
|
+
const katalog = [{ name: 'Kopi Susu' }, { name: 'Teh Poci' }];
|
|
23
|
+
|
|
24
|
+
test('query dengan huruf besar tetap nemu', () => {
|
|
25
|
+
assert.deepEqual(searchProducts(katalog, 'Kopi'), [{ name: 'Kopi Susu' }]);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('query huruf kecil juga nemu', () => {
|
|
29
|
+
assert.deepEqual(searchProducts(katalog, 'poci'), [{ name: 'Teh Poci' }]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('query yang tidak ada isinya kosong', () => {
|
|
33
|
+
assert.deepEqual(searchProducts(katalog, 'sambal'), []);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('query kosong ngasih semua produk', () => {
|
|
37
|
+
assert.equal(searchProducts(katalog, '').length, 2);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('potongan di tengah nama juga nemu', () => {
|
|
41
|
+
assert.deepEqual(searchProducts(katalog, 'oci'), [{ name: 'Teh Poci' }]);
|
|
42
|
+
});
|
|
43
|
+
solution:
|
|
44
|
+
- path: src/searchProducts.js
|
|
45
|
+
content: |
|
|
46
|
+
export function searchProducts(products, query) {
|
|
47
|
+
const needle = query.toLowerCase();
|
|
48
|
+
return products.filter((product) => product.name.toLowerCase().includes(needle));
|
|
49
|
+
}
|
|
50
|
+
tests: 5
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
schema: hahahah.coding.fixture/v1
|
|
2
|
+
id: cod-shallow-copy-leak
|
|
3
|
+
language: id
|
|
4
|
+
tags: [copying, aliasing, objects]
|
|
5
|
+
brief: >-
|
|
6
|
+
cloneOrder(order) dipakai buat bikin draft dari order yang sudah disimpan.
|
|
7
|
+
Draft boleh diedit tanpa mengubah order aslinya — termasuk daftar items-nya,
|
|
8
|
+
yang sekarang masih merujuk array yang sama.
|
|
9
|
+
files:
|
|
10
|
+
- path: src/order.js
|
|
11
|
+
content: |
|
|
12
|
+
export function cloneOrder(order) {
|
|
13
|
+
return { ...order };
|
|
14
|
+
}
|
|
15
|
+
- path: test/order.test.js
|
|
16
|
+
content: |
|
|
17
|
+
import test from 'node:test';
|
|
18
|
+
import assert from 'node:assert/strict';
|
|
19
|
+
import { cloneOrder } from '../src/order.js';
|
|
20
|
+
|
|
21
|
+
test('menambah item ke draft tidak mengubah aslinya', () => {
|
|
22
|
+
const asli = { id: 1, items: ['baju'] };
|
|
23
|
+
const draft = cloneOrder(asli);
|
|
24
|
+
draft.items.push('celana');
|
|
25
|
+
assert.deepEqual(asli.items, ['baju']);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('mengubah field biasa tidak mengubah aslinya', () => {
|
|
29
|
+
const asli = { id: 1, items: ['baju'] };
|
|
30
|
+
const draft = cloneOrder(asli);
|
|
31
|
+
draft.id = 2;
|
|
32
|
+
assert.equal(asli.id, 1);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('draft punya isi yang sama', () => {
|
|
36
|
+
const asli = { id: 7, items: ['a', 'b'] };
|
|
37
|
+
assert.deepEqual(cloneOrder(asli), asli);
|
|
38
|
+
});
|
|
39
|
+
solution:
|
|
40
|
+
- path: src/order.js
|
|
41
|
+
content: |
|
|
42
|
+
export function cloneOrder(order) {
|
|
43
|
+
return { ...order, items: order.items.slice() };
|
|
44
|
+
}
|
|
45
|
+
tests: 3
|