-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
96 lines (77 loc) · 2.72 KB
/
Copy pathtest.php
File metadata and controls
96 lines (77 loc) · 2.72 KB
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
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
<?php
$attr = array(
'email2' => '/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/ig',
);
?>
<div id="example"
data-setting-field--a="This is A"
data-setting-field--px="50"
data-setting-field--email="/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/ig"
data-setting-field3--options='<?php
echo json_encode( array( 'x' => 'xxxxxxx' ) ) ?>'
data-setting-field="<?php
echo str_ireplace( '"', '"', json_encode( $attr ) ) ?>"
data-setting-field1='<?php
echo json_encode( $attr ) ?>'
data-setting-field2='{"email": "/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/ig"}'
data-setting-field3="{'email': '/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/ig', 'options': {'x':'XX', 'y': 'YY'}}"></div>
<div id="example2"
data-setting-field--options--z--b="/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/ig"
data-setting-field--options--z--a="1000"
data-setting-field--options--x="x from deep"
data-setting-field--options="{'x':'x from sub', 'y':'y from sub', 'z': {'a': 10, 'b': '50'}}"
data-setting-field="{'options': {'x':'x from main', 'y': 'Y from main', 'p': 'from main'}, 'settings': [{'m':10, 'n': '100'}, {'m':['50']}]}">
</div>
<script type="text/javascript">
function findObjectValue (obj, path, defValue, notation = ['.', '-', '<>']) {
// If path is not defined or it has false value
if (!path) return undefined
// If path is already an array, use it directly
if (Array.isArray(path)) {
const result = path.reduce((prevObj, key) => prevObj && prevObj[key], obj)
return result === undefined ? defValue : result
}
// Normalize notation to array
const separators = Array.isArray(notation) ? notation : [notation]
// Create regex pattern to match separators (escape special regex characters)
const escapedSeparators = separators.map(separator => RegExp.escape(separator))
const separatorPattern = escapedSeparators.join('')
// Create regex to split on separators but not within brackets
const regex = new RegExp(`([^[${separatorPattern}\\]])+`, 'g')
const pathArray = path.match(regex) || []
// Find value
const result = pathArray.reduce(
(prevObj, key) => prevObj && prevObj[key],
obj,
)
// If found value is undefined return default value; otherwise return the value
return result === undefined ? defValue : result
}
const data = {
'email': ['Email X', 'y'],
'options': {
'data': {
'deep': [
{
'x': 'XXX',
'y': 'YYY',
}],
1000: [
{
'x': '100XXX',
'y': '100YYY',
}],
},
},
}
console.log(findObjectValue(data, 'email.[0]', 'def'))
</script>
</body>
</html>