This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ENV['REDIS_ACCESS_MODE'] == 'readonly' | |
class Redis | |
class Client | |
WRITE_COMMANDS = ::Rails.cache.data.command.map { |a| a[0] if a[2].include?('write') }.compact.to_set.freeze | |
def process(commands) | |
if commands.flatten.any? { |c| WRITE_COMMANDS.include?(c.to_s) } | |
raise NotImplementedError, "REDIS_ACCESS_MODE is set to 'readonly', disallowing writes" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/redis.rb | |
require 'redis' | |
# Disables the `flushdb` and `flushall` commands. | |
class Redis | |
module DangerousCommands | |
def flushdb | |
raise 'This is EXTREMELY DANGEROUS! If you really want to EMPTY THE ENTIRE DATABASE, do it from `redis-cli`.' | |
# You could call `super` here if you want to allow access in some circumstances. | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let obj = { a: 2, b: 4, c: 2, k: 1 } | |
let sortedKeys = Object.keys(obj).sort((a,b)=>{obj[b]-obj[a]}) | |
console.log(sortedKeys) // [ 'b', 'a', 'c', 'k' ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sortedObj = sortedKeys.reduce((acc,ltr) => { | |
acc[ltr] = obj[ltr] | |
return acc | |
},{}) | |
console.log(sortedObj) // { b: 4, a: 2, c: 2, k: 1 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let obj = { a: 2, b: 4, c: 2, k: 1 } | |
let sortedKeys = Object.keys(obj).sort((a,b)=>obj[b]-obj[a]) | |
console.log(sortedKeys) // [ 'b', 'a', 'c', 'k' ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// subtracting arr2 from arr1 | |
let subtracting = arr1.filter(x => !arr2.includes(x)); | |
// intersection between arr1 and arr2 | |
let intersection = arr1.filter(x => arr2.includes(x)); | |
// NotIntersection: all elements but intersection elements | |
let NotIntersection = | |
arr1.filter(x => !arr2.includes(x)) | |
.concat(arr2.filter(x => !arr1.includes(x))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let strArr = [ 'a', 'a', 'b', 'b', 'b', 'c', 'b', 'c', 'k' ] | |
let uniqueVal = strArr.filter((elem, i) => strArr.indexOf(elem) == i); | |
console.log(uniqueVal) // [ 'a', 'b', 'c', 'k' ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let obj = { a: 2, b: 4, c: 2, k: 1 } | |
let sortedArr = Object.entries(obj).sort((a,b)=> b[1] - a[1]) | |
console.log(sortedArr) // [ [ 'b', 4 ], [ 'a', 2 ], [ 'c', 2 ], [ 'k', 1 ] ] | |
let sortedToObject = sortedArr.reduce((acc, itr)=>{ | |
acc[itr[0]] = itr[1] | |
return acc; | |
}, {}) | |
console.log(sortedToObject) // { b: 4, a: 2, c: 2, k: 1 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let string = "aabbbcbck" | |
let strArr = string.split("") // [ 'a', 'a', 'b', 'b', 'b', 'c', 'b', 'c', 'k' ] | |
// Array to object using For-Loop - counting each letter occurances | |
let usingForLoop = (arr)=>{ | |
let newObj={}; | |
for(let i=0; i<arr.length; ++i){ | |
newObj[arr[i]] = newObj[arr[i]]+1 || 1; | |
} | |
return newObj; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let string = "aabbbcbck" | |
let strArr = string.split("") // [ 'a', 'a', 'b', 'b', 'b', 'c', 'b', 'c', 'k' ] | |
// Array to object using reduce - counting each letter occurances | |
let obj = strArr.reduce((acc,ltr) => { | |
acc[ltr] = ++acc[ltr] || 1 | |
return acc | |
},{}) | |
console.log("reduce - object: ", obj) // { a: 2, b: 4, c: 2, k: 1 } |