-
Notifications
You must be signed in to change notification settings - Fork 0
/
dra-dual-reference-array.js
67 lines (51 loc) · 1.93 KB
/
dra-dual-reference-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*!
* DRA - DUAL REFERENCE ARRAY
* Makes a array's value available through a objects's property, in the same array
* Author: Jorge Feldmann (jotafeldmann) <https://github.com/jotafeldmann>
* Docs and examples: https://github.com/jotafeldmann/dra-dual-reference-array
* 2013 Jorge Feldmann (jotafeldmann)
* MIT Licensed
*/
'use strict';
/**
Sandbox IIF pattern to expose the app
@param {object} objContext : the context to expose the app
@param {string} strNamespace : the name to insert in the context
@return {object}
**/
( function ( objContext , strNamespace ) {
function app () {
/**
* DualReferenceArray makes a array's value available through a objects's property, in the same array
* @param {array} DualReferenceArray
* @param {string} strPropertyNameToUseLikeAObjectProperty
* @return {array}
*/
function DualReferenceArray ( arrArrayToCreateDualReferenceArray , strPropertyNameToUseLikeAObjectProperty ) {
// Comma-first FTW: I'm Sorry, If I Ever Hurt Your Feelings
var
arrNewArrayToReturn = arrArrayToCreateDualReferenceArray
, strTemplateToAvoidReservedWordsCollision = '_'
;
for ( var count = 0 , q = arrNewArrayToReturn.length ; count < q ; count++ ) {
arrNewArrayToReturn [
strTemplateToAvoidReservedWordsCollision
+ arrNewArrayToReturn [ count ] [ strPropertyNameToUseLikeAObjectProperty ]
] = arrNewArrayToReturn [ count ];
}
return arrNewArrayToReturn;
}
// You can use the explicit method to achieve the same result and improve readability
DualReferenceArray.convertPropsToObject = DualReferenceArray;
return DualReferenceArray;
}
function exposeNamespaceOnContext () {
objContext = objContext || this;
objContext [ strNamespace ] = app();
}
function init () {
exposeNamespaceOnContext();
}
init();
})( window , 'DRA' );
// Now its available through window.DRA or just DRA