This repository has been archived by the owner on Jun 30, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
in stacking added back exceptions when items
feature removed during reimplementation 6f9fee6
- Loading branch information
1 parent
7cb2800
commit ead196d
Showing
12 changed files
with
603 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 0 additions & 95 deletions
95
...createStackWhenIdentifierOrItemOrLevelOrAddWhenStack/createGetItemWithIdentifier/index.js
This file was deleted.
Oops, something went wrong.
78 changes: 0 additions & 78 deletions
78
.../createStackWhenIdentifierOrItemOrLevelOrAddWhenStack/createGetItemWithIdentifier/test.js
This file was deleted.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
...eStackWhenIdentifierOrItemOrLevelOrAddWhenStack/createItemLookup/createForLevelOrStack.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
32 changes: 32 additions & 0 deletions
32
.../createStackWhenIdentifierOrItemOrLevelOrAddWhenStack/createItemLookup/createForSingle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}; |
54 changes: 54 additions & 0 deletions
54
stacking/createStackWhenIdentifierOrItemOrLevelOrAddWhenStack/createItemLookup/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}) | ||
); | ||
} | ||
}; |
Oops, something went wrong.