Skip to content

Commit 3937623

Browse files
committed
Version 2.14 update. Added type 2 pattern (extended pattern). Type1 (extended:true) is deprecated in favor of type 2.
1 parent 18103ae commit 3937623

3 files changed

Lines changed: 1316 additions & 50 deletions

File tree

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,25 @@ This is the main function that outputs the user's typing pattern as a `String`
3636

3737
**Returns**: A typing pattern in `String` form
3838

39-
**optionsObject**: An object of the following form {type:Number, text:String, textId:Number, length: Number, extended:Boolean, targetId:String, caseSensitive:Boolean}. Detail table below.
39+
**optionsObject**: An object of the following form {type:Number, text:String, textId:Number, length: Number, targetId:String, caseSensitive:Boolean}. Detail table below.
4040

4141
| Param | Type | Description |
4242
| --- | --- | --- |
43-
| **type** | `Number` | `0 for anytext pattern` (when you compare random typed texts of usually 120-180 chars long) <br> `1 for diagram pattern` (recommended in most cases, for emails, passwords, phone numbers, credit cards, short texts) |
44-
| **text** | `String` | (Only for type 1) a typed string that you want the typing pattern for |
45-
| **textId** | `Number` | (Optional, only for type 1) a personalized id for the typed text |
43+
| **type** | `Number` | `0 for anytext pattern` (when you compare random typed texts of usually 120-180 chars long) <br> `1 for sametext pattern` (also called diagram pattern, recommended in most cases, for emails, passwords, phone numbers, credit cards, short texts)<br> ` 2 for extended pattern` (most versatile, can replace both anytext and sametext patterns) |
44+
| **text** | `String` | (Only for type 1 and type 2) a typed string that you want the typing pattern for |
45+
| **textId** | `Number` | (Optional, only for type 1 and type 2) a personalized id for the typed text |
4646
| **length** | `Number` | (Optional) the length of the text in the history for which you want the typing pattern, for type 0 is usually 140 or more |
47-
| **extended** | `Boolean` | (Only for type 1) specifies if full information about what was typed is produced, including the actual key pressed, if false, only the order of pressed keys is kept (no actual content) |
4847
| **targetId** | `String` | (Optional) specifies if pattern is obtain only from text typed in a certain target |
49-
| **caseSensitive** | `Boolean` | (Optional, default: false, Only for type 1) Used if you pass a text for type 1 |
48+
| **caseSensitive** | `Boolean` | (Optional, default: false) Used if you pass a text for type 1 or type 2|
5049

5150
**Examples**
5251
```js
53-
var diagramPattern = tdna.getTypingPattern({type=1, text="Hello5g21?*", extedend=false});
54-
var extendedDiagramPattern = tdna.getTypingPattern({type=1, length=160, extended=true});
55-
var generalPattern = tdna.getTypingPattern({type=0, length=160});
52+
//anytext pattern
53+
var typingPattern = tdna.getTypingPattern({type=0, length=160});
54+
//sametext pattern
55+
var typingPattern = tdna.getTypingPattern({type=1, text="Hello5g21?*"});
56+
//extended pattern
57+
var typingPattern = tdna.getTypingPattern({type=2, text="example@mail.com"});
5658
```
5759

5860
### TypingDNA.addTarget(element_id)

