Skip to content

Instantly share code, notes, and snippets.

View ahmehri's full-sized avatar

Ahmed Mehri ahmehri

  • Berlin, Germany
View GitHub Profile
@ahmehri
ahmehri / find-duplicates.js
Created December 5, 2019 14:17
How to find duplicates in an array
import groupBy from 'lodash.groupby';
// How to find duplicates, e.g in this example by "label"
// from
// [
// { label: "brand", key: "1" },
// { label: "brand", key: "2" },
// { label: "state", key: "3" }
// { label: "state", key: "4" },
// { label: "uniq", key: "5" },
@ahmehri
ahmehri / object-values-to-array-without-losing-keys.js
Last active December 27, 2020 23:15
How to convert an object of elements to an array without losing the object keys
const mapValues = require("lodash.mapvalues");
// How to convert an object of elements to an array without losing the object
// keys?
// e.g going from
// {
// "attr-enum-sfa": { mode: "UPDATE", value: undefined },
// "attr-set-enum-sfa": { mode: "ADD_VALUES", value: undefined },
// "attr-text-sfa-s": { mode: "UPDATE", value: undefined }
// }
const { keyBy, mapKeys } = require("lodash");
let result;
const current = {
firstName: "Ahmed",
lastName: "Mehri",
age: 30
};
const { assign, cloneDeep } = require('lodash');
let obj = {
type: {
values: {
results: [1, 2]
}
}
};
let copy;
const crypto = require('crypto');
const bytes = crypto.randomBytes(8);
bytes
console.log(bytes.toString('hex'))
const queryString = require('query-string');
const result = queryString.stringify({
status: 'ready',
page: 0,
filterByQuery: 'filter',
})
result
const normalizr = require('normalizr');
const job = new normalizr.schema.Entity('jobs');
// normalizing []
const result = normalizr.normalize([], job);
console.log(result);
console.log(result.entities.jobs);
// normalizing {}
const _ = require('lodash');
let original = [1, 2];
// 2 has been removed, 3 has been added
let after = [1, 3];
console.log('removal:', _.difference(original, after));
console.log('addition:', _.difference(after, original));
const queryString = require('query-string');
const result = queryString.stringify({ name: 'ahmed', items: [1] });
result;
const arrayFormat = queryString.stringify(
{ name: 'ahmed', items: [1] },
{ arrayFormat: 'bracket' }
);
arrayFormat;
const _ = require('lodash');
function customizer(objValue, srcValue) {
if (_.isArray(objValue)) {
return srcValue;
}
}
var object = { ids: [1] };
var other = { ids: [] };