Skip to content

Instantly share code, notes, and snippets.

View faridv's full-sized avatar
😐
‌Dealing with challenges like this -> :|

Farid Rn faridv

😐
‌Dealing with challenges like this -> :|
View GitHub Profile
@faridv
faridv / last-word.js
Created October 31, 2015 12:42 — forked from ramseyp/last-word.js
Select the last word in an element & wrap it with a span tag
jQuery(document).ready(function($){
$('h2.title').html(function(){
// separate the text by spaces
var text= $(this).text().split(' ');
// drop the last word and store it in a variable
var last = text.pop();
// join the text back and if it has more than 1 word add the span tag
// to the last word
return text.join(" ") + (text.length > 0 ? ' <span class="last">'+last+'</span>' : last);
@faridv
faridv / serializeobject.js
Last active July 27, 2017 08:34
jQuery plugin to serialize form as an object
// Serialize Object Plugin
"use strict";
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray(); // serializeArray - serialize form as an array instead of default object
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
@faridv
faridv / serializeobject.js
Created July 27, 2017 08:34
jQuery plugin to serialize form as an object (also using underscore.js)
"use strict";
$.fn.serializeObject = function () {
var arr = this.serializeArray();
return _.reduce(arr, function (memo, f) {
var objField = _.reduceRight(f.name.replace(/\[/g, ".").replace(/\]/g, "").split("."), function (memo, p) {
var n = (/^[0-9]+$/.test(p)) ? [] : {};
n[p] = memo;
return n;
}, f.value);
@faridv
faridv / $_get.js
Created July 27, 2017 08:39
Get URL query parameters and store the in a global object
window.$_GET = {};
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
function decode(s) {
return decodeURIComponent(s.split("+").join(" "));
}
$_GET[decode(arguments[1])] = decode(arguments[2]);
});
@faridv
faridv / persianToLatinDigits.js
Created July 27, 2017 08:42
Convert Persian digits to Latin digits
function persianToLatinDigits (s) {
var a = ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"];
var p = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
for (var i = 0; i < 10; i++) {
s = s.replace(new RegExp(a[i], 'g'), i).replace(new RegExp(p[i], 'g'), i);
}
return s;
}
@faridv
faridv / Convert Persian & English digits together
Created August 7, 2017 13:09 — forked from MeTe-30/Convert Persian & English digits together
Convert Persian & English digits together | Javascript
String.prototype.toPersianDigits = function () {
var id = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
return this.replace(/[0-9]/g, function (w) {
return id[+w];
});
};
String.prototype.toEnglishDigits = function () {
var id = { '۰': '0', '۱': '1', '۲': '2', '۳': '3', '۴': '4', '۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9' };
@faridv
faridv / strip_tags.js
Created August 8, 2017 06:51
Removing unwanted html tags while keeping their contents -- similar to PHP's strip_tags()
// Grabbed from here: https://stackoverflow.com/a/5601929/893111
var whitelist = 'p, img, strong';
$("*").not(self.whitelist).each(function(){
var content = $(this).contents();
$(this).replaceWith(content);
});
@faridv
faridv / StorageHelper.ts
Created December 16, 2017 13:20 — forked from Digiman/StorageHelper.ts
Simple helper module on TypeScript for using local storage (HTML5) in browser. Also have the class to store the list of emails that need to use for autocomplete in the some pages.
// module with classes and logic for working with local storage in browsers via JavaScript
// see also: http://professorweb.ru/my/html/html5/level5/5_1.php
module StorageHelper {
export interface IStorageItem {
key: string;
value: any;
}
export class StorageItem {
key: string;
@faridv
faridv / gist:c595a687eb8fa065885d1f05d621f434
Created June 17, 2018 07:48 — forked from psdtohtml5/gist:5908031
jQuery: Drag and scroll horizontal div
<script type="text/javascript">
$(document).ready(function() {
$('.dragger').mousedown(function (event) {
$(this)
.data('down', true)
.data('x', event.clientX)
.data('scrollLeft', this.scrollLeft)
.addClass("dragging");
return false;
@faridv
faridv / active-tab.js
Last active June 4, 2024 08:09
Event listener to check if window/tab is active or inactive
$(window).on("blur focus", function (e) {
var prevType = $(this).data("prevType");
if (prevType != e.type) { // reduce double fire issues
switch (e.type) {
case "blur":
// do work
console.log('blurred');
break;
case "focus":
// do work