Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Andrii Dubinchuk Exam #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
69 changes: 6 additions & 63 deletions libs/variant1.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,12 @@

export function isNumber (x) {
return typeof x === 'number'
}

export function isBigNumber (x) {
if (
!x || typeof x !== 'object' ||
typeof x.constructor !== 'function'
) {
return false
}

if (
x.isBigNumber === true &&
typeof x.constructor.prototype === 'object' &&
x.constructor.prototype.isBigNumber === true
) {
return true
}

if (
typeof x.constructor.isDecimal === 'function' &&
x.constructor.isDecimal(x) === true
) {
return true
}

return false
}

export function isComplex (x) {
function isComplex (x) {
return (x && typeof x === 'object' && Object.getPrototypeOf(x).isComplex === true) || false
}

export function isFraction (x) {
function isFraction (x) {
return (x && typeof x === 'object' && Object.getPrototypeOf(x).isFraction === true) || false
}

export function isUnit (x) {
return (x && x.constructor.prototype.isUnit === true) || false
}

export function isString (x) {
return typeof x === 'string'
}

export const isArray = Array.isArray

export function isMatrix (x) {
return (x && x.constructor.prototype.isMatrix === true) || false
}

/**
* Test whether a value is a collection: an Array or Matrix
* @param {*} x
* @returns {boolean} isCollection
*/
export function isCollection (x) {
return Array.isArray(x) || isMatrix(x)
}

export function isDenseMatrix (x) {
return (x && x.isDenseMatrix && x.constructor.prototype.isMatrix === true) || false
}

export function isSparseMatrix (x) {
return (x && x.isSparseMatrix && x.constructor.prototype.isMatrix === true) || false
}
module.exports = {
isComplex,
isFraction
}
36 changes: 25 additions & 11 deletions libs/variant2.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,59 @@
import {isComplex, isFraction} from "./variant1";
// import {isComplex, isFraction} from "./variant1";
const { isComplex, isFraction } = require("../libs/variant1");

export function isRange (x) {
function isRange(x) {
return (x && x.constructor.prototype.isRange === true) || false
}

export function isIndex (x) {
function isIndex(x) {
return (x && x.constructor.prototype.isIndex === true) || false
}

export function isBoolean (x) {
function isBoolean(x) {
return typeof x === 'boolean'
}

export function isResultSet (x) {
function isResultSet(x) {
return (x && x.constructor.prototype.isResultSet === true) || false
}

export function isHelp (x) {
function isHelp(x) {
return (x && x.constructor.prototype.isHelp === true) || false
}

export function isFunction (x) {
function isFunction(x) {
return typeof x === 'function'
}

export function isDate (x) {
function isDate(x) {
return x instanceof Date
}

export function isRegExp (x) {
function isRegExp(x) {
return x instanceof RegExp
}

export function isObject (x) {
function isObject(x) {
return !!(x &&
typeof x === 'object' &&
x.constructor === Object &&
!isComplex(x) &&
!isFraction(x))
}

export function isNull (x) {
function isNull(x) {
return x === null
}

module.exports = {
isRange,
isIndex,
isBoolean,
isResultSet,
isHelp,
isFunction,
isDate,
isRegExp,
isObject,
isNull
}
94 changes: 94 additions & 0 deletions libs/variant2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const {
isRange,
isIndex,
isBoolean,
isResultSet,
isHelp,
isFunction,
isDate,
isRegExp,
isObject,
isNull
} = require("../libs/variant2");

jest.mock("../libs/variant1", () => ({
isComplex: jest.fn(),
isFraction: jest.fn()
}));

const { isComplex, isFraction } = require("../libs/variant1");

describe("variant2.js utilities", () => {
test("isRange should identify objects with isRange prototype", () => {
const mockRange = { constructor: { prototype: { isRange: true } } };
expect(isRange(mockRange)).toBe(true);
expect(isRange({})).toBe(false);
});

test("isIndex should identify objects with isIndex prototype", () => {
const mockIndex = { constructor: { prototype: { isIndex: true } } };
expect(isIndex(mockIndex)).toBe(true);
expect(isIndex({})).toBe(false);
});

test("isBoolean should correctly identify boolean values", () => {
expect(isBoolean(true)).toBe(true);
expect(isBoolean(false)).toBe(true);
expect(isBoolean(0)).toBe(false);
expect(isBoolean("true")).toBe(false);
});

test("isResultSet should identify objects with isResultSet prototype", () => {
const mockResultSet = { constructor: { prototype: { isResultSet: true } } };
expect(isResultSet(mockResultSet)).toBe(true);
expect(isResultSet({})).toBe(false);
});

test("isHelp should identify objects with isHelp prototype", () => {
const mockHelp = { constructor: { prototype: { isHelp: true } } };
expect(isHelp(mockHelp)).toBe(true);
expect(isHelp({})).toBe(false);
});

test("isFunction should correctly identify functions", () => {
expect(isFunction(() => { })).toBe(true);
expect(isFunction(function () { })).toBe(true);
expect(isFunction(null)).toBe(false);
expect(isFunction({})).toBe(false);
});

test("isDate should correctly identify Date objects", () => {
expect(isDate(new Date())).toBe(true);
expect(isDate("2025-01-08")).toBe(false);
expect(isDate({})).toBe(false);
});

test("isRegExp should correctly identify RegExp objects", () => {
expect(isRegExp(/test/)).toBe(true);
expect(isRegExp(new RegExp("test"))).toBe(true);
expect(isRegExp("test")).toBe(false);
});

test("isObject should identify plain objects and exclude others", () => {
isComplex.mockReturnValue(false);
isFraction.mockReturnValue(false);

expect(isObject({})).toBe(true);
expect(isObject(new Date())).toBe(false);
expect(isObject([])).toBe(false);
expect(isObject(null)).toBe(false);

isComplex.mockReturnValue(true);
expect(isObject({})).toBe(false);
isComplex.mockReturnValue(false);
isFraction.mockReturnValue(true);
expect(isObject({})).toBe(false);
});

test("isNull should correctly identify null values", () => {
expect(isNull(null)).toBe(true);
expect(isNull(undefined)).toBe(false);
expect(isNull(0)).toBe(false);
expect(isNull({})).toBe(false);
});
});
Loading