UNPKG

gdo

Version:

Group- and Dependency-based Ordering

29 lines (27 loc) 6.9 kB
/* ** GDO -- Group- and Dependency-based Ordering ** Copyright (c) 2015-2023 Dr. Ralf S. Engelschall <rse@engelschall.com> ** ** Permission is hereby granted, free of charge, to any person obtaining ** a copy of this software and associated documentation files (the ** "Software"), to deal in the Software without restriction, including ** without limitation the rights to use, copy, modify, merge, publish, ** distribute, sublicense, and/or sell copies of the Software, and to ** permit persons to whom the Software is furnished to do so, subject to ** the following conditions: ** ** The above copyright notice and this permission notice shall be included ** in all copies or substantial portions of the Software. ** ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.GDO = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(_dereq_,module,exports){ "use strict";var _toposort=_interopRequireDefault(_dereq_("toposort"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _slicedToArray(e,r){return _arrayWithHoles(e)||_iterableToArrayLimit(e,r)||_unsupportedIterableToArray(e,r)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(e,r){if(e){if("string"==typeof e)return _arrayLikeToArray(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_arrayLikeToArray(e,r):void 0}}function _arrayLikeToArray(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function _iterableToArrayLimit(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var n,o,a,i,u=[],c=!0,l=!1;try{if(a=(t=t.call(e)).next,0===r){if(Object(t)!==t)return;c=!1}else for(;!(c=(n=a.call(t)).done)&&(u.push(n.value),u.length!==r);c=!0);}catch(e){l=!0,o=e}finally{try{if(!c&&null!=t.return&&(i=t.return(),Object(i)!==i))return}finally{if(l)throw o}}return u}}function _arrayWithHoles(e){if(Array.isArray(e))return e}function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _classCallCheck(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,_toPropertyKey(n.key),n)}}function _createClass(e,r,t){return r&&_defineProperties(e.prototype,r),t&&_defineProperties(e,t),Object.defineProperty(e,"prototype",{writable:!1}),e}function _toPropertyKey(e){var r=_toPrimitive(e,"string");return"symbol"===_typeof(r)?r:String(r)}function _toPrimitive(e,r){if("object"!==_typeof(e)||null===e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var n=t.call(e,r||"default");if("object"!==_typeof(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(e)}var GDO=function(){function e(){_classCallCheck(this,e),this.reset()}return _createClass(e,[{key:"reset",value:function(){this._groups=[],this._elements=[]}},{key:"groups",value:function(e){this._groups=e}},{key:"element",value:function(e){this._elements.push(e)}},{key:"order",value:function(){var e=this,r={};this._elements.forEach(function(e){if(r[e.name])throw new Error('element named "'.concat(e.name,'" occurs multiple times (has to be unique)'));r[e.name]=!0});var t={},n={},o={},a={},i={};this._groups.forEach(function(e){if(o[e])throw new Error('group named "'.concat(e,'" occurs multiple times (has to be unique)'));o[e]=["@@@".concat(e)],r["@@@".concat(e)]=!0});var u=function(e){return"object"===_typeof(e)&&e instanceof Array?e:"string"==typeof e?[e]:[]};this._elements.forEach(function(r){var t=r.name,c=u(r.tag),l=u(r.before),f=u(r.after),s=r.group;if(a[t]=[].concat(l),i[t]=[].concat(f),c.forEach(function(o){if(e._groups.indexOf(o)>-1)throw new Error('element "'.concat(r.name,'" has invalid tag "').concat(o,'" ')+"(tag cannot have same name as existing group)");void 0===n[o]&&(n[o]=[]),n[o].push(t)}),void 0!==s){var y=e._groups.indexOf(s);if(-1===y)throw new Error('element "'.concat(r.name,'" has invalid group "').concat(s,'" ')+"(group has to be explicitly defined)");o[s].push(t),y<e._groups.length-1&&a[t].push(e._groups[y+1]),y>0&&i[t].push(e._groups[y-1])}});var c=function(e,a,i){a.forEach(function(a){var u,c;void 0!==n[a]?(u=n[a],c="tag-based"):void 0!==o[a]?(u=o[a],c="group-based"):(u=[a],c="direct"),u.forEach(function(n){var o=_slicedToArray(i(e,n),2),a=o[0],u=o[1];if(void 0===r[a])throw new Error('element "'.concat(e,'" has invalid ').concat(c," before-reference ")+'to unknown element "'.concat(a,'"'));if(void 0===r[u])throw new Error('element "'.concat(e,'" has invalid ').concat(c," after-reference ")+'to unknown element "'.concat(u,'"'));void 0===t[a]&&(t[a]={}),t[a][u]=!0})})};this._elements.forEach(function(e){var r=e.name,t=a[r],n=i[r];c(r,n,function(e,r){return[r,e]}),c(r,t,function(e,r){return[e,r]})});var l=[];Object.keys(t).forEach(function(e){Object.keys(t[e]).forEach(function(r){l.push([e,r])})});var f=_toposort.default.array(Object.keys(r),l);return f=f.filter(function(e){return!e.match(/^@@@.+/)})}}]),e}();module.exports=GDO; },{"toposort":"toposort"}]},{},[1])(1) });