all files / src/ utils.js

100% Statements 32/32
100% Branches 8/8
100% Functions 7/7
100% Lines 32/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 931× 1× 1×   1×                                     1× 2× 2× 1×   1×       1× 1×     1× 307×     1× 7× 3×   4× 3×   1×                 1× 7× 1×   6× 6×                     1× 42× 42× 42× 42× 36×   6×         1×                
var crypto = require('crypto');
var fs = require('fs');
var path = require('path');
 
var javascript_demo_constraint = "// session = a neo4j session\n" +
"// session.run(cypher_string) returns a promise (see the docs)\n" +
"new Promise(function(resolve, reject){\n" +
"    var variables = {\"fragment_id\": fragment_id};\n" +
"    session.run(\"MATCH(f:Fragment)-[]-(t:Token {value: 'Token'}) WHERE ID(f) = {fragment_id} RETURN t.value as value;\", variables)\n" +
"        .then(function(result){ \n" +
"            var value;\n" +
"            result.records.forEach(function(res){\n" +
"                value = res.get('value');\n" +
"                if (value === \"Token\") {\n" +
"                    reject(\"There was a token called Token.\");\n" +
"                }\n" +
"            });\n" +
"            resolve();\n" +
"    }).catch(function(err){\n" +
"        reject(err);\n" +
"    });\n" +
"});\n";
 
function hash_of_file_content(file_path) {
    try {
        var data = fs.readFileSync(__dirname + '/' + file_path);
        return crypto.createHash('sha1').update(data.toString()).digest('hex').toString();
    } catch (e) {
        return null;
    }
}
 
function hash_xml_fragment (fragment_id) {
    return hash_of_file_content('../media/uploaded_xmls/' + fragment_id + '.xml');
}
 
function remove_image(file_path) {
    return fs.unlinkSync(path.join(__dirname, '..', 'media', 'uploaded_images', file_path));
}
 
function token_type_mapping(token_type) {
    if (['token', 'modification', 'symbol'].includes(token_type)) {
        return 'Token';
    }
    if (['frame', 'comment', 'blanco'].includes(token_type)) {
        return 'Group';
    }
    return null;
}
/**
 * chains a list of functions (that return promises) and executes them in the right order
 * [function() {return Promise.resolve();}, function() {return Promise.resolve();}]
 *
 * @param funcs is an array of functions returning promises
 * @returns {Promise}
 */
function chain_promises(funcs) {
    if (funcs.length < 1) {
        return Promise.resolve();
    }
    var i = 0;
    return chain_executor(funcs, i);
}
 
/**
 * Recursive help method for chain_promises
 * 1) executes a function that returns a promise (no params allowed)
 * 2) chains itself to the success resolve of the promise
 *
 * @param funcs is an array of functions returning promises
 * @param i is the current working index
 */
function chain_executor(funcs, i) {
    var promise = funcs[i]();
    return promise.then(function(){
        console.log(i);
        if (funcs.length > i+1) {
            return chain_executor(funcs, i+1);
        } else {
            return Promise.resolve();
        }
    })
}
 
module.exports = {
    'javascript_demo_constraint':javascript_demo_constraint,
    'hash_of_file_content': hash_of_file_content,
    'hash_xml_fragment': hash_xml_fragment,
    'remove_image': remove_image,
    'token_type_mapping': token_type_mapping,
    'chain_promises': chain_promises
};