WBE_Praktikum_3/script-of-sample.js

54 lines
1.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

require('./scripts.js')
const oldAndLiving = function(scripts){
return (scripts.filter(script => (script.year < 0 && script.living))).map(script => script.name)
}
const numberOfCodes = function({ranges}) {
return ranges.map(range => range[1] - range[0]).reduce((a, b) => a + b, 0)
}
// const numberOfCodes = ({ranges}) =>
// ranges.reduce((curr, [from, to]) => curr+to-from, 0)
const scriptOfSample = function(letter, scripts) {
code = letter.codePointAt(0)
script = scripts.filter(script => script.ranges.filter(range => code >= range[0] && code < range[1]).length > 0)[0]
if(script){
return script.name
}
else {
return 'unknown'
}
}
const scriptsInString = function(text, scripts) {
console.log(text)
let result = {}
for(letter in text){
script = scriptOfSample(text[letter], scripts)
console.log(text[letter], script)
result[script] = (script in result) ? result[script] + 1 : 1
}
return result
}
//console.log(oldAndLiving(SCRIPTS))
//console.log(numberOfCodes(SCRIPTS[3]))
//console.log(scriptOfSample("A", SCRIPTS))
//console.log( scriptOfSample("英", SCRIPTS))
//console.log( scriptOfSample("я", SCRIPTS))
//console.log(scriptsInString('英国的狗说 "JavaScript", "тяв"', SCRIPTS) )
//console.log(scriptsInString("https://pоstfinance.ch", SCRIPTS))
module.exports = { scriptOfSample }