Skip to content

Instantly share code, notes, and snippets.

View VanDalkvist's full-sized avatar

Ivan VanDalkvist

  • Yandex
View GitHub Profile
@VanDalkvist
VanDalkvist / _missExecutionIfAnotherIsProcessing.js
Created July 13, 2017 13:29
Miss execution if another is processing
function _missExecutionIfAnotherIsProcessing(action) {
return function () {
if (action.busy) {
logger.debug("Missing new execution. Another instance is processing now.");
return Q.when(action.waiting);
}
action.busy = true;
action.waiting = action.apply(undefined, Array.prototype.slice.call(arguments)).then(function (res) {
action.busy = false;
@VanDalkvist
VanDalkvist / cases.md
Created March 29, 2017 14:11
HTML and JS interesting cases

Detect resizing

$(".box").on("webkitTransitionEnd transitionend oTransitionEnd", function (event) {
  if (event.originalEvent.propertyName == "width") {
    // bla-bla
  }
  if (event.originalEvent.propertyName == "height") {
    // bla-bla
 }
function encodeAndSerializeJson(obj) {
if (typeof obj === 'string') {
return encodeURIComponent(obj);
} else if (Array.isArray(obj)) {
for (var i = obj.length; i--;) {
obj[i] = encodeAndSerializeJson(obj[i]);
}
} else {
var keys = Object.keys(obj);
for (var i = keys.length; i--;) {
/**
* Performance reporting for Knockout binding handlers
*
* Usage: Include after all bindings are declared, view console for results.
*/
(function () {
var report = [];
var lastReport = 0;
var debounceWait = 500;

AngularJS Directive Attribute Binding Explanation

When using directives, you often need to pass parameters to the directive. This can be done in several ways. The first 3 can be used whether scope is true or false. This is still a WIP, so validate for yourself.

  1. Raw Attribute Strings

    <div my-directive="some string" another-param="another string"></div>
@VanDalkvist
VanDalkvist / angularjs-providers-explained.md
Created April 20, 2016 20:05 — forked from demisx/angularjs-providers-explained.md
AngularJS Providers: Constant/Value/Service/Factory/Decorator/Provider
Provider Singleton Instantiable Configurable
Constant Yes No No
Value Yes No No
Service Yes No No
Factory Yes Yes No
Decorator Yes No? No
Provider Yes Yes Yes

Constant

@VanDalkvist
VanDalkvist / timeout.js
Last active April 7, 2016 13:06
Timeout on promises with cancelling support
function startOnce(callback, delay) {
var delayed = Q.delay(delay);
var deferred = Q.defer();
delayed.then(deferred.resolve);
var promise = deferred.promise.then(function () {
_handleDeferred(deferred, callback());
}, function (res) {
if (!res.cancelled) throw res;
@VanDalkvist
VanDalkvist / hash-files.js
Created April 1, 2016 13:52
Hash files in Node.Js
var hash = crypto.createHash('md5'),
stream = fs.createReadStream('mybigfile.dat');
stream.on('data', function (data) {
hash.update(data, 'utf8')
})
stream.on('end', function () {
hash.digest('hex'); // 34f7a3113803f8ed3b8fd7ce5656ebec
})
@VanDalkvist
VanDalkvist / execution-time.js
Created April 1, 2016 13:51
How to Measure Execution Time in Node.js
var start = new Date();
var hrstart = process.hrtime();
setTimeout(function (argument) {
// execution time simulated with setTimeout function
var end = new Date() - start,
hrend = process.hrtime(hrstart);
console.info("Execution time: %dms", end);
console.info("Execution time (hr): %ds %dms", hrend[0], hrend[1]/1000000);
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param(
[string]$archive,
[string]$out
)
[System.IO.Compression.ZipFile]::ExtractToDirectory($archive, $out)
}