Function: extract version 58

Function:
Description: Imports variables into symbol table from an array.
Version:
Status: latest
Date: 2011-12-20 04:39
Summary: Import from phpjs.
Programmer: jj5
Code
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function extract (arr, type, prefix) {
    // http://jsphp.co/jsphp/fn/view/extract
    // +   original by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: Only works by extracting into global context (whether called in the global scope or
    // %        note 1: within a function); also, the EXTR_REFS flag I believe can't be made to work
    // *     example 1: size = 'large';
    // *     example 1: var_array = {'color' : 'blue', 'size' : 'medium', 'shape' : 'sphere'};
    // *     example 1: extract(var_array, 'EXTR_PREFIX_SAME', 'wddx');
    // *     example 1: color+'-'+size+'-'+shape+'-'+wddx_size;
    // *     returns 1: 'blue-large-sphere-medium'
    if (Object.prototype.toString.call(arr) === '[object Array]' && 
        (type !== 'EXTR_PREFIX_ALL' && type !== 'EXTR_PREFIX_INVALID')) {
        return 0;
    }
    var targetObj = this.window;
    if (this.php_js && this.php_js.ini && this.php_js.ini['phpjs.extractTargetObj'] && this.php_js.ini['phpjs.extractTargetObj'].local_value) { // Allow designated object to be used instead of window
        targetObj = this.php_js.ini['phpjs.extractTargetObj'].local_value;
    }
    var chng = 0;

    for (var i in arr) {
        var validIdent = /^[_a-zA-Z$][\w|$]*$/; // TODO: Refine regexp to allow JS 1.5+ Unicode identifiers
        var prefixed = prefix + '_' + i;
        try {
            switch (type) {
            case 'EXTR_PREFIX_SAME' || 2:
                if (targetObj[i] !== undefined) {
                    if (prefixed.match(validIdent) !== null) {
                        targetObj[prefixed] = arr[i];
                        ++chng;
                    }
                } else {
                    targetObj[i] = arr[i];
                    ++chng;
                }
                break;
            case 'EXTR_SKIP' || 1:
                if (targetObj[i] === undefined) {
                    targetObj[i] = arr[i];
                    ++chng;
                }
                break;
            case 'EXTR_PREFIX_ALL' || 3:
                if (prefixed.match(validIdent) !== null) {
                    targetObj[prefixed] = arr[i];
                    ++chng;
                }
                break;
            case 'EXTR_PREFIX_INVALID' || 4:
                if (i.match(validIdent) !== null) {
                    if (prefixed.match(validIdent) !== null) {
                        targetObj[prefixed] = arr[i];
                        ++chng;
                    }
                } else {
                    targetObj[i] = arr[i];
                    ++chng;
                }
                break;
            case 'EXTR_IF_EXISTS' || 6:
                if (targetObj[i] !== undefined) {
                    targetObj[i] = arr[i];
                    ++chng;
                }
                break;
            case 'EXTR_PREFIX_IF_EXISTS' || 5:
                if (targetObj[i] !== undefined && prefixed.match(validIdent) !== null) {
                    targetObj[prefixed] = arr[i];
                    ++chng;
                }
                break;
            case 'EXTR_REFS' || 256:
                throw 'The EXTR_REFS type will not work in JavaScript';
            case 'EXTR_OVERWRITE' || 0:
                // Fall-through
            default:
                targetObj[i] = arr[i];
                ++chng;
                break;
            }
        } catch (e) { // Just won't increment for problem assignments
        }
    }
    return chng;
}
[top]

Comments

There are no comments yet, be the first!

Please Login or Register to post comments.