Published
Edited
Apr 30, 2021
3 stars
Insert cell
Insert cell
_ = require("lodash@4")
Insert cell
popularMethodNames = _.map(mostPopularPerMethodPackages, d => getMethodName(d.name))
Insert cell
mostPopularPerMethodPackages = _.take(ranking, 10) // per 2021-04-21
Insert cell
Insert cell
Insert cell
Insert cell
users = [
{user: "fred", age: 48},
{user: "barney", age: 36},
{user: "fred", age: 40},
{user: "barney", age: 34}
]
Insert cell
_.sortBy(users, [o => o.user])
Insert cell
{
const shallowCopy = users.slice();
return shallowCopy.sort((a, b) => a.user.localeCompare(b.user)) // native equivalent (for this specific case)
}
Insert cell
_.sortBy(users, ["user", "age"])
Insert cell
users.slice().sort((a, b) => a.age - b.age) // native equivalent (for this specific case)
Insert cell
Insert cell
Insert cell
_.uniq([2, 1, 2])
Insert cell
[...new Set([2, 1, 2])] // native equivalent
Insert cell
Insert cell
Insert cell
{
var object = {a: 1, b: 2};
var other = {c: 3, d: 4};
var values = _.memoize(_.values);
values(object);
// => [1, 2]
values(other);
// => [3, 4]
object.a = 2;
values(object);
// => [1, 2]
// Modify the result cache.
values.cache.set(object, ["a", "b"]);
values(object);
// => ["a", "b"]
}
Insert cell
Insert cell
Insert cell
{
function Foo() {
this.a = 1;
}

return _.isPlainObject(new Foo);
}
Insert cell
_.isPlainObject([1, 2, 3])
Insert cell
_.isPlainObject({x: 0, y: 0})
Insert cell
_.isPlainObject(Object.create(null))
Insert cell
Insert cell
Insert cell
{
// Use the "interpolate" delimiter to create a compiled template.
const compiled = _.template("hello <%= user %>!");
return compiled({user: "fred"});
}
Insert cell
{
// Use the HTML "escape" delimiter to escape data property values.
const compiled = _.template("<b><%- value %></b>");
return compiled({value: "<script>"});
}
Insert cell
{
// Use the "evaluate" delimiter to execute JavaScript and generate HTML.
const compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
return compiled({users: ["fred", "barney"]});
}
Insert cell
{
// Use the internal `print` function in "evaluate" delimiters.
const compiled = _.template('<% print("hello " + user); %>!');
return compiled({user: "barney"});
}
Insert cell
{
// Use the ES template literal delimiter as an "interpolate" delimiter.
// Disable support by replacing the "interpolate" delimiter.
var compiled = _.template('hello ${ user }!');
return compiled({user: "pebbles"});
}
Insert cell
{
// Use backslashes to treat delimiters as plain text.
const compiled = _.template('<%= "\\<%- value %\\>" %>');
return compiled({value: "ignored"});
}
Insert cell
{
const jQuery = await require("jquery@3");
// Use the `imports` option to import `jQuery` as `jq`.
const text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
const compiled = _.template(text, {imports: {jq: jQuery}});
return compiled({users: ["fred", "barney"]});
}
Insert cell
{
const jQuery = await require("jquery@3");
const users = ["fred", "barney"];
return jQuery.each(users, user => `<li>${user}</li>`)
}
Insert cell
{
// Use the `variable` option to ensure a with-statement isn't used in the compiled template.
const compiled = _.template('hi <%= data.user %>!', {variable: "data"});
return compiled.source;
}
Insert cell
{
// Use new pristine lodash function.
const lo = _.runInContext();
// Use custom template delimiters.
lo.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
const compiled = lo.template('hello {{ user }}!');
return compiled({ 'user': 'mustache' });
}
Insert cell
Insert cell
Insert cell
object = ({
a: [{b: {c: 3}}]
})
Insert cell
_.get(object, "a[0].b.c")
Insert cell
_.get(object, ["a", "0", "b", "c"])
Insert cell
_.get(object, "a.b.c", "default")
Insert cell
object.a?.b?.c ?? "default" // native equivalent (for this specific case)
Insert cell
Insert cell
Insert cell
{
const form = html`<form>
<input type="range" max="1" step="any" name="input">
<output name="output">0.5</output>
</form>`;
const onInput = (e) => form.output.innerText = e.target.value;
const delayInMilliseconds = 100;
form.input.oninput = _.debounce(onInput, delayInMilliseconds);
return form;
}
Insert cell
Insert cell
Insert cell
objects = [
{a: 1},
{b: 2}
];
Insert cell
deep = _.cloneDeep(objects);
Insert cell
JSON.parse(JSON.stringify(objects)) // native equivalent (for this specific case)
Insert cell
Insert cell
Insert cell
_.isString("abc")
Insert cell
_.isString(1)
Insert cell
typeof 1 === "string" // native equivalent (for this specific case)
Insert cell
Insert cell
Insert cell
initialize = {
const createApplication = () => Date.now();
return _.once(createApplication);
}
Insert cell
initialize()
Insert cell
{
await Promises.delay(10);
return initialize();
}
Insert cell
Insert cell
import {compose, composeParagraphed}
with {getDocumentationUrl as getDocumentationUrl, formatExample as formatExample}
from "@nikita-sharov/lodash-documentation-generator"
Insert cell
getDocumentationUrl = (methodName) => `https://lodash.com/docs/4#${methodName}`
Insert cell
formatExample = () => _.stubString()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more