Skip to content

Commit 3e507e0

Browse files
committed
Implement .length property on strings and arrays
phug-php/phug#25
1 parent 596d2da commit 3e507e0

File tree

6 files changed

+27
-1
lines changed

6 files changed

+27
-1
lines changed

examples/length.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
a = [];
2+
b = 'bb';
3+
4+
c = [
5+
a.length,
6+
b.length
7+
];
8+
9+
return c.join(',');

examples/length.return

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0,2

src/JsPhpize/Compiler/Helpers/Dot.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ function ($base) {
2020
if (preg_match('/^[-+]?\d+$/', strval($key))) {
2121
return substr($base, intval($key), 1);
2222
}
23+
if ($key === 'length') {
24+
return strlen($base);
25+
}
2326
if ($key === 'substr' || $key === 'slice') {
2427
return function ($start, $length = null) use ($base) {
2528
return func_num_args() === 1 ? substr($base, $start) : substr($base, $start, $length);

src/JsPhpize/Compiler/Helpers/DotObject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ function ($base) {
2020
if (preg_match('/^[-+]?\d+$/', strval($key))) {
2121
return substr($base, intval($key), 1);
2222
}
23+
if ($key === 'length') {
24+
return strlen($base);
25+
}
2326
if ($key === 'substr' || $key === 'slice') {
2427
return function ($start, $length = null) use ($base) {
2528
return func_num_args() === 1 ? substr($base, $start) : substr($base, $start, $length);

src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
function ($base) {
22
$arrayPrototype = function ($base, $key) {
3+
if ($key === 'length') {
4+
return count($base);
5+
}
36
if ($key === 'forEach') {
47
return function ($callback, $userData = null) use (&$base) {
58
return array_walk($base, $callback, $userData);
@@ -96,6 +99,9 @@ function ($base) {
9699
if (preg_match('/^[-+]?\d+$/', strval($key))) {
97100
return substr($base, intval($key), 1);
98101
}
102+
if ($key === 'length') {
103+
return strlen($base);
104+
}
99105
if ($key === 'substr' || $key === 'slice') {
100106
return function ($start, $length = null) use ($base) {
101107
return func_num_args() === 1 ? substr($base, $start) : substr($base, $start, $length);

tests/render.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ public function caseProvider()
2626
public function testJsPhpizeGeneration($returnFile, $jsFile)
2727
{
2828
$examples = __DIR__ . '/../examples';
29-
$jsPhpize = new JsPhpize();
29+
$jsPhpize = new JsPhpize([
30+
'helpers' => [
31+
'dot' => 'dotWithArrayPrototype',
32+
],
33+
]);
3034
$expected = file_get_contents($examples . '/' . $returnFile);
3135

3236
try {

0 commit comments

Comments
 (0)