Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 0 additions & 107 deletions src/addData.js

This file was deleted.

112 changes: 111 additions & 1 deletion src/addTypeDict.js → src/data.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,110 @@
function addTypeDict(p5, fn){
function addData(p5, fn){
fn.append = function (array, value) {
array.push(value);
return array;
};

fn.arrayCopy = function (src, srcPosition, dst, dstPosition, length) {
// the index to begin splicing from dst array
let start;
let end;

if (typeof length !== 'undefined') {
end = Math.min(length, src.length);
start = dstPosition;
src = src.slice(srcPosition, end + srcPosition);
} else {
if (typeof dst !== 'undefined') {
// src, dst, length
// rename so we don't get confused
end = dst;
end = Math.min(end, src.length);
} else {
// src, dst
end = src.length;
}

start = 0;
// rename so we don't get confused
dst = srcPosition;
src = src.slice(0, end);
}

// Since we are not returning the array and JavaScript is pass by reference
// we must modify the actual values of the array
// instead of reassigning arrays
Array.prototype.splice.apply(dst, [start, end].concat(src));
};

fn.concat = (list0, list1) => list0.concat(list1);

fn.reverse = list => list.reverse();

fn.shorten = function (list) {
list.pop();
return list;
};

fn.sort = function (list, count) {
let arr = count ? list.slice(0, Math.min(count, list.length)) : list;
const rest = count ? list.slice(Math.min(count, list.length)) : [];
if (typeof arr[0] === 'string') {
arr = arr.sort();
} else {
arr = arr.sort((a, b) => a - b);
}
return arr.concat(rest);
};

fn.splice = function (list, value, index) {
// note that splice returns spliced elements and not an array
Array.prototype.splice.apply(list, [index, 0].concat(value));

return list;
};

fn.subset = function (list, start, count) {
if (typeof count !== 'undefined') {
return list.slice(start, start + count);
} else {
return list.slice(start, list.length);
}
};

fn.join = function(list, separator) {
return list.join(separator);
};

fn.match = function(str, reg) {
return str.match(reg);
};

fn.matchAll = function(str, reg) {
const re = new RegExp(reg, 'g');
let match = re.exec(str);
const matches = [];
while (match !== null) {
matches.push(match);
// matched text: match[0]
// match start: match.index
// capturing group n: match[n]
match = re.exec(str);
}
return matches;
};

fn.split = function(str, delim) {
return str.split(delim);
};

fn.trim = function(str) {
if (str instanceof Array) {
return str.map(this.trim);
} else {
return str.trim();
}
};

fn.createStringDict = function (key, value) {
return new p5.StringDict(key, value);
};
Expand Down Expand Up @@ -196,4 +302,8 @@ function addTypeDict(p5, fn){
return this._keyTest(-1);
}
};
}

if (typeof p5 !== undefined) {
p5.registerAddon(addData);
}