mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-07-03 09:35:05 +00:00
feat: add full support for refinement cards
This commit is contained in:
@@ -44,7 +44,7 @@ function findNode(obj, key, target, depth, safe = true) {
|
||||
* Creates a trap to intercept property access
|
||||
* and add utilities to an object.
|
||||
*
|
||||
* @param {*} obj
|
||||
* @param {object} obj
|
||||
* @returns
|
||||
*/
|
||||
function observe(obj) {
|
||||
@@ -55,11 +55,17 @@ function observe(obj) {
|
||||
* Returns the first object to match the rule.
|
||||
* @name get
|
||||
* @param {object} rule
|
||||
* @param {boolean} del_item
|
||||
*/
|
||||
return (rule) => target
|
||||
.find((obj) => {
|
||||
const rule_keys = Reflect.ownKeys(rule);
|
||||
return rule_keys.some((key) => obj[key] === rule[key]);
|
||||
return (rule, del_item) => target
|
||||
.find((obj, index) => {
|
||||
const match = deepCompare(rule, obj);
|
||||
|
||||
if (match && del_item) {
|
||||
target.splice(index, 1);
|
||||
}
|
||||
|
||||
return match;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -68,21 +74,53 @@ function observe(obj) {
|
||||
* Returns all objects that match the rule.
|
||||
* @name findAll
|
||||
* @param {object} rule
|
||||
* @param {boolean} del_items
|
||||
*/
|
||||
return (rule) => target
|
||||
.filter((obj) => {
|
||||
const rule_keys = Reflect.ownKeys(rule);
|
||||
return rule_keys.some((key) => obj[key] === rule[key]);
|
||||
return (rule, del_items) => target
|
||||
.filter((obj, index) => {
|
||||
const match = deepCompare(rule, obj);
|
||||
|
||||
if (match && del_items) {
|
||||
target.splice(index, 1);
|
||||
}
|
||||
|
||||
return match;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
if (prop == 'remove') {
|
||||
/**
|
||||
* Removes the item at the given index.
|
||||
* @name remove
|
||||
* @param {number} index
|
||||
*/
|
||||
return (index) => target.splice(index, 1);
|
||||
}
|
||||
|
||||
return Reflect.get(...arguments);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function escapeStringRegexp(string) {
|
||||
return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
|
||||
|
||||
/**
|
||||
* Compares given objects. May not work correctly for
|
||||
* objects with methods.
|
||||
*
|
||||
* @param {object} obj1
|
||||
* @param {object} obj2
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function deepCompare(obj1, obj2) {
|
||||
const keys = Reflect.ownKeys(obj1);
|
||||
|
||||
return keys.some((key) => {
|
||||
if (typeof obj1[key] == 'object') {
|
||||
return JSON.stringify(obj1[key]) === JSON.stringify(obj2[key]);
|
||||
} else {
|
||||
return obj1[key] === obj2[key];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,6 +138,10 @@ function getStringBetweenStrings(data, start_string, end_string) {
|
||||
return match ? match[1] : undefined;
|
||||
}
|
||||
|
||||
function escapeStringRegexp(string) {
|
||||
return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random user agent.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user