@dsottimano/trendspy-js
Version:
ALPHA VERSION: JavaScript port of trendspy - A library for analyzing Google Trends data
230 lines (216 loc) • 10.3 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
/**
* Recursively transforms a tree structure into a flat list.
* @param {Object} node - Tree node with 'name', 'id' and optional 'children' keys
* @param {string} parentId - Parent node ID
* @param {Array} result - Accumulated result
* @param {boolean} joinIds - Whether to join IDs with parent
* @returns {Array} List of objects with name and id
*/
function flattenTree(node) {
var parentId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var result = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var joinIds = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
if (!result) {
result = [];
}
var currentId = node.id;
// Join IDs only for geographical data
var fullId = joinIds && parentId ? "".concat(parentId, "-").concat(currentId) : currentId;
result.push({
name: node.name,
id: fullId
});
if (node.children) {
var _iterator = _createForOfIteratorHelper(node.children),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var child = _step.value;
flattenTree(child, joinIds ? fullId : '', result, joinIds);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
return result;
}
/**
* An index for efficient searches in hierarchical Google Trends data structures.
* Provides fast lookups for hierarchical data like locations and categories,
* supporting both exact and partial matching of names.
*/
var HierarchicalIndex = /*#__PURE__*/function () {
/**
* Initialize the search index.
* @param {Array} items - List of objects with 'name' and 'id'
* @param {boolean} partialIdSearch - Whether to allow partial ID matches
*/
function HierarchicalIndex(items) {
var partialIdSearch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
_classCallCheck(this, HierarchicalIndex);
// Main storage: Map with lowercase name as key
this.nameToItem = new Map();
// Inverted index for partial matching
this.wordIndex = new Map();
// Store search mode
this.partialIdSearch = partialIdSearch;
// Build indexes
var _iterator2 = _createForOfIteratorHelper(items),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var item = _step2.value;
this.addItem(item);
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
}
/**
* Add a single item to the index.
* @param {Object} item - Object with 'name' and 'id'
*/
return _createClass(HierarchicalIndex, [{
key: "addItem",
value: function addItem(item) {
var name = item.name.toLowerCase();
// Add to main storage
this.nameToItem.set(name, item);
// Split name into words and add to inverted index
var words = new Set(name.split(/\W+/));
var _iterator3 = _createForOfIteratorHelper(words),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var word = _step3.value;
if (word) {
if (!this.wordIndex.has(word)) {
this.wordIndex.set(word, []);
}
this.wordIndex.get(word).push(name);
}
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
}
/**
* Perform exact name search (case-insensitive).
* @param {string} name - Name to search for
* @returns {Object|null} Item object if found, null otherwise
*/
}, {
key: "exactSearch",
value: function exactSearch(name) {
return this.nameToItem.get(name.toLowerCase()) || null;
}
/**
* Perform partial name search (case-insensitive).
* @param {string} query - Search query string
* @returns {Array} List of matching item objects
*/
}, {
key: "partialSearch",
value: function partialSearch(query) {
var _this = this;
query = query.toLowerCase();
var results = new Set();
// Search for partial matches in word index
var _iterator4 = _createForOfIteratorHelper(this.wordIndex.entries()),
_step4;
try {
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
var _step4$value = _slicedToArray(_step4.value, 2),
word = _step4$value[0],
items = _step4$value[1];
if (word.includes(query)) {
items.forEach(function (name) {
return results.add(name);
});
}
}
// Also check if query matches any part of full names
} catch (err) {
_iterator4.e(err);
} finally {
_iterator4.f();
}
var _iterator5 = _createForOfIteratorHelper(this.nameToItem.keys()),
_step5;
try {
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
var name = _step5.value;
if (name.includes(query)) {
results.add(name);
}
}
// Return found items
} catch (err) {
_iterator5.e(err);
} finally {
_iterator5.f();
}
return Array.from(results).map(function (name) {
return _this.nameToItem.get(name);
});
}
/**
* Search by ID.
* @param {string} idQuery - ID or partial ID to search for
* @returns {Array} List of matching item objects
*/
}, {
key: "idSearch",
value: function idSearch(idQuery) {
if (this.partialIdSearch) {
// For geo data - allow partial matches
return Array.from(this.nameToItem.values()).filter(function (item) {
return item.id.includes(idQuery);
});
} else {
// For categories - only exact matches
return Array.from(this.nameToItem.values()).filter(function (item) {
return item.id === idQuery;
});
}
}
}]);
}();
/**
* Create a complete search system from a hierarchical tree structure.
* @param {Object} treeData - Original tree structure
* @param {boolean} joinIds - Whether to join IDs with parent
* @returns {HierarchicalIndex} Initialized search system
*/
function createHierarchicalIndex(treeData) {
var joinIds = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
// First flatten the tree
var flatItems = flattenTree(treeData, '', null, joinIds);
// Then create and return the search index
return new HierarchicalIndex(flatItems, joinIds);
}
module.exports = {
flattenTree: flattenTree,
HierarchicalIndex: HierarchicalIndex,
createHierarchicalIndex: createHierarchicalIndex
};