|
| 1 | +/*jshint boss:true, node:true, eqnull:true, laxbreak:true, newcap:false, shadow:true, funcscope:true*/ |
| 2 | +/*eslint-env node*/ |
| 3 | +// Copyright 2013 Andy Earnshaw, MIT License |
| 4 | + |
| 5 | +/** |
| 6 | + * Downloads and parses the IANA Language Subtag Registry into a JavaScript object that can |
| 7 | + * be used for mapping. |
| 8 | + * |
| 9 | + * http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry |
| 10 | + * |
| 11 | + * Also downloads the current currency & funds code list, Table A.1 of ISO 4217 |
| 12 | + * and parses the code and minor unit value into a JavaScript object. |
| 13 | + * |
| 14 | + * http://www.currency-iso.org/dam/downloads/dl_iso_table_a1.xml |
| 15 | + * |
| 16 | + * The script requires a single argument to be passed with either of these values: |
| 17 | + * |
| 18 | + * iana: download, parse and output an object map for redundant tags and subtags |
| 19 | + * 4217: download, parse and output an object map for ISO 4217 minor currency units |
| 20 | + * |
| 21 | + * The result is output to stdOut, which makes it useful for easily inserting |
| 22 | + * into a file using Vim or emacs, or stdOut can be redirected to a file path instead. |
| 23 | + * |
| 24 | + */ |
| 25 | +'use strict'; |
| 26 | +var http = require('http'); |
| 27 | + |
| 28 | +module.exports = function (grunt) { |
| 29 | + grunt.registerTask('get-subtag-mappings', getIANA); |
| 30 | + grunt.registerTask('get-currency-units', get4217); |
| 31 | + |
| 32 | + // First the IANA mapping |
| 33 | + function getIANA () { |
| 34 | + var |
| 35 | + done = this.async(), |
| 36 | + url = 'http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry'; |
| 37 | + |
| 38 | + grunt.verbose.write('Fetching subtag registry data...'); |
| 39 | + http.get(url, function (res) { |
| 40 | + var txt = ''; |
| 41 | + |
| 42 | + if (res.statusCode !== 200) { |
| 43 | + grunt.verbose.error(); |
| 44 | + grunt.fail.warn('Request failed with status code ' + res.statusCode); |
| 45 | + return done(false); |
| 46 | + } |
| 47 | + |
| 48 | + res.on('error', function (err) { |
| 49 | + grunt.verbose.error(err); |
| 50 | + done(false); |
| 51 | + }); |
| 52 | + |
| 53 | + res.on('data', function (chunk) { |
| 54 | + txt += chunk; |
| 55 | + }); |
| 56 | + |
| 57 | + res.on('end', function () { |
| 58 | + grunt.verbose.ok(); |
| 59 | + parseData(txt); |
| 60 | + done(true); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + function parseData(txt) { |
| 65 | + var arr = txt.split('%%'), |
| 66 | + reg = /^([^:]+):\s+(.*)$/gm, |
| 67 | + res = {}; |
| 68 | + |
| 69 | + // Convert the strings to object maps |
| 70 | + arr = arr.map(function (str) { |
| 71 | + var obj = {}, |
| 72 | + field; |
| 73 | + |
| 74 | + while (field = reg.exec(str)) |
| 75 | + obj[field[1].match(/\w+/g).join('')] = field[2]; |
| 76 | + |
| 77 | + return obj; |
| 78 | + }); |
| 79 | + |
| 80 | + // RFC 5646 section 4.5, step 2: |
| 81 | + // 2. Redundant or grandfathered tags are replaced by their 'Preferred- |
| 82 | + // Value', if there is one. |
| 83 | + res.tags = arr.filter(function (obj) { |
| 84 | + return (obj.Type === "grandfathered" || obj.Type === "redundant") && obj.PreferredValue; |
| 85 | + }).reduce(function (tags, obj) { |
| 86 | + tags[obj.Tag] = obj.PreferredValue; |
| 87 | + return tags; |
| 88 | + }, {}); |
| 89 | + |
| 90 | + // 3. Subtags are replaced by their 'Preferred-Value', if there is one. |
| 91 | + // For extlangs, the original primary language subtag is also |
| 92 | + // replaced if there is a primary language subtag in the 'Preferred- |
| 93 | + // Value'. |
| 94 | + res.subtags = arr.filter(function (obj) { |
| 95 | + return (/language|script|variant|region/).test(obj.Type) && obj.PreferredValue; |
| 96 | + }).reduce(function (tags, obj) { |
| 97 | + tags[obj.Subtag] = obj.PreferredValue; |
| 98 | + return tags; |
| 99 | + }, {}); |
| 100 | + |
| 101 | + res.extLang = arr.filter(function (obj) { |
| 102 | + return obj.Type === 'extlang' && obj.PreferredValue; |
| 103 | + }).reduce(function (tags, obj) { |
| 104 | + tags[obj.Subtag] = [ obj.PreferredValue, obj.Prefix ]; |
| 105 | + return tags; |
| 106 | + }, {}); |
| 107 | + |
| 108 | + grunt.log.write(JSON.stringify(res, null, 4).replace(/"(\w+)":/g, "$1:") + '\n'); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Now the currency code minor units mapping |
| 113 | + function get4217 () { |
| 114 | + var |
| 115 | + done = this.async(), |
| 116 | + url = 'http://www.currency-iso.org/dam/downloads/table_a1.xml', |
| 117 | + evil = /<Ccy>([A-Z]{3})<[\s\S]+?<CcyMnrUnts>([^<]+)/gi; |
| 118 | + |
| 119 | + grunt.verbose.write('Fetching currency data...'); |
| 120 | + http.get(url, function (res) { |
| 121 | + var xml = '', |
| 122 | + obj = {}; |
| 123 | + |
| 124 | + if (res.statusCode !== 200) { |
| 125 | + grunt.verbose.error(); |
| 126 | + grunt.fail.warn('Request failed with status code ' + res.statusCode); |
| 127 | + return done(false); |
| 128 | + } |
| 129 | + |
| 130 | + res.on('error', function (err) { |
| 131 | + grunt.fail.warn(err); |
| 132 | + done(false); |
| 133 | + }); |
| 134 | + |
| 135 | + res.on('data', function (chunk) { |
| 136 | + xml += chunk; |
| 137 | + }); |
| 138 | + |
| 139 | + res.on('end', function () { |
| 140 | + var result; |
| 141 | + |
| 142 | + grunt.verbose.ok(); |
| 143 | + while (evil.exec(xml)) { |
| 144 | + // We already fallback to 2 as the number of digits |
| 145 | + if (isFinite(RegExp.$2) && RegExp.$2 !== '2') { |
| 146 | + obj[RegExp.$1] = +RegExp.$2; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + result = JSON.stringify(obj, null, 4).replace(/"(\w+)":/g, "$1:").split('\n'); |
| 151 | + grunt.log.write( |
| 152 | + '{' |
| 153 | + + result.slice(1, -1).reduce(function (o, v, i) { |
| 154 | + return o + (i % 9 === 0 ? '\n ' : ' ') + v.trim(); |
| 155 | + }, '') |
| 156 | + + '\n}\n' |
| 157 | + ); |
| 158 | + }); |
| 159 | + }); |
| 160 | + } |
| 161 | +}; |
0 commit comments