Commit 6ddff01c authored by Siltaar's avatar Siltaar
Browse files

convert to module JavaScript to simplify the testing process

parent fcaf875c
......@@ -19,35 +19,17 @@ name or abbreviation. It returns `undefined` if no corresponding number is found
To use it, include the `month_nb.js` file and define a `month_nb_json` variable
containing the `month_nb.json` instantiated object :
```html
<script src="month_nb.js" type="text/javascript"></script>
<script type="text/javascript">
month_nb_json = {};
fetch("month_nb.json").then(r => r.json()).then(json => month_nb_json = json);
</script>
```
A working expliciter approach :
```html
<script src="month_nb.js" type="text/javascript"></script>
<script type="text/javascript">
fetch("month_nb.json").then(r => r.json()).then(month_nb_json => {
month_nb('août', month_nb_json);
});
</script>
```
Modern method, using `async`/`await` :
```javascript
(async () => {
"use strict"
let month_nb_json = await fetch("js/month_nb/month_nb.json")
month_nb_json = await month_nb_json.json()
month_nb('août')
month_nb('août') <1>
month_nb('août', month_nb_json) <1>
})()
```
<1> both are equivalent
== Architecture
......@@ -111,7 +93,7 @@ file).
To test `month_nb.js` against the language definition file, run :
```sh
$ node test_month_nb.js
$ node --experimental-json-modules test_month_nb.mjs
```
The `node` v15.8.0 command comes from the `nodejs` package on Arch-based
......@@ -124,7 +106,6 @@ It will display various informations about the working and broken languages.
`month_nb` have been initially developped for https://www.meta-press.es/[Meta-Press.es].
== TODO
=== Update according to Wiktionary evolution
......@@ -138,3 +119,13 @@ initial release of `month_nb`, they are not all supported yet.
=== Node packaging
Make a NodeJS package out of it.
== Support
Please consider supporting this work via the Meta-Press.es project :
- via https://patreon.com/metapress[*Patreon*]
- https://liberapay.com/Meta-Press.es[Liberapay]
- Bitcoins : bc1qpkj7cu33e5y4jxjuml3e799jv6kxra729c4u03c9vyk85cz6379swqdzs3
- or Ethereums : 0x89f5f205141fc8eE6540e96BF2DCec20190c584
......@@ -5,7 +5,7 @@
// Licence: GPLv3
/* globals month_nb_json */
var month_nb = function (a, json_obj, check_keys=false) {
export const month_nb = function (a, json_obj, check_keys=false) {
json_obj = json_obj ? json_obj : month_nb_json
var keys = [Object.keys(json_obj)]
var objs = [json_obj]
......
// Name: month_nb.js
// Desc: returns month number from given litteral case-insensitive name or abbreviation
// Author: Simon 'Siltaar' Descarpentries ; siltaar § acoeuro ' com
// Version: 4
// Licence: GPLv3
/* globals month_nb_json */
exports.month_nb = (a, json_obj, check_keys=false) => {
json_obj = json_obj ? json_obj : month_nb_json
var keys = [Object.keys(json_obj)]
var objs = [json_obj]
var o_ks = keys[keys.length-1]
var o = objs[objs.length-1]
var r_range = ''
while(o_ks.length) {
let k = o_ks.shift()
if (k == "comment") continue
// console.log(a +' '+ k)
if (check_keys) { // show chars out of lang range in keys
if (keys.length == 1) {
var str_r_range = `${k.slice(1,k.length-1)}|[\\$\\.\\?\\^\\(\\)\\|\\[\\] ])`
r_range = new RegExp(str_r_range, 'gi')
} else if (keys.length > 1) {
var char_to_remove = k.replace(r_range, '')
if (char_to_remove.length > 1) {
console.log(str_r_range)
console.log(`You should remove ${char_to_remove} from ${k}`)
}
}
}
let r = new RegExp(k, 'i')
if (r.test(a)) {
o = o[k]
if (typeof(o) === 'number') {
return o
} else if (typeof(o) === 'object') {
objs.push(o)
o_ks = Object.keys(o)
keys.push(o_ks)
} else {
throw 'The JSON language-regexp-tree structure should only contain string '+
'keys as nodes, sub-objects as branchs and number leafs'
}
} else if (o_ks.length == 0) {
objs.pop() // backtrack
o = objs[objs.length-1]
keys.pop() // console.log(`depth level ${keys.length}`)
if (keys.length) {
o_ks = keys[keys.length-1] // console.log(`keys to explore ${o_ks.length}`)
} else { // console.error(`Did not found for ${a}.`)
return undefined
}
} else {
continue
}
}
}
{
"type":"module"
}
/* globals process require */
// import * as month_nb from "./month_nb_node-10.x.js"
// import * as month_nb_json_default from "./month_nb.json"
// import * as month_definitions from "./month_definitions/2019_definitions.json"
/* globals process */
'use strict'
var month_nb = require('./month_nb_node-10.x.js')
var month_nb_json_default = require('./month_nb.json')
var month_definitions = require('./month_definitions/2019_definitions.json')
import {month_nb} from "./month_nb.js"
import month_nb_json_default from "./month_nb.json"
import month_definitions from "./month_definitions/2019_definitions.json"
const month_nb_json = month_nb_json_default
var langs = Object.keys(month_definitions)
......@@ -32,7 +30,7 @@ for (let lang of langs) {
while(month_index < 12) {
month_name = month_names[month_index]
month_value = month_index + 1
month_nb_res = month_nb.month_nb(month_name, month_nb_json)
month_nb_res = month_nb(month_name, month_nb_json)
if (!(month_nb_res == month_value)) {
console.log(`${month_name} gave ${month_nb_res} instead of ${month_value}`)
broken_lang.push(lang)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment