diff --git a/lib/parser.js b/lib/parser.js index ad5924a..137975c 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -4,13 +4,15 @@ const fs = require('fs') const { resolve, dirname } = require('path') const glob = require('glob') +const DOT_TOKEN = '<_DOT_TOKEN_>' + /** - * Converts ! in back to . + * Converts DOT_TOKEN in back to . * @param key * @return {*|void|string|never} */ function unsafeKey (key) { - return key.replace(/(!)/g, '.') + return key.replace(new RegExp(DOT_TOKEN, 'g'), '.') } /** @@ -19,7 +21,7 @@ function unsafeKey (key) { * @return {*|void|string|never} */ function safeKey (key) { - return key.replace(/(\.)/g, '!') + return key.replace(/(\.)/g, DOT_TOKEN) } class IncludeResolutionError extends ReferenceError {} diff --git a/test/parser.js b/test/parser.js index 5b845b6..cc68d3b 100644 --- a/test/parser.js +++ b/test/parser.js @@ -24,7 +24,7 @@ describe('toJSON', () => { parser.toJSON(configString).should.deep.equal({ server: {} }) }) - it('should support dots in keys (like if)', () => { + it('should support dots in keys (like `if`)', () => { const configString = [ 'server {', ' if ($host = example.example.com) {', @@ -44,6 +44,150 @@ describe('toJSON', () => { }) }) + it('should support = operator in `if`', () => { + const configString = [ + 'server {', + ' if ($var = foo) {', + ' return 301 https://$host$request_uri;', + ' }', + '}' + ].join('\n') + + parser.toJSON(configString).should.deep.equal({ + server: { + 'if ($var = foo)': { + return: '301 https://$host$request_uri' + } + } + }) + }) + + it('should support != operator in `if`', () => { + const configString = [ + 'server {', + ' if ($var != foo) {', + ' return 301 https://$host$request_uri;', + ' }', + '}' + ].join('\n') + + parser.toJSON(configString).should.deep.equal({ + server: { + 'if ($var != foo)': { + return: '301 https://$host$request_uri' + } + } + }) + }) + + it('should support ~ operator in `if`', () => { + const configString = [ + 'server {', + ' if ($var ~ "foo") {', + ' return 301 https://$host$request_uri;', + ' }', + '}' + ].join('\n') + + parser.toJSON(configString).should.deep.equal({ + server: { + 'if ($var ~ "foo")': { + return: '301 https://$host$request_uri' + } + } + }) + }) + + it('should support !~ operator in if', () => { + const configString = [ + 'server {', + ' if ($var !~ "foo") {', + ' return 301 https://$host$request_uri;', + ' }', + '}' + ].join('\n') + + parser.toJSON(configString).should.deep.equal({ + server: { + 'if ($var !~ "foo")': { + return: '301 https://$host$request_uri' + } + } + }) + }) + + it('should support ~* operator in `if`', () => { + const configString = [ + 'server {', + ' if ($var ~* "foo") {', + ' return 301 https://$host$request_uri;', + ' }', + '}' + ].join('\n') + + parser.toJSON(configString).should.deep.equal({ + server: { + 'if ($var ~* "foo")': { + return: '301 https://$host$request_uri' + } + } + }) + }) + + it('should support !~* operator in `if`', () => { + const configString = [ + 'server {', + ' if ($var !~* "foo") {', + ' return 301 https://$host$request_uri;', + ' }', + '}' + ].join('\n') + + parser.toJSON(configString).should.deep.equal({ + server: { + 'if ($var !~* "foo")': { + return: '301 https://$host$request_uri' + } + } + }) + }) + + it('should support `-f`-like operators in `if`', () => { + const configString = [ + 'server {', + ' if ($var -f "foo") {', + ' return 301 https://$host$request_uri;', + ' }', + '}' + ].join('\n') + + parser.toJSON(configString).should.deep.equal({ + server: { + 'if ($var -f "foo")': { + return: '301 https://$host$request_uri' + } + } + }) + }) + + it('should support `!-f`-like operators in `if`', () => { + const configString = [ + 'server {', + ' if ($var !-f "foo") {', + ' return 301 https://$host$request_uri;', + ' }', + '}' + ].join('\n') + + parser.toJSON(configString).should.deep.equal({ + server: { + 'if ($var !-f "foo")': { + return: '301 https://$host$request_uri' + } + } + }) + }) + it('should support nested directives', () => { const configString = ['server {', ' listen 443;',