Skip to content

Instantly share code, notes, and snippets.

Avatar
🌱
what's up!

cyrilf cyrilf

🌱
what's up!
View GitHub Profile
@cyrilf
cyrilf / generatePassword.js
Created Oct 18, 2018
Simple password generator in JS
View generatePassword.js
// Add IE11 support
const crypto = window.crypto || window.msCrypto
const generatePassword = (length = 10) => {
const buffer = new Uint8Array(12)
crypto.getRandomValues(buffer)
return btoa(String.fromCharCode.apply(null, buffer)).substr(0, length)
}
export default generatePassword
@cyrilf
cyrilf / flattenArray.js
Created Oct 18, 2018
React util function to find and replace string with links in a label
View flattenArray.js
const flattenArray = (arr) => arr.reduce((result, value) => result.concat(value), [])
@cyrilf
cyrilf / react-select-event.jsx
Created Aug 30, 2018
Solution to receive an event with react-select onChange method
View react-select-event.jsx
// Issue: `react-select` `onChange` method doesn't return a proper event
//
// Solution used:
//
// The main idea is to have an hidden `select` that will trigger a real event
// When `ReactSelect` triggers it's `onChange` we set the state with the `selectedOptions`
// these `selectedOptions` will be what populate our hidden select.
// Using the callback from `setState` (it ensures that the new values are already into our hidden select),
// we then create a change event and dispatch it into our hidden select.
// This will cause the `onChange` from this select to be triggered with a proper `event`.
View proxy.js
// From: https://stackoverflow.com/questions/50963215/can-we-specify-a-generic-getter-on-an-object-in-javascript
// Allows you to access nested property (1 level) without the need to to safety check
const properties = { email: { title: "Email", value: "xyz@gh.com" } }
const proxy = new Proxy(properties, {
get: (target, prop) => (prop in target) ? target[prop] : {},
})
// or
View get.js
// Utility function to get access nested object properties securely
//
// Usage:
// const invoiceName = get(account, "user.invoiceAddress.name")
//
// Instead of:
// const invoiceName = (account && account.user && account.user.invoiceAddress && account.user.invoiceAddress.name) || null
// https://stackoverflow.com/a/23809123/1410020
const get = (obj, key) =>
@cyrilf
cyrilf / hoc.js
Created Jan 17, 2018
React custom life-cycle method
View hoc.js
// Inspired by https://github.com/MicheleBertoli/react-automata/blob/d9128bebe30df83c41bff4ed806549241fcf3b04/src/withStatechart.js
// Example of how to use a custom life-cycle method for a wrapped component
import React from 'react'
const isStateless = Component =>
!(Component.prototype && Component.prototype.isReactComponent)
const withSomething = Component => {
@cyrilf
cyrilf / includedByState-example.js
Last active Aug 29, 2015
includedByState filter improvement
View includedByState-example.js
// Improvement proposal
$IncludedByStateFilter.$inject = ['$state'];
function $IncludedByStateFilter($state) {
var includesFilter = function (state, params, options) {
return $state.includes(state, params, options);
};
includesFilter.$stateful = true;
return includesFilter;
}
@cyrilf
cyrilf / ZfcUserZendDb.php
Created Jul 23, 2013
BjyAuthorize issue #170
View ZfcUserZendDb.php
<?php
/**
* BjyAuthorize Module (https://github.com/bjyoungblood/BjyAuthorize)
*
* @link https://github.com/bjyoungblood/BjyAuthorize for the canonical source repository
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace BjyAuthorize\Provider\Identity;
@cyrilf
cyrilf / hasNestedProperties.js
Created Jun 11, 2013
Helper functions in JavaScript
View hasNestedProperties.js
/**
* hasNestedProperties check if an object has the nested
* properties passed in params
*
* @param {Object} obj object we want to test
* @param {String} properties nested property we want
* @return {Boolean} either the object has these
* nested properties or not
*/
@cyrilf
cyrilf / saveWAH.js
Created Apr 12, 2013
Save your We Are Hunted playlist. Download all your We Are Hunted tracks infos in a JSON file.
View saveWAH.js
// Save your We Are Hunted playlist
// Download all your We Are Hunted tracks infos in a JSON file
// by Cyril F (github.com/cyrilf)
// Instructions
//
// Go to wearehunted.com
// Change the USERNAME below by your username or email.
// Copy and paste the following code into your javascript
// console.