新增调试信息
This commit is contained in:
+202
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
# Salty
|
||||
|
||||
Salty is a drop-in replacement for (some of) [TaffyDB](https://github.com/typicaljoe/taffydb). It
|
||||
supports only the TaffyDB features that JSDoc has historically used.
|
||||
|
||||
## Why Salty?
|
||||
|
||||
TaffyDB lets you query an array of objects, similar to the way you would query a database. JSDoc 3.x
|
||||
used TaffyDB to manage _doclets_, or objects that contain information about your code.
|
||||
|
||||
Specifically, after JSDoc parsed your code, it passed the doclets to your JSDoc template as a
|
||||
TaffyDB object. The template then used TaffyDB queries to remove unneeded doclets, and to retrieve
|
||||
the doclets that were needed to generate documentation.
|
||||
|
||||
Salty exists because TaffyDB is no longer maintained and is the subject of a
|
||||
[CVE](https://www.cve.org/CVERecord?id=CVE-2019-10790), causing TaffyDB to be flagged as a security
|
||||
risk. There's no real security risk, but it sure looks like there is.
|
||||
|
||||
Also, TaffyDB [can't decide what license it uses](https://github.com/typicaljoe/taffydb/issues/166):
|
||||
|
||||
- The [README](https://github.com/typicaljoe/taffydb/blame/d4870cee370abffe510ba598b02e4e7ad6af5d2a/README.md#L146-L156)
|
||||
says that TaffyDB uses the [1-clause BSD License](https://opensource.org/licenses/BSD-1-Clause).
|
||||
- The [`package.json` file](https://github.com/typicaljoe/taffydb/blob/d4870cee370abffe510ba598b02e4e7ad6af5d2a/package.json#L32)
|
||||
says that TaffyDB uses the [2-clause BSD License](https://opensource.org/licenses/BSD-2-Clause).
|
||||
- The [`License` file](https://github.com/typicaljoe/taffydb/blob/d4870cee370abffe510ba598b02e4e7ad6af5d2a/License)
|
||||
says that TaffyDB uses the [MIT License](https://opensource.org/licenses/MIT).
|
||||
|
||||
By replacing TaffyDB with Salty, which uses the
|
||||
[Apache 2.0 License](https://github.com/jsdoc/jsdoc/blob/main/packages/jsdoc-salty/LICENSE), JSDoc
|
||||
resolved the purported security issue and the licensing uncertainty.
|
||||
|
||||
## Use Salty in a JSDoc template
|
||||
|
||||
Starting in version 4.0.0, JSDoc no longer uses the `taffydb` package as a dependency. If you use a
|
||||
JSDoc template that comes with JSDoc 4.0.0 or later, then you don't need to make any changes to the
|
||||
template.
|
||||
|
||||
Otherwise, your template might use the `taffydb` package. After you replace `taffydb` with
|
||||
`@jsdoc/salty`, your template should remain compatible with JSDoc 3.x.
|
||||
|
||||
To replace `taffydb` with `@jsdoc/salty`, do the following:
|
||||
|
||||
1. In the template's `publish.js` file, find the line or lines that require `taffydb`. They should
|
||||
look similar to one of the following:
|
||||
|
||||
```js
|
||||
// Old-school, without object destructuring (might use `var` or `let` instead of `const`)
|
||||
const taffy = require('taffydb').taffy;
|
||||
// New-school, with object destructuring
|
||||
const { taffy } = require('taffydb');
|
||||
```
|
||||
|
||||
2. Replace `taffydb` with `@jsdoc/salty`:
|
||||
|
||||
```js
|
||||
// Old-school, without object destructuring
|
||||
const taffy = require('@jsdoc/salty').taffy;
|
||||
// New-school, with object destructuring
|
||||
const { taffy } = require('@jsdoc/salty');
|
||||
```
|
||||
|
||||
3. If the template has a `package.json` file that includes `taffydb` as a dependency, then remove
|
||||
the `taffydb` dependency.
|
||||
|
||||
4. In the template's `package.json` file, add `@jsdoc/salty` as a dependency.
|
||||
|
||||
5. Run `npm install` in the template directory, then confirm that the template works as expected.
|
||||
|
||||
## Supported TaffyDB features
|
||||
|
||||
Salty supports only the TaffyDB features that have historically been used by JSDoc templates. That
|
||||
means that most TaffyDB functionality is missing.
|
||||
|
||||
Specifically, Salty lets you do the following:
|
||||
|
||||
### Create a database
|
||||
|
||||
```js
|
||||
let db = taffy([{ a: 3 }, { a: 1, b: 'hello' }, { a: 7, b: 2 }]);
|
||||
```
|
||||
|
||||
### Sort items in a database
|
||||
|
||||
Items are sorted in place. Salty uses the following sort order, which differs from, but is more
|
||||
predictable than, TaffyDB:
|
||||
|
||||
1. Non-null, non-undefined values, in standard sort order
|
||||
2. Null values
|
||||
3. Explicitly undefined values: key is present, value is undefined
|
||||
4. Implicitly undefined values: key is not present
|
||||
|
||||
```js
|
||||
// Sort by the `a` property
|
||||
db.sort('a');
|
||||
// Sort by the `a` property, then the `b` property
|
||||
db.sort('a, b');
|
||||
```
|
||||
|
||||
### Get items from a database
|
||||
|
||||
```js
|
||||
// Get array of all items
|
||||
const emptyMatcher = db().get();
|
||||
// Get array of items where `a` is either `1` or `3`
|
||||
const arrayMatcher = db({ a: [1, 3] }).get();
|
||||
// Get array of items where `a` is `1` and `b` is `hello`
|
||||
const multiMatcher = db({ a: 1 }, { b: 'hello' }).get();
|
||||
// Get array of items where `b` is undefined
|
||||
const undefinedMatcher = db({ b: { isUndefined: true } }).get();
|
||||
// Get array of items where `b` starts with `he`
|
||||
const leftMatcher = db({ b: { left: 'he' } }).get();
|
||||
// Get array of items where `b` ends with `lo`
|
||||
const rightMatcher = db({ b: { right: 'lo' } }).get();
|
||||
```
|
||||
|
||||
### Get items with a custom query function
|
||||
|
||||
Within the query function, `this` is bound to the current item. As a result, the custom query
|
||||
function cannot be an arrow function.
|
||||
|
||||
```js
|
||||
function query() {
|
||||
return this.a > 1;
|
||||
}
|
||||
|
||||
const items = db(query).get();
|
||||
// `items` is `[{ a: 3 }, { a: 7, b: 2 }]`
|
||||
```
|
||||
|
||||
### Iterate over items in a database
|
||||
|
||||
```js
|
||||
// Iterate over all items
|
||||
db().each((item, i) => console.log(`'b' property at index ${i}: ${item.b}`));
|
||||
// Iterate over items where `b` equals `hello`
|
||||
db({ b: 'hello' }).each((item) => console.log(`'a' property: ${item.a}`));
|
||||
```
|
||||
|
||||
### Remove items from a database
|
||||
|
||||
```js
|
||||
// Remove items where `a` equals `7`
|
||||
db({ a: 7 }).remove(); // returns `1` (the number of items removed)
|
||||
// Remove items where `b` is undefined
|
||||
db({ b: { isUndefined: true } }).remove(); // returns `1`
|
||||
// Remove items where `b` is defined
|
||||
db({ b: { isUndefined: false } }).remove(); // returns `1`
|
||||
// Remove all items
|
||||
db().remove();
|
||||
```
|
||||
|
||||
## New features
|
||||
|
||||
Salty probably won't gain any new features, ever. It exists solely to meet the requirements of
|
||||
JSDoc.
|
||||
|
||||
The exception is if a JSDoc template uses TaffyDB features that aren't available in Salty. If that's
|
||||
the case, [create an issue](https://github.com/jsdoc/jsdoc/issues) with details about the template
|
||||
you're using and the feature that's missing.
|
||||
|
||||
## TaffyDB, JSDoc, and security
|
||||
|
||||
Is TaffyDB a security risk? And has JSDoc ever used TaffyDB in a way that creates a security risk?
|
||||
The answer to both questions is no.
|
||||
|
||||
First, [CVE-2019-10790](https://www.cve.org/CVERecord?id=CVE-2019-10790) says that "attackers can
|
||||
use [the TaffyDB] vulnerability to access any data items in the DB." But JSDoc used TaffyDB only to
|
||||
store data about your JavaScript code. That data is no more sensitive than the code itself.
|
||||
|
||||
Also, JSDoc doesn't persist the TaffyDB data to disk. It exists only while JSDoc is running.
|
||||
|
||||
Most important of all, TaffyDB doesn't pretend to have any sort of access control. To the contrary,
|
||||
TaffyDB _intentionally_ makes it very easy to access all of the data in a DB. If your DB is stored
|
||||
in a variable named `db`, then calling `db().get()` retrieves all of the data in the DB.
|
||||
[This method is documented](https://taffydb.com/working_with_data.html) as the "[p]refered [sic]
|
||||
method for extracting data." Because you can always access _all_ of the data, it's unclear why a bug
|
||||
that lets you access _some_ of the data would create a security risk.
|
||||
|
||||
So Salty doesn't mitigate a security risk or fix a security issue. However, in general, it's not a
|
||||
good idea to tell people to ignore CVEs; also, it can be difficult to convince your colleagues or
|
||||
employer to ignore a specific CVE. For those reasons, it was worth the trouble to replace TaffyDB.
|
||||
|
||||
## What's with the name?
|
||||
|
||||
It's a play on "saltwater taffy." Hilarious!
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
Copyright 2022 the JSDoc Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
const Salty = require('./lib/salty');
|
||||
|
||||
module.exports = {
|
||||
taffy: (items) => new Salty(items),
|
||||
};
|
||||
+255
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
Copyright 2022 the JSDoc Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
const _ = require('lodash');
|
||||
|
||||
// Install shim for Object.hasOwn() if necessary.
|
||||
/* istanbul ignore next */
|
||||
if (!Object.hasOwn) {
|
||||
Object.hasOwn = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
|
||||
}
|
||||
|
||||
function addToSelection(salty, item, i) {
|
||||
salty._selectedItems.push(item);
|
||||
salty._selectedIndexes.push(i);
|
||||
}
|
||||
|
||||
function selectAllItems(salty) {
|
||||
salty._selectedItems = salty._items.slice();
|
||||
salty._selectedIndexes = [...Array(salty._items.length).keys()];
|
||||
}
|
||||
|
||||
function createSelection(salty) {
|
||||
salty._selectedIndexes = [];
|
||||
salty._selectedItems = [];
|
||||
}
|
||||
|
||||
function eraseSelection(salty) {
|
||||
salty._selectedIndexes = null;
|
||||
salty._selectedItems = null;
|
||||
}
|
||||
|
||||
function finderWithFunction(salty, func) {
|
||||
createSelection(salty);
|
||||
|
||||
salty._items.forEach((item, i) => {
|
||||
if (func.bind(item)()) {
|
||||
addToSelection(salty, item, i);
|
||||
}
|
||||
});
|
||||
|
||||
return salty;
|
||||
}
|
||||
|
||||
function applyMatcherObject(value, matcherObject) {
|
||||
if (matcherObject.isUndefined === true) {
|
||||
return _.isUndefined(value);
|
||||
}
|
||||
|
||||
if (matcherObject.isUndefined === false) {
|
||||
return !_.isUndefined(value);
|
||||
}
|
||||
|
||||
if (matcherObject.left) {
|
||||
return value && value.startsWith && value.startsWith(matcherObject.left);
|
||||
}
|
||||
|
||||
if (matcherObject.right) {
|
||||
return value && value.endsWith && value.endsWith(matcherObject.right);
|
||||
}
|
||||
}
|
||||
|
||||
function finderWithMatcher(salty, ...args) {
|
||||
let item;
|
||||
let matches;
|
||||
const len = salty._items.length;
|
||||
const matcher = _.defaults({}, ...args);
|
||||
const matcherKeys = Object.keys(matcher);
|
||||
let matcherValue;
|
||||
|
||||
// Fast path if matcher object was empty or missing; that means "select all."
|
||||
if (matcherKeys.length === 0) {
|
||||
selectAllItems(salty);
|
||||
} else {
|
||||
createSelection(salty);
|
||||
|
||||
for (let i = 0; i < len; i++) {
|
||||
matches = true;
|
||||
|
||||
item = salty._items[i];
|
||||
for (const key of matcherKeys) {
|
||||
matcherValue = matcher[key];
|
||||
if (Array.isArray(matcherValue)) {
|
||||
if (!matcherValue.includes(item[key])) {
|
||||
matches = false;
|
||||
}
|
||||
} else if (_.isObject(matcherValue)) {
|
||||
matches = applyMatcherObject(item[key], matcherValue);
|
||||
} else if (matcherValue !== item[key]) {
|
||||
matches = false;
|
||||
}
|
||||
|
||||
if (matches === false) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (matches) {
|
||||
addToSelection(salty, item, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return salty;
|
||||
}
|
||||
|
||||
function finder(salty, ...args) {
|
||||
if (args.length && _.isFunction(args[0])) {
|
||||
return finderWithFunction(salty, args[0]);
|
||||
} else {
|
||||
return finderWithMatcher(salty, ...args);
|
||||
}
|
||||
}
|
||||
|
||||
function isNullOrUndefined(value) {
|
||||
return _.isNull(value) || _.isUndefined(value);
|
||||
}
|
||||
|
||||
function sorter(items, keys) {
|
||||
keys = keys.split(',').map((key) => key.trim());
|
||||
keys.reverse();
|
||||
for (const key of keys) {
|
||||
// TaffyDB has an unusual approach to sorting that can yield surprising results. Rather than
|
||||
// duplicate that approach, we use the following sort order, which is close enough to TaffyDB
|
||||
// and is easier to reason about:
|
||||
//
|
||||
// 1. Non-null, non-undefined values, in standard sort order
|
||||
// 2. Null values
|
||||
// 3. Explicitly undefined values: key is present, value is undefined
|
||||
// 4. Implicitly undefined values: key is not present
|
||||
items.sort((a, b) => {
|
||||
const aValue = a[key];
|
||||
const bValue = b[key];
|
||||
|
||||
if (isNullOrUndefined(aValue) || isNullOrUndefined(bValue)) {
|
||||
// Null and undefined come after all other values.
|
||||
if (!isNullOrUndefined(aValue)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!isNullOrUndefined(bValue)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Null comes before undefined.
|
||||
if (_.isNull(aValue) && _.isUndefined(bValue)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (_.isUndefined(aValue) && _.isNull(bValue)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Explicitly undefined comes before implicitly undefined.
|
||||
if (_.isUndefined(aValue) && _.isUndefined(bValue)) {
|
||||
if (Object.hasOwn(a, key)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (Object.hasOwn(b, key)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Both values are null, or both values are undefined.
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Neither value is null or undefined, so just use standard sort order.
|
||||
if (aValue < bValue) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (aValue > bValue) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function makeDb(salty) {
|
||||
/*
|
||||
Selections are persisted a bit differently in TaffyDB and Salty. Consider the following:
|
||||
|
||||
```js
|
||||
let db2 = db({ a: 1 });
|
||||
|
||||
db2.remove();
|
||||
db().get;
|
||||
db2.get();
|
||||
```
|
||||
|
||||
In TaffyDB, `db` and `db2` track selected items separately. `db().get()` returns all items;
|
||||
`db2.get()` returns an empty array.
|
||||
|
||||
In Salty, `db` and `db2` are the same object, so they share information about selected items.
|
||||
`db().get()` returns all items; `db2.get()` also returns all items, because the selection from
|
||||
`db().get()` remains active.
|
||||
*/
|
||||
const db = (...args) => finder(salty, ...args);
|
||||
|
||||
db.sort = (keys) => sorter(salty._items, keys);
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
class Salty {
|
||||
constructor(items) {
|
||||
this._items = items ? items.slice() : [];
|
||||
eraseSelection(this);
|
||||
|
||||
return makeDb(this);
|
||||
}
|
||||
|
||||
each(func) {
|
||||
this._selectedItems.forEach((item, i) => func(item, i));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
get() {
|
||||
return this._selectedItems.slice();
|
||||
}
|
||||
|
||||
remove() {
|
||||
let removedItems = 0;
|
||||
|
||||
if (this._selectedIndexes && this._selectedIndexes.length) {
|
||||
removedItems = this._selectedIndexes.length;
|
||||
this._items = this._items.filter((_item, i) => !this._selectedIndexes.includes(i));
|
||||
}
|
||||
|
||||
// Make the selection empty so that calling `get()` returns an empty array.
|
||||
createSelection(this);
|
||||
|
||||
return removedItems;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Salty;
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "@jsdoc/salty",
|
||||
"version": "0.2.8",
|
||||
"description": "A drop-in replacement for (some of) TaffyDB.",
|
||||
"keywords": [
|
||||
"jsdoc"
|
||||
],
|
||||
"author": "Jeff Williams <jeffrey.l.williams@gmail.com>",
|
||||
"homepage": "https://jsdoc.app/",
|
||||
"license": "Apache-2.0",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/jsdoc/jsdoc.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/jsdoc/jsdoc/issues"
|
||||
},
|
||||
"type": "commonjs",
|
||||
"dependencies": {
|
||||
"lodash": "^4.17.21"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=v12.0.0"
|
||||
},
|
||||
"gitHead": "7942901ff766e74d5d800ae5c708ab72cf89cefe"
|
||||
}
|
||||
Reference in New Issue
Block a user