@stless/modify-js 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/.nomedia +0 -0
- package/LICENSE +202 -0
- package/README.md +299 -0
- package/dist/index.min.js +9 -0
- package/dist/index.min.js.map +1 -0
- package/package.json +56 -0
- package/src/index.d.ts +110 -0
- package/src/index.js +342 -0
package/.nomedia
ADDED
|
File without changes
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Aries Harbinger
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/harnuma9/modify-js/refs/heads/main/media/banner.png" alt="Banner" width="1250">
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<br />
|
|
6
|
+
|
|
7
|
+
# @stless/modify-js
|
|
8
|
+
|
|
9
|
+
[](https://www.npmjs.com/package/@stless/modify-js)
|
|
10
|
+

|
|
11
|
+
[](https://github.com/harnuma9/modify-js/blob/main/LICENSE)
|
|
12
|
+

|
|
13
|
+
[](https://badge.socket.dev/npm/package/@stless/modify-js/1.0.0)
|
|
14
|
+
[](https://harnuma9.github.io/donate/)
|
|
15
|
+
|
|
16
|
+
<br />
|
|
17
|
+
|
|
18
|
+
**Lightweight functional chaining for JavaScript.**
|
|
19
|
+
`modify-js` is a zero-dependency utility that provides a user-land polyfill for the proposed **[TC39](https://github.com/tc39/proposal-pipeline-operator) Pipeline Operator**.
|
|
20
|
+
It safely extends native prototypes, and it allows you to transform data through readable, linear pipelines without the syntactic “callback hell” of nested functions.
|
|
21
|
+
|
|
22
|
+
<br />
|
|
23
|
+
|
|
24
|
+
> **Full API context optimized for AI assistants and contributors** is available in [`llms-full.txt`](https://github.com/harnuma9/modify-js/blob/main/llms-full.txt).
|
|
25
|
+
|
|
26
|
+
<br />
|
|
27
|
+
<br />
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
<br />
|
|
32
|
+
|
|
33
|
+
## 🚀 Getting Started
|
|
34
|
+
|
|
35
|
+
### NPM
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm install @stless/modify-js
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Yarn
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
yarn add @stless/modify-js
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### PNPM
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
pnpm add @stless/modify-js
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
<br />
|
|
54
|
+
<br />
|
|
55
|
+
|
|
56
|
+
## 💡 Usage
|
|
57
|
+
|
|
58
|
+
<br />
|
|
59
|
+
|
|
60
|
+
### Initialize the Environment
|
|
61
|
+
|
|
62
|
+
`modify-js` follows the [Principle of Least Power](https://en.wikipedia.org/wiki/Rule_of_least_power). It does not patch anything until you tell it to. Use `discover()` to find all global constructors (Array, String, Map, URL, etc.) or pass an explicit list.
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
// ESM
|
|
66
|
+
import applyModify, { discover, __F0V0 } from '@stless/modify-js';
|
|
67
|
+
// or CJS
|
|
68
|
+
const { default: applyModify, discover, __F0V0 } = require('@stless/modify-js');
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
// Option A: Explicit injection
|
|
72
|
+
applyModify([String, Array, Promise]);
|
|
73
|
+
|
|
74
|
+
// Option B: Global discovery and silent injection
|
|
75
|
+
applyModify(discover(), __F0V0);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
<br />
|
|
79
|
+
|
|
80
|
+
### Clean, Linear Pipelines
|
|
81
|
+
|
|
82
|
+
Transform data from left-to-right. Use `._p()`, `.$p()`, or `.modify()` to chain functions.
|
|
83
|
+
|
|
84
|
+
```javascript
|
|
85
|
+
const result = " hello world "
|
|
86
|
+
._p(s => s.trim())
|
|
87
|
+
._p(s => s.toUpperCase())
|
|
88
|
+
._p(s => s + "!!!")
|
|
89
|
+
.out();
|
|
90
|
+
|
|
91
|
+
console.log(result); // "HELLO WORLD!!!"
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
<br />
|
|
95
|
+
|
|
96
|
+
### Tapping Mid-chain
|
|
97
|
+
|
|
98
|
+
You can also “tap” into the pipe, mid-chain without interruption.
|
|
99
|
+
|
|
100
|
+
```javascript
|
|
101
|
+
const result = "100"
|
|
102
|
+
._p(s => parseInt(s, 10))
|
|
103
|
+
._p(n => Math.pow(n, 2))
|
|
104
|
+
._p(u => (console.log(u), u)) // console and return the same value
|
|
105
|
+
._p(v => ({ value: v }))
|
|
106
|
+
.out();
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
<br />
|
|
110
|
+
|
|
111
|
+
### Standalone Chaining (No Prototype Pollution)
|
|
112
|
+
|
|
113
|
+
If you prefer not to touch native prototypes, use `chain_` (or shorthand `chain$`) to wrap values manually.
|
|
114
|
+
|
|
115
|
+
```javascript
|
|
116
|
+
import { chain$ } from '@stless/modify-js';
|
|
117
|
+
|
|
118
|
+
const val = chain$("data")
|
|
119
|
+
.$p(d => d.split(""))
|
|
120
|
+
.out();
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
<br />
|
|
124
|
+
|
|
125
|
+
### Restore the Environment
|
|
126
|
+
|
|
127
|
+
You can completely remove `modify-js` from the environment at runtime.
|
|
128
|
+
|
|
129
|
+
```javascript
|
|
130
|
+
import { ejectModify } from '@stless/modify-js';
|
|
131
|
+
|
|
132
|
+
ejectModify({ verbose: true }); // Restores all prototypes to factory state
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
<br />
|
|
136
|
+
|
|
137
|
+
### Shortcut one-liner
|
|
138
|
+
|
|
139
|
+
It is possible to achieve a functional code with almost no `const`, `let`, or `var`.
|
|
140
|
+
|
|
141
|
+
<br />
|
|
142
|
+
|
|
143
|
+
**Before:**
|
|
144
|
+
|
|
145
|
+
```javascript
|
|
146
|
+
function encryptMsg(algo, data, key, iv) {
|
|
147
|
+
const cipher = crypto.createCipheriv(algo, key, iv);
|
|
148
|
+
const part1 = cipher.update(data);
|
|
149
|
+
const part2 = cipher.final();
|
|
150
|
+
const encrypted = Buffer.concat([part1, part2]);
|
|
151
|
+
key.fill(0);
|
|
152
|
+
iv.fill(0);
|
|
153
|
+
return encrypted;
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
<br />
|
|
158
|
+
|
|
159
|
+
**After (Modify-JS applied):**
|
|
160
|
+
|
|
161
|
+
```javascript
|
|
162
|
+
function encryptMsg(algo, data, key, iv) {
|
|
163
|
+
return [algo, key, iv]
|
|
164
|
+
._p(a => crypto.createCipheriv(...a)) // Create cipher
|
|
165
|
+
._p(c => Buffer.concat([c.update(data), c.final()])) // Process data
|
|
166
|
+
._p(enc => (key.fill(0), iv.fill(0), enc)) // Cleanup & return
|
|
167
|
+
.out();
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
<br />
|
|
172
|
+
|
|
173
|
+
**Visual comparison to TC39 (Hack-style pipes):**
|
|
174
|
+
|
|
175
|
+
```javascript
|
|
176
|
+
function encryptMsg(algo, data, key, iv) {
|
|
177
|
+
return [algo, key, iv]
|
|
178
|
+
|> crypto.createCipheriv(...%)
|
|
179
|
+
|> Buffer.concat([%.update(data), %.final()])
|
|
180
|
+
|> (key.fill(0), iv.fill(0), %)
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
<br />
|
|
185
|
+
<br />
|
|
186
|
+
|
|
187
|
+
## Advanced Injection Options
|
|
188
|
+
|
|
189
|
+
Control how `modify-js` handles collisions with existing methods using pre-defined shorthand constants:
|
|
190
|
+
|
|
191
|
+
| Constant | Force | Verbose | Description |
|
|
192
|
+
|--------------|--------------|--------------|--------------|
|
|
193
|
+
|`__F0V1` | `false` | `true` | **Standard:** Only patch new methods, log collisions. |
|
|
194
|
+
|`__F1V1` | `true` | `true` | **Debug:** Overwrite everything and log actions. |
|
|
195
|
+
|`__F0V0` | `false` | `false` | **Silent:** Patch if possible, no logs. |
|
|
196
|
+
|`__F1V0` | `true` | `false` | **Override:** Overwrite everything silently. |
|
|
197
|
+
|
|
198
|
+
<br />
|
|
199
|
+
<br />
|
|
200
|
+
|
|
201
|
+
## 🔩 API Reference
|
|
202
|
+
|
|
203
|
+
<br />
|
|
204
|
+
|
|
205
|
+
### `applyModify(targets, options)`
|
|
206
|
+
|
|
207
|
+
Injects pipeline methods into prototype chains.
|
|
208
|
+
|
|
209
|
+
* `targets`: `Array<Function>` - List of constructors (e.g. `[String, Array]`).
|
|
210
|
+
* `options`: `ModifyOptions` - `{ force: boolean, verbose: boolean }`.
|
|
211
|
+
|
|
212
|
+
<br />
|
|
213
|
+
|
|
214
|
+
### `discover()`
|
|
215
|
+
|
|
216
|
+
Reflectively scans `globalThis` for all [PascalCase](https://convertcase.help/blog/what-is-pascalcase/) functions with prototypes. Includes `Buffer` in Node environments.
|
|
217
|
+
|
|
218
|
+
<br />
|
|
219
|
+
|
|
220
|
+
### `hasBeenApplied()`
|
|
221
|
+
|
|
222
|
+
Returns a `boolean` indicating if the library has been initialized. Useful for conditional logic in environments with [hot-module reloading (HMR)](https://webpack.js.org/guides/hot-module-replacement/) or complex entry points.
|
|
223
|
+
|
|
224
|
+
<br />
|
|
225
|
+
|
|
226
|
+
### `getAppliedTargets()`
|
|
227
|
+
|
|
228
|
+
Returns an `Array` of all currently patched constructors. This provides full transparency into what native objects have been modified by the library.
|
|
229
|
+
|
|
230
|
+
<br />
|
|
231
|
+
|
|
232
|
+
### `chain_(val)` / `chain$(val)`
|
|
233
|
+
|
|
234
|
+
Wraps a value in a `ChainBox` for processing without prototype extensions.
|
|
235
|
+
|
|
236
|
+
<br />
|
|
237
|
+
|
|
238
|
+
### `ejectModify(options)`
|
|
239
|
+
|
|
240
|
+
Reverses all changes made by `applyModify` and resets internal library state.
|
|
241
|
+
|
|
242
|
+
<br />
|
|
243
|
+
<br />
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
<br />
|
|
248
|
+
|
|
249
|
+
## 🛠 ️Development & Verification
|
|
250
|
+
|
|
251
|
+
```bash
|
|
252
|
+
npm run check # test + verify checksums
|
|
253
|
+
npm run build # minify + generate docs + checksums
|
|
254
|
+
npm test
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
<br />
|
|
258
|
+
|
|
259
|
+
The project uses:
|
|
260
|
+
|
|
261
|
+
* `terser` for minification
|
|
262
|
+
* SHA-256 checksums for all built files
|
|
263
|
+
* Strict mode + comprehensive test suite
|
|
264
|
+
|
|
265
|
+
<br />
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
<br />
|
|
270
|
+
|
|
271
|
+
## ⭐ Contributor(s)
|
|
272
|
+
|
|
273
|
+
<a href="https://github.com/harnuma9/modify-js/graphs/contributors">
|
|
274
|
+
<img alt="contributors" src="https://contrib.rocks/image?repo=harnuma9/modify-js"/>
|
|
275
|
+
</a>
|
|
276
|
+
|
|
277
|
+
<br />
|
|
278
|
+
<br />
|
|
279
|
+
<br />
|
|
280
|
+
|
|
281
|
+
## License
|
|
282
|
+
|
|
283
|
+
Licensed under the **Apache License, Version 2.0**; a robust, permissive license that includes an explicit grant of patent rights and provides protection against contributor liability.
|
|
284
|
+
|
|
285
|
+
Copyright © 2026 Aries Harbinger. See the **[LICENSE](https://github.com/harnuma9/modify-js/blob/main/LICENSE)** file for full details.
|
|
286
|
+
|
|
287
|
+
<br />
|
|
288
|
+
<br />
|
|
289
|
+
|
|
290
|
+
## 🔗 Links
|
|
291
|
+
|
|
292
|
+
* [Github Repository](https://github.com/harnuma9/modify-js)
|
|
293
|
+
* [npm Package](https://www.npmjs.com/package/@stless/modify-js)
|
|
294
|
+
* Full AI Context **[llms-full.txt](https://github.com/harnuma9/modify-js/blob/main/llms-full.txt)** (for contributors / LLM assistants)
|
|
295
|
+
|
|
296
|
+
<br />
|
|
297
|
+
<br />
|
|
298
|
+
<br />
|
|
299
|
+
<br />
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Prototype extension utility for functional chaining.
|
|
3
|
+
* @summary A user-land polyfill for the proposed TC39 Pipeline Operator.
|
|
4
|
+
* @author Aries Harbinger
|
|
5
|
+
* @license Apache-2.0
|
|
6
|
+
* @module modify-js
|
|
7
|
+
*/
|
|
8
|
+
const appliedTargets=new Set;let isApplied=!1;export const __F0V0={force:!1,verbose:!1};export const __F1V1={force:!0,verbose:!0};export const __F0V1={force:!1,verbose:!0};export const __F1V0={force:!0,verbose:!1};export const hasBeenApplied=()=>isApplied;export const getAppliedTargets=()=>Array.from(appliedTargets);export function discover(){const e=Object.getOwnPropertyNames(globalThis).filter(e=>{try{const o=globalThis[e];return/^[A-Z]/.test(e)&&"function"==typeof o&&o.prototype}catch(e){return!1}}).map(e=>globalThis[e]);return"undefined"==typeof Buffer||e.includes(Buffer)||e.push(Buffer),e}export const chain_=e=>{const o=e=>chain_(e);return Object.freeze({modify:t=>o(t(e)),_p:t=>o(t(e)),$p:t=>o(t(e)),out:o=>null==e?o:e})};export const chain$=chain_;const modifyDefinition=Object.freeze({enumerable:!1,configurable:!0,writable:!0,value:function(e){if("function"!=typeof e)throw new TypeError("Expected a function");return chain_(e(this))}});export function ejectModify({verbose:e=!0}={}){getAppliedTargets().forEach(o=>{try{const e=o.prototype;delete e.modify,delete e._p,delete e.$p,appliedTargets.delete(o)}catch(t){e&&console.warn(`[modify-js] Skipping ${o.name||"Anonymous"}: Unable to eject. Reason: ${t.message}`)}}),appliedTargets.size<=0&&(e&&console.log("[modify-js] Environment fully restored."),isApplied=!1)}export function applyModify(e=[],{force:o=!1,verbose:t=!0}={}){if(!(isApplied&&!o&&e.length<=0)){if(!Array.isArray(e))throw new Error("applyModify requires an array of constructors. Use discover() to get all available.");e.forEach(e=>{if(appliedTargets.has(e)&&!o)return;const r=e?.prototype;if(!r)return void(t&&console.warn(`[modify-js] Skipping ${e}: No prototype available.`));["modify","_p","$p"].forEach(n=>{if(!Object.prototype.hasOwnProperty.call(r,n)||o)try{Object.defineProperty(r,n,modifyDefinition)}catch(o){t&&console.warn(`[modify-js] Skipping ${e.name||"Anonymous"}: Unable to inject .${n}(). Reason: ${o.message}`)}else t&&console.warn(`[modify-js] Skipping ${e.name||"Anonymous"}: .${n}() already exists.`)}),appliedTargets.add(e)}),isApplied=!0}}export default applyModify;
|
|
9
|
+
//# sourceMappingURL=index.min.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dist/index.min.js.map","names":["appliedTargets","Set","isApplied","__F0V0","force","verbose","__F1V1","__F0V1","__F1V0","hasBeenApplied","getAppliedTargets","Array","from","discover","selection","Object","getOwnPropertyNames","globalThis","filter","name","item","test","prototype","_","map","Buffer","includes","push","chain_","val","inner","v","freeze","modify","fn","_p","$p","out","def","chain$","modifyDefinition","enumerable","configurable","writable","value","TypeError","this","ejectModify","forEach","target","proto","delete","e","console","warn","message","size","log","applyModify","targets","length","isArray","Error","has","methodName","hasOwnProperty","call","defineProperty","add"],"sources":["src/index.js"],"mappings":";;;;;;;AA8BA,MAAMA,eAAiB,IAAIC,IAO3B,IAAIC,WAAY,SAcT,MAAMC,OAAS,CAAEC,OAAO,EAAOC,SAAS,UAMxC,MAAMC,OAAS,CAAEF,OAAO,EAAOC,SAAS,UAMxC,MAAME,OAAS,CAAEH,OAAO,EAAOC,SAAS,UAMxC,MAAMG,OAAS,CAAEJ,OAAO,EAAOC,SAAS,UAQxC,MAAMI,eAAiB,IAAMP,iBAQ7B,MAAMQ,kBAAoB,IAAMC,MAAMC,KAAKZ,uBAS3C,SAASa,WACd,MAAMC,EAAYC,OAAOC,oBAAoBC,YAC1CC,OAAOC,IACN,IACE,MAAMC,EAAOH,WAAWE,GAExB,MAAO,SAASE,KAAKF,IACK,mBAATC,GACPA,EAAKE,SACjB,CAEA,MAAOC,GAAK,OAAO,CAAO,IAE3BC,IAAIL,GAAQF,WAAWE,IAM1B,MAHsB,oBAAXM,QAA2BX,EAAUY,SAASD,SACvDX,EAAUa,KAAKF,QAEVX,CACT,QAwBO,MAAMc,OAAUC,IAErB,MAAMC,EAASC,GAAMH,OAAOG,GAE5B,OAAOhB,OAAOiB,OAAO,CAOnBC,OAASC,GAAOJ,EAAMI,EAAGL,IAQzBM,GAAKD,GAAOJ,EAAMI,EAAGL,IAQrBO,GAAKF,GAAOJ,EAAMI,EAAGL,IAQrBQ,IAAMC,GAAgB,MAAPT,EAAcS,EAAMT,YAShC,MAAMU,OAAgBX,OAe7B,MAAMY,iBAAmBzB,OAAOiB,OAAO,CAErCS,YAAY,EAGZC,cAAc,EAGdC,UAAU,EAOVC,MACS,SAASV,GACd,GAAkB,mBAAPA,EAAmB,MAAM,IAAIW,UAAU,uBAGlD,OAAOjB,OAAOM,EAAGY,MACnB,WAiBG,SAASC,aAAY1C,QAAEA,GAAU,GAAS,CAAC,GAEhCK,oBAERsC,QAAQC,IACd,IACE,MAAMC,EAAQD,EAAO3B,iBAId4B,EAAMjB,cACNiB,EAAMf,UACNe,EAAMd,GAGbpC,eAAemD,OAAOF,EACxB,CAEA,MAAOG,GACD/C,GACFgD,QAAQC,KACN,wBAAwBL,EAAO9B,MAAQ,yCACXiC,EAAEG,UAEpC,IAIEvD,eAAewD,MAAQ,IACrBnD,GACFgD,QAAQI,IAAI,2CACdvD,WAAY,EAEhB,QA0BO,SAASwD,YAAYC,EAAU,IAAIvD,MAAEA,GAAQ,EAAKC,QAAEA,GAAU,GAAS,CAAC,GAE7E,KAAIH,YAAcE,GAASuD,EAAQC,QAAU,GAA7C,CAEA,IAAKjD,MAAMkD,QAAQF,GACjB,MAAM,IAAIG,MAAM,uFAElBH,EAAQX,QAAQC,IAEd,GAAIjD,eAAe+D,IAAId,KAAY7C,EAAO,OAE1C,MAAM8C,EAAQD,GAAQ3B,UAGtB,IAAK4B,EAEH,YADI7C,GAASgD,QAAQC,KAAK,wBAAwBL,+BAIpC,CAAC,SAAU,KAAM,MAEzBD,QAAQgB,IAGd,IAFejD,OAAOO,UAAU2C,eAAeC,KAAKhB,EAAOc,IAE5C5D,EACb,IACEW,OAAOoD,eAAejB,EAAOc,EAAYxB,iBAC3C,CACA,MAAOY,GACD/C,GACFgD,QAAQC,KACN,wBAAwBL,EAAO9B,MAAQ,kCAClB6C,gBAAyBZ,EAAEG,UAEtD,MAESlD,GACTgD,QAAQC,KAAK,wBAAwBL,EAAO9B,MAAQ,iBAAiB6C,yBAIzEhE,eAAeoE,IAAInB,KAGrB/C,WAAY,CA1C0C,CA2CxD,gBAEewD","ignoreList":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stless/modify-js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A zero-dependency prototype extension utility for functional chaining and null-safe pipelines.",
|
|
5
|
+
"author": "Aries Harbinger",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"homepage": "https://github.com/harnuma9/modify-js#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/harnuma9/modify-js.git"
|
|
11
|
+
},
|
|
12
|
+
"funding": [
|
|
13
|
+
{
|
|
14
|
+
"type": "individual",
|
|
15
|
+
"url": "https://harnuma9.github.io/donate/"
|
|
16
|
+
}
|
|
17
|
+
],
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/harnuma9/modify-js/issues"
|
|
20
|
+
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"types": "./src/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./src/index.d.ts",
|
|
26
|
+
"import": "./dist/index.min.js",
|
|
27
|
+
"require": "./dist/index.min.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=20"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "bash update.sh",
|
|
35
|
+
"verify": "bash verify.sh",
|
|
36
|
+
"test": "node --test ./test/test_suite.js",
|
|
37
|
+
"check": "npm run test && npm run verify",
|
|
38
|
+
"prepublishOnly": "npm run check"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"pipeline",
|
|
42
|
+
"functional",
|
|
43
|
+
"chaining",
|
|
44
|
+
"utility",
|
|
45
|
+
"prototype",
|
|
46
|
+
"tc39",
|
|
47
|
+
"fluent-interface"
|
|
48
|
+
],
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/node": "^25.6.0",
|
|
51
|
+
"docdash": "^2.0.2"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility: Scans the global environment for valid constructors with prototypes.
|
|
3
|
+
* This identifies native objects (Array, String, Map, etc.) available for patching.
|
|
4
|
+
* @function discover
|
|
5
|
+
* @returns {Array<Function>} A list of discoverable global constructors.
|
|
6
|
+
*/
|
|
7
|
+
export function discover(): Array<Function>;
|
|
8
|
+
/**
|
|
9
|
+
* Removes .modify() and shorthand pipeline methods from all patched constructors.
|
|
10
|
+
* This restores the environment to its original state by deleting the injected
|
|
11
|
+
* prototype properties.
|
|
12
|
+
* @function ejectModify
|
|
13
|
+
* @memberof module:modify-js
|
|
14
|
+
* @param {ModifyOptions} [options={}] - Configuration for the ejection process.
|
|
15
|
+
* @param {boolean} [options.verbose=true] - If true, logs restoration status and warnings.
|
|
16
|
+
* @returns {void}
|
|
17
|
+
* @example
|
|
18
|
+
* ejectModify({ verbose: false }); // Clean up the prototypes
|
|
19
|
+
*/
|
|
20
|
+
export function ejectModify({ verbose }?: ModifyOptions): void;
|
|
21
|
+
/**
|
|
22
|
+
* Injects .modify() and shorthands into specified prototype chains.
|
|
23
|
+
* Follows the Principle of Least Power; it does not patch globally
|
|
24
|
+
* unless explicitly passed the results of {@link module:modify-js.discover}.
|
|
25
|
+
* These methods return a {@link ChainBox} to ensure subsequent calls are null-safe.
|
|
26
|
+
*
|
|
27
|
+
* @function applyModify
|
|
28
|
+
* @example
|
|
29
|
+
* // Manual explicit injection
|
|
30
|
+
* applyModify([Array, String, Boolean]);
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* // Intentional global injection
|
|
34
|
+
* applyModify(discover(), __F0V0);
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* // Usage in code
|
|
38
|
+
* "hello"._p(s => s.toUpperCase()).modify(s => s + "!!!").out(); // "HELLO!!!"
|
|
39
|
+
*
|
|
40
|
+
* @param {Array<Function>} [targets=[]] - An array of constructors to patch.
|
|
41
|
+
* @param {ModifyOptions} [options={}] - Configuration for injection behavior.
|
|
42
|
+
* @returns {void}
|
|
43
|
+
*/
|
|
44
|
+
export function applyModify(targets?: Array<Function>, { force, verbose }?: ModifyOptions): void;
|
|
45
|
+
/**
|
|
46
|
+
* Configuration options for injection behavior.
|
|
47
|
+
* @typedef {Object} ModifyOptions
|
|
48
|
+
* @property {boolean} [force=false] - If true, overwrites existing .modify() or shorthand methods.
|
|
49
|
+
* @property {boolean} [verbose=true] - If true, logs warnings when collisions occur.
|
|
50
|
+
*/
|
|
51
|
+
/**
|
|
52
|
+
* Shorthand: Force=False, Verbose=False (Silent Mode)
|
|
53
|
+
* @type {ModifyOptions}
|
|
54
|
+
*/
|
|
55
|
+
export const __F0V0: ModifyOptions;
|
|
56
|
+
/**
|
|
57
|
+
* Shorthand: Force=True, Verbose=True (Debug/Aggressive Mode)
|
|
58
|
+
* @type {ModifyOptions}
|
|
59
|
+
*/
|
|
60
|
+
export const __F1V1: ModifyOptions;
|
|
61
|
+
/**
|
|
62
|
+
* Shorthand: Force=False, Verbose=True (Standard Mode)
|
|
63
|
+
* @type {ModifyOptions}
|
|
64
|
+
*/
|
|
65
|
+
export const __F0V1: ModifyOptions;
|
|
66
|
+
/**
|
|
67
|
+
* Shorthand: Force=True, Verbose=False (Override Mode)
|
|
68
|
+
* @type {ModifyOptions}
|
|
69
|
+
*/
|
|
70
|
+
export const __F1V0: ModifyOptions;
|
|
71
|
+
export function hasBeenApplied(): boolean;
|
|
72
|
+
export function getAppliedTargets(): Array<Function>;
|
|
73
|
+
export function chain_(val: any): ChainBox;
|
|
74
|
+
export function chain$(val: any): ChainBox;
|
|
75
|
+
export default applyModify;
|
|
76
|
+
/**
|
|
77
|
+
* Configuration options for injection behavior.
|
|
78
|
+
*/
|
|
79
|
+
export type ModifyOptions = {
|
|
80
|
+
/**
|
|
81
|
+
* - If true, overwrites existing .modify() or shorthand methods.
|
|
82
|
+
*/
|
|
83
|
+
force?: boolean | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* - If true, logs warnings when collisions occur.
|
|
86
|
+
*/
|
|
87
|
+
verbose?: boolean | undefined;
|
|
88
|
+
};
|
|
89
|
+
export type ChainBox = {
|
|
90
|
+
/**
|
|
91
|
+
* - Shorthand pipeline method.
|
|
92
|
+
*/
|
|
93
|
+
_p: (arg0: ModifyCallback) => ChainBox;
|
|
94
|
+
/**
|
|
95
|
+
* - Shorthand pipeline method.
|
|
96
|
+
*/
|
|
97
|
+
$p: (arg0: ModifyCallback) => ChainBox;
|
|
98
|
+
/**
|
|
99
|
+
* - Explicit pipeline method.
|
|
100
|
+
*/
|
|
101
|
+
modify: (arg0: ModifyCallback) => ChainBox;
|
|
102
|
+
/**
|
|
103
|
+
* - Unwraps the value and exits the chain.
|
|
104
|
+
*/
|
|
105
|
+
out: (arg0: any | undefined) => any;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* The transformation callback passed to .modify() or shorthands.
|
|
109
|
+
*/
|
|
110
|
+
export type ModifyCallback = (instance: any) => any;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Aries Harbinger
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @file Prototype extension utility for functional chaining.
|
|
19
|
+
* @summary A user-land polyfill for the proposed TC39 Pipeline Operator.
|
|
20
|
+
* @author Aries Harbinger
|
|
21
|
+
* @license Apache-2.0
|
|
22
|
+
* @module modify-js
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Internal state to track patched constructors.
|
|
28
|
+
* @type {Set<Function>}
|
|
29
|
+
* @private
|
|
30
|
+
*/
|
|
31
|
+
const appliedTargets = new Set();
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Internal state to track if the utility has been initialized at least once.
|
|
35
|
+
* @type {boolean}
|
|
36
|
+
* @private
|
|
37
|
+
*/
|
|
38
|
+
let isApplied = false;
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Configuration options for injection behavior.
|
|
43
|
+
* @typedef {Object} ModifyOptions
|
|
44
|
+
* @property {boolean} [force=false] - If true, overwrites existing .modify() or shorthand methods.
|
|
45
|
+
* @property {boolean} [verbose=true] - If true, logs warnings when collisions occur.
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Shorthand: Force=False, Verbose=False (Silent Mode)
|
|
50
|
+
* @type {ModifyOptions}
|
|
51
|
+
*/
|
|
52
|
+
export const __F0V0 = { force: false, verbose: false };
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Shorthand: Force=True, Verbose=True (Debug/Aggressive Mode)
|
|
56
|
+
* @type {ModifyOptions}
|
|
57
|
+
*/
|
|
58
|
+
export const __F1V1 = { force: true, verbose: true };
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Shorthand: Force=False, Verbose=True (Standard Mode)
|
|
62
|
+
* @type {ModifyOptions}
|
|
63
|
+
*/
|
|
64
|
+
export const __F0V1 = { force: false, verbose: true };
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Shorthand: Force=True, Verbose=False (Override Mode)
|
|
68
|
+
* @type {ModifyOptions}
|
|
69
|
+
*/
|
|
70
|
+
export const __F1V0 = { force: true, verbose: false };
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Utility: Checks if the library has been executed at least once.
|
|
75
|
+
* @function hasBeenApplied
|
|
76
|
+
* @returns {boolean} True if applyModify has been called successfully.
|
|
77
|
+
*/
|
|
78
|
+
export const hasBeenApplied = () => isApplied;
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Utility: Returns an array of all currently patched constructors.
|
|
83
|
+
* @function getAppliedTargets
|
|
84
|
+
* @returns {Array<Function>} An array containing the constructors currently modified.
|
|
85
|
+
*/
|
|
86
|
+
export const getAppliedTargets = () => Array.from(appliedTargets);
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Utility: Scans the global environment for valid constructors with prototypes.
|
|
91
|
+
* This identifies native objects (Array, String, Map, etc.) available for patching.
|
|
92
|
+
* @function discover
|
|
93
|
+
* @returns {Array<Function>} A list of discoverable global constructors.
|
|
94
|
+
*/
|
|
95
|
+
export function discover() {
|
|
96
|
+
const selection = Object.getOwnPropertyNames(globalThis)
|
|
97
|
+
.filter(name => {
|
|
98
|
+
try {
|
|
99
|
+
const item = globalThis[name];
|
|
100
|
+
// Filter for PascalCase functions that have a prototype (standard JS classes)
|
|
101
|
+
return /^[A-Z]/.test(name)
|
|
102
|
+
&& typeof item === 'function'
|
|
103
|
+
&& item.prototype;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
catch (_) { return false; }
|
|
107
|
+
})
|
|
108
|
+
.map(name => globalThis[name]);
|
|
109
|
+
|
|
110
|
+
// Include Buffer if in a Node-like environment
|
|
111
|
+
if (typeof Buffer !== 'undefined' && !selection.includes(Buffer))
|
|
112
|
+
selection.push(Buffer);
|
|
113
|
+
|
|
114
|
+
return selection;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @typedef {Object} ChainBox
|
|
120
|
+
* @property {function(ModifyCallback): ChainBox} _p - Shorthand pipeline method.
|
|
121
|
+
* @property {function(ModifyCallback): ChainBox} $p - Shorthand pipeline method.
|
|
122
|
+
* @property {function(ModifyCallback): ChainBox} modify - Explicit pipeline method.
|
|
123
|
+
* @property {function(any=): any} out - Unwraps the value and exits the chain.
|
|
124
|
+
*/
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A safe wrapper for functional chaining that handles null/undefined values.
|
|
128
|
+
* Once inside a chain_, all subsequent calls are null-safe.
|
|
129
|
+
* @function chain_
|
|
130
|
+
* @param {any} val - The initial value to wrap.
|
|
131
|
+
* @returns {ChainBox} A chainable object containing transformation methods.
|
|
132
|
+
* @example
|
|
133
|
+
* const result = chain_(" hello ")
|
|
134
|
+
* .$p(s => s.trim())
|
|
135
|
+
* .$p(s => null) // Pipeline continues safely
|
|
136
|
+
* .$p(s => console.log(s)) // Output mid-chain
|
|
137
|
+
* .out("DEFAULT"); // Returns "DEFAULT"
|
|
138
|
+
*/
|
|
139
|
+
export const chain_ = (val) => {
|
|
140
|
+
/** @private */
|
|
141
|
+
const inner = (v) => chain_(v);
|
|
142
|
+
|
|
143
|
+
return Object.freeze({
|
|
144
|
+
/**
|
|
145
|
+
* @method modify
|
|
146
|
+
* @memberof module:modify-js.ChainBox
|
|
147
|
+
* @param {ModifyCallback} fn - Transformation function.
|
|
148
|
+
* @returns {ChainBox}
|
|
149
|
+
*/
|
|
150
|
+
modify: (fn) => inner(fn(val)),
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @method _p
|
|
154
|
+
* @memberof module:modify-js.ChainBox
|
|
155
|
+
* @param {ModifyCallback} fn - Transformation function.
|
|
156
|
+
* @returns {ChainBox}
|
|
157
|
+
*/
|
|
158
|
+
_p: (fn) => inner(fn(val)),
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* @method $p
|
|
162
|
+
* @memberof module:modify-js.ChainBox
|
|
163
|
+
* @param {ModifyCallback} fn - Transformation function.
|
|
164
|
+
* @returns {ChainBox}
|
|
165
|
+
*/
|
|
166
|
+
$p: (fn) => inner(fn(val)),
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @method out
|
|
170
|
+
* @memberof module:modify-js.ChainBox
|
|
171
|
+
* @param {any} [def] - Optional fallback value if the result is null/undefined.
|
|
172
|
+
* @returns {any}
|
|
173
|
+
*/
|
|
174
|
+
out: (def) => (val == null ? def : val)
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* A shorthand method for chain_ function.
|
|
180
|
+
* @function chain$
|
|
181
|
+
* @returns {ChainBox} A chainable object containing transformation methods.
|
|
182
|
+
*/
|
|
183
|
+
export const chain$ = (() => chain_)();
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* The transformation callback passed to .modify() or shorthands.
|
|
188
|
+
* @callback ModifyCallback
|
|
189
|
+
* @param {any} instance - The object instance the method was called upon.
|
|
190
|
+
* @returns {any} - The result of the transformation.
|
|
191
|
+
*/
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Property descriptor for the utility methods.
|
|
195
|
+
* @type {PropertyDescriptor}
|
|
196
|
+
* @private
|
|
197
|
+
*/
|
|
198
|
+
const modifyDefinition = Object.freeze({
|
|
199
|
+
// This prevents the new methods from breaking other libraries that loop over objects.
|
|
200
|
+
enumerable: false,
|
|
201
|
+
|
|
202
|
+
// Allows the property to be deleted or changed later.
|
|
203
|
+
configurable: true,
|
|
204
|
+
|
|
205
|
+
// Allows the value (the function itself) to be changed using an assignment operator (=).
|
|
206
|
+
writable: true,
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* @param {ModifyCallback} fn - The transformation function.
|
|
210
|
+
* @throws {TypeError} If fn is not a function.
|
|
211
|
+
* @returns {any} The result of fn(this).
|
|
212
|
+
*/
|
|
213
|
+
value: (function () {
|
|
214
|
+
return function(fn) {
|
|
215
|
+
if (typeof fn !== 'function') throw new TypeError('Expected a function');
|
|
216
|
+
// Once the user uses .modify() or shorthands, they get a chain_ back.
|
|
217
|
+
// This may "solve" the friction of mid-chain null values.
|
|
218
|
+
return chain_(fn(this));
|
|
219
|
+
}
|
|
220
|
+
})()
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Removes .modify() and shorthand pipeline methods from all patched constructors.
|
|
226
|
+
* This restores the environment to its original state by deleting the injected
|
|
227
|
+
* prototype properties.
|
|
228
|
+
* @function ejectModify
|
|
229
|
+
* @memberof module:modify-js
|
|
230
|
+
* @param {ModifyOptions} [options={}] - Configuration for the ejection process.
|
|
231
|
+
* @param {boolean} [options.verbose=true] - If true, logs restoration status and warnings.
|
|
232
|
+
* @returns {void}
|
|
233
|
+
* @example
|
|
234
|
+
* ejectModify({ verbose: false }); // Clean up the prototypes
|
|
235
|
+
*/
|
|
236
|
+
export function ejectModify({ verbose = true } = {}) {
|
|
237
|
+
/** @type {Array<Function>} */
|
|
238
|
+
const targets = getAppliedTargets();
|
|
239
|
+
|
|
240
|
+
targets.forEach(target => {
|
|
241
|
+
try {
|
|
242
|
+
const proto = target.prototype;
|
|
243
|
+
|
|
244
|
+
// Remove the methods from the prototype chain
|
|
245
|
+
// These correspond to the methods defined in applyModify
|
|
246
|
+
delete proto.modify;
|
|
247
|
+
delete proto._p;
|
|
248
|
+
delete proto.$p;
|
|
249
|
+
|
|
250
|
+
// Remove specifically this target from the tracking Set
|
|
251
|
+
appliedTargets.delete(target);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
catch (e) {
|
|
255
|
+
if (verbose)
|
|
256
|
+
console.warn(
|
|
257
|
+
`[modify-js] Skipping ${target.name || 'Anonymous'}: ` +
|
|
258
|
+
`Unable to eject. Reason: ${e.message}`
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
// Final reset of global internal state
|
|
264
|
+
if (appliedTargets.size <= 0) {
|
|
265
|
+
if (verbose)
|
|
266
|
+
console.log("[modify-js] Environment fully restored.");
|
|
267
|
+
isApplied = false;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Injects .modify() and shorthands into specified prototype chains.
|
|
274
|
+
* Follows the Principle of Least Power; it does not patch globally
|
|
275
|
+
* unless explicitly passed the results of {@link module:modify-js.discover}.
|
|
276
|
+
* These methods return a {@link ChainBox} to ensure subsequent calls are null-safe.
|
|
277
|
+
*
|
|
278
|
+
* @function applyModify
|
|
279
|
+
* @example
|
|
280
|
+
* // Manual explicit injection
|
|
281
|
+
* applyModify([Array, String, Boolean]);
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* // Intentional global injection
|
|
285
|
+
* applyModify(discover(), __F0V0);
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* // Usage in code
|
|
289
|
+
* "hello"._p(s => s.toUpperCase()).modify(s => s + "!!!").out(); // "HELLO!!!"
|
|
290
|
+
*
|
|
291
|
+
* @param {Array<Function>} [targets=[]] - An array of constructors to patch.
|
|
292
|
+
* @param {ModifyOptions} [options={}] - Configuration for injection behavior.
|
|
293
|
+
* @returns {void}
|
|
294
|
+
*/
|
|
295
|
+
export function applyModify(targets = [], { force = false, verbose = true } = {}) {
|
|
296
|
+
// Exit early if no work is requested and library was already used
|
|
297
|
+
if (isApplied && !force && targets.length <= 0) return;
|
|
298
|
+
|
|
299
|
+
if (!Array.isArray(targets))
|
|
300
|
+
throw new Error('applyModify requires an array of constructors. Use discover() to get all available.');
|
|
301
|
+
|
|
302
|
+
targets.forEach(target => {
|
|
303
|
+
// Idempotency check
|
|
304
|
+
if (appliedTargets.has(target) && !force) return;
|
|
305
|
+
|
|
306
|
+
const proto = target?.prototype;
|
|
307
|
+
|
|
308
|
+
// Validate target has a prototype to extend
|
|
309
|
+
if (!proto) {
|
|
310
|
+
if (verbose) console.warn(`[modify-js] Skipping ${target}: No prototype available.`);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const methods = ['modify', '_p', '$p'];
|
|
315
|
+
|
|
316
|
+
methods.forEach(methodName => {
|
|
317
|
+
const exists = Object.prototype.hasOwnProperty.call(proto, methodName);
|
|
318
|
+
|
|
319
|
+
if (!exists || force) {
|
|
320
|
+
try {
|
|
321
|
+
Object.defineProperty(proto, methodName, modifyDefinition);
|
|
322
|
+
}
|
|
323
|
+
catch (e) {
|
|
324
|
+
if (verbose)
|
|
325
|
+
console.warn(
|
|
326
|
+
`[modify-js] Skipping ${target.name || 'Anonymous'}: ` +
|
|
327
|
+
`Unable to inject .${methodName}(). Reason: ${e.message}`
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
} else if (verbose) {
|
|
332
|
+
console.warn(`[modify-js] Skipping ${target.name || 'Anonymous'}: .${methodName}() already exists.`);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
appliedTargets.add(target);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
isApplied = true;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export default applyModify;
|