bayda-maghreb 1.0.0 → 1.0.2

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.
Files changed (3) hide show
  1. package/index.js +46 -32
  2. package/package.json +10 -2
  3. package/test.mjs +9 -0
package/index.js CHANGED
@@ -1,39 +1,53 @@
1
- import fs from 'fs';
2
- import { dialects } from './src/dialect-registry.js';
1
+ import path from 'path'
2
+ import { fileURLToPath } from 'url'
3
+ import fs from 'fs'
4
+ import { dialects } from './src/dialect-registry.js'
5
+
6
+ const __filename = fileURLToPath(import.meta.url)
7
+ const __dirname = path.dirname(__filename)
3
8
 
4
9
  function load(entry) {
5
- const dialect = JSON.parse(fs.readFileSync(`./data/dialects/${entry.file}`, 'utf8'));
6
- const problems = [];
7
- if (dialect.code!== entry.code) problems.push(`code في الملف (${dialect.code}) لا يطابق السجل (${entry.code})`);
8
- if (!dialect.name) problems.push('name مفقود');
9
- if (!dialect.script) problems.push('script مفقود');
10
- const kw = dialect.keywords;
11
- if (!kw || typeof kw!== 'object' ||!Object.keys(kw).length) problems.push('keywords فارغة أو مفقودة');
12
- else for (const [word, target] of Object.entries(kw)) {
13
- if (!/^[\p{L}\p{M}\p{N}_ ]+$/u.test(word)) problems.push(`كلمة غير مقبولة: ${word}`);
14
- if (typeof target!== 'string' ||!target) problems.push(`هدف غير صالح: ${word}`);
15
- }
16
- return { dialect, problems };
10
+ const p = path.join(__dirname, 'data/dialects', entry.file)
11
+ const dialect = JSON.parse(fs.readFileSync(p, 'utf8'))
12
+ return dialect
13
+ }
14
+
15
+ export function getDialect(code = 'kab') {
16
+ const entry = dialects.find(d => d.code === code)
17
+ if (!entry) throw new Error(`${code} not found`)
18
+ return load(entry)
17
19
  }
18
20
 
19
- const arg = process.argv[2] || 'kab';
20
- if (arg === '--test') {
21
- let bad = 0;
22
- for (const e of dialects) {
21
+ export function getAllDialects() {
22
+ return dialects.map(e => load(e))
23
+ }
24
+
25
+ export { dialects }
26
+
27
+ // باش يبقى يخدم كيما قبل كي تعمل node index.js kab
28
+ const arg = process.argv[2] || 'kab'
29
+ if (process.argv[1] && process.argv[1].endsWith('index.js')) {
30
+ if (arg === '--test') {
31
+ let bad = 0
32
+ for (const e of dialects) {
33
+ try {
34
+ const d = load(e)
35
+ console.log(`✅ ${e.code} - ${Object.keys(d.keywords).length} words`)
36
+ } catch (err) {
37
+ console.log(`❌ ${e.code}: ${err.message}`); bad++
38
+ }
39
+ }
40
+ process.exit(bad ? 1 : 0)
41
+ } else {
23
42
  try {
24
- const { problems } = load(e);
25
- console.log(problems.length? `❌ ${e.code}` : `✅ ${e.code}`);
26
- problems.forEach(p => console.log(' -', p));
27
- bad += problems.length;
28
- } catch (err) { console.log(`❌ ${e.code}: ${err.message}`); bad++; }
43
+ const d = getDialect(arg)
44
+ console.log(`✅ loaded: ${d.name} - ${Object.keys(d.keywords).length} words`)
45
+ } catch (e) {
46
+ console.error(`❌ ${e.message}`); process.exit(1)
47
+ }
29
48
  }
30
- process.exit(bad? 1 : 0);
31
49
  }
32
- const entry = dialects.find(d => d.code === arg);
33
- if (!entry) { console.error(`❌ لهجة غير معروفة: ${arg}\nالمتاح: ${dialects.map(d => d.code).join(', ')}`); process.exit(1); }
34
- try {
35
- const { dialect, problems } = load(entry);
36
- console.log(`✅ تم تحميل لهجة: ${dialect.name}`);
37
- console.log('الكلمات:', Object.keys(dialect.keywords).slice(0, 5));
38
- problems.forEach(p => console.error('⚠️', p));
39
- } catch (e) { console.error('❌ خطأ:', e.message); process.exit(1); }
50
+
51
+ // export default باش يخدم مع require و import
52
+ export default { getDialect, getAllDialects, dialects }
53
+ export const kab = getDialect('kab')
package/package.json CHANGED
@@ -1,13 +1,21 @@
1
1
  {
2
2
  "name": "bayda-maghreb",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Bayda Maghreb - Tunisian, Algerian, Moroccan, Libyan, Mauritanian Arabic and Franco transliteration",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
- "keywords": ["arabic", "darija", "tunisian", "maghreb", "transliteration", "franco"],
7
+ "keywords": [
8
+ "arabic",
9
+ "darija",
10
+ "tunisian",
11
+ "maghreb",
12
+ "transliteration",
13
+ "franco"
14
+ ],
8
15
  "author": "ajilis",
9
16
  "license": "MIT",
10
17
  "scripts": {
11
18
  "test": "node index.js --test"
12
19
  }
13
20
  }
21
+
package/test.mjs ADDED
@@ -0,0 +1,9 @@
1
+ import { kab, getDialect, getAllDialects } from './index.js'
2
+
3
+ const kabData = getDialect('kab')
4
+ console.log('✅ kab:', kabData.name, '-', Object.keys(kabData.keywords).length, 'words')
5
+
6
+ const dzData = getDialect('dz')
7
+ console.log('✅ dz:', dzData.name, '-', Object.keys(dzData.keywords).length, 'words')
8
+
9
+ console.log('✅ كلهم:', getAllDialects().length, 'لهجات')