Skip to content
This repository has been archived by the owner on Jun 30, 2020. It is now read-only.

Commit

Permalink
in stacking added back exceptions when items
Browse files Browse the repository at this point in the history
feature removed during reimplementation 6f9fee6
  • Loading branch information
grahamdyson committed Jul 6, 2019
1 parent 7cb2800 commit ead196d
Show file tree
Hide file tree
Showing 12 changed files with 603 additions and 216 deletions.
16 changes: 14 additions & 2 deletions .vscode/launch.json
Expand Up @@ -249,6 +249,18 @@
"request": "launch",
"type": "node"
},
{
"args":
[
"--runInBand",
"stacking/createOrAddToStacksUniformly/test.js"
],
"disableOptimisticBPs": true,
"name": "stacking createOrAddToStacksUniformly jest",
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
"request": "launch",
"type": "node"
},
{
"args":
[
Expand Down Expand Up @@ -276,10 +288,10 @@
"args":
[
"--runInBand",
"stacking/createStackWhenIdentifierOrItemOrLevelOrAddWhenStack/createGetItemWithIdentifier/test.js"
"stacking/createStackWhenIdentifierOrItemOrLevelOrAddWhenStack/createItemLookup/test.js"
],
"disableOptimisticBPs": true,
"name": "stacking createStackWhenIdentifierOrItemOrLevelOrAddWhenStack createGetItemWithIdentifier jest",
"name": "stacking createStackWhenIdentifierOrItemOrLevelOrAddWhenStack createItemLookup jest",
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
"request": "launch",
"type": "node"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -90,5 +90,5 @@
"spellcheck": "find -type f \\( -name \"*.js\" -or -name \"*.md\" \\) ! -path \"./dist/*\" ! -path \"./dist/**/*\" ! -path \"./node_modules/**/*\" ! -path \"./test-coverage/**/*\" -exec npx cspell {} +",
"test": "npx jest --runInBand"
},
"version": "6.0.2"
"version": "6.1.0"
}

This file was deleted.

This file was deleted.

@@ -0,0 +1,78 @@
/* Copyright (c) 2019 Graham Dyson. All Rights Reserved.
This library is free software, licensed under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */

require("array.prototype.flatMap")
.shim();

module.exports =
levelOrStack => {
const itemsByIdentifier = new Map(generateKeyValuePairs());

return (
{
getIdentifiersNotUsed,
getItemWithIdentifier,
}
);

function * generateKeyValuePairs() {
for (const identifierOrItemOrLevel of levelOrStack)
if (Array.isArray(identifierOrItemOrLevel))
for (const identifierOrItem of identifierOrItemOrLevel)
yield getKeyValuePairForIdentifierOrItem(identifierOrItem);
else
yield getKeyValuePairForIdentifierOrItem(identifierOrItemOrLevel);
}

function getIdentifiersNotUsed() {
return (
getIterableAsArrayWhenHasItems(generate())
||
null
);

function * generate() {
for (const { isUsed, item } of itemsByIdentifier.values())
if (!isUsed)
yield getIdentifier(item);
}

function getIterableAsArrayWhenHasItems(
iterable,
) {
const array = [ ...iterable ];

return array.length && array;
}
}

function getItemWithIdentifier(
identifier,
) {
const itemAndIsUsed = itemsByIdentifier.get(identifier);

if (itemAndIsUsed) {
itemAndIsUsed.isUsed = true;

return itemAndIsUsed.item;
} else
return null;
}
};

function getKeyValuePairForIdentifierOrItem(
identifierOrItem,
) {
return (
[
getIdentifier(identifierOrItem),
{ item: identifierOrItem },
]
);
}

function getIdentifier(
identifierOrItem,
) {
return identifierOrItem.id || identifierOrItem;
}
@@ -0,0 +1,32 @@
/* Copyright (c) 2019 Graham Dyson. All Rights Reserved.
This library is free software, licensed under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */

module.exports =
({
identifier,
item,
}) => {
let used = false;

return (
{
getIdentifiersNotUsed,
getItemWithIdentifier,
}
);

function getIdentifiersNotUsed() {
return used ? null : [ identifier ];
}

function getItemWithIdentifier(
targetIdentifier,
) {
if (identifier === targetIdentifier) {
used = true;

return item;
} else
return null;
}
};
@@ -0,0 +1,54 @@
/* Copyright (c) 2019 Graham Dyson. All Rights Reserved.
This library is free software, licensed under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */

const
createForLevelOrStack = require("./createForLevelOrStack"),
createForSingle = require("./createForSingle");

module.exports =
identifierOrItemOrLevelOrStack => {
return (
whenNoValue()
||
whenLevelOrStack()
||
whenItem()
||
createForSingle({
identifier: identifierOrItemOrLevelOrStack,
item: identifierOrItemOrLevelOrStack,
})
);

function whenNoValue() {
return (
!identifierOrItemOrLevelOrStack
&&
{
getIdentifiersNotUsed: () => null,
// identifier parameter intentionally ignored as there are no items
// eslint-disable-next-line no-unused-vars
getItemWithIdentifier: identifier => null,
}
);
}

function whenLevelOrStack() {
return (
Array.isArray(identifierOrItemOrLevelOrStack)
&&
createForLevelOrStack(identifierOrItemOrLevelOrStack)
);
}

function whenItem() {
return (
identifierOrItemOrLevelOrStack.id
&&
createForSingle({
identifier: identifierOrItemOrLevelOrStack.id,
item: identifierOrItemOrLevelOrStack,
})
);
}
};

0 comments on commit ead196d

Please sign in to comment.