Skip to content

Instantly share code, notes, and snippets.

Mike Cavaliere mcavaliere

Block or report user

Report or block mcavaliere

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View namespacing-strategy.js
// Top level. All reusable code goes somewhere under this main object.
var MyApp = {};
// Widgets, and any components that manipulate the DOM.
MyApp.UI = {};
// Classes that have reusable logic or calculation.
MyApp.Lib = {};
// Custom code that works with external javascript libraries in a reusable way.
View es6-obj-class-conversion-initial-obj.js
export default {
configSettingExists( config, key ) {
// Search the config object for the key value
let found = Object.keys( config ).indexOf( key )
return found;
},
indexArrayBy(arr, attrName, group=false) {
// ...
},
View es6-obj-class-conversion-final-result.js
// Final config.js
export default class {
init() {
this.config = // ... load the object ...
// For posterity, but we don't need to return it at the moment.
return this.config;
},
settingExists( key ) {
View es6-obj-class-conversion-change-static-method-call.js
// Old
if ( ! Utils.configSettingExists( config, 'foo' ) ) {
Utils.printConfigFormatInstructions();
}
// New
if ( ! Config.configSettingExists( 'foo' ) ) {
Config.printConfigFormatInstructions();
View es6-obj-class-conversion-add-instantiation.js
// Old
import Utils from 'utils.js';
// Load the config.
let config = Utils.initConfig();
// New
import Config from 'config.js';
Config.initConfig();
View es6-obj-class-conversion-make-methods-static.js
export default class {
// ...
static printConfigFormatInstructions() {
console.log('Your config file must be in json format, and contain the following keys: foo, bar, baz.');
}
}
View es6-obj-class-conversion-save-data-to-instance.js
export default class {
initConfig() {
this.config = // ... load a json file via AJAX, filesystem, etc. ...
return this.config;
},
configSettingExists( key ) {
// Search the config object for the key value
View es6-obj-class-conversion-initial-references.js
import Utils from 'utils.js';
// Load the config.
let config = Utils.initConfig();
// Later, use it
if ( ! Utils.configSettingExists( config, 'foo' ) ) {
Utils.printConfigFormatInstructions();
}
View es6-obj-class-conversion-change-obj-def.js
// Old
export default {
// ...
}
// New
export default class {
// ...
}
@mcavaliere
mcavaliere / AsyncStorageSupplement.js
Last active Jul 2, 2018
Workaround for promise issues with AsyncStorage multiSet/multiGet/multiRemove in React Native (unresolved as of 2018-07-02). See: https://github.com/facebook/react-native/issues/14101
View AsyncStorageSupplement.js
export default class AsyncStorageSupplement {
static multiGet(keys) {
return Promise.all(
keys.map(key => AsyncStorage.getItem(key))
)
}
static multiRemove(keys) {
return Promise.all(
keys.map(key => AsyncStorage.removeItem(key))
You can’t perform that action at this time.