@nejs/basic-extensions 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Brielle Harrison
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # @nejs/basic-extensions
2
+
3
+ ## Overview
4
+
5
+ @nejs/basic-extensions is a JavaScript library that provides a collection of essential extensions
6
+ to built-in JavaScript objects like Array, Object, Function, and Reflect. These extensions are
7
+ designed to enhance the native capabilities of JavaScript objects, providing developers with
8
+ additional utility methods for common tasks and improving code readability and efficiency.
9
+
10
+ ## Features
11
+
12
+ - **Array Extensions**: Adds convenience methods to JavaScript arrays, like `first` and `last`,
13
+ for easy access to the first and last elements.
14
+
15
+ - **Object Extensions**: Introduces utility functions to the Object class, such as methods for
16
+ checking object types and manipulating properties.
17
+
18
+ - **Function Extensions**: Enriches the Function class with methods to identify function types,
19
+ such as arrow functions, async functions, and bound functions.
20
+
21
+ - **Reflect Extensions**: Extends the Reflect object with advanced property interaction methods,
22
+ including checks for the presence of multiple or specific keys.
23
+
24
+ ## Installation
25
+
26
+ Install @nejs/basic-extensions using npm:
27
+
28
+ ```bash
29
+ npm install @nejs/basic-extensions
30
+ ```
31
+
32
+ Or using yarn:
33
+
34
+ ```bash
35
+ yarn add @nejs/basic-extensions
36
+ ```
37
+
38
+ ## Usage
39
+
40
+ Import the desired extensions in your JavaScript project:
41
+
42
+ ```javascript
43
+ import { ArrayPrototypeExtensions } from '@nejs/basic-extensions';
44
+ // Use the Array extensions
45
+ ```
46
+
47
+ ```javascript
48
+ import { FunctionExtensions } from '@nejs/basic-extensions';
49
+ // Use the Function extensions
50
+ ```
51
+
52
+ ## Documentation
53
+
54
+ For detailed documentation on each extension and its methods, please refer to the respective
55
+ sections in this README or visit our [documentation website](#).
56
+
57
+ ## Contributing
58
+
59
+ Contributions to @nejs/basic-extensions are always welcome. Please refer to our contributing
60
+ guidelines for more information on how to report issues, submit pull requests, and more.
61
+
62
+ ## License
63
+
64
+ @nejs/basic-extensions is licensed under the MIT License. See the [LICENSE](LICENSE) file for
65
+ more details.
package/bin/build ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env zsh
2
+
3
+ inRoot() {
4
+ test -e tsconfig.base.json &&
5
+ test -e tsconfig.esm.json &&
6
+ test -e tsconfig.cjs.json &&
7
+ test -e bin &&
8
+ test -e src
9
+ }
10
+
11
+ if ! inRoot; then
12
+ printf "Please move to the root of the package\n"
13
+ printf "You are currently in $(pwd)\n"
14
+ exit 1
15
+ fi
16
+
17
+ # Make dist if missing
18
+ [[ ! -e dist ]] && mkdir dist
19
+
20
+ # Clean the dist directory
21
+ rm -fr dist/*
22
+
23
+ # Build ecmascript modules build
24
+ npx tsc -p tsconfig.esm.json
25
+
26
+ # Build commonjs build
27
+ npx tsc -p tsconfig.cjs.json
28
+
29
+ # Generate unfortunately necessary sub- package.json files
30
+ bin/fixup
package/bin/fixup ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env zsh
2
+
3
+ cat >dist/cjs/package.json <<!EOF
4
+ {
5
+ "type": "commonjs"
6
+ }
7
+ !EOF
8
+
9
+ cat >dist/mjs/package.json <<!EOF
10
+ {
11
+ "type": "module"
12
+ }
13
+ !EOF
package/dist/.gitkeep ADDED
File without changes
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The `ArrayPrototypeExtensions` patch extends the prototype of the built-in
3
+ * JavaScript `Array` with additional properties for convenience and improved
4
+ * readability. By applying this patch, all array instances gain new getter
5
+ * properties `first` and `last`, which provide quick access to the first and
6
+ * last elements of the array, respectively. This enhancement simplifies common
7
+ * operations on arrays and makes code more expressive and concise.
8
+ */
9
+ export const ArrayPrototypeExtensions: Patch;
10
+ import { Patch } from '@nejs/extension';
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayPrototypeExtensions = void 0;
4
+ const extension_1 = require("@nejs/extension");
5
+ /**
6
+ * The `ArrayPrototypeExtensions` patch extends the prototype of the built-in
7
+ * JavaScript `Array` with additional properties for convenience and improved
8
+ * readability. By applying this patch, all array instances gain new getter
9
+ * properties `first` and `last`, which provide quick access to the first and
10
+ * last elements of the array, respectively. This enhancement simplifies common
11
+ * operations on arrays and makes code more expressive and concise.
12
+ */
13
+ exports.ArrayPrototypeExtensions = new extension_1.Patch(Array.prototype, {
14
+ /**
15
+ * A getter property that returns the first element of the array. If the
16
+ * array is empty, it returns `undefined`. This property is useful for
17
+ * scenarios where you need to quickly access the first item of an array
18
+ * without the need for additional checks or method calls.
19
+ *
20
+ * @returns {*} The first element of the array or `undefined` if the array
21
+ * is empty.
22
+ */
23
+ get first() {
24
+ return this[0];
25
+ },
26
+ /**
27
+ * A getter property that returns the last element of the array. It
28
+ * calculates the last index based on the array's length. If the array is
29
+ * empty, it returns `undefined`. This property is beneficial when you need
30
+ * to access the last item in an array, improving code readability and
31
+ * avoiding manual index calculation.
32
+ *
33
+ * @returns {*} The last element of the array or `undefined` if the
34
+ * array is empty.
35
+ */
36
+ get last() {
37
+ return this[this.length - 1];
38
+ }
39
+ });
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The `FunctionExtensions` class is a patch applied to the built-in JavaScript `Function`
3
+ * constructor. It extends `Function` with additional utility methods for determining the
4
+ * specific type or nature of function-like objects. These methods allow developers to
5
+ * distinguish between classes, regular functions, async functions, and arrow functions
6
+ * in a more intuitive and straightforward manner. This class is part of the `@nejs/extension`
7
+ * library and enhances the capabilities of function handling and introspection in JavaScript.
8
+ */
9
+ export const FunctionExtensions: Patch;
10
+ import { Patch } from '@nejs/extension';
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FunctionExtensions = void 0;
4
+ const extension_1 = require("@nejs/extension");
5
+ /**
6
+ * The `FunctionExtensions` class is a patch applied to the built-in JavaScript `Function`
7
+ * constructor. It extends `Function` with additional utility methods for determining the
8
+ * specific type or nature of function-like objects. These methods allow developers to
9
+ * distinguish between classes, regular functions, async functions, and arrow functions
10
+ * in a more intuitive and straightforward manner. This class is part of the `@nejs/extension`
11
+ * library and enhances the capabilities of function handling and introspection in JavaScript.
12
+ */
13
+ exports.FunctionExtensions = new extension_1.Patch(Function, {
14
+ /**
15
+ * Determines if a given value is a class. It checks if the value is an instance of
16
+ * `Function` and if its string representation includes the keyword 'class'. This method
17
+ * is useful for distinguishing classes from other function types in JavaScript.
18
+ *
19
+ * @param {*} value - The value to be checked.
20
+ * @returns {boolean} Returns `true` if the value is a class, otherwise `false`.
21
+ */
22
+ isClass(value) {
23
+ return value instanceof Function && String(value).includes('class');
24
+ },
25
+ /**
26
+ * Checks if a given value is a regular function. This method verifies if the value is
27
+ * an instance of `Function`, which includes regular functions, classes, and async
28
+ * functions but excludes arrow functions.
29
+ *
30
+ * @param {*} value - The value to be checked.
31
+ * @returns {boolean} Returns `true` if the value is a regular function, otherwise `false`.
32
+ */
33
+ isFunction(value) {
34
+ return value instanceof Function;
35
+ },
36
+ /**
37
+ * Determines if a given value is an asynchronous function. It checks if the value is an
38
+ * instance of `Function` and if its string representation includes the keyword 'Async'.
39
+ * This method is particularly useful for identifying async functions.
40
+ *
41
+ * @param {*} value - The value to be checked.
42
+ * @returns {boolean} Returns `true` if the value is an async function, otherwise `false`.
43
+ */
44
+ isAsync(value) {
45
+ const stringTag = /(\w+)]/g.exec(Object.prototype.toString.call(value))[1];
46
+ return (value instanceof Function &&
47
+ stringTag.includes('Async'));
48
+ },
49
+ /**
50
+ * Checks if a given value is an arrow function. It verifies if the value is an instance
51
+ * of `Function`, if its string representation includes the '=>' symbol, and if it lacks
52
+ * a prototype, which is a characteristic of arrow functions in JavaScript.
53
+ *
54
+ * @param {*} value - The value to be checked.
55
+ * @returns {boolean} Returns `true` if the value is an arrow function, otherwise `false`.
56
+ */
57
+ isBigArrow(value) {
58
+ return (value instanceof Function &&
59
+ String(value).includes('=>') &&
60
+ !String(value).startsWith('bound') &&
61
+ !Reflect.has(value, 'prototype'));
62
+ },
63
+ /**
64
+ * Determines if a given value is a bound function. Bound functions are created using
65
+ * the `Function.prototype.bind` method, which allows setting the `this` value at the
66
+ * time of binding. This method checks if the value is an instance of `Function`, if
67
+ * its string representation starts with 'bound', and if it lacks a `prototype`
68
+ * property. These characteristics are indicative of bound functions in JavaScript.
69
+ *
70
+ * @param {*} value - The value to be checked, typically a function.
71
+ * @returns {boolean} Returns `true` if the value is a bound function, otherwise
72
+ * `false`. Bound functions have a specific format in their string representation
73
+ * and do not have their own `prototype` property.
74
+ */
75
+ isBound(value) {
76
+ return (value instanceof Function &&
77
+ String(value).startsWith('bound') &&
78
+ !Reflect.has(value, 'prototype'));
79
+ },
80
+ });
@@ -0,0 +1,7 @@
1
+ export function enableAll(owners: any): void;
2
+ export function disableAll(owners: any): void;
3
+ import { ObjectExtensions } from './objectextensions.js';
4
+ import { FunctionExtensions } from './functionextensions.js';
5
+ import { ReflectExtensions } from './reflectextensions.js';
6
+ import { ArrayPrototypeExtensions } from './arrayextensions.js';
7
+ export { ObjectExtensions, FunctionExtensions, ReflectExtensions, ArrayPrototypeExtensions };
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayPrototypeExtensions = exports.ReflectExtensions = exports.FunctionExtensions = exports.ObjectExtensions = exports.disableAll = exports.enableAll = void 0;
4
+ const functionextensions_js_1 = require("./functionextensions.js");
5
+ Object.defineProperty(exports, "FunctionExtensions", { enumerable: true, get: function () { return functionextensions_js_1.FunctionExtensions; } });
6
+ const objectextensions_js_1 = require("./objectextensions.js");
7
+ Object.defineProperty(exports, "ObjectExtensions", { enumerable: true, get: function () { return objectextensions_js_1.ObjectExtensions; } });
8
+ const reflectextensions_js_1 = require("./reflectextensions.js");
9
+ Object.defineProperty(exports, "ReflectExtensions", { enumerable: true, get: function () { return reflectextensions_js_1.ReflectExtensions; } });
10
+ const arrayextensions_js_1 = require("./arrayextensions.js");
11
+ Object.defineProperty(exports, "ArrayPrototypeExtensions", { enumerable: true, get: function () { return arrayextensions_js_1.ArrayPrototypeExtensions; } });
12
+ const extension_1 = require("@nejs/extension");
13
+ const Owners = [
14
+ Object,
15
+ Function,
16
+ Reflect,
17
+ Array.prototype,
18
+ ];
19
+ function enableAll(owners) {
20
+ (owners || Owners).forEach(owner => {
21
+ extension_1.Patch.enableFor(owner);
22
+ });
23
+ }
24
+ exports.enableAll = enableAll;
25
+ function disableAll(owners) {
26
+ (owners || Owners).forEach(owner => {
27
+ extension_1.Patch.disableFor(owner);
28
+ });
29
+ }
30
+ exports.disableAll = disableAll;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * `ObjectExtensions` is a patch for the JavaScript built-in `Object` class. It
3
+ * adds utility methods to the `Object` class without modifying the global namespace
4
+ * directly. This patch includes methods for key validation, object type checking,
5
+ * and retrieving the string tag of an object. These methods are useful for
6
+ * enhancing the capabilities of the standard `Object` class with additional
7
+ * utility functions.
8
+ */
9
+ export const ObjectExtensions: Patch;
10
+ import { Patch } from '@nejs/extension';
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ObjectExtensions = void 0;
4
+ const extension_1 = require("@nejs/extension");
5
+ /**
6
+ * `ObjectExtensions` is a patch for the JavaScript built-in `Object` class. It
7
+ * adds utility methods to the `Object` class without modifying the global namespace
8
+ * directly. This patch includes methods for key validation, object type checking,
9
+ * and retrieving the string tag of an object. These methods are useful for
10
+ * enhancing the capabilities of the standard `Object` class with additional
11
+ * utility functions.
12
+ */
13
+ exports.ObjectExtensions = new extension_1.Patch(Object, {
14
+ /**
15
+ * Checks if the given value is a valid key for an object. In JavaScript, a valid
16
+ * key can be either a string or a symbol. This method is useful for validating
17
+ * object keys before using them in operations like setting or getting object properties.
18
+ *
19
+ * @param {*} value - The value to be checked.
20
+ * @returns {boolean} - Returns `true` if the value is a valid object key (string or symbol),
21
+ * otherwise `false`.
22
+ */
23
+ isValidKey(value) {
24
+ return (typeof value === 'string' || typeof value === 'symbol');
25
+ },
26
+ /**
27
+ * Determines if the provided value is an object. This method checks whether the
28
+ * value is an instance of `Object` or if its type is 'object'. It's a utility
29
+ * method for type-checking, ensuring that a value is an object before performing
30
+ * operations that are specific to objects.
31
+ *
32
+ * @param {*} value - The value to be checked.
33
+ * @returns {boolean} - Returns `true` if the value is an object, otherwise `false`.
34
+ */
35
+ isObject(value) {
36
+ return value && (value instanceof Object || typeof value === 'object');
37
+ },
38
+ /**
39
+ * Retrieves the string tag of an object. The string tag is a representation of
40
+ * the object's type, as defined by its `Object.prototype.toString` method. This
41
+ * utility method is helpful for getting a more descriptive type of an object than
42
+ * what is returned by the `typeof` operator, especially for custom objects.
43
+ *
44
+ * @param {*} value - The object whose string tag is to be retrieved.
45
+ * @returns {string} - The string tag of the object, indicating its type.
46
+ */
47
+ getStringTag(value) {
48
+ return /(\w+)]/.exec(Object.prototype.toString.call(value))[1];
49
+ },
50
+ /**
51
+ * Determines the type of the given value based on its string tag. This method
52
+ * uses `Object.getStringTag` to obtain the string tag of the value, which
53
+ * represents its more specific type (e.g., Array, Map, Set) rather than just
54
+ * 'object'. The method then maps this string tag to the corresponding type
55
+ * present in the provided `owner` object, which defaults to `globalThis`.
56
+ * This utility method is especially useful for identifying the specific
57
+ * constructor or class of an object, beyond the basic types identified by
58
+ * the `typeof` operator.
59
+ *
60
+ * @param {any} value - The value whose type is to be determined.
61
+ * @param {object} [owner=globalThis] - The object in which to look up the
62
+ * constructor corresponding to the string tag. Defaults to `globalThis`, which
63
+ * covers global constructors like `Array`, `Object`, etc.
64
+ * @returns {Function|object|null|undefined} - Returns the constructor or type
65
+ * of the value based on its string tag. For 'Null' and 'Undefined', it returns
66
+ * `null` and `undefined`, respectively. For other types, it returns the
67
+ * corresponding constructor (e.g., `Array` for arrays) if available in the
68
+ * `owner` object.
69
+ */
70
+ getType(value, owner = globalThis) {
71
+ const stringTag = Object.getStringTag(value);
72
+ switch (stringTag) {
73
+ case 'Null': return null;
74
+ case 'Undefined': return undefined;
75
+ default:
76
+ return owner[stringTag];
77
+ }
78
+ },
79
+ });
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The `ReflectExtensions` class is a patch applied to the built-in JavaScript
3
+ * `Reflect` object. It extends `Reflect` with additional utility methods that
4
+ * enhance its capabilities. These methods provide more advanced ways of
5
+ * interacting with object properties, such as checking for the presence of
6
+ * multiple keys at once (`hasAll`) or verifying if at least one specified key
7
+ * exists in an object (`hasSome`). This class is part of the `@nejs/extension`
8
+ * library and is designed to offer these extended functionalities in a way
9
+ * that is consistent with the existing `Reflect` API, making it intuitive for
10
+ * developers who are already familiar with standard reflection methods in
11
+ * JavaScript.
12
+ */
13
+ export const ReflectExtensions: Patch;
14
+ import { Patch } from '@nejs/extension';
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReflectExtensions = void 0;
4
+ const extension_1 = require("@nejs/extension");
5
+ /**
6
+ * The `ReflectExtensions` class is a patch applied to the built-in JavaScript
7
+ * `Reflect` object. It extends `Reflect` with additional utility methods that
8
+ * enhance its capabilities. These methods provide more advanced ways of
9
+ * interacting with object properties, such as checking for the presence of
10
+ * multiple keys at once (`hasAll`) or verifying if at least one specified key
11
+ * exists in an object (`hasSome`). This class is part of the `@nejs/extension`
12
+ * library and is designed to offer these extended functionalities in a way
13
+ * that is consistent with the existing `Reflect` API, making it intuitive for
14
+ * developers who are already familiar with standard reflection methods in
15
+ * JavaScript.
16
+ */
17
+ exports.ReflectExtensions = new extension_1.Patch(Reflect, {
18
+ /**
19
+ * The function checks if an object has all the specified keys.
20
+ *
21
+ * @param object - The `object` parameter is the object that we want to
22
+ * check if it has all the specified keys.
23
+ * @param keys - The `keys` parameter is a rest parameter, which means
24
+ * it can accept any number of arguments. In this case, it is expected
25
+ * to receive multiple keys as arguments.
26
+ * @returns a boolean value.
27
+ */
28
+ hasAll(object, ...keys) {
29
+ return Object.isObject(object) && (keys.flat(Infinity)
30
+ .map(key => Reflect.has(object, key))
31
+ .every(has => has));
32
+ },
33
+ /**
34
+ * The function checks if an object has at least one of the specified keys.
35
+ *
36
+ * @param object - The `object` parameter is the object that we want to check
37
+ * for the presence of certain keys.
38
+ * @param keys - The `keys` parameter is a rest parameter, which means it can
39
+ * accept any number of arguments. These arguments are the keys that we want
40
+ * to check if they exist in the `object`.
41
+ * @returns The function `hasSome` returns a boolean value indicating whether
42
+ * at least one of the keys provided as arguments exists in the given object.
43
+ */
44
+ hasSome(object, ...keys) {
45
+ return Object.isObject(object) && (keys.flat(Infinity)
46
+ .map(key => Reflect.has(object, key))
47
+ .some(has => has));
48
+ },
49
+ /**
50
+ * Retrieves an array of [key, descriptor] pairs for each property of the
51
+ * provided object. This method is akin to `Object.entries` but includes
52
+ * property descriptors instead of the property values. It's useful for cases
53
+ * where you need detailed information about properties, including their
54
+ * configurability, enumerability, and accessors.
55
+ *
56
+ * @param {object} object - The object whose property entries are to be
57
+ * retrieved.
58
+ * @returns {Array} An array of [key, descriptor] pairs, where each pair
59
+ * consists of the property name (key) and its descriptor. Returns an empty
60
+ * array if the input is not a valid object.
61
+ */
62
+ entries(object) {
63
+ if (!object || typeof object !== 'object') {
64
+ return [];
65
+ }
66
+ return Reflect.ownKeys(object).map(key => [
67
+ key, Object.getOwnPropertyDescriptor(object, key)
68
+ ]);
69
+ },
70
+ /**
71
+ * Retrieves an array of values from the property descriptors of the given
72
+ * object. This method works similarly to `Object.values` but operates on
73
+ * property descriptors instead. It's useful when you need the values of
74
+ * properties including getters, setters, and other descriptor-specific
75
+ * attributes.
76
+ *
77
+ * @param {object} object - The object whose property values are to be
78
+ * retrieved.
79
+ * @returns {Array} An array of values extracted from the object's property
80
+ * descriptors. The values correspond to the `value` attribute in each
81
+ * property's descriptor. Returns an empty array if the input is not a valid
82
+ * object.
83
+ */
84
+ values(object) {
85
+ return Reflect.entries.map(([, value]) => value);
86
+ }
87
+ });
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The `ArrayPrototypeExtensions` patch extends the prototype of the built-in
3
+ * JavaScript `Array` with additional properties for convenience and improved
4
+ * readability. By applying this patch, all array instances gain new getter
5
+ * properties `first` and `last`, which provide quick access to the first and
6
+ * last elements of the array, respectively. This enhancement simplifies common
7
+ * operations on arrays and makes code more expressive and concise.
8
+ */
9
+ export const ArrayPrototypeExtensions: Patch;
10
+ import { Patch } from '@nejs/extension';
@@ -0,0 +1,36 @@
1
+ import { Patch } from '@nejs/extension';
2
+ /**
3
+ * The `ArrayPrototypeExtensions` patch extends the prototype of the built-in
4
+ * JavaScript `Array` with additional properties for convenience and improved
5
+ * readability. By applying this patch, all array instances gain new getter
6
+ * properties `first` and `last`, which provide quick access to the first and
7
+ * last elements of the array, respectively. This enhancement simplifies common
8
+ * operations on arrays and makes code more expressive and concise.
9
+ */
10
+ export const ArrayPrototypeExtensions = new Patch(Array.prototype, {
11
+ /**
12
+ * A getter property that returns the first element of the array. If the
13
+ * array is empty, it returns `undefined`. This property is useful for
14
+ * scenarios where you need to quickly access the first item of an array
15
+ * without the need for additional checks or method calls.
16
+ *
17
+ * @returns {*} The first element of the array or `undefined` if the array
18
+ * is empty.
19
+ */
20
+ get first() {
21
+ return this[0];
22
+ },
23
+ /**
24
+ * A getter property that returns the last element of the array. It
25
+ * calculates the last index based on the array's length. If the array is
26
+ * empty, it returns `undefined`. This property is beneficial when you need
27
+ * to access the last item in an array, improving code readability and
28
+ * avoiding manual index calculation.
29
+ *
30
+ * @returns {*} The last element of the array or `undefined` if the
31
+ * array is empty.
32
+ */
33
+ get last() {
34
+ return this[this.length - 1];
35
+ }
36
+ });
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The `FunctionExtensions` class is a patch applied to the built-in JavaScript `Function`
3
+ * constructor. It extends `Function` with additional utility methods for determining the
4
+ * specific type or nature of function-like objects. These methods allow developers to
5
+ * distinguish between classes, regular functions, async functions, and arrow functions
6
+ * in a more intuitive and straightforward manner. This class is part of the `@nejs/extension`
7
+ * library and enhances the capabilities of function handling and introspection in JavaScript.
8
+ */
9
+ export const FunctionExtensions: Patch;
10
+ import { Patch } from '@nejs/extension';
@@ -0,0 +1,77 @@
1
+ import { Patch } from '@nejs/extension';
2
+ /**
3
+ * The `FunctionExtensions` class is a patch applied to the built-in JavaScript `Function`
4
+ * constructor. It extends `Function` with additional utility methods for determining the
5
+ * specific type or nature of function-like objects. These methods allow developers to
6
+ * distinguish between classes, regular functions, async functions, and arrow functions
7
+ * in a more intuitive and straightforward manner. This class is part of the `@nejs/extension`
8
+ * library and enhances the capabilities of function handling and introspection in JavaScript.
9
+ */
10
+ export const FunctionExtensions = new Patch(Function, {
11
+ /**
12
+ * Determines if a given value is a class. It checks if the value is an instance of
13
+ * `Function` and if its string representation includes the keyword 'class'. This method
14
+ * is useful for distinguishing classes from other function types in JavaScript.
15
+ *
16
+ * @param {*} value - The value to be checked.
17
+ * @returns {boolean} Returns `true` if the value is a class, otherwise `false`.
18
+ */
19
+ isClass(value) {
20
+ return value instanceof Function && String(value).includes('class');
21
+ },
22
+ /**
23
+ * Checks if a given value is a regular function. This method verifies if the value is
24
+ * an instance of `Function`, which includes regular functions, classes, and async
25
+ * functions but excludes arrow functions.
26
+ *
27
+ * @param {*} value - The value to be checked.
28
+ * @returns {boolean} Returns `true` if the value is a regular function, otherwise `false`.
29
+ */
30
+ isFunction(value) {
31
+ return value instanceof Function;
32
+ },
33
+ /**
34
+ * Determines if a given value is an asynchronous function. It checks if the value is an
35
+ * instance of `Function` and if its string representation includes the keyword 'Async'.
36
+ * This method is particularly useful for identifying async functions.
37
+ *
38
+ * @param {*} value - The value to be checked.
39
+ * @returns {boolean} Returns `true` if the value is an async function, otherwise `false`.
40
+ */
41
+ isAsync(value) {
42
+ const stringTag = /(\w+)]/g.exec(Object.prototype.toString.call(value))[1];
43
+ return (value instanceof Function &&
44
+ stringTag.includes('Async'));
45
+ },
46
+ /**
47
+ * Checks if a given value is an arrow function. It verifies if the value is an instance
48
+ * of `Function`, if its string representation includes the '=>' symbol, and if it lacks
49
+ * a prototype, which is a characteristic of arrow functions in JavaScript.
50
+ *
51
+ * @param {*} value - The value to be checked.
52
+ * @returns {boolean} Returns `true` if the value is an arrow function, otherwise `false`.
53
+ */
54
+ isBigArrow(value) {
55
+ return (value instanceof Function &&
56
+ String(value).includes('=>') &&
57
+ !String(value).startsWith('bound') &&
58
+ !Reflect.has(value, 'prototype'));
59
+ },
60
+ /**
61
+ * Determines if a given value is a bound function. Bound functions are created using
62
+ * the `Function.prototype.bind` method, which allows setting the `this` value at the
63
+ * time of binding. This method checks if the value is an instance of `Function`, if
64
+ * its string representation starts with 'bound', and if it lacks a `prototype`
65
+ * property. These characteristics are indicative of bound functions in JavaScript.
66
+ *
67
+ * @param {*} value - The value to be checked, typically a function.
68
+ * @returns {boolean} Returns `true` if the value is a bound function, otherwise
69
+ * `false`. Bound functions have a specific format in their string representation
70
+ * and do not have their own `prototype` property.
71
+ */
72
+ isBound(value) {
73
+ return (value instanceof Function &&
74
+ String(value).startsWith('bound') &&
75
+ !Reflect.has(value, 'prototype'));
76
+ },
77
+ });