bayda-maghreb 1.0.1 → 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 -27
  2. package/package.json +2 -1
  3. package/test.mjs +9 -0
package/index.js CHANGED
@@ -1,34 +1,53 @@
1
- import path from 'path';
2
- import { fileURLToPath } from 'url';
3
- const __filename = fileURLToPath(import.meta.url);
4
- const __dirname = path.dirname(__filename);
5
- import fs from 'fs';
6
- 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)
7
8
 
8
9
  function load(entry) {
9
- const p = path.join(__dirname, 'data/dialects', entry.file);
10
- const dialect = JSON.parse(fs.readFileSync(p, 'utf8'));
11
- const problems = [];
12
- if (dialect.code!== entry.code) problems.push(`code mismatch`);
13
- if (!dialect.name) problems.push('name missing');
14
- if (!dialect.script) problems.push('script missing');
15
- if (!dialect.keywords ||!Object.keys(dialect.keywords).length) problems.push('keywords empty');
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
- if(problems.length) bad++;
27
- } 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
+ }
28
48
  }
29
- process.exit(bad? 1 : 0);
30
49
  }
31
- const entry = dialects.find(d => d.code === arg);
32
- if (!entry) { console.error(`❌ ${arg} not found`); process.exit(1); }
33
- const { dialect } = load(entry);
34
- console.log(`✅ loaded: ${dialect.name} - ${Object.keys(dialect.keywords).length} words`);
50
+
51
+ // export default باش يخدم مع require و import
52
+ export default { getDialect, getAllDialects, dialects }
53
+ export const kab = getDialect('kab')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bayda-maghreb",
3
- "version": "1.0.1",
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",
@@ -18,3 +18,4 @@
18
18
  "test": "node index.js --test"
19
19
  }
20
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, 'لهجات')