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
@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}}]);
@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 / 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 / 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 / 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>"
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 / fuzzysearch.md
Created August 26, 2016 06:44
Fuzzy search algorithm

What you need is (as i know) not supported by Parse Engine. You need a Phonetic search algorithm, but you have to retrieve all records from your PFObject to perform a Phonetic match on all the names (or fetch all starting with "D" and then perform the match on those only).

The Phonetic search algorithm is called SoundEx and various implementations have been done by developers all over the world, taking into account i.e. UK abbreviations like "St. = Saint" etc.

The SoundEx algorithm is very intelligent and is working as follows:

  1. Retain the first letter of the name and drop all other occurrences of a, e, i, o, u, y, h, w.
  2. Replace consonants with digits as follows (after the first letter):
  3. b, f, p, v => 1
  4. c, g, j, k, q, s, x, z => 2
@tejpratap46
tejpratap46 / app.js
Created December 26, 2016 07:16
Disable angularjs debug mode.
module.config(["$httpProvider", "$compileProvider", (
$httpProvider:IHttpProvider,
$compileProvider:ICompileProvider
) => {
$httpProvider.useApplyAsync(true);
$compileProvider.debugInfoEnabled(false);
}]);
@tejpratap46
tejpratap46 / SnapAdapter.java
Created February 13, 2017 12:10
Android RecyclerView Snap Adapter
// Horizontal snap adapter for recycler view, LinearSnapHelper is available from support lib 24.2.0
LinearSnapHelper snapHelper = new LinearSnapHelper() {
@Override
public int findTargetSnapPosition(RecyclerView.LayoutManager lm, int velocityX, int velocityY) {
View centerView = findSnapView(lm);
if (centerView == null)
return RecyclerView.NO_POSITION;
int position = lm.getPosition(centerView);
int targetPosition = -1;
@tejpratap46
tejpratap46 / SingletonEvent.js
Created May 25, 2017 14:39
A singleton class to manage events (pus-sub)
// A singleton class to manage events (pus-sub)
let eventManagerInstance = null;
export default class Event {
constructor(){
if (!stateManagerInstance) {
stateManagerInstance = this;
this.events = {};