Skip to content

Instantly share code, notes, and snippets.

View cirocosta's full-sized avatar

Ciro S. Costa cirocosta

View GitHub Profile
@cirocosta
cirocosta / traverse-obj.js
Created May 9, 2014 20:49
Traverse JS object
function traverseObj (obj) {
if (typeof obj === "object") {
$.each(obj, function(index, value) {
console.log(index);
traverse(value);
});
} else {
console.log(obj);
}
}
@cirocosta
cirocosta / example.js
Last active August 29, 2015 14:01
Simple NodeJS dir Watcher
#!/usr/bin/env node
var watchFile = require('./watcher');
watchFiles(process.cwd(), function (file) {
console.log(file);
});
@cirocosta
cirocosta / find-r.js
Created May 20, 2014 03:03
Recursivelly find js files given root and ignored paths
var path = require('path');
var fs = require('fs');
function getJsFilesInPath (dir, ignoredPaths, files) {
'use strict';
var join = path.join;
files = files || [];
fs.readdirSync(dir).filter(function (filePath) {
@cirocosta
cirocosta / eased-inp.js
Last active August 29, 2015 14:01
Eased input in AngularJS
/**
* Applies logic from [debounce](http://underscorejs.org/#debounce)
* to angularjs.
* ver mais em http://loopinfinito.com.br/2013/09/24/throttle-e-debounce-patterns-em-javascript/
*/
function Ctrl($scope) {
$scope.$watch('typing', debounce(function() {
$scope.typed = $scope.typing;
$scope.$apply();
@cirocosta
cirocosta / has-duplicates.js
Created May 23, 2014 19:10
Verifies if there's a duplicated string in an array (javascript)
function hasDuplicates(array) {
var valuesSoFar = {};
for (var i = 0; i < array.length; ++i) {
var value = array[i];
if (Object.prototype.hasOwnProperty.call(valuesSoFar, value)) {
return true;
}
valuesSoFar[value] = true;
}
return false;
@cirocosta
cirocosta / format-number.js
Last active August 29, 2015 14:01
Dado um numero, formata o mesmo (para 'currency-a-like') de acordo com um divisor passado.
/**
* Dado um numero, formata o mesmo de acordo com um divisor passado.
* @param {'number'} number numero a formatar
* @param {'string'} divider dos centavos
* @return {'string'} o numero formatado.
*/
function formatNumber (number, cDivider) {
number = number.toFixed(2) + '';
var x = number.split('.')
, x1 = x[0]
@cirocosta
cirocosta / translate-file-with-list.js
Last active August 29, 2015 14:01
Translates a file with a list of texts to be translated
#!/usr/bin/env node
var Lazy = require('lazy');
var fs = require('fs');
var poliglota = require('poliglota');
var argv = require('minimist')(process.argv.slice(2));
new Lazy(fs.createReadStream(argv.filename || 'port.txt'))
.lines
.forEach(function (line) {
@cirocosta
cirocosta / iframe.html
Last active January 6, 2024 23:02
Sending messages from child iframe to parent webpage
<!DOCTYPE html>
<html>
<head>
<title>My Iframe</title>
</head>
<body>
<button>Botão</button>
<script type="text/javascript">
@cirocosta
cirocosta / ddds.json
Created June 10, 2014 02:21
Brazilian DDDs
{
"ddds":[
68,
82,
97,
92,
96,
75,
73,
74,
@cirocosta
cirocosta / flatten.js
Created June 22, 2014 15:04
array flattenizer
function isArray (obj) {
return toString.call(obj) === '[object Array]';
}
function flatten (arr) {
var result = [];
for (var i in arr) {
if (isArray(arr[i])) {
flatten(arr[i]).forEach(function (elem) {