You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
7.8 KiB

4 years ago
{"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/user/Documents/myapp/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;;;;;;;;;;;;AAWA,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,CAACT,MAApB,EAA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