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

feat(ts): Mise en place du typage dans fonctions JavaScript #3

Merged
merged 13 commits into from
Apr 23, 2020
Merged
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Solution logicielle pour la détection anticipée d'entreprises en difficulté
- frontend vuetify
- mongodb

## Dépendances / pré-requis

- `npx` (installé avec [Node.js](https://nodejs.org/)), pour lancer la transpilation des fichiers TypeScript vers JavaScript

## Installation

```bash
Expand Down
43 changes: 20 additions & 23 deletions dbmongo/js/common/raison_sociale.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
function raison_sociale (
denomination_unite_legale,
nom_unite_legale,
nom_usage_unite_legale,
prenom1_unite_legale,
prenom2_unite_legale,
prenom3_unite_legale,
prenom4_unite_legale
){

if (!nom_usage_unite_legale){
var nom_usage_unite_legale = ''
} else {
var nom_usage_unite_legale = nom_usage_unite_legale + '/'
}
var raison_sociale = denomination_unite_legale ||
(nom_unite_legale + '*' + nom_usage_unite_legale +
prenom1_unite_legale + ' ' +
(prenom2_unite_legale || '') + ' ' +
(prenom3_unite_legale || '') + ' ' +
(prenom4_unite_legale || '') + ' ').trim() + '/'

return(raison_sociale)
function raison_sociale(denomination_unite_legale, nom_unite_legale, nom_usage_unite_legale, prenom1_unite_legale, prenom2_unite_legale, prenom3_unite_legale, prenom4_unite_legale) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ce fichier étant désormais généré par transpilation TypeScript depuis raison_sociale.ts, son formattage est un peu différent.

Il serait idéal de ne plus stocker dans git les fichiers JS résultats de transpilation, mais à noter que les tests existants s'appuient sur jsc pour tester ces fonctions, et donc sur leur format JS et non TS.

if (!nom_usage_unite_legale) {
var nom_usage_unite_legale = "";
}
else {
var nom_usage_unite_legale = nom_usage_unite_legale + "/";
}
var raison_sociale = denomination_unite_legale ||
(nom_unite_legale +
"*" +
nom_usage_unite_legale +
prenom1_unite_legale +
" " +
(prenom2_unite_legale || "") +
" " +
(prenom3_unite_legale || "") +
" " +
(prenom4_unite_legale || "") +
" ").trim() + "/";
return raison_sociale;
}
34 changes: 34 additions & 0 deletions dbmongo/js/common/raison_sociale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
declare function debug(string); // supported by jsc, to print in stdout

function raison_sociale(
denomination_unite_legale: string,
nom_unite_legale: string,
nom_usage_unite_legale: string,
prenom1_unite_legale: string,
prenom2_unite_legale: string,
prenom3_unite_legale: string,
prenom4_unite_legale: string
): string {
if (!nom_usage_unite_legale) {
var nom_usage_unite_legale = "";
} else {
var nom_usage_unite_legale = nom_usage_unite_legale + "/";
}
var raison_sociale =
denomination_unite_legale ||
(
nom_unite_legale +
"*" +
nom_usage_unite_legale +
prenom1_unite_legale +
" " +
(prenom2_unite_legale || "") +
" " +
(prenom3_unite_legale || "") +
" " +
(prenom4_unite_legale || "") +
" "
).trim() + "/";

return raison_sociale;
}
4 changes: 2 additions & 2 deletions dbmongo/js/reduce.algo2/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ function map () {

if (v.scope == "etablissement") {
let o = f.outputs(v, serie_periode)
let output_array = o[0]
let output_indexed = o[1]
let output_array = o[0] // [ OutputValue ] // in chronological order
let output_indexed = o[1] // { Periode -> OutputValue } // OutputValue: cf outputs()

// Les periodes qui nous interessent, triées
var periodes = Object.keys(output_indexed).sort((a,b) => (a >= b))
Expand Down
19 changes: 18 additions & 1 deletion dbmongo/lib/engine/js/loadJS.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)

// Reads all .js files in the current folder
// and encodes them as strings maps in jsFunctions.go
func main() {
func bundleJsFunctions() {
jsRootDir := filepath.Join("..", "..", "js")
folders, err := ioutil.ReadDir(jsRootDir)
if err != nil {
Expand Down Expand Up @@ -54,3 +55,19 @@ func main() {
}
out.Write([]byte("}\n"))
}

func transpileTsFunctions() {
// TODO: also transpile any other TS files
adrienjoly marked this conversation as resolved.
Show resolved Hide resolved
cmd := exec.Command("npx", "typescript", "../../js/common/raison_sociale.ts") // output: dbmongo/js/common/raison_sociale.js
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}

func main() {
transpileTsFunctions() // convert *.ts files to .js
bundleJsFunctions() // bundle *.js files to jsFunctions.go
}
2 changes: 0 additions & 2 deletions dbmongo/lib/engine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/signaux-faibles/gournal"
)

//go:generate go run js/loadJS.go

adrienjoly marked this conversation as resolved.
Show resolved Hide resolved
// Db connecteur exportable
var Db DB

Expand Down
3 changes: 2 additions & 1 deletion dbmongo/lib/engine/reduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func Reduce(batch AdminBatch, algo string, types []string) error {
dbTemp := "reduce" + strconv.Itoa(i)

functions := scope["f"].(map[string]bson.JavaScript)
// Injection des fonctions JavaScript pour exécution par MongoDB
job := &mgo.MapReduce{
Map: functions["map"].Code,
Reduce: functions["reduce"].Code,
Expand Down Expand Up @@ -178,7 +179,7 @@ func reduceFinalAggregation(tempDatabase *mgo.Database, tempCollection, outDatab
pipeline = append(pipeline, []bson.M{
bson.M{
"$unwind": bson.M{
"path": "$value",
"path": "$value",
"preserveNullAndEmptyArrays": false,
},
},
Expand Down
7 changes: 7 additions & 0 deletions test_js_functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
echo "Transpiling TypeScript files, and generating the jsFunctions.go bundle..."
cd dbmongo/lib/engine
go generate -x
cd -
echo "Running tests against the JS files (including the ones transpiled from TS)..."
cd dbmongo/js/test/ && ./test_common.sh
echo "✅ Tests passed."