function similaritySearch(content) {
return collection.find({}, { sort: { $vectorize: content }, limit: 5 })
.then(cursor => cursor.toArray())
.then(results => results.join("\n\n"))
.catch(error => console.error);
}
async function similaritySearch(content) {
try {
const cursor = await collection.find({}, { sort: { $vectorize: content }, limit: 5 });
const results = await cursor.toArray();
return results.join("\n\n");
} catch (error) {
console.log(error);
}
}
// Step 1
const weak = new WeakMap();
const key = Symbol('my ref');
const someObject = { foo: 'bar' };
weak.set(key, someObject);
// Step 2
// ???
// Step 3
// Profit!
const uniqueArray = Array.from(new Set(array));
array.includes?(item);
// O(n)
set.has(item);
// O(1)
const frontEndLanguages = new Set(["JavaScript", "HTML", "CSS"]);
const backEndLanguages = new Set(["Python", "Java", "JavaScript"]);
const allLanguages = new Set([...frontEndLanguages, ...backEndLanguages]);
// => Set(5) {"JavaScript", "HTML", "CSS", "Python", "Java"}
const frontEndLanguages = new Set(["JavaScript", "HTML", "CSS"]);
const backEndLanguages = new Set(["Python", "Java", "JavaScript"]);
const allLanguages = frontEndLanguages.union(backEndLanguages);
// => Set(5) {"JavaScript", "HTML", "CSS", "Python", "Java"}
const frontEndLanguages = new Set(["JavaScript", "HTML", "CSS"]);
const backEndLanguages = new Set(["Python", "Java", "JavaScript"]);
const frontAndBackLanguages = new Set([...frontEndLanguages].filter(item => {
return backEndLanguages.has(item);
}));
// => Set(1) {"JavaScript"}
const frontEndLanguages = new Set(["JavaScript", "HTML", "CSS"]);
const backEndLanguages = new Set(["Python", "Java", "JavaScript"]);
const frontAndBackLanguages = frontEndLanguages.intersection(backEndLanguages);
// => Set(1) {"JavaScript"}
const frontEndLanguages = new Set(["JavaScript", "HTML", "CSS"]);
const backEndLanguages = new Set(["Python", "Java", "JavaScript"]);
const onlyFrontEnd = new Set([...frontEndLanguages].filter(item => {
return !backEndLanguages.has(item);
}));
// => Set(1) {"HTML", "CSS"}
const frontEndLanguages = new Set(["JavaScript", "HTML", "CSS"]);
const backEndLanguages = new Set(["Python", "Java", "JavaScript"]);
const frontAndBackLanguages = frontEndLanguages.difference(backEndLanguages);
// => Set(1) {"HTML", "CSS"}
const frontEndLanguages = new Set(["JavaScript", "HTML", "CSS"]);
const backEndLanguages = new Set(["Python", "Java", "JavaScript"]);
const onlyFrontEnd = [...frontEndLanguages].filter(item =>
!backEndLanguages.has(item));
const onlyBackEnd = [...backEndLanguages].filter(item =>
!frontEndLanguages.has(item));
const singleEnvLanguages = new Set([...onlyFrontEnd, ...onlyBackEnd]);
// => Set(1) {"HTML", "CSS", "Python", "Java"}
const frontEndLanguages = new Set(["JavaScript", "HTML", "CSS"]);
const backEndLanguages = new Set(["Python", "Java", "JavaScript"]);
const singleEnvLanguages = frontEndLanguages.symmetricDifference(backEndLanguages);
// => Set(1) {"HTML", "CSS", "Python", "Java"}
const frontEndLanguages = new Set(["JavaScript", "HTML", "CSS"]);
const backEndLanguages = new Set(["Python", "Java", "JavaScript"]);
const markupAndStyle = new Set(["HTML", "CSS"]);
markupAndStyle.isSubsetOf(frontEndLanguages);
// => true
frontEndLanguages.isSupersetOf(markupAndStyle);
// => true
markupAndStyle.isDisjointFrom(backEndLanguages);
// => true
_.union(array1, array2);
lodash.union
receives 6.5 million downloads/week
_.difference(array1, array2);
lodash.difference
receives 6.5 million downloads/week
const array = ["JavaScript", "HTML", "CSS"];
const sorted = array.sort();
console.log(sorted);
// => ["CSS", "HTML", "JavaScript"]
console.log(array);
// => ["CSS", "HTML", "JavaScript"]
const array = ["JavaScript", "HTML", "CSS"];
const sorted = [...array].sort();
console.log(sorted);
// => ["CSS", "HTML", "JavaScript"]
console.log(array);
// => ["JavaScript", "HTML", "CSS"]
const array = ["JavaScript", "HTML", "CSS"];
const sorted = array.toSorted();
console.log(sorted);
// => ["CSS", "HTML", "JavaScript"]
console.log(array);
// => ["JavaScript", "HTML", "CSS"]
const array = ["JavaScript", "HTML", "CSS"];
const reversed = array.toReversed();
console.log(reversed);
// => ["CSS", "HTML", "JavaScript"]
console.log(array);
// => ["JavaScript", "HTML", "CSS"]
const array = ["JavaScript", "HTML", "CSS"];
const updated = array.toSpliced(0, 1, "TypeScript");
console.log(updated);
// => ["TypeScript", "HTML", "CSS"]
console.log(array);
// => ["JavaScript", "HTML", "CSS"]
const array = ["JavaScript", "HTML", "CSS"];
const removed = array.splice(0, 1, "TypeScript");
console.log(removed);
// => ["JavaScript"]
console.log(array);
// => ["TypeScript", "HTML", "CSS"]
const array = ["JavaScript", "HTML", "CSS"];
const removed = array.slice(0, 1);
console.log(removed);
// => ["JavaScript"]
console.log(array);
// => ["JavaScript", "HTML", "CSS"]
const array = ["JavaScript", "HTML", "CSS"];
array[0] = "TypeScript";
console.log(array);
// => ["TypeScript", "HTML", "CSS"]
const array = ["JavaScript", "HTML", "CSS"];
const updated = array.with(0, "TypeScript");
console.log(updated);
// => ["TypeScript", "HTML", "CSS"]
console.log(array);
// => ["JavaScript", "HTML", "CSS"]
class MyArray extends Array {};
const languages = new MyArray("JavaScript", "TypeScript", "CoffeeScript");
const upcasedLanguages = languages.map(language => language.toUpperCase());
console.log(upcasedLanguages instanceof MyArray);
// => true
const reversed = languages.toReversed();
console.log(reversed instanceof MyArray);
// => false
class MyArray extends Array {};
const languages = new MyArray("JavaScript", "TypeScript", "CoffeeScript");
const reversed = MyArray.from(languages.toReversed());
console.log(reversed instanceof MyArray);
// => true
const people = [
{ name: "Alice", age: 28 }, { name: "Bob", age: 30 }, { name: "Eve", age: 28 }
];
const peopleByAge = {};
people.forEach((person) => {
const age = person.age;
if (!peopleByAge[age]) {
peopleByAge[age] = [];
}
peopleByAge[age].push(person);
});
console.log(peopleByAge);
/*
{
"28": [{"name":"Alice","age":28}, {"name":"Eve","age":28}],
"30": [{"name":"Bob","age":30}]
}
*/
const people = [
{ name: "Alice", age: 28 }, { name: "Bob", age: 30 }, { name: "Eve", age: 28 }
];
const peopleByAge = Object.groupBy(people, (person) => person.age);
console.log(peopleByAge);
/*
{
"28": [{"name":"Alice","age":28}, {"name":"Eve","age":28}],
"30": [{"name":"Bob","age":30}]
}
*/
const people = [
{ name: "Alice", age: 28 }, { name: "Bob", age: 30 }, { name: "Eve", age: 28 }
];
const peopleByAge = Object.groupBy(people, (person) => person.age);
console.log(peopleByAge.hasOwnProperty("28"));
// TypeError: peopleByAge.hasOwnProperty is not a function
const ceo = { name: "Jamie", age: 40, reportsTo: null };
const manager = { name: "Alice", age: 28, reportsTo: ceo };
const people = [
ceo,
manager,
{ name: "Bob", age: 30, reportsTo: manager },
{ name: "Eve", age: 28, reportsTo: ceo },
];
const peopleByManager = Map.groupBy(people, (person) => person.reportsTo);
console.log(peopleByManager.get(ceo));
/* => [
{ name: "Alice", age: 28, reportsTo: ceo },
{ name: "Eve", age: 28, reportsTo: ceo }
] */
peopleByManager.get({ name: "Jamie", age: 40, reportsTo: null });
// => undefined
Or why is it Object.groupBy
and not
Array.prototype.group
?
_.groupBy(people, person => person.age);
lodash.groupby
receives 2.5 million downloads/week
Built-in immutable objects
Compound primitives
Deep immutability
New syntax
const tuple = #[1,2,3];
const record = #{ name: "Alice", age: 28 };
const newTuple = tuple.with(0, 4);
// => #[4,2,3]
record === #{ age: 28, name: "Alice" };
// => true
record["name"] = "Betty";
// TypeError '"name" is read-only'
const recordWithObject = #{ name: "Betty", dob: new Date(1984, 8, 14) };
// TypeError 'cannot use an object as a value in a record'
const registry = new WeakMap();
const dob = Symbol('dob');
registry.set(dob, new Date(1984, 8, 14));
const record = #{ name: "Betty", dob: dob };
console.log(registry.get(record.dob));
// => "Fri Sep 14 1984"
Check out the playground: