Skip to content

Instantly share code, notes, and snippets.

View auxcoder's full-sized avatar
🗼
Working from home

AuxCoder auxcoder

🗼
Working from home
View GitHub Profile
@auxcoder
auxcoder / whatever.spec.js
Last active July 26, 2016 19:26
Test sample mocking $window service function
// http://stackoverflow.com/questions/20252382/how-to-mock-window-location-replace-in-angularjs-unit-test
describe('whatever', function() {
var $window, whatever;
beforeEach(module('services'));
beforeEach(function() {
$window = {location: { replace: jasmine.createSpy()} };
module(function($provide) {
@auxcoder
auxcoder / interact-angular-from-console.md
Last active August 21, 2017 17:23
Access to Angular from console to debug

Access to Angular from console to debug

We can get whatever service we like and then call an specific method.

var serviceName = angular.element(document.body).injector().get('serviceName');
serviceName.someMethod();
@auxcoder
auxcoder / createPromiseSpy.js
Created August 6, 2016 16:12 — forked from bbraithwaite/createPromiseSpy.js
Utility function for working with promises in angular tests.
var createPromiseSpy = function(obj, name, method, $q) {
var createdSpy = jasmine.createSpy(name, obj);
var returnObj = {};
var promise = {};
if (typeof(method) === 'string') {
var deferred = $q.defer();
spyOn(createdSpy, method).and.returnValue(deferred.promise);
promise[method] = deferred;
}
@auxcoder
auxcoder / supplant.js
Created August 8, 2016 16:01 — forked from pbroschwitz/supplant.js
supplant - Crockford
/**
* supplant() does variable substitution on the string. It scans through the string looking for
* expressions enclosed in { } braces. If an expression is found, use it as a key on the object,
* and if the key has a string value or number value, it is substituted for the bracket expression
* and it repeats.
*
* Written by Douglas Crockford
* http://www.crockford.com/
*/
String.prototype.supplant = function (o) {
@auxcoder
auxcoder / dynamic-name.directive.js
Last active August 17, 2016 15:52
form validation with ng-repeat ( undefined input fields )
(function() {
'use strict';
angular
.module('myApp')
.directive('dynamicName', dynamicName);
/** @ngInject */
function dynamicName($compile) {
// put name from attribute dynamic-name (input or form tag)
var directive = {
@auxcoder
auxcoder / array-sort-by-obj-key.js
Last active August 30, 2016 16:39
Order array of objects by object.attribute (sort)
// function passed into sort to order by obj.key
function compare(a,b) {
if (a.sequence < b.sequence){
return -1;
}
if (a.sequence > b.sequence){
return 1;
}
return 0;
}
@auxcoder
auxcoder / generate-guid.js
Last active November 23, 2016 15:43
Create GUID / UUID in JavaScript
// http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
// option 1
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
@auxcoder
auxcoder / acronym.filter.js
Created November 30, 2016 15:18
Angular filter to convert a frase/name (string input) into an acronym.
(function() {
'use strict';
angular.module('someApp').filter('acronym', function(){
return function(input){
return input.split(' ').map(function(x){ return x.charAt(0)}).join('').toUpperCase();
}
});
}();
@auxcoder
auxcoder / virtual-host-mac.markdown
Last active December 26, 2016 17:38
Solving issues with virtual host in MacOS

Virtual host entry in /etc/hosts

127.0.0.1	testwp.dev www.testwp.dev

When try to see it in browser respond with: testwp.dev refused to connect

Check doing a ping and works

ping testwp.dev
@auxcoder
auxcoder / map-object.js
Created December 28, 2016 21:37
Map over an Object returning a new Array
var data = {
'0': 'Some content',
'1': 'Another content',
'2': 23
};
var newArray = Object.keys(data).map(function(key, index) {
return {label: index + 1, value: data[key]};
});