MCPcopy
hub / github.com/josdejong/mathjs / sortFactories

Function sortFactories

tools/entryGenerator.js:495–538  ·  view source on GitHub ↗
(factories)

Source from the content-addressed store, hash-verified

493 * @return {Array} Returns sorted factories
494 */
495export function sortFactories (factories) {
496 const loaded = {}
497 const leftOverFactories = factories.slice()
498 const sortedFactories = []
499
500 const exists = {}
501 factories.forEach(factory => {
502 exists[factory.fn] = true
503 })
504
505 function allDependenciesResolved (dependencies) {
506 return dependencies.every(dependency => {
507 const d = stripOptionalNotation(dependency)
508 return loaded[d] === true ||
509 IGNORED_DEPENDENCIES[d] === true ||
510 (dependency[0] === '?' && !exists[d])
511 })
512 }
513
514 let changed = true
515 while (leftOverFactories.length > 0 && changed) {
516 changed = false
517
518 for (let i = 0; i < leftOverFactories.length; i++) {
519 const factory = leftOverFactories[i]
520 if (allDependenciesResolved(factory.dependencies)) {
521 if (!isTransform(factory)) {
522 loaded[factory.fn] = true
523 }
524 sortedFactories.push(factory)
525 leftOverFactories.splice(i, 1)
526 changed = true
527 }
528 }
529 }
530
531 if (leftOverFactories.length > 0) {
532 const first = leftOverFactories[0]
533 throw new Error('Cannot resolve dependencies of factory "' + first.fn + '". ' +
534 'Dependencies: ' + first.dependencies.map(d => '"' + d + '"').join(', '))
535 }
536
537 return sortedFactories
538}
539
540function isTransform (factory) {
541 return (factory && factory.meta && factory.meta.isTransformFunction === true) || false

Callers 2

factory.test.jsFile · 0.90
generateFunctionsFilesFunction · 0.70

Calls 4

allDependenciesResolvedFunction · 0.85
isTransformFunction · 0.85
forEachMethod · 0.65
mapMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…