-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
139 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,5 @@ lib | |
/*.map | ||
/*.d.ts | ||
.*_cache | ||
!/*.config.js | ||
!/*.conf.js |
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,38 @@ | ||
const realBrowser = String(process.env.BROWSER).match(/^(1|true)$/gi) | ||
const travisLaunchers = { | ||
chrome_travis: { | ||
base: 'Chrome', | ||
flags: ['--no-sandbox'] | ||
} | ||
} | ||
|
||
const localBrowsers = realBrowser ? Object.keys(travisLaunchers) : ['Chrome'] | ||
|
||
module.exports = (config) => { | ||
const env = process.env['NODE_ENV'] || 'development' | ||
|
||
config.set({ | ||
frameworks: ['jasmine', 'karma-typescript'], | ||
plugins: [ | ||
'karma-jasmine', | ||
'karma-typescript', | ||
'karma-chrome-launcher', | ||
'karma-jasmine-html-reporter', | ||
'karma-spec-reporter' | ||
], | ||
client: { | ||
clearContext: false // leave Jasmine Spec Runner output visible in browser | ||
}, | ||
files: [{ pattern: 'src/**/*.ts' }, { pattern: 'test/**/*.spec.ts' }], | ||
preprocessors: { | ||
'**/*.ts': ['karma-typescript'], | ||
'test/**/*.spec.ts': ['karma-typescript'] | ||
}, | ||
reporters: ['progress', 'kjhtml'], | ||
colors: true, | ||
logLevel: env === 'debug' ? config.LOG_DEBUG : config.LOG_INFO, | ||
autoWatch: true, | ||
browsers: localBrowsers, | ||
singleRun: false | ||
}) | ||
} |
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,27 @@ | ||
import { name } from "./package.json"; | ||
import typescript from "rollup-plugin-typescript2"; | ||
import { terser } from "rollup-plugin-terser"; | ||
|
||
export default { | ||
input: 'src/index.ts', | ||
name, | ||
output: { | ||
file: `./${name}.umd.min.js`, | ||
format: 'umd', | ||
sourcemap: false, | ||
exports: 'named' | ||
}, | ||
plugins: [ | ||
typescript(), | ||
terser() | ||
], | ||
onwarn | ||
} | ||
|
||
function onwarn(message) { | ||
const suppressed = ['UNRESOLVED_IMPORT', 'THIS_IS_UNDEFINED'] | ||
|
||
if (!suppressed.find((code) => message.code === code)) { | ||
return console.warn(message.message) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,41 +1,65 @@ | ||
const chars = 's' | ||
|
||
/** | ||
* Return a formatted string and accepts a variable number of arguments. | ||
* Return a formatted string using an array of arguments | ||
* | ||
* @param args array of arguments | ||
* @returns a formatted string | ||
*/ | ||
export function sprintf (...args: any[]): string { | ||
const len: number = args.length | ||
const format: string = (args[0] || '').toString() | ||
const argv: any[] = len > 0 ? new Array(len - 1) : [] | ||
function sprintfv (args: any[]): string { | ||
const argsLen: number = args.length | ||
const formatv: string = (args[0] || '').toString() | ||
const argv: any[] = [] | ||
const argvLen: number = argsLen > 0 ? argsLen - 1 : 0 | ||
|
||
// TODO: type specifier '%s' supported only | ||
const formats = format.split(`%${chars}`) | ||
// Notice: type specifier '%s' is only supported | ||
const formats: string[] = formatv.split('%s') | ||
const formatsLen: number = formats.length | ||
|
||
if (formats.length <= 0) { | ||
return format | ||
if (formatsLen <= 0) { | ||
return formatv | ||
} | ||
|
||
const times = formats.length - 1 | ||
const formatLen: number = formatsLen - 1 | ||
|
||
if (times > argv.length) throw new Error('Too few arguments supplied.') | ||
if (formatLen > argvLen) { | ||
throw new Error('Too few arguments supplied.') | ||
} | ||
|
||
for (let i = 0; i < len - 1; ++i) { | ||
argv[i] = (args[i + 1] || '').toString() | ||
for (let i = 0; i < argsLen - 1; ++i) { | ||
argv.push( | ||
(args[i + 1] || '').toString() | ||
) | ||
} | ||
|
||
let str = '' | ||
|
||
for (let i = 0; i < formats.length; i++) { | ||
for (let i = 0; i < formatsLen; i++) { | ||
str += formats[i] || '' | ||
|
||
if (i < times) { | ||
if (i < formatLen) { | ||
str += argv[i] || '' | ||
} | ||
} | ||
|
||
return str | ||
} | ||
|
||
export default sprintf | ||
/** | ||
* Return a formatted string and accepts a variable number of arguments | ||
* whose first argument correspond to string format | ||
* | ||
* @returns a formatted string | ||
*/ | ||
export function sprintf (...args: any[]): string { | ||
return sprintfv(args) | ||
} | ||
|
||
/** | ||
* Operates as `sprintf()` but accepts an array of arguments | ||
* | ||
* @param format string format | ||
* @param argsv an array of arguments | ||
* @returns a formatted string | ||
*/ | ||
export function vsprintf (format: any = '', argsv: any[] = []): string { | ||
return sprintfv([ format ].concat(argsv)) | ||
} |
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,25 @@ | ||
import { vsprintf } from '../src' | ||
|
||
describe('vsprintf', () => { | ||
it('undefined', () => expect(vsprintf()).toBe('')) | ||
it('null', () => expect(vsprintf(null)).toBe('')) | ||
it('empty', () => expect(vsprintf('')).toBe('')) | ||
it('array', () => expect(vsprintf([ 1, 2, 3 ])).toBe('1,2,3')) | ||
it('object', () => expect(vsprintf({})).toBe('[object Object]')) | ||
it('true', () => expect(vsprintf(true)).toBe('true')) | ||
it('false', () => expect(vsprintf(false)).toBe('')) | ||
it('void 0', () => expect(vsprintf(void 0)).toBe('')) | ||
it('string', () => expect(vsprintf('Hello')).toBe('Hello')) | ||
it('format with no arguments', () => expect(() => vsprintf('Hello %s')) | ||
.toThrowError('Too few arguments supplied.')) | ||
it('format with no arguments explicit', () => expect(() => vsprintf('Hello %s', [])) | ||
.toThrowError('Too few arguments supplied.')) | ||
it('format with one argument', () => expect(vsprintf('Hello %s', [ 'world!' ])) | ||
.toBe('Hello world!')) | ||
it('format with many arguments', () => expect(vsprintf('foo %s', [ 'bar', 'baz' ])) | ||
.toBe('foo bar')) | ||
it('format arguments (middle)', () => expect(vsprintf('a %s c %s e %s g', [ 'B', 'D', 'F' ])) | ||
.toBe('a B c D e F g')) | ||
it('format arguments (left)', () => expect(vsprintf('%s b %s d %s f %s', [ 'A', 'C', 'E', 'G' ])) | ||
.toBe('A b C d E f G')) | ||
}) |
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