11a87fdec1b56bc2552b1a39aac721bc.json 7.9 KB

1
  1. {"ast":null,"code":"const 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 const graph = {}; // https://jsperf.com/object-keys-vs-for-in-with-closure/3\n\n const models = Object.keys(conversions);\n\n for (let 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 const graph = buildGraph();\n const queue = [fromModel]; // Unshift -> queue -> pop\n\n graph[fromModel].distance = 0;\n\n while (queue.length) {\n const current = queue.pop();\n const adjacents = Object.keys(conversions[current]);\n\n for (let len = adjacents.length, i = 0; i < len; i++) {\n const adjacent = adjacents[i];\n const 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 const path = [graph[toModel].parent, toModel];\n let fn = conversions[graph[toModel].parent][toModel];\n let 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 const graph = deriveBFS(fromModel);\n const conversion = {};\n const models = Object.keys(graph);\n\n for (let len = models.length, i = 0; i < len; i++) {\n const toModel = models[i];\n const 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":["/Users/jane/Documents/Курс Front-end/HW8/myproject/node_modules/pretty-format/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,MAAMA,WAAW,GAAGC,OAAO,CAAC,eAAD,CAA3B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AAEA,SAASC,UAAT,GAAsB;AACrB,QAAMC,KAAK,GAAG,EAAd,CADqB,CAErB;;AACA,QAAMC,MAAM,GAAGC,MAAM,CAACC,IAAP,CAAYN,WAAZ,CAAf;;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,QAAMV,KAAK,GAAGD,UAAU,EAAxB;AACA,QAAMY,KAAK,GAAG,CAACD,SAAD,CAAd,CAF6B,CAEF;;AAE3BV,EAAAA,KAAK,CAACU,SAAD,CAAL,CAAiBH,QAAjB,GAA4B,CAA5B;;AAEA,SAAOI,KAAK,CAACN,MAAb,EAAqB;AACpB,UAAMO,OAAO,GAAGD,KAAK,CAACE,GAAN,EAAhB;AACA,UAAMC,SAAS,GAAGZ,MAAM,CAACC,IAAP,CAAYN,WAAW,CAACe,OAAD,CAAvB,CAAlB;;AAEA,SAAK,IAAIR,GAAG,GA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A/B,CAAD,EAAsCD,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,QAAMV,KAAK,GAAGS,SAAS,CAACC,SAAD,CAAvB;AACA,QAAMiB,UAAU,GAAG,EAAnB;AAEA,QAAM1B,MAAM,GAAGC,MAAM,CAACC,IAAP,CAAYH,KAAZ,CAAf;;AACA,OAAK,IAAII,GAAG,GAAGH,MAAM,CAACI,MAAjB,EAAyBC,CAAC,GAAG,CAAlC,EAAqCA,CAAC,GAAGF,GAAzC,EAA8CE,CAAC,EAA/C,EAAmD;AAClD,UAAMiB,OAAO,GAAGtB,MAAM,CAACK,CAAD,CAAtB;AACA,UAAMU,IAAI,GAAGhB,KAAK,CAACuB,OAAD,CAAlB;;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":["const 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\tconst graph = {};\n\t// https://jsperf.com/object-keys-vs-for-in-with-closure/3\n\tconst models = Object.keys(conversions);\n\n\tfor (let 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\tconst graph = buildGraph();\n\tconst queue = [fromModel]; // Unshift -> queue -> pop\n\n\tgraph[fromModel].distance = 0;\n\n\twhile (queue.length) {\n\t\tconst current = queue.pop();\n\t\tconst adjacents = Object.keys(conversions[current]);\n\n\t\tfor (let len = adjacents.length, i = 0; i < len; i++) {\n\t\t\tconst adjacent = adjacents[i];\n\t\t\tconst 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\tconst path = [graph[toModel].parent, toModel];\n\tlet fn = conversions[graph[toModel].parent][toModel];\n\n\tlet 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\tconst graph = deriveBFS(fromModel);\n\tconst conversion = {};\n\n\tconst models = Object.keys(graph);\n\tfor (let len = models.length, i = 0; i < len; i++) {\n\t\tconst toModel = models[i];\n\t\tconst 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"}