typingdna.js

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* https://api.typingdna.com/scripts/typingdna.js
44
* https://typingdna.com/scripts/typingdna.js (alternative)
55
*
6-
* @version 2.13
6+
* @version 2.14
77
* @author Raul Popa
88
* @copyright SC TypingDNA SRL, http://typingdna.com
99
* @license http://www.apache.org/licenses/LICENSE-2.0
@@ -20,7 +20,7 @@
2020
*
2121
* Typical usage:
2222
* var tdna = new TypingDNA(); // creates a new TypingDNA object and starts recording
23-
* var typingPattern = tdna.getTypingPattern({type=1, text="Hello5g21?*", extedend=false});
23+
* var typingPattern = tdna.getTypingPattern({type=1, text="Hello5g21?*"});
2424
* returns a type 1 typing pattern (and continues recording afterwards)
2525
*
2626
* Optional:
@@ -102,7 +102,7 @@ function TypingDNA() {
102102
TypingDNA.prototype.defaultHistoryLength = TypingDNA.defaultHistoryLength;
103103
TypingDNA.prototype.maxSeekTime = TypingDNA.maxSeekTime;
104104
TypingDNA.prototype.maxPressTime = TypingDNA.maxPressTime;
105-
TypingDNA.version = 2.13;
105+
TypingDNA.version = 2.14;
106106
TypingDNA.flags = 0;
107107
TypingDNA.instance = this;
108108
TypingDNA.document = document;
@@ -111,7 +111,11 @@ function TypingDNA() {
111111
TypingDNA.maxPressTime = 300;
112112
TypingDNA.defaultHistoryLength = 160;
113113
TypingDNA.spKeyCodes = [8, 13, 32];
114-
TypingDNA.spKeyCodesObj = {8: 1, 13: 1, 32: 1};
114+
TypingDNA.spKeyCodesObj = {
115+
8: 1,
116+
13: 1,
117+
32: 1
118+
};
115119
TypingDNA.keyCodes = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 32, 222, 188, 190, 186, 187, 189, 191, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57];
116120
TypingDNA.keyCodesObj = {}
117121
for (var i in TypingDNA.keyCodes) {
@@ -621,21 +625,23 @@ function TypingDNA() {
621625

622626
/**
623627
* This is the main function that outputs the typing pattern as a String
624-
* {type:Number, text:String, textId:Number, length: Number, extended:Boolean, targetId:String, caseSensitive:Boolean}
628+
* {type:Number, text:String, textId:Number, length: Number, targetId:String, caseSensitive:Boolean}
625629
* @param {Object} obj an object with the following properties
626-
* * * @param {String} type 0 for standard pattern, 1 for diagram pattern
630+
* * * @param {String} type 0 for anytext pattern, 1 for sametext pattern (also called diagram pattern)
631+
* * * and 2 for extended pattern (most versatile, can replace both anytext and sametext patterns)
627632
* * * @param {Number} length (Optional) the length of the text in the history for which you want
628633
* * * the typing pattern. length is ignored when text or targetId is set (or both).
629-
* * * @param {String} text (Only for type 1) a typed string that you want the typing pattern for
630-
* * * @param {Number} textId (Optional, only for type 1) a personalized id for the typed text
634+
* * * @param {String} text (Only for type 1 and type 2) a typed string that you want the typing pattern for
635+
* * * @param {Number} textId (Optional, only for type 1 and type 2) a personalized id for the typed text
636+
* * * @param {String} targetId (Optional) specifies if pattern is obtain only from text typed in a certain target
637+
* * * @param {Boolean} caseSensitive (Optional, default: false) Used if you pass a text for type 1 or type 2
638+
* * * DEPRECATED * * * in favor of type = 2 * * *
631639
* * * @param {Boolean} extended (Only for type 1) specifies if full information about what was typed is produced,
632640
* * * including the actual key pressed, if false, only the order of pressed keys is kept (no actual content)
633-
* * * @param {String} targetId (Optional) specifies if pattern is obtain only from text typed in a certain target
634-
* * * @param {Boolean} caseSensitive (Optional, default: false, Only for type 1) Used if you pass a text for type 1
635641
* @return {String} A typing pattern in string form
636-
* @example var typingPattern = tdna.getTypingPattern({type=1, length=160, extended=true});
637-
* @example var typingPattern = tdna.getTypingPattern({type=1, text="Hello5g21?*", extedend=false});
638642
* @example var typingPattern = tdna.getTypingPattern({type=0, length=180});
643+
* @example var typingPattern = tdna.getTypingPattern({type=1, text="Hello5g21?*"});
644+
* @example var typingPattern = tdna.getTypingPattern({type=2, text="example@mail.com"});
639645
*/
640646
TypingDNA.getTypingPattern = function(obj) {
641647
if (typeof obj === 'object') {
@@ -644,11 +650,12 @@ function TypingDNA() {
644650
return TypingDNA.get(obj.length, obj.targetId);
645651
break;
646652
case 1:
647-
if (obj.text != undefined) {
648-
return TypingDNA.history.getDiagram(obj.extended, obj.text, obj.textId, obj.targetId, obj.caseSensitive);
649-
} else {
650-
return TypingDNA.history.getDiagram(obj.extended, obj.length, obj.textId, obj.targetId, obj.caseSensitive);
651-
}
653+
var str = (obj.text != undefined) ? obj.text : obj.length;
654+
return TypingDNA.history.getDiagram(obj.extended, str, obj.textId, obj.targetId, obj.caseSensitive);
655+
break;
656+
case 2:
657+
var str = (obj.text != undefined) ? obj.text : obj.length;
658+
return TypingDNA.history.getDiagram(true, str, obj.textId, obj.targetId, obj.caseSensitive);
652659
break;
653660
default:
654661
return TypingDNA.get(obj.length);
@@ -851,17 +858,17 @@ function TypingDNA() {
851858

852859
// Calculate a 32 bit FNV-1a hash
853860
TypingDNA.math.fnv1aHash = function(str) {
854-
if (str == undefined && typeof str !== 'string') {
855-
return 0;
856-
}
857-
str = str.toLowerCase();
858-
var i, l;
859-
var hval = 0x721b5ad4;
860-
for (i = 0, l = str.length; i < l; i++) {
861-
hval ^= str.charCodeAt(i);
862-
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
863-
}
864-
return hval >>> 0;
861+
if (str == undefined && typeof str !== 'string') {
862+
return 0;
863+
}
864+
str = str.toLowerCase();
865+
var i, l;
866+
var hval = 0x721b5ad4;
867+
for (i = 0, l = str.length; i < l; i++) {
868+
hval ^= str.charCodeAt(i);
869+
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
870+
}
871+
return hval >>> 0;
865872
}
866873

867874
TypingDNA.history = {};
@@ -886,16 +893,16 @@ function TypingDNA() {
886893
var stackDiagram = this.stackDiagram;
887894
if (targetId != undefined && targetId != "" && stackDiagram.length > 0) {
888895
stackDiagram = TypingDNA.sliceStackByTargetId(stackDiagram, targetId);
889-
if (str == undefined || str == ""){
896+
if (str == undefined || str == "") {
890897
var element = TypingDNA.document.getElementById(targetId);
891898
if (element != null) {
892899
str = element.value;
893900
}
894901
}
895902
} else {
896903
var targetLength = TypingDNA.targetIds.length;
897-
if (str == undefined || str == ""){
898-
if(targetLength > 0) {
904+
if (str == undefined || str == "") {
905+
if (targetLength > 0) {
899906
str = "";
900907
for (var i = 0; i < targetLength; i++) {
901908
var element = TypingDNA.document.getElementById(TypingDNA.targetIds[i]);
@@ -905,7 +912,7 @@ function TypingDNA() {
905912
}
906913
} else {
907914
if (!extended) {
908-
console.log("Please use extended diagram OR provide a fixed string param OR use the addTarget method.");
915+
console.log("Please use extended pattern OR provide a fixed string param OR use the addTarget method.");
909916
}
910917
}
911918
}
@@ -914,9 +921,9 @@ function TypingDNA() {
914921
var diagramHistoryLength = stackDiagram.length;
915922
var strLength = diagramHistoryLength;
916923
if (typeof str === 'string') {
917-
strLength = str.length;
924+
strLength = str.length;
918925
} else if (typeof str === 'number' && str < diagramHistoryLength) {
919-
strLength = str;
926+
strLength = str;
920927
}
921928
var returnTextId = 0;
922929
if (textId != undefined) {
@@ -959,8 +966,8 @@ function TypingDNA() {
959966
} else {
960967
lastFoundPos.push(j);
961968
var len = lastFoundPos.length;
962-
if (len > 1 && lastFoundPos[len-1] == lastFoundPos[len-2] + 1) {
963-
lastPos = j+1;
969+
if (len > 1 && lastFoundPos[len - 1] == lastFoundPos[len - 2] + 1) {
970+
lastPos = j + 1;
964971
lastFoundPos = [];
965972
}
966973
}
@@ -988,7 +995,11 @@ function TypingDNA() {
988995
var histPrtF = TypingDNA.math.fo(TypingDNA.history.get(0, "press"));
989996
var seekTime = Math.round(TypingDNA.math.avg(histSktF));
990997
var pressTime = Math.round(TypingDNA.math.avg(histPrtF));
991-
TypingDNA.savedMissingAvgValues = {seekTime:seekTime, pressTime:pressTime, historyLength:diagramHistoryLength};
998+
TypingDNA.savedMissingAvgValues = {
999+
seekTime: seekTime,
1000+
pressTime: pressTime,
1001+
historyLength: diagramHistoryLength
1002+
};
9921003
} else {
9931004
var seekTime = TypingDNA.savedMissingAvgValues.seekTime;
9941005
var pressTime = TypingDNA.savedMissingAvgValues.pressTime;
@@ -1004,7 +1015,7 @@ function TypingDNA() {
10041015
}
10051016
}
10061017
}
1007-
if (TypingDNA.replaceMissingKeysPerc < missingCount*100/strLength) {
1018+
if (TypingDNA.replaceMissingKeysPerc < missingCount * 100 / strLength) {
10081019
return null;
10091020
}
10101021
}
@@ -1032,12 +1043,12 @@ function TypingDNA() {
10321043
return returnArr.join("|");
10331044
}
10341045

1035-
TypingDNA.sliceStackByTargetId = function(stack, targetId){
1046+
TypingDNA.sliceStackByTargetId = function(stack, targetId) {
10361047
var length = stack.length;
10371048
var newStack = [];
10381049
for (i = 0; i < length; i++) {
10391050
var arr = stack[i];
1040-
if (arr[5] == targetId){
1051+
if (arr[5] == targetId) {
10411052
newStack.push(arr);
10421053
}
10431054
}
@@ -1206,7 +1217,7 @@ function TypingDNA() {
12061217
for (var i in revs) {
12071218
rec += Number(revs[i]);
12081219
}
1209-
return rec/totalEvents;
1220+
return rec / totalEvents;
12101221
}
12111222

12121223
TypingDNA.getLength = function(typingPattern) {

0 commit comments

Comments
 (0)