Skip to content

Commit f1bcfd7

Browse files
committed
0.2.0 (base function set w/o timedLoops)
1 parent 5601e6f commit f1bcfd7

File tree

15 files changed

+186
-31
lines changed

15 files changed

+186
-31
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
node_modules
2-
package-lock.json
2+
package-lock.json
3+
tests
4+
5+
# WIP functions
6+
bin/stopTimedLoop.js
7+
bin/timedLoop.js

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ code-org is a **WIP** module to allow utilization of JavaScript functions from [
1313
- [Code.org App Lab Docs](https://studio.code.org/docs/applab/)
1414
*Our endgoal for this project is to have all relevant functions added to this module from App Lab*
1515
- [GitHub](https://github.com/csokolove/code-org)
16+
- [Project Map](https://github.com/users/csokolove/projects/1/views/1)
1617

1718
## Contributing
1819

bin/addPair.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Add a new key/value pair to an existing object.
3+
* @param {Object} object The object to add the key and value pair.
4+
* @param {String} key The name of the key to look up the value for in the object.
5+
* @param {*} value The value to store in the object under the key provided.
6+
* @link https://studio.code.org/docs/applab/addPair/
7+
*/
8+
9+
const addPair = (object, key, value) => {
10+
if(!arguments[2]) throw new Error('All parameters are required');
11+
if (typeof(object) !== 'object') throw new TypeError('The object must be a valid object');
12+
if (typeof(key) !== 'string') throw new TypeError('The key must be a valid string');
13+
14+
object[key] = value;
15+
}
16+
17+
module.exports = addPair;

bin/appendItem.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Appends an item to the end of an array.
3+
* @param {Object} array The variable name of the array (list) you want to append to the end of.
4+
* @param {*} item The number or string item to be inserted at the end of the list.
5+
* @link https://studio.code.org/docs/applab/appendItem/
6+
*/
7+
8+
const appendItem = (array, item) => {
9+
if (!arguments[1]) throw new Error('All parameters are required');
10+
if (typeof(array) !== 'object') throw new TypeError('Array must be referenced as a variable');
11+
12+
// JavaScript will natively catch if the array referenced is undefined
13+
14+
array.splice(array.length, 0, item)
15+
}
16+
17+
module.exports = appendItem;

bin/getTime.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
2-
*
3-
* @returns Number of milliseconds elapsed since UNIX Epoch (January 1, 1970)
2+
* Gets the number of milliseconds elapsed since January 1, 1970.
3+
* @returns Returns the number of milliseconds elapsed since January 1, 1970.
4+
* @link https://studio.code.org/docs/applab/getTime/
45
*/
56

67
const getTime = () => {

bin/getValue.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Gets the value associated with a specific `key` in an object.
3+
* @param {Object} object The object to access the value from.
4+
* @param {String} key The name of the key to look up the value for in `object`.
5+
* @link https://studio.code.org/docs/applab/getValue/
6+
*/
7+
8+
const getValue = (object, key) => {
9+
if (typeof(object) !== 'object') throw new TypeError('The object must be a valid object');
10+
if (typeof(key) !== 'string') throw new TypeError('The key must be a valid string');
11+
12+
return object[key];
13+
}
14+
15+
module.exports = getValue;

bin/insertItem.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Inserts an item into an array at the specified index position.
3+
* @param {Object} array The variable name of the array (list) you want to insert the item into.
4+
* @param {Number} index The index position you want to insert the item at.
5+
* @param {*} item The number or string item to be inserted into the list.
6+
* @link https://studio.code.org/docs/applab/insertItem/
7+
*/
8+
9+
const insertItem = (array, index, item) => {
10+
if (!arguments[2]) throw new Error('All parameters are required');
11+
if (typeof(array) !== 'object') throw new TypeError('Array must be referenced as a variable');
12+
13+
// JavaScript will natively catch if the array referenced is undefined
14+
15+
if (isNaN(index)) {
16+
index = parseInt(index)
17+
if (isNaN(index)) {
18+
array.unshift(item);
19+
} else {
20+
array.splice(index, 0, item);
21+
}
22+
} else {
23+
array.splice(index, 0, item)
24+
}
25+
}
26+
27+
module.exports = insertItem;

bin/open.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const openPkg = require('open');
2+
const colors = require('colors');
3+
4+
/**
5+
* Open a URL in the computer's default browser
6+
* @param {String} url - The URL to open
7+
* @example https://code.org (valid URL)
8+
* @example www.code.org (gray area, providing a protocol is recommended)
9+
* @example code.org (invalid URL)
10+
* @link https://studio.code.org/docs/applab/open/
11+
*/
12+
13+
const open = async (url) => {
14+
if (typeof(url) !== 'string') throw new TypeError('URL must be a string');
15+
16+
try {
17+
new URL(url);
18+
console.log(colors.yellow(`WARNING: The function open() is opening a URL in your default browser (${url})`))
19+
await openPkg(url);
20+
} catch (_) {
21+
console.log(`Attempting to open requested URL (${url}), however it may be malformed. It is highly recommended to provide a protocol in the URL`)
22+
await openPkg(url);
23+
}
24+
}
25+
26+
module.exports = open;

bin/prompt.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const readline = require('readline');
2+
const util = require('util');
3+
4+
/**
5+
* Prompts the user for a value
6+
* @async Utilize IIFE to immediately call the function.
7+
* @param query - Value that will be sent to the user when prompted to answer
8+
* @returns Input received from the user
9+
* @example (async () => {
10+
var firstName = await prompt("What's your famous person's first name?");
11+
var lastName = await prompt("What's your famous person's last name?");
12+
console.log("Hi " + firstName + " " + lastName);
13+
})();
14+
* @link https://studio.code.org/docs/applab/declareAssign_x_prompt/
15+
*/
16+
17+
const prompt = async (query) => {
18+
if (!query) throw new SyntaxError('A query is required to run prompt()');
19+
20+
const rl = readline.createInterface({
21+
input: process.stdin,
22+
output: process.stdout
23+
});
24+
25+
const question = util.promisify(rl.question).bind(rl);
26+
27+
try {
28+
let res = await question(query);
29+
rl.close();
30+
31+
return res;
32+
} catch (err) {
33+
throw new Error(`Unable to run prompt() ${err}`);
34+
}
35+
}
36+
37+
module.exports = prompt;

bin/randomNumber.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/**
2+
* Returns a random number in the closed range from `min` to `max`.
23
* @param {Number} [min=0] - The minimum number returned
34
* @param {Number} max - The maximum number returned
4-
* @returns Random number in the closed range from min to max
5+
* @returns Returns a random number in the range min to max (inclusive). The number returned will always be an integer.
6+
* @link https://studio.code.org/docs/applab/randomNumber/
57
*/
68

79
const randomNumber = (min, max) => {
@@ -10,14 +12,14 @@ const randomNumber = (min, max) => {
1012
min = 0;
1113
max = 0;
1214
} else {
13-
throw new Error(`${max ? 'Min' : 'Max'} must be a number`);
15+
throw new TypeError(`${max ? 'Min' : 'Max'} must be a number`);
1416
}
1517
}
1618

1719
if (max === undefined) {
1820
max = min;
1921
min = 0;
20-
} else if (typeof(max) !== 'number') throw new Error('Max must be a number')
22+
} else if (typeof(max) !== 'number') throw new TypeError('Max must be a number')
2123

2224
return Math.floor(Math.random() * (max - min + 1) + min);
2325
}

0 commit comments

Comments
 (0)