main
Last change
on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
727 bytes
|
Rev | Line | |
---|
[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const { SAFE_IDENTIFIER, RESERVED_IDENTIFIER } = require("./propertyName");
|
---|
| 9 |
|
---|
| 10 | /**
|
---|
| 11 | * @param {ArrayLike<string>} properties properties
|
---|
| 12 | * @param {number} start start index
|
---|
| 13 | * @returns {string} chain of property accesses
|
---|
| 14 | */
|
---|
| 15 | const propertyAccess = (properties, start = 0) => {
|
---|
| 16 | let str = "";
|
---|
| 17 | for (let i = start; i < properties.length; i++) {
|
---|
| 18 | const p = properties[i];
|
---|
| 19 | if (`${Number(p)}` === p) {
|
---|
| 20 | str += `[${p}]`;
|
---|
| 21 | } else if (SAFE_IDENTIFIER.test(p) && !RESERVED_IDENTIFIER.has(p)) {
|
---|
| 22 | str += `.${p}`;
|
---|
| 23 | } else {
|
---|
| 24 | str += `[${JSON.stringify(p)}]`;
|
---|
| 25 | }
|
---|
| 26 | }
|
---|
| 27 | return str;
|
---|
| 28 | };
|
---|
| 29 |
|
---|
| 30 | module.exports = propertyAccess;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.