-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #205 from jmclean-cnexus/master
- Loading branch information
Showing
6 changed files
with
132 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ compiled | |
.rpt2_cache | ||
docs | ||
build | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import SqrlErr from './err' | ||
|
||
export function errWithBlocksOrFilters( | ||
name: string, | ||
blocks: Array<any> | false, // false means don't check | ||
filters: Array<any> | false, | ||
native?: boolean | ||
) { | ||
if (blocks && blocks.length > 0) { | ||
throw SqrlErr((native ? 'Native' : '') + "Helper '" + name + "' doesn't accept blocks") | ||
} | ||
if (filters && filters.length > 0) { | ||
throw SqrlErr((native ? 'Native' : '') + "Helper '" + name + "' doesn't accept filters") | ||
} | ||
} | ||
|
||
/* ASYNC LOOP FNs */ | ||
export function asyncArrLoop(arr: Array<any>, index: number, fn: Function, res: string, cb: Function) { | ||
fn(arr[index], index).then(function (val: string) { | ||
res += val | ||
if (index === arr.length - 1) { | ||
cb(res) | ||
} else { | ||
asyncArrLoop(arr, index + 1, fn, res, cb) | ||
} | ||
}) | ||
} | ||
|
||
export function asyncObjLoop( | ||
obj: { [index: string]: any }, | ||
keys: Array<string>, | ||
index: number, | ||
fn: Function, | ||
res: string, | ||
cb: Function | ||
) { | ||
fn(keys[index], obj[keys[index]]).then(function (val: string) { | ||
res += val | ||
if (index === keys.length - 1) { | ||
cb(res) | ||
} else { | ||
asyncObjLoop(obj, keys, index + 1, fn, res, cb) | ||
} | ||
}) | ||
} | ||
|
||
export function replaceChar(s: string): string { | ||
const escMap: EscapeMap = { | ||
'&': '&', | ||
'<': '<', | ||
'>': '>', | ||
'"': '"', | ||
"'": ''' | ||
} | ||
return escMap[s] | ||
} | ||
|
||
export function XMLEscape(str: unknown) { | ||
// To deal with XSS. Based on Escape implementations of Mustache.JS and Marko, then customized. | ||
var newStr = String(str) | ||
if (/[&<>"']/.test(newStr)) { | ||
return newStr.replace(/[&<>"']/g, replaceChar) | ||
} else { | ||
return newStr | ||
} | ||
} | ||
|
||
/* INTERFACES */ | ||
interface EscapeMap { | ||
'&': '&' | ||
'<': '<' | ||
'>': '>' | ||
'"': '"' | ||
"'": ''' | ||
[index: string]: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as utils from '../src/container-utils' | ||
import SqrlErr from '../src/err' | ||
|
||
describe('replaceChar', () => { | ||
it('should replace & with &', () => { | ||
let res = utils.replaceChar("&") | ||
expect(res).toEqual("&") | ||
}) | ||
it('should replace < with <', () => { | ||
let res = utils.replaceChar("<") | ||
expect(res).toEqual("<") | ||
}) | ||
it('should replace > with >', () => { | ||
let res = utils.replaceChar(">") | ||
expect(res).toEqual(">") | ||
}) | ||
it('should replace " with "', () => { | ||
let res = utils.replaceChar("\"") | ||
expect(res).toEqual(""") | ||
}) | ||
it('should replace \' with '', () => { | ||
let res = utils.replaceChar("'") | ||
expect(res).toEqual("'") | ||
}) | ||
}) | ||
|
||
describe('errWithBlocksOrFilters', () => { | ||
it('should throw error if array of blosk is supplied', (done) => { | ||
try { | ||
utils.errWithBlocksOrFilters("testHelper", ["block"], false) | ||
done("An error should have thrown") | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(SqrlErr) | ||
expect(e.message).toEqual("Helper 'testHelper' doesn't accept blocks") | ||
done() | ||
} | ||
}) | ||
|
||
it('should throw error if array of filters is supplied', (done) => { | ||
try { | ||
utils.errWithBlocksOrFilters("testHelper",false , ["filter"]) | ||
done("An error should have thrown") | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(SqrlErr) | ||
expect(e.message).toEqual("Helper 'testHelper' doesn't accept filters") | ||
done() | ||
} | ||
}) | ||
|
||
}) |