Skip to content

Instantly share code, notes, and snippets.

View tejpratap46's full-sized avatar
🏠
Working from home

Tej Pratap Singh tejpratap46

🏠
Working from home
View GitHub Profile
angular.module('test', [])
.directive('ngDraggable', function($document) {
return {
restrict: 'A',
scope: {
dragOptions: '=ngDraggable'
},
link: function(scope, elem, attr) {
var startX, startY, x = 0, y = 0,
@tejpratap46
tejpratap46 / app.js
Created July 25, 2016 06:58
Nested comments in Angular 1.x
angular.module('APP', [])
.directive('collection', function () {
return {
restrict: "E",
replace: true,
scope: {
collection: '='
},
template: "<ul><member ng-repeat='member in collection' member='member'></member></ul>"
@tejpratap46
tejpratap46 / validUsername.js
Created July 21, 2016 12:36
Check if string is valid as a variable. It is useful for selection username etc.
function isVarName(str) {
'use strict';
if (typeof str !== 'string') {
return false;
}
try {
new Function('var ' + str)();
} catch (e) {
@tejpratap46
tejpratap46 / removeDuplicateFromMongoDB.js
Created July 12, 2016 11:19
Delete duplicates from mongodb
`dropDups: true` option is not available in 3.0.
I have solution with aggregation framework for collecting duplicates and then removing in one go.
It might be somewhat slower than system level "index" changes. But it is good by considering way you want to remove duplicate documents.
a. Remove all documents in one go
var duplicates = [];
@tejpratap46
tejpratap46 / androidPrint.java
Created July 12, 2016 07:23
print a simple webview android
webView.loadData(html, "text/html", "UTF-8");
private void print(WebView webView) {
try {
// PrintManager
String PRINT_SERVICE = (String) Context.class.getDeclaredField(
"PRINT_SERVICE").get(null);
Object printManager = this.getSystemService(PRINT_SERVICE);
// PrintDocumentAdapter
@tejpratap46
tejpratap46 / mongodb_distinct_count.js
Created May 25, 2016 09:06 — forked from clarkenheim/mongodb_distinct_count.js
MongoDB equivalent of an SQL query to get the distinct values of a field in a collection including the count of documents which have each distinct value (distinct with count)
//equivalent of MySQL "SELECT COUNT(*) AS `count`, `fieldName` FROM `someTable` GROUP BY `fieldName
db.someCollection.aggregate([{"$group" : {_id:"$fieldName", count:{$sum:1}}}]);
//as above but ordered by the count descending
db.someCollection.aggregate([{"$group" : {_id:"$fieldName", count:{$sum:1}}}, {$sort:{'count':-1}}]);