Skip to content

Instantly share code, notes, and snippets.

View jimmont's full-sized avatar

Jim Montgomery jimmont

View GitHub Profile
@jimmont
jimmont / transitionEnd.js
Created May 3, 2014 20:34
transitionEnd attribute directive
/*
element{transition:height 500ms ease-out;height:10px;}
element.change{height:200px;}
<element transition-end="endTransition($event)"></element>
$scope.endTransition = function(e){ }
*/
.directive('transitionEnd', function($parse){
return {
restrict: 'A'
,link: function(scope,elem,attr){
@jimmont
jimmont / detectTextFile.js
Created May 23, 2014 17:39
check if a file is text based on its content
// detect if a text file, like Perl's stat(file); if(-T) ...
fs.open(file,'r',function(err, fd){
if(err) return;
var len = 100;
var buf = new Buffer(len);
var re = /[.a-z0-9(){}\[\]? "':;<>?\s\n\r\t "':;#@~$%*<>.,_=+-]/ig;
fs.read(fd,buf,0,len,0,function(err,num,buf){
if(err || num < 1) return;
var str = buf.toString('utf-8',0,num);
// do we have enough of the types of characters typical of a text file?
@jimmont
jimmont / anagramDetector
Last active August 29, 2015 14:02
your anagrams are showing doctor
/* usage: isAnagram('Elvis','lives'); */
function isAnagram(read,dear){
return read.toLowerCase().split('').sort().join('') === dear.toLowerCase().split('').sort().join('');
};
@jimmont
jimmont / sumArray-ng-filter.js
Created August 13, 2014 03:06
Infinity number error handling in Angular filters for pass-thru. Positive stuff!
/**
* @ngdoc filter
* @name sumArray
* @description
* return the sum of a list of values (no explicit type coercion)
*
* @example
expression:
`we have {{ [1, 2, 3] | sumArray }} items`
@jimmont
jimmont / ng-expressions.html
Last active August 29, 2015 14:05
Angular expression collection. Or something like that.
<script>
$scope.ping;
$scope.j = 4;
$scope.stuff = [3,7,1,0];
$scome.time = new Date
</script>
<hr ng-init="ping=[1,2,3]" ng-bind="::once" ng-attr-j="::{{ 1 + jOnce }}" ng-repeat="a in b = (stuff | orderBy: 'toString()' ) track by $index">
<time ng-attr-title="{{ time | date:'EEEE MMMM d, yyyy h:mm:ss a (\'GMT\'Z)' }}">{{time | date:'MM/dd/yy HH:mm:ss'}}</time>
(function() {
'use strict';
angular.module('derModule')
.directive('inputRange', ['$parse', function ngRangeSlider($parse) {
/**
* @ngdoc directive
* @name input-range
* @description
dual-control range inputs
* @example
@jimmont
jimmont / diff
Last active August 29, 2015 14:09
what changed from the default
function diff(defaults, customs, diffs){
/**
* @description
compare an object against the default returning an object with the differences
in the customs object separated out in the returned value
when there are no unique values in the customs object undefined is returned
* @returns
diff value, when no differences undefined, when an object returns object with keys and other values
* @example
usr.diff(defaults, custom);
@jimmont
jimmont / extend.js
Created January 14, 2015 23:03
Extend something, experimental alternative to SomeClass.prototype = AnotherClass() then extend further.
function extend( proto, template ){
var prop;
for(prop in template){
try{
if( typeof(template[prop]) === 'function' ){
// relay arguments, using predefined functions where possible
proto[ prop ] = proto[ prop ] || (function(prop){
return function(){ return this._xhr[ prop ].apply(this._xhr, arguments); };
})(prop);
continue;
@jimmont
jimmont / paginate.js
Created April 16, 2015 20:33
paginate
.directive('paginateThis', function(paginate, $parse, $location){
return {
restrict: 'A',
link: function (scope, elem, attr){
function mapOnToModel(res){
var p = scope.paginate
, _esp = JSON.stringify( res.options.query )
, _sort = JSON.stringify( res.options.sort )
;
@jimmont
jimmont / sortable.js
Created April 16, 2015 18:54
sort it out in angular
/**
* @ngdoc directive
* @name sortable
* @restrict A
*
* @description
* setup an object on the scope for handling a list: sorting, filtering, etc
* NOTE that track by statements must go at the end of the expression, after the orderBy statement
*
* @example