Skip to content
Open
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
129 changes: 129 additions & 0 deletions solutions/javascript/recycling-robot/1/assembly-line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// @ts-check
//
// The line above enables type checking for this file. Various IDEs interpret
// the @ts-check directive. It will give you helpful autocompletion when
// implementing this exercise.

import { ElectronicDevice } from './lib.js';

/**
* Checks if input is a boolean.
*
* @param {unknown} value
* @returns {boolean} whether the input is a boolean
*/
export function isBoolean(value) {
return typeof value === 'boolean';
}

/**
* Checks if input is a finite number or bigint.
*
* @param {unknown} value
* @returns {boolean} whether the input is a finite number or bigint
*/
export function isNumber(value) {
if (typeof value === 'bigint') return true;
if (typeof value === 'number') return Number.isFinite(value);
return false;
}

/**
* Checks if a value is an object.
*
* @param {unknown} value
* @returns {boolean} whether the input is an object.
*/
export function isObject(value) {
return typeof value === 'object' && value !== null;
}

/**
* Checks if a value is a numeric string.
*
* @param {unknown} value
* @returns {boolean} whether the input is a numeric string.
*/
export function isNumericString(value) {
return typeof value === 'string' && /^-?\d+$/.test(value);
}

/**
* Checks if an object is an instance of the `ElectronicDevice` class or one of its children.
*
* @param {object} object
* @returns {boolean} whether the object is an instance of the `ElectronicDevice` class or one of its children.
*/
export function isElectronic(object) {
return object instanceof ElectronicDevice;
}

/**
* Checks if a value is a non empty array.
*
* @param {unknown} value
* @returns {boolean} whether the input is a non empty array.
*/
export function isNonEmptyArray(value) {
return Array.isArray(value) && value.length > 0;
}

/**
* Checks if a value is an empty array.
*
* @param {unknown} value
* @returns {boolean} whether the input is an empty array.
*/
export function isEmptyArray(value) {
return Array.isArray(value) && value.length === 0;
}

/**
* Checks if a value has a "type" property or method.
*
* @param {object} object
* @returns {boolean} whether the input has a "type" property or method.
*/
export function hasType(object) {
return 'type' in object;
}

/**
* Throws an error if an object is missing an "id" property or method.
*
* @param {object} object
* @returns {never|void} undefined if the input has an "id" property or method, otherwise throws an error.
*/
export function assertHasId(object) {
if (!('id' in object)) {
throw new Error("Object is missing the 'id' property");
}
}

/**
* Checks if a value has an "id" property.
*
* @param {object} object
* @returns {boolean} whether the input has an "id" property.
*/
export function hasIdProperty(object) {
if (!Object.hasOwn(object, 'id')) return false;

const desc = Object.getOwnPropertyDescriptor(object, 'id');
if (!desc) return false;

// Data property has a "value". Accessors have "get"/"set" instead.
return 'value' in desc;
}

/**
* Checks if a value has a defined "type" property.
*
* @param {object} object
* @returns {boolean} whether the input has a defined "type" property.
*/
export function hasDefinedType(object) {
return Object.hasOwn(object, 'type') &&
object.type !== undefined &&
typeof object.type !== 'function';
}