Skip to content

Commit

Permalink
new 'text' list conversion selector
Browse files Browse the repository at this point in the history
recursively joins all text and number leaf elements with spaces in between, filtering out and ignoring whitespace
  • Loading branch information
jmoenig committed Nov 22, 2023
1 parent aec56b0 commit 6e00b17
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Snap! (BYOB) History

## in development:
* **New Features:**
* new 'text' list conversion selector, recursively joins all text and number leaf elements with spaces in between, filtering out and ignoring whitespace

2023-11-22
* new dev version for v9.0.15
* lists, blocks, threads: new 'text' list conversion selector, recursively joins all text and number leaf elements with spaces in between, filtering out and ignoring whitespace

## 9.0.14:
* **Notable Fixes:**
Expand Down
6 changes: 3 additions & 3 deletions snap.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
<script src="src/morphic.js?version=2023-11-07"></script>
<script src="src/symbols.js?version=2023-07-13"></script>
<script src="src/widgets.js?version=2023-05-24"></script>
<script src="src/blocks.js?version=2023-11-07"></script>
<script src="src/threads.js?version=2023-10-21"></script>
<script src="src/blocks.js?version=2023-11-22"></script>
<script src="src/threads.js?version=2023-10-22"></script>
<script src="src/objects.js?version=2023-11-02"></script>
<script src="src/scenes.js?version=2022-10-25"></script>
<script src="src/gui.js?version=2023-11-22"></script>
<script src="src/paint.js?version=2023-05-24"></script>
<script src="src/lists.js?version=2023-07-18"></script>
<script src="src/lists.js?version=2023-07-22"></script>
<script src="src/byob.js?version=2023-07-14"></script>
<script src="src/tables.js?version=2023-07-05"></script>
<script src="src/sketch.js?version=2023-05-24"></script>
Expand Down
3 changes: 2 additions & 1 deletion src/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ SVG_Costume, embedMetadataPNG, ThreadManager, snapEquals*/

// Global stuff ////////////////////////////////////////////////////////

modules.blocks = '2023-November-07';
modules.blocks = '2023-November-22';

var SyntaxElementMorph;
var BlockMorph;
Expand Down Expand Up @@ -499,6 +499,7 @@ SyntaxElementMorph.prototype.labelParts = {
'shuffled' : ['shuffled'],
'reverse' : ['reverse'],
'~' : null,
'text' : ['text'],
'lines' : ['lines'],
'csv' : ['csv'],
'json' : ['json']
Expand Down
21 changes: 19 additions & 2 deletions src/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Context, ZERO, WHITE*/

// Global settings /////////////////////////////////////////////////////

modules.lists = '2023-July-18';
modules.lists = '2023-November-22';

var List;
var ListWatcherMorph;
Expand Down Expand Up @@ -1081,10 +1081,27 @@ List.prototype.canBeTXT = function () {

List.prototype.asTXT = function () {
// Caution, no error catching!
// this method assumes that the list.canBeJSON()
// this method assumes that the list.canBeTXT()
return this.itemsArray().join('\n');
};

List.prototype.canBeWords = function () {
return this.itemsArray().every(item =>
isString(item) ||
(typeof item === 'number') ||
(item instanceof List && item.canBeWords())
);
};

List.prototype.asWords = function () {
// recursively join all leaf items with spaces between.
// Caution, no error catching!
// this method assumes that the list.canBeWords()
return this.itemsArray().map(each =>
each instanceof List ? each.asWords() : each.trim()
).filter(word => word.length).join(' ');
};

// List testing

List.prototype.equalTo = function (other) {
Expand Down
10 changes: 9 additions & 1 deletion src/threads.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ StagePickerMorph, CustomBlockDefinition, CommentMorph*/

/*jshint esversion: 11, bitwise: false, evil: true*/

modules.threads = '2023-November-21';
modules.threads = '2023-November-22';

var ThreadManager;
var Process;
Expand Down Expand Up @@ -2317,6 +2317,14 @@ Process.prototype.reportListAttribute = function (choice, list) {
case 'reverse':
this.assertType(list, 'list');
return list.reversed();
case 'text':
this.assertType(list, 'list');
if (list.canBeWords()) {
return list.asWords();
}
throw new Error(
localize('unable to convert to') + ' ' + localize('text')
);
case 'lines':
this.assertType(list, 'list');
if (list.canBeTXT()) {
Expand Down

0 comments on commit 6e00b17

Please sign in to comment.