From ac049057495e1d39f3f6fff047b2ef7bfbfc2456 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Wed, 31 Dec 2025 17:41:50 +0000 Subject: [PATCH] [Sync Iteration] javascript/recycling-robot/1 --- .../recycling-robot/1/assembly-line.js | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 solutions/javascript/recycling-robot/1/assembly-line.js diff --git a/solutions/javascript/recycling-robot/1/assembly-line.js b/solutions/javascript/recycling-robot/1/assembly-line.js new file mode 100644 index 0000000..e9fde99 --- /dev/null +++ b/solutions/javascript/recycling-robot/1/assembly-line.js @@ -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'; +}