540a8179d5e5d2dec64e1d14e5278bb7.json
7.82 KB
{"ast":null,"code":"var conversions = require('./conversions');\n/*\n\tthis function routes a model to all other models.\n\n\tall functions that are routed have a property `.conversion` attached\n\tto the returned synthetic function. This property is an array\n\tof strings, each with the steps in between the 'from' and 'to'\n\tcolor models (inclusive).\n\n\tconversions that are not possible simply are not included.\n*/\n\n\nfunction buildGraph() {\n var graph = {}; // https://jsperf.com/object-keys-vs-for-in-with-closure/3\n\n var models = Object.keys(conversions);\n\n for (var len = models.length, i = 0; i < len; i++) {\n graph[models[i]] = {\n // http://jsperf.com/1-vs-infinity\n // micro-opt, but this is simple.\n distance: -1,\n parent: null\n };\n }\n\n return graph;\n} // https://en.wikipedia.org/wiki/Breadth-first_search\n\n\nfunction deriveBFS(fromModel) {\n var graph = buildGraph();\n var queue = [fromModel]; // unshift -> queue -> pop\n\n graph[fromModel].distance = 0;\n\n while (queue.length) {\n var current = queue.pop();\n var adjacents = Object.keys(conversions[current]);\n\n for (var len = adjacents.length, i = 0; i < len; i++) {\n var adjacent = adjacents[i];\n var node = graph[adjacent];\n\n if (node.distance === -1) {\n node.distance = graph[current].distance + 1;\n node.parent = current;\n queue.unshift(adjacent);\n }\n }\n }\n\n return graph;\n}\n\nfunction link(from, to) {\n return function (args) {\n return to(from(args));\n };\n}\n\nfunction wrapConversion(toModel, graph) {\n var path = [graph[toModel].parent, toModel];\n var fn = conversions[graph[toModel].parent][toModel];\n var cur = graph[toModel].parent;\n\n while (graph[cur].parent) {\n path.unshift(graph[cur].parent);\n fn = link(conversions[graph[cur].parent][cur], fn);\n cur = graph[cur].parent;\n }\n\n fn.conversion = path;\n return fn;\n}\n\nmodule.exports = function (fromModel) {\n var graph = deriveBFS(fromModel);\n var conversion = {};\n var models = Object.keys(graph);\n\n for (var len = models.length, i = 0; i < len; i++) {\n var toModel = models[i];\n var node = graph[toModel];\n\n if (node.parent === null) {\n // no possible conversion, or this node is the source model.\n continue;\n }\n\n conversion[toModel] = wrapConversion(toModel, graph);\n }\n\n return conversion;\n};","map":{"version":3,"sources":["C:/Users/kkwan_000/Desktop/git/2017110269/minsung/node_modules/color-convert/route.js"],"names":["conversions","require","buildGraph","graph","models","Object","keys","len","length","i","distance","parent","deriveBFS","fromModel","queue","current","pop","adjacents","adjacent","node","unshift","link","from","to","args","wrapConversion","toModel","path","fn","cur","conversion","module","exports"],"mappings":"AAAA,IAAIA,WAAW,GAAGC,OAAO,CAAC,eAAD,CAAzB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AAEA,SAASC,UAAT,GAAsB;AACrB,MAAIC,KAAK,GAAG,EAAZ,CADqB,CAErB;;AACA,MAAIC,MAAM,GAAGC,MAAM,CAACC,IAAP,CAAYN,WAAZ,CAAb;;AAEA,OAAK,IAAIO,GAAG,GAAGH,MAAM,CAACI,MAAjB,EAAyBC,CAAC,GAAG,CAAlC,EAAqCA,CAAC,GAAGF,GAAzC,EAA8CE,CAAC,EAA/C,EAAmD;AAClDN,IAAAA,KAAK,CAACC,MAAM,CAACK,CAAD,CAAP,CAAL,GAAmB;AAClB;AACA;AACAC,MAAAA,QAAQ,EAAE,CAAC,CAHO;AAIlBC,MAAAA,MAAM,EAAE;AAJU,KAAnB;AAMA;;AAED,SAAOR,KAAP;AACA,C,CAED;;;AACA,SAASS,SAAT,CAAmBC,SAAnB,EAA8B;AAC7B,MAAIV,KAAK,GAAGD,UAAU,EAAtB;AACA,MAAIY,KAAK,GAAG,CAACD,SAAD,CAAZ,CAF6B,CAEJ;;AAEzBV,EAAAA,KAAK,CAACU,SAAD,CAAL,CAAiBH,QAAjB,GAA4B,CAA5B;;AAEA,SAAOI,KAAK,CAACN,MAAb,EAAqB;AACpB,QAAIO,OAAO,GAAGD,KAAK,CAACE,GAAN,EAAd;AACA,QAAIC,SAAS,GAAGZ,MAAM,CAACC,IAAP,CAAYN,WAAW,CAACe,OAAD,CAAvB,CAAhB;;AAEA,SAAK,IAAIR,GAAG,GAAGU,SAAS,CAAC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sCD,EAAtC,CAAT;AACAC,IAAAA,GAAG,GAAG1B,KAAK,CAAC0B,GAAD,CAAL,CAAWlB,MAAjB;AACA;;AAEDiB,EAAAA,EAAE,CAACE,UAAH,GAAgBH,IAAhB;AACA,SAAOC,EAAP;AACA;;AAEDG,MAAM,CAACC,OAAP,GAAiB,UAAUnB,SAAV,EAAqB;AACrC,MAAIV,KAAK,GAAGS,SAAS,CAACC,SAAD,CAArB;AACA,MAAIiB,UAAU,GAAG,EAAjB;AAEA,MAAI1B,MAAM,GAAGC,MAAM,CAACC,IAAP,CAAYH,KAAZ,CAAb;;AACA,OAAK,IAAII,GAAG,GAAGH,MAAM,CAACI,MAAjB,EAAyBC,CAAC,GAAG,CAAlC,EAAqCA,CAAC,GAAGF,GAAzC,EAA8CE,CAAC,EAA/C,EAAmD;AAClD,QAAIiB,OAAO,GAAGtB,MAAM,CAACK,CAAD,CAApB;AACA,QAAIU,IAAI,GAAGhB,KAAK,CAACuB,OAAD,CAAhB;;AAEA,QAAIP,IAAI,CAACR,MAAL,KAAgB,IAApB,EAA0B;AACzB;AACA;AACA;;AAEDmB,IAAAA,UAAU,CAACJ,OAAD,CAAV,GAAsBD,cAAc,CAACC,OAAD,EAAUvB,KAAV,CAApC;AACA;;AAED,SAAO2B,UAAP;AACA,CAlBD","sourcesContent":["var conversions = require('./conversions');\n\n/*\n\tthis function routes a model to all other models.\n\n\tall functions that are routed have a property `.conversion` attached\n\tto the returned synthetic function. This property is an array\n\tof strings, each with the steps in between the 'from' and 'to'\n\tcolor models (inclusive).\n\n\tconversions that are not possible simply are not included.\n*/\n\nfunction buildGraph() {\n\tvar graph = {};\n\t// https://jsperf.com/object-keys-vs-for-in-with-closure/3\n\tvar models = Object.keys(conversions);\n\n\tfor (var len = models.length, i = 0; i < len; i++) {\n\t\tgraph[models[i]] = {\n\t\t\t// http://jsperf.com/1-vs-infinity\n\t\t\t// micro-opt, but this is simple.\n\t\t\tdistance: -1,\n\t\t\tparent: null\n\t\t};\n\t}\n\n\treturn graph;\n}\n\n// https://en.wikipedia.org/wiki/Breadth-first_search\nfunction deriveBFS(fromModel) {\n\tvar graph = buildGraph();\n\tvar queue = [fromModel]; // unshift -> queue -> pop\n\n\tgraph[fromModel].distance = 0;\n\n\twhile (queue.length) {\n\t\tvar current = queue.pop();\n\t\tvar adjacents = Object.keys(conversions[current]);\n\n\t\tfor (var len = adjacents.length, i = 0; i < len; i++) {\n\t\t\tvar adjacent = adjacents[i];\n\t\t\tvar node = graph[adjacent];\n\n\t\t\tif (node.distance === -1) {\n\t\t\t\tnode.distance = graph[current].distance + 1;\n\t\t\t\tnode.parent = current;\n\t\t\t\tqueue.unshift(adjacent);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn graph;\n}\n\nfunction link(from, to) {\n\treturn function (args) {\n\t\treturn to(from(args));\n\t};\n}\n\nfunction wrapConversion(toModel, graph) {\n\tvar path = [graph[toModel].parent, toModel];\n\tvar fn = conversions[graph[toModel].parent][toModel];\n\n\tvar cur = graph[toModel].parent;\n\twhile (graph[cur].parent) {\n\t\tpath.unshift(graph[cur].parent);\n\t\tfn = link(conversions[graph[cur].parent][cur], fn);\n\t\tcur = graph[cur].parent;\n\t}\n\n\tfn.conversion = path;\n\treturn fn;\n}\n\nmodule.exports = function (fromModel) {\n\tvar graph = deriveBFS(fromModel);\n\tvar conversion = {};\n\n\tvar models = Object.keys(graph);\n\tfor (var len = models.length, i = 0; i < len; i++) {\n\t\tvar toModel = models[i];\n\t\tvar node = graph[toModel];\n\n\t\tif (node.parent === null) {\n\t\t\t// no possible conversion, or this node is the source model.\n\t\t\tcontinue;\n\t\t}\n\n\t\tconversion[toModel] = wrapConversion(toModel, graph);\n\t}\n\n\treturn conversion;\n};\n\n"]},"metadata":{},"sourceType":"script"}