pax_global_header00006660000000000000000000000064130615720650014517gustar00rootroot0000000000000052 comment=6ea6cb4f3d2d889d87c336487f4940eb4aa11980 node-jsprim-1.4.0/000077500000000000000000000000001306157206500137505ustar00rootroot00000000000000node-jsprim-1.4.0/.gitignore000066400000000000000000000000151306157206500157340ustar00rootroot00000000000000node_modules node-jsprim-1.4.0/.gitmodules000066400000000000000000000000001306157206500161130ustar00rootroot00000000000000node-jsprim-1.4.0/.npmignore000066400000000000000000000001461306157206500157500ustar00rootroot00000000000000/node_modules /test /lib/jsprim-jsv.js /jsl.node.conf /Makefile* /.gitignore /.gitmodules /.npmignore node-jsprim-1.4.0/CHANGES.md000066400000000000000000000015771306157206500153540ustar00rootroot00000000000000# Changelog ## not yet released None yet. ## v1.4.0 (2017-03-13) * #7 Add parseInteger() function for safer number parsing ## v1.3.1 (2016-09-12) * #13 Incompatible with webpack ## v1.3.0 (2016-06-22) * #14 add safer version of hasOwnProperty() * #15 forEachKey() should ignore inherited properties ## v1.2.2 (2015-10-15) * #11 NPM package shouldn't include any code that does `require('JSV')` * #12 jsl.node.conf missing definition for "module" ## v1.2.1 (2015-10-14) * #8 odd date parsing behaviour ## v1.2.0 (2015-10-13) * #9 want function for returning RFC1123 dates ## v1.1.0 (2015-09-02) * #6 a new suite of hrtime manipulation routines: `hrtimeAdd()`, `hrtimeAccum()`, `hrtimeNanosec()`, `hrtimeMicrosec()` and `hrtimeMillisec()`. ## v1.0.0 (2015-09-01) First tracked release. Includes everything in previous releases, plus: * #4 want function for merging objects node-jsprim-1.4.0/LICENSE000066400000000000000000000020651306157206500147600ustar00rootroot00000000000000Copyright (c) 2012, Joyent, Inc. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE node-jsprim-1.4.0/Makefile000066400000000000000000000013541306157206500154130ustar00rootroot00000000000000# # Copyright (c) 2012, Joyent, Inc. All rights reserved. # # Makefile: top-level Makefile # # This Makefile contains only repo-specific logic and uses included makefiles # to supply common targets (javascriptlint, jsstyle, restdown, etc.), which are # used by other repos as well. # # # Tools # NPM ?= npm # # Files # JS_FILES := $(shell find lib test -name '*.js') JSL_FILES_NODE = $(JS_FILES) JSSTYLE_FILES = $(JS_FILES) JSL_CONF_NODE = jsl.node.conf .PHONY: all all: $(NPM) install .PHONY: test test: node test/basic.js node test/validate.js node test/hrtimediff.js node test/hrtimesecs.js node test/hrtimeadd.js node test/extraprops.js node test/merge.js node test/parse-integer.js @echo tests okay include ./Makefile.targ node-jsprim-1.4.0/Makefile.targ000066400000000000000000000203061306157206500163450ustar00rootroot00000000000000# -*- mode: makefile -*- # # Copyright (c) 2012, Joyent, Inc. All rights reserved. # # Makefile.targ: common targets. # # NOTE: This makefile comes from the "eng" repo. It's designed to be dropped # into other repos as-is without requiring any modifications. If you find # yourself changing this file, you should instead update the original copy in # eng.git and then update your repo to use the new version. # # This Makefile defines several useful targets and rules. You can use it by # including it from a Makefile that specifies some of the variables below. # # Targets defined in this Makefile: # # check Checks JavaScript files for lint and style # Checks bash scripts for syntax # Checks SMF manifests for validity against the SMF DTD # # clean Removes built files # # docs Builds restdown documentation in docs/ # # prepush Depends on "check" and "test" # # test Does nothing (you should override this) # # xref Generates cscope (source cross-reference index) # # For details on what these targets are supposed to do, see the Joyent # Engineering Guide. # # To make use of these targets, you'll need to set some of these variables. Any # variables left unset will simply not be used. # # BASH_FILES Bash scripts to check for syntax # (paths relative to top-level Makefile) # # CLEAN_FILES Files to remove as part of the "clean" target. Note # that files generated by targets in this Makefile are # automatically included in CLEAN_FILES. These include # restdown-generated HTML and JSON files. # # DOC_FILES Restdown (documentation source) files. These are # assumed to be contained in "docs/", and must NOT # contain the "docs/" prefix. # # JSL_CONF_NODE Specify JavaScriptLint configuration files # JSL_CONF_WEB (paths relative to top-level Makefile) # # Node.js and Web configuration files are separate # because you'll usually want different global variable # configurations. If no file is specified, none is given # to jsl, which causes it to use a default configuration, # which probably isn't what you want. # # JSL_FILES_NODE JavaScript files to check with Node config file. # JSL_FILES_WEB JavaScript files to check with Web config file. # # You can also override these variables: # # BASH Path to bash (default: bash) # # CSCOPE_DIRS Directories to search for source files for the cscope # index. (default: ".") # # JSL Path to JavaScriptLint (default: "jsl") # # JSL_FLAGS_NODE Additional flags to pass through to JSL # JSL_FLAGS_WEB # JSL_FLAGS # # JSSTYLE Path to jsstyle (default: jsstyle) # # JSSTYLE_FLAGS Additional flags to pass through to jsstyle # # # Defaults for the various tools we use. # BASH ?= bash BASHSTYLE ?= tools/bashstyle CP ?= cp CSCOPE ?= cscope CSCOPE_DIRS ?= . JSL ?= jsl JSSTYLE ?= jsstyle MKDIR ?= mkdir -p MV ?= mv RESTDOWN_FLAGS ?= RMTREE ?= rm -rf JSL_FLAGS ?= --nologo --nosummary ifeq ($(shell uname -s),SunOS) TAR ?= gtar else TAR ?= tar endif # # Defaults for other fixed values. # BUILD = build DISTCLEAN_FILES += $(BUILD) DOC_BUILD = $(BUILD)/docs/public # # Configure JSL_FLAGS_{NODE,WEB} based on JSL_CONF_{NODE,WEB}. # ifneq ($(origin JSL_CONF_NODE), undefined) JSL_FLAGS_NODE += --conf=$(JSL_CONF_NODE) endif ifneq ($(origin JSL_CONF_WEB), undefined) JSL_FLAGS_WEB += --conf=$(JSL_CONF_WEB) endif # # Targets. For descriptions on what these are supposed to do, see the # Joyent Engineering Guide. # # # Instruct make to keep around temporary files. We have rules below that # automatically update git submodules as needed, but they employ a deps/*/.git # temporary file. Without this directive, make tries to remove these .git # directories after the build has completed. # .SECONDARY: $($(wildcard deps/*):%=%/.git) # # This rule enables other rules that use files from a git submodule to have # those files depend on deps/module/.git and have "make" automatically check # out the submodule as needed. # deps/%/.git: git submodule update --init deps/$* # # These recipes make heavy use of dynamically-created phony targets. The parent # Makefile defines a list of input files like BASH_FILES. We then say that each # of these files depends on a fake target called filename.bashchk, and then we # define a pattern rule for those targets that runs bash in check-syntax-only # mode. This mechanism has the nice properties that if you specify zero files, # the rule becomes a noop (unlike a single rule to check all bash files, which # would invoke bash with zero files), and you can check individual files from # the command line with "make filename.bashchk". # .PHONY: check-bash check-bash: $(BASH_FILES:%=%.bashchk) $(BASH_FILES:%=%.bashstyle) %.bashchk: % $(BASH) -n $^ %.bashstyle: % $(BASHSTYLE) $^ .PHONY: check-jsl check-jsl-node check-jsl-web check-jsl: check-jsl-node check-jsl-web check-jsl-node: $(JSL_FILES_NODE:%=%.jslnodechk) check-jsl-web: $(JSL_FILES_WEB:%=%.jslwebchk) %.jslnodechk: % $(JSL_EXEC) $(JSL) $(JSL_FLAGS) $(JSL_FLAGS_NODE) $< %.jslwebchk: % $(JSL_EXEC) $(JSL) $(JSL_FLAGS) $(JSL_FLAGS_WEB) $< .PHONY: check-jsstyle check-jsstyle: $(JSSTYLE_FILES:%=%.jsstylechk) %.jsstylechk: % $(JSSTYLE_EXEC) $(JSSTYLE) $(JSSTYLE_FLAGS) $< .PHONY: check check: check-jsl check-jsstyle check-bash @echo check ok .PHONY: clean clean:: -$(RMTREE) $(CLEAN_FILES) .PHONY: distclean distclean:: clean -$(RMTREE) $(DISTCLEAN_FILES) CSCOPE_FILES = cscope.in.out cscope.out cscope.po.out CLEAN_FILES += $(CSCOPE_FILES) .PHONY: xref xref: cscope.files $(CSCOPE) -bqR .PHONY: cscope.files cscope.files: find $(CSCOPE_DIRS) -name '*.c' -o -name '*.h' -o -name '*.cc' \ -o -name '*.js' -o -name '*.s' -o -name '*.cpp' > $@ # # The "docs" target is complicated because we do several things here: # # (1) Use restdown to build HTML and JSON files from each of DOC_FILES. # # (2) Copy these files into $(DOC_BUILD) (build/docs/public), which # functions as a complete copy of the documentation that could be # mirrored or served over HTTP. # # (3) Then copy any directories and media from docs/media into # $(DOC_BUILD)/media. This allows projects to include their own media, # including files that will override same-named files provided by # restdown. # # Step (3) is the surprisingly complex part: in order to do this, we need to # identify the subdirectories in docs/media, recreate them in # $(DOC_BUILD)/media, then do the same with the files. # DOC_MEDIA_DIRS := $(shell find docs/media -type d 2>/dev/null | grep -v "^docs/media$$") DOC_MEDIA_DIRS := $(DOC_MEDIA_DIRS:docs/media/%=%) DOC_MEDIA_DIRS_BUILD := $(DOC_MEDIA_DIRS:%=$(DOC_BUILD)/media/%) DOC_MEDIA_FILES := $(shell find docs/media -type f 2>/dev/null) DOC_MEDIA_FILES := $(DOC_MEDIA_FILES:docs/media/%=%) DOC_MEDIA_FILES_BUILD := $(DOC_MEDIA_FILES:%=$(DOC_BUILD)/media/%) # # Like the other targets, "docs" just depends on the final files we want to # create in $(DOC_BUILD), leveraging other targets and recipes to define how # to get there. # .PHONY: docs docs: \ $(DOC_FILES:%.restdown=$(DOC_BUILD)/%.html) \ $(DOC_FILES:%.restdown=$(DOC_BUILD)/%.json) \ $(DOC_MEDIA_FILES_BUILD) # # We keep the intermediate files so that the next build can see whether the # files in DOC_BUILD are up to date. # .PRECIOUS: \ $(DOC_FILES:%.restdown=docs/%.html) \ $(DOC_FILES:%.restdown=docs/%json) # # We do clean those intermediate files, as well as all of DOC_BUILD. # CLEAN_FILES += \ $(DOC_BUILD) \ $(DOC_FILES:%.restdown=docs/%.html) \ $(DOC_FILES:%.restdown=docs/%.json) # # Before installing the files, we must make sure the directories exist. The | # syntax tells make that the dependency need only exist, not be up to date. # Otherwise, it might try to rebuild spuriously because the directory itself # appears out of date. # $(DOC_MEDIA_FILES_BUILD): | $(DOC_MEDIA_DIRS_BUILD) $(DOC_BUILD)/%: docs/% | $(DOC_BUILD) $(CP) $< $@ docs/%.json docs/%.html: docs/%.restdown | $(DOC_BUILD) $(RESTDOWN_EXEC) $(RESTDOWN) $(RESTDOWN_FLAGS) -m $(DOC_BUILD) $< $(DOC_BUILD): $(MKDIR) $@ $(DOC_MEDIA_DIRS_BUILD): $(MKDIR) $@ # # The default "test" target does nothing. This should usually be overridden by # the parent Makefile. It's included here so we can define "prepush" without # requiring the repo to define "test". # .PHONY: test test: .PHONY: prepush prepush: check test node-jsprim-1.4.0/README.md000066400000000000000000000254171306157206500152400ustar00rootroot00000000000000# jsprim: utilities for primitive JavaScript types This module provides miscellaneous facilities for working with strings, numbers, dates, and objects and arrays of these basic types. ### deepCopy(obj) Creates a deep copy of a primitive type, object, or array of primitive types. ### deepEqual(obj1, obj2) Returns whether two objects are equal. ### isEmpty(obj) Returns true if the given object has no properties and false otherwise. This is O(1) (unlike `Object.keys(obj).length === 0`, which is O(N)). ### hasKey(obj, key) Returns true if the given object has an enumerable, non-inherited property called `key`. [For information on enumerability and ownership of properties, see the MDN documentation.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties) ### forEachKey(obj, callback) Like Array.forEach, but iterates enumerable, owned properties of an object rather than elements of an array. Equivalent to: for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { callback(key, obj[key]); } } ### flattenObject(obj, depth) Flattens an object up to a given level of nesting, returning an array of arrays of length "depth + 1", where the first "depth" elements correspond to flattened columns and the last element contains the remaining object . For example: flattenObject({ 'I': { 'A': { 'i': { 'datum1': [ 1, 2 ], 'datum2': [ 3, 4 ] }, 'ii': { 'datum1': [ 3, 4 ] } }, 'B': { 'i': { 'datum1': [ 5, 6 ] }, 'ii': { 'datum1': [ 7, 8 ], 'datum2': [ 3, 4 ], }, 'iii': { } } }, 'II': { 'A': { 'i': { 'datum1': [ 1, 2 ], 'datum2': [ 3, 4 ] } } } }, 3) becomes: [ [ 'I', 'A', 'i', { 'datum1': [ 1, 2 ], 'datum2': [ 3, 4 ] } ], [ 'I', 'A', 'ii', { 'datum1': [ 3, 4 ] } ], [ 'I', 'B', 'i', { 'datum1': [ 5, 6 ] } ], [ 'I', 'B', 'ii', { 'datum1': [ 7, 8 ], 'datum2': [ 3, 4 ] } ], [ 'I', 'B', 'iii', {} ], [ 'II', 'A', 'i', { 'datum1': [ 1, 2 ], 'datum2': [ 3, 4 ] } ] ] This function is strict: "depth" must be a non-negative integer and "obj" must be a non-null object with at least "depth" levels of nesting under all keys. ### flattenIter(obj, depth, func) This is similar to `flattenObject` except that instead of returning an array, this function invokes `func(entry)` for each `entry` in the array that `flattenObject` would return. `flattenIter(obj, depth, func)` is logically equivalent to `flattenObject(obj, depth).forEach(func)`. Importantly, this version never constructs the full array. Its memory usage is O(depth) rather than O(n) (where `n` is the number of flattened elements). There's another difference between `flattenObject` and `flattenIter` that's related to the special case where `depth === 0`. In this case, `flattenObject` omits the array wrapping `obj` (which is regrettable). ### pluck(obj, key) Fetch nested property "key" from object "obj", traversing objects as needed. For example, `pluck(obj, "foo.bar.baz")` is roughly equivalent to `obj.foo.bar.baz`, except that: 1. If traversal fails, the resulting value is undefined, and no error is thrown. For example, `pluck({}, "foo.bar")` is just undefined. 2. If "obj" has property "key" directly (without traversing), the corresponding property is returned. For example, `pluck({ 'foo.bar': 1 }, 'foo.bar')` is 1, not undefined. This is also true recursively, so `pluck({ 'a': { 'foo.bar': 1 } }, 'a.foo.bar')` is also 1, not undefined. ### randElt(array) Returns an element from "array" selected uniformly at random. If "array" is empty, throws an Error. ### startsWith(str, prefix) Returns true if the given string starts with the given prefix and false otherwise. ### endsWith(str, suffix) Returns true if the given string ends with the given suffix and false otherwise. ### parseInteger(str, options) Parses the contents of `str` (a string) as an integer. On success, the integer value is returned (as a number). On failure, an error is **returned** describing why parsing failed. By default, leading and trailing whitespace characters are not allowed, nor are trailing characters that are not part of the numeric representation. This behaviour can be toggled by using the options below. The empty string (`''`) is not considered valid input. If the return value cannot be precisely represented as a number (i.e., is smaller than `Number.MIN_SAFE_INTEGER` or larger than `Number.MAX_SAFE_INTEGER`), an error is returned. Additionally, the string `'-0'` will be parsed as the integer `0`, instead of as the IEEE floating point value `-0`. This function accepts both upper and lowercase characters for digits, similar to `parseInt()`, `Number()`, and [strtol(3C)](https://illumos.org/man/3C/strtol). The following may be specified in `options`: Option | Type | Default | Meaning ------------------ | ------- | ------- | --------------------------- base | number | 10 | numeric base (radix) to use, in the range 2 to 36 allowSign | boolean | true | whether to interpret any leading `+` (positive) and `-` (negative) characters allowImprecise | boolean | false | whether to accept values that may have lost precision (past `MAX_SAFE_INTEGER` or below `MIN_SAFE_INTEGER`) allowPrefix | boolean | false | whether to interpret the prefixes `0b` (base 2), `0o` (base 8), `0t` (base 10), or `0x` (base 16) allowTrailing | boolean | false | whether to ignore trailing characters trimWhitespace | boolean | false | whether to trim any leading or trailing whitespace/line terminators leadingZeroIsOctal | boolean | false | whether a leading zero indicates octal Note that if `base` is unspecified, and `allowPrefix` or `leadingZeroIsOctal` are, then the leading characters can change the default base from 10. If `base` is explicitly specified and `allowPrefix` is true, then the prefix will only be accepted if it matches the specified base. `base` and `leadingZeroIsOctal` cannot be used together. **Context:** It's tricky to parse integers with JavaScript's built-in facilities for several reasons: - `parseInt()` and `Number()` by default allow the base to be specified in the input string by a prefix (e.g., `0x` for hex). - `parseInt()` allows trailing nonnumeric characters. - `Number(str)` returns 0 when `str` is the empty string (`''`). - Both functions return incorrect values when the input string represents a valid integer outside the range of integers that can be represented precisely. Specifically, `parseInt('9007199254740993')` returns 9007199254740992. - Both functions always accept `-` and `+` signs before the digit. - Some older JavaScript engines always interpret a leading 0 as indicating octal, which can be surprising when parsing input from users who expect a leading zero to be insignificant. While each of these may be desirable in some contexts, there are also times when none of them are wanted. `parseInteger()` grants greater control over what input's permissible. ### iso8601(date) Converts a Date object to an ISO8601 date string of the form "YYYY-MM-DDTHH:MM:SS.sssZ". This format is not customizable. ### parseDateTime(str) Parses a date expressed as a string, as either a number of milliseconds since the epoch or any string format that Date accepts, giving preference to the former where these two sets overlap (e.g., strings containing small numbers). ### hrtimeDiff(timeA, timeB) Given two hrtime readings (as from Node's `process.hrtime()`), where timeA is later than timeB, compute the difference and return that as an hrtime. It is illegal to invoke this for a pair of times where timeB is newer than timeA. ### hrtimeAdd(timeA, timeB) Add two hrtime intervals (as from Node's `process.hrtime()`), returning a new hrtime interval array. This function does not modify either input argument. ### hrtimeAccum(timeA, timeB) Add two hrtime intervals (as from Node's `process.hrtime()`), storing the result in `timeA`. This function overwrites (and returns) the first argument passed in. ### hrtimeNanosec(timeA), hrtimeMicrosec(timeA), hrtimeMillisec(timeA) This suite of functions converts a hrtime interval (as from Node's `process.hrtime()`) into a scalar number of nanoseconds, microseconds or milliseconds. Results are truncated, as with `Math.floor()`. ### validateJsonObject(schema, object) Uses JSON validation (via JSV) to validate the given object against the given schema. On success, returns null. On failure, *returns* (does not throw) a useful Error object. ### extraProperties(object, allowed) Check an object for unexpected properties. Accepts the object to check, and an array of allowed property name strings. If extra properties are detected, an array of extra property names is returned. If no properties other than those in the allowed list are present on the object, the returned array will be of zero length. ### mergeObjects(provided, overrides, defaults) Merge properties from objects "provided", "overrides", and "defaults". The intended use case is for functions that accept named arguments in an "args" object, but want to provide some default values and override other values. In that case, "provided" is what the caller specified, "overrides" are what the function wants to override, and "defaults" contains default values. The function starts with the values in "defaults", overrides them with the values in "provided", and then overrides those with the values in "overrides". For convenience, any of these objects may be falsey, in which case they will be ignored. The input objects are never modified, but properties in the returned object are not deep-copied. For example: mergeObjects(undefined, { 'objectMode': true }, { 'highWaterMark': 0 }) returns: { 'objectMode': true, 'highWaterMark': 0 } For another example: mergeObjects( { 'highWaterMark': 16, 'objectMode': 7 }, /* from caller */ { 'objectMode': true }, /* overrides */ { 'highWaterMark': 0 }); /* default */ returns: { 'objectMode': true, 'highWaterMark': 16 } # Contributing Code should be "make check" clean. This target assumes that [jsl](http://github.com/davepacheco/javascriptlint) and [jsstyle](http://github.com/davepacheco/jsstyle) are on your path. New tests should generally accompany new functions and bug fixes. The tests should pass cleanly (run tests/basic.js). node-jsprim-1.4.0/jsl.node.conf000066400000000000000000000155251306157206500163430ustar00rootroot00000000000000# # Configuration File for JavaScript Lint # # This configuration file can be used to lint a collection of scripts, or to enable # or disable warnings for scripts that are linted via the command line. # ### Warnings # Enable or disable warnings based on requirements. # Use "+WarningName" to display or "-WarningName" to suppress. # +ambiguous_else_stmt # the else statement could be matched with one of multiple if statements (use curly braces to indicate intent +ambiguous_nested_stmt # block statements containing block statements should use curly braces to resolve ambiguity +ambiguous_newline # unexpected end of line; it is ambiguous whether these lines are part of the same statement +anon_no_return_value # anonymous function does not always return value +assign_to_function_call # assignment to a function call -block_without_braces # block statement without curly braces +comma_separated_stmts # multiple statements separated by commas (use semicolons?) +comparison_type_conv # comparisons against null, 0, true, false, or an empty string allowing implicit type conversion (use === or !==) +default_not_at_end # the default case is not at the end of the switch statement +dup_option_explicit # duplicate "option explicit" control comment +duplicate_case_in_switch # duplicate case in switch statement +duplicate_formal # duplicate formal argument {name} +empty_statement # empty statement or extra semicolon +identifier_hides_another # identifer {name} hides an identifier in a parent scope -inc_dec_within_stmt # increment (++) and decrement (--) operators used as part of greater statement +incorrect_version # Expected /*jsl:content-type*/ control comment. The script was parsed with the wrong version. +invalid_fallthru # unexpected "fallthru" control comment +invalid_pass # unexpected "pass" control comment +jsl_cc_not_understood # couldn't understand control comment using /*jsl:keyword*/ syntax +leading_decimal_point # leading decimal point may indicate a number or an object member +legacy_cc_not_understood # couldn't understand control comment using /*@keyword@*/ syntax +meaningless_block # meaningless block; curly braces have no impact +mismatch_ctrl_comments # mismatched control comment; "ignore" and "end" control comments must have a one-to-one correspondence +misplaced_regex # regular expressions should be preceded by a left parenthesis, assignment, colon, or comma +missing_break # missing break statement +missing_break_for_last_case # missing break statement for last case in switch +missing_default_case # missing default case in switch statement +missing_option_explicit # the "option explicit" control comment is missing +missing_semicolon # missing semicolon +missing_semicolon_for_lambda # missing semicolon for lambda assignment +multiple_plus_minus # unknown order of operations for successive plus (e.g. x+++y) or minus (e.g. x---y) signs +nested_comment # nested comment +no_return_value # function {name} does not always return a value +octal_number # leading zeros make an octal number +parseint_missing_radix # parseInt missing radix parameter +partial_option_explicit # the "option explicit" control comment, if used, must be in the first script tag +redeclared_var # redeclaration of {name} +trailing_comma_in_array # extra comma is not recommended in array initializers +trailing_decimal_point # trailing decimal point may indicate a number or an object member +undeclared_identifier # undeclared identifier: {name} +unreachable_code # unreachable code -unreferenced_argument # argument declared but never referenced: {name} -unreferenced_function # function is declared but never referenced: {name} +unreferenced_variable # variable is declared but never referenced: {name} +unsupported_version # JavaScript {version} is not supported +use_of_label # use of label +useless_assign # useless assignment +useless_comparison # useless comparison; comparing identical expressions -useless_quotes # the quotation marks are unnecessary +useless_void # use of the void type may be unnecessary (void is always undefined) +var_hides_arg # variable {name} hides argument +want_assign_or_call # expected an assignment or function call +with_statement # with statement hides undeclared variables; use temporary variable instead ### Output format # Customize the format of the error message. # __FILE__ indicates current file path # __FILENAME__ indicates current file name # __LINE__ indicates current line # __COL__ indicates current column # __ERROR__ indicates error message (__ERROR_PREFIX__: __ERROR_MSG__) # __ERROR_NAME__ indicates error name (used in configuration file) # __ERROR_PREFIX__ indicates error prefix # __ERROR_MSG__ indicates error message # # For machine-friendly output, the output format can be prefixed with # "encode:". If specified, all items will be encoded with C-slashes. # # Visual Studio syntax (default): +output-format __FILE__(__LINE__): __ERROR__ # Alternative syntax: #+output-format __FILE__:__LINE__: __ERROR__ ### Context # Show the in-line position of the error. # Use "+context" to display or "-context" to suppress. # +context ### Control Comments # Both JavaScript Lint and the JScript interpreter confuse each other with the syntax for # the /*@keyword@*/ control comments and JScript conditional comments. (The latter is # enabled in JScript with @cc_on@). The /*jsl:keyword*/ syntax is preferred for this reason, # although legacy control comments are enabled by default for backward compatibility. # -legacy_control_comments ### Defining identifiers # By default, "option explicit" is enabled on a per-file basis. # To enable this for all files, use "+always_use_option_explicit" -always_use_option_explicit # Define certain identifiers of which the lint is not aware. # (Use this in conjunction with the "undeclared identifier" warning.) # # Common uses for webpages might be: +define __dirname +define clearInterval +define clearTimeout +define console +define exports +define global +define module +define process +define require +define setInterval +define setTimeout +define Buffer +define JSON +define Math ### JavaScript Version # To change the default JavaScript version: #+default-type text/javascript;version=1.5 #+default-type text/javascript;e4x=1 ### Files # Specify which files to lint # Use "+recurse" to enable recursion (disabled by default). # To add a set of files, use "+process FileName", "+process Folder\Path\*.js", # or "+process Folder\Path\*.htm". # node-jsprim-1.4.0/lib/000077500000000000000000000000001306157206500145165ustar00rootroot00000000000000node-jsprim-1.4.0/lib/jsprim-jsv.js000066400000000000000000000021641306157206500171630ustar00rootroot00000000000000/* * lib/jsprim-jsv.js: extras for testing performance vs JSV */ var mod_assert = require('assert'); var mod_jsv; /* lazy-loaded because it may not be here */ module.exports = { validateJsonObjectJSV: validateJsonObjectJSV }; function validateJsonObjectJSV(schema, input) { if (!mod_jsv) mod_jsv = require('JSV'); var env = mod_jsv.JSV.createEnvironment(); var report = env.validate(input, schema); if (report.errors.length === 0) return (null); /* Currently, we only do anything useful with the first error. */ mod_assert.ok(report.errors.length > 0); var error = report.errors[0]; /* The failed property is given by a URI with an irrelevant prefix. */ var propname = error['uri'].substr(error['uri'].indexOf('#') + 2); var reason; /* * Some of the default error messages are pretty arcane, so we define * new ones here. */ switch (error['attribute']) { case 'type': reason = 'expected ' + error['details']; break; default: reason = error['message'].toLowerCase(); break; } var message = reason + ': "' + propname + '"'; var rv = new Error(message); rv.jsv_details = error; return (rv); } node-jsprim-1.4.0/lib/jsprim.js000066400000000000000000000412571306157206500163710ustar00rootroot00000000000000/* * lib/jsprim.js: utilities for primitive JavaScript types */ var mod_assert = require('assert-plus'); var mod_util = require('util'); var mod_extsprintf = require('extsprintf'); var mod_verror = require('verror'); var mod_jsonschema = require('json-schema'); /* * Public interface */ exports.deepCopy = deepCopy; exports.deepEqual = deepEqual; exports.isEmpty = isEmpty; exports.hasKey = hasKey; exports.forEachKey = forEachKey; exports.pluck = pluck; exports.flattenObject = flattenObject; exports.flattenIter = flattenIter; exports.validateJsonObject = validateJsonObjectJS; exports.validateJsonObjectJS = validateJsonObjectJS; exports.randElt = randElt; exports.extraProperties = extraProperties; exports.mergeObjects = mergeObjects; exports.startsWith = startsWith; exports.endsWith = endsWith; exports.parseInteger = parseInteger; exports.iso8601 = iso8601; exports.rfc1123 = rfc1123; exports.parseDateTime = parseDateTime; exports.hrtimediff = hrtimeDiff; exports.hrtimeDiff = hrtimeDiff; exports.hrtimeAccum = hrtimeAccum; exports.hrtimeAdd = hrtimeAdd; exports.hrtimeNanosec = hrtimeNanosec; exports.hrtimeMicrosec = hrtimeMicrosec; exports.hrtimeMillisec = hrtimeMillisec; /* * Deep copy an acyclic *basic* Javascript object. This only handles basic * scalars (strings, numbers, booleans) and arbitrarily deep arrays and objects * containing these. This does *not* handle instances of other classes. */ function deepCopy(obj) { var ret, key; var marker = '__deepCopy'; if (obj && obj[marker]) throw (new Error('attempted deep copy of cyclic object')); if (obj && obj.constructor == Object) { ret = {}; obj[marker] = true; for (key in obj) { if (key == marker) continue; ret[key] = deepCopy(obj[key]); } delete (obj[marker]); return (ret); } if (obj && obj.constructor == Array) { ret = []; obj[marker] = true; for (key = 0; key < obj.length; key++) ret.push(deepCopy(obj[key])); delete (obj[marker]); return (ret); } /* * It must be a primitive type -- just return it. */ return (obj); } function deepEqual(obj1, obj2) { if (typeof (obj1) != typeof (obj2)) return (false); if (obj1 === null || obj2 === null || typeof (obj1) != 'object') return (obj1 === obj2); if (obj1.constructor != obj2.constructor) return (false); var k; for (k in obj1) { if (!obj2.hasOwnProperty(k)) return (false); if (!deepEqual(obj1[k], obj2[k])) return (false); } for (k in obj2) { if (!obj1.hasOwnProperty(k)) return (false); } return (true); } function isEmpty(obj) { var key; for (key in obj) return (false); return (true); } function hasKey(obj, key) { mod_assert.equal(typeof (key), 'string'); return (Object.prototype.hasOwnProperty.call(obj, key)); } function forEachKey(obj, callback) { for (var key in obj) { if (hasKey(obj, key)) { callback(key, obj[key]); } } } function pluck(obj, key) { mod_assert.equal(typeof (key), 'string'); return (pluckv(obj, key)); } function pluckv(obj, key) { if (obj === null || typeof (obj) !== 'object') return (undefined); if (obj.hasOwnProperty(key)) return (obj[key]); var i = key.indexOf('.'); if (i == -1) return (undefined); var key1 = key.substr(0, i); if (!obj.hasOwnProperty(key1)) return (undefined); return (pluckv(obj[key1], key.substr(i + 1))); } /* * Invoke callback(row) for each entry in the array that would be returned by * flattenObject(data, depth). This is just like flattenObject(data, * depth).forEach(callback), except that the intermediate array is never * created. */ function flattenIter(data, depth, callback) { doFlattenIter(data, depth, [], callback); } function doFlattenIter(data, depth, accum, callback) { var each; var key; if (depth === 0) { each = accum.slice(0); each.push(data); callback(each); return; } mod_assert.ok(data !== null); mod_assert.equal(typeof (data), 'object'); mod_assert.equal(typeof (depth), 'number'); mod_assert.ok(depth >= 0); for (key in data) { each = accum.slice(0); each.push(key); doFlattenIter(data[key], depth - 1, each, callback); } } function flattenObject(data, depth) { if (depth === 0) return ([ data ]); mod_assert.ok(data !== null); mod_assert.equal(typeof (data), 'object'); mod_assert.equal(typeof (depth), 'number'); mod_assert.ok(depth >= 0); var rv = []; var key; for (key in data) { flattenObject(data[key], depth - 1).forEach(function (p) { rv.push([ key ].concat(p)); }); } return (rv); } function startsWith(str, prefix) { return (str.substr(0, prefix.length) == prefix); } function endsWith(str, suffix) { return (str.substr( str.length - suffix.length, suffix.length) == suffix); } function iso8601(d) { if (typeof (d) == 'number') d = new Date(d); mod_assert.ok(d.constructor === Date); return (mod_extsprintf.sprintf('%4d-%02d-%02dT%02d:%02d:%02d.%03dZ', d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds())); } var RFC1123_MONTHS = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; var RFC1123_DAYS = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; function rfc1123(date) { return (mod_extsprintf.sprintf('%s, %02d %s %04d %02d:%02d:%02d GMT', RFC1123_DAYS[date.getUTCDay()], date.getUTCDate(), RFC1123_MONTHS[date.getUTCMonth()], date.getUTCFullYear(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds())); } /* * Parses a date expressed as a string, as either a number of milliseconds since * the epoch or any string format that Date accepts, giving preference to the * former where these two sets overlap (e.g., small numbers). */ function parseDateTime(str) { /* * This is irritatingly implicit, but significantly more concise than * alternatives. The "+str" will convert a string containing only a * number directly to a Number, or NaN for other strings. Thus, if the * conversion succeeds, we use it (this is the milliseconds-since-epoch * case). Otherwise, we pass the string directly to the Date * constructor to parse. */ var numeric = +str; if (!isNaN(numeric)) { return (new Date(numeric)); } else { return (new Date(str)); } } /* * Number.*_SAFE_INTEGER isn't present before node v0.12, so we hardcode * the ES6 definitions here, while allowing for them to someday be higher. */ var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; var MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER || -9007199254740991; /* * Default options for parseInteger(). */ var PI_DEFAULTS = { base: 10, allowSign: true, allowPrefix: false, allowTrailing: false, allowImprecise: false, trimWhitespace: false, leadingZeroIsOctal: false }; var CP_0 = 0x30; var CP_9 = 0x39; var CP_A = 0x41; var CP_B = 0x42; var CP_O = 0x4f; var CP_T = 0x54; var CP_X = 0x58; var CP_Z = 0x5a; var CP_a = 0x61; var CP_b = 0x62; var CP_o = 0x6f; var CP_t = 0x74; var CP_x = 0x78; var CP_z = 0x7a; var PI_CONV_DEC = 0x30; var PI_CONV_UC = 0x37; var PI_CONV_LC = 0x57; /* * A stricter version of parseInt() that provides options for changing what * is an acceptable string (for example, disallowing trailing characters). */ function parseInteger(str, uopts) { mod_assert.string(str, 'str'); mod_assert.optionalObject(uopts, 'options'); var baseOverride = false; var options = PI_DEFAULTS; if (uopts) { baseOverride = hasKey(uopts, 'base'); options = mergeObjects(options, uopts); mod_assert.number(options.base, 'options.base'); mod_assert.ok(options.base >= 2, 'options.base >= 2'); mod_assert.ok(options.base <= 36, 'options.base <= 36'); mod_assert.bool(options.allowSign, 'options.allowSign'); mod_assert.bool(options.allowPrefix, 'options.allowPrefix'); mod_assert.bool(options.allowTrailing, 'options.allowTrailing'); mod_assert.bool(options.allowImprecise, 'options.allowImprecise'); mod_assert.bool(options.trimWhitespace, 'options.trimWhitespace'); mod_assert.bool(options.leadingZeroIsOctal, 'options.leadingZeroIsOctal'); if (options.leadingZeroIsOctal) { mod_assert.ok(!baseOverride, '"base" and "leadingZeroIsOctal" are ' + 'mutually exclusive'); } } var c; var pbase = -1; var base = options.base; var start; var mult = 1; var value = 0; var idx = 0; var len = str.length; /* Trim any whitespace on the left side. */ if (options.trimWhitespace) { while (idx < len && isSpace(str.charCodeAt(idx))) { ++idx; } } /* Check the number for a leading sign. */ if (options.allowSign) { if (str[idx] === '-') { idx += 1; mult = -1; } else if (str[idx] === '+') { idx += 1; } } /* Parse the base-indicating prefix if there is one. */ if (str[idx] === '0') { if (options.allowPrefix) { pbase = prefixToBase(str.charCodeAt(idx + 1)); if (pbase !== -1 && (!baseOverride || pbase === base)) { base = pbase; idx += 2; } } if (pbase === -1 && options.leadingZeroIsOctal) { base = 8; } } /* Parse the actual digits. */ for (start = idx; idx < len; ++idx) { c = translateDigit(str.charCodeAt(idx)); if (c !== -1 && c < base) { value *= base; value += c; } else { break; } } /* If we didn't parse any digits, we have an invalid number. */ if (start === idx) { return (new Error('invalid number: ' + JSON.stringify(str))); } /* Trim any whitespace on the right side. */ if (options.trimWhitespace) { while (idx < len && isSpace(str.charCodeAt(idx))) { ++idx; } } /* Check for trailing characters. */ if (idx < len && !options.allowTrailing) { return (new Error('trailing characters after number: ' + JSON.stringify(str.slice(idx)))); } /* If our value is 0, we return now, to avoid returning -0. */ if (value === 0) { return (0); } /* Calculate our final value. */ var result = value * mult; /* * If the string represents a value that cannot be precisely represented * by JavaScript, then we want to check that: * * - We never increased the value past MAX_SAFE_INTEGER * - We don't make the result negative and below MIN_SAFE_INTEGER * * Because we only ever increment the value during parsing, there's no * chance of moving past MAX_SAFE_INTEGER and then dropping below it * again, losing precision in the process. This means that we only need * to do our checks here, at the end. */ if (!options.allowImprecise && (value > MAX_SAFE_INTEGER || result < MIN_SAFE_INTEGER)) { return (new Error('number is outside of the supported range: ' + JSON.stringify(str.slice(start, idx)))); } return (result); } /* * Interpret a character code as a base-36 digit. */ function translateDigit(d) { if (d >= CP_0 && d <= CP_9) { /* '0' to '9' -> 0 to 9 */ return (d - PI_CONV_DEC); } else if (d >= CP_A && d <= CP_Z) { /* 'A' - 'Z' -> 10 to 35 */ return (d - PI_CONV_UC); } else if (d >= CP_a && d <= CP_z) { /* 'a' - 'z' -> 10 to 35 */ return (d - PI_CONV_LC); } else { /* Invalid character code */ return (-1); } } /* * Test if a value matches the ECMAScript definition of trimmable whitespace. */ function isSpace(c) { return (c === 0x20) || (c >= 0x0009 && c <= 0x000d) || (c === 0x00a0) || (c === 0x1680) || (c === 0x180e) || (c >= 0x2000 && c <= 0x200a) || (c === 0x2028) || (c === 0x2029) || (c === 0x202f) || (c === 0x205f) || (c === 0x3000) || (c === 0xfeff); } /* * Determine which base a character indicates (e.g., 'x' indicates hex). */ function prefixToBase(c) { if (c === CP_b || c === CP_B) { /* 0b/0B (binary) */ return (2); } else if (c === CP_o || c === CP_O) { /* 0o/0O (octal) */ return (8); } else if (c === CP_t || c === CP_T) { /* 0t/0T (decimal) */ return (10); } else if (c === CP_x || c === CP_X) { /* 0x/0X (hexadecimal) */ return (16); } else { /* Not a meaningful character */ return (-1); } } function validateJsonObjectJS(schema, input) { var report = mod_jsonschema.validate(input, schema); if (report.errors.length === 0) return (null); /* Currently, we only do anything useful with the first error. */ var error = report.errors[0]; /* The failed property is given by a URI with an irrelevant prefix. */ var propname = error['property']; var reason = error['message'].toLowerCase(); var i, j; /* * There's at least one case where the property error message is * confusing at best. We work around this here. */ if ((i = reason.indexOf('the property ')) != -1 && (j = reason.indexOf(' is not defined in the schema and the ' + 'schema does not allow additional properties')) != -1) { i += 'the property '.length; if (propname === '') propname = reason.substr(i, j - i); else propname = propname + '.' + reason.substr(i, j - i); reason = 'unsupported property'; } var rv = new mod_verror.VError('property "%s": %s', propname, reason); rv.jsv_details = error; return (rv); } function randElt(arr) { mod_assert.ok(Array.isArray(arr) && arr.length > 0, 'randElt argument must be a non-empty array'); return (arr[Math.floor(Math.random() * arr.length)]); } function assertHrtime(a) { mod_assert.ok(a[0] >= 0 && a[1] >= 0, 'negative numbers not allowed in hrtimes'); mod_assert.ok(a[1] < 1e9, 'nanoseconds column overflow'); } /* * Compute the time elapsed between hrtime readings A and B, where A is later * than B. hrtime readings come from Node's process.hrtime(). There is no * defined way to represent negative deltas, so it's illegal to diff B from A * where the time denoted by B is later than the time denoted by A. If this * becomes valuable, we can define a representation and extend the * implementation to support it. */ function hrtimeDiff(a, b) { assertHrtime(a); assertHrtime(b); mod_assert.ok(a[0] > b[0] || (a[0] == b[0] && a[1] >= b[1]), 'negative differences not allowed'); var rv = [ a[0] - b[0], 0 ]; if (a[1] >= b[1]) { rv[1] = a[1] - b[1]; } else { rv[0]--; rv[1] = 1e9 - (b[1] - a[1]); } return (rv); } /* * Convert a hrtime reading from the array format returned by Node's * process.hrtime() into a scalar number of nanoseconds. */ function hrtimeNanosec(a) { assertHrtime(a); return (Math.floor(a[0] * 1e9 + a[1])); } /* * Convert a hrtime reading from the array format returned by Node's * process.hrtime() into a scalar number of microseconds. */ function hrtimeMicrosec(a) { assertHrtime(a); return (Math.floor(a[0] * 1e6 + a[1] / 1e3)); } /* * Convert a hrtime reading from the array format returned by Node's * process.hrtime() into a scalar number of milliseconds. */ function hrtimeMillisec(a) { assertHrtime(a); return (Math.floor(a[0] * 1e3 + a[1] / 1e6)); } /* * Add two hrtime readings A and B, overwriting A with the result of the * addition. This function is useful for accumulating several hrtime intervals * into a counter. Returns A. */ function hrtimeAccum(a, b) { assertHrtime(a); assertHrtime(b); /* * Accumulate the nanosecond component. */ a[1] += b[1]; if (a[1] >= 1e9) { /* * The nanosecond component overflowed, so carry to the seconds * field. */ a[0]++; a[1] -= 1e9; } /* * Accumulate the seconds component. */ a[0] += b[0]; return (a); } /* * Add two hrtime readings A and B, returning the result as a new hrtime array. * Does not modify either input argument. */ function hrtimeAdd(a, b) { assertHrtime(a); var rv = [ a[0], a[1] ]; return (hrtimeAccum(rv, b)); } /* * Check an object for unexpected properties. Accepts the object to check, and * an array of allowed property names (strings). Returns an array of key names * that were found on the object, but did not appear in the list of allowed * properties. If no properties were found, the returned array will be of * zero length. */ function extraProperties(obj, allowed) { mod_assert.ok(typeof (obj) === 'object' && obj !== null, 'obj argument must be a non-null object'); mod_assert.ok(Array.isArray(allowed), 'allowed argument must be an array of strings'); for (var i = 0; i < allowed.length; i++) { mod_assert.ok(typeof (allowed[i]) === 'string', 'allowed argument must be an array of strings'); } return (Object.keys(obj).filter(function (key) { return (allowed.indexOf(key) === -1); })); } /* * Given three sets of properties "provided" (may be undefined), "overrides" * (required), and "defaults" (may be undefined), construct an object containing * the union of these sets with "overrides" overriding "provided", and * "provided" overriding "defaults". None of the input objects are modified. */ function mergeObjects(provided, overrides, defaults) { var rv, k; rv = {}; if (defaults) { for (k in defaults) rv[k] = defaults[k]; } if (provided) { for (k in provided) rv[k] = provided[k]; } if (overrides) { for (k in overrides) rv[k] = overrides[k]; } return (rv); } node-jsprim-1.4.0/package.json000066400000000000000000000006121306157206500162350ustar00rootroot00000000000000{ "name": "jsprim", "version": "1.4.0", "description": "utilities for primitive JavaScript types", "main": "./lib/jsprim.js", "repository": { "type": "git", "url": "git://github.com/joyent/node-jsprim.git" }, "dependencies": { "assert-plus": "1.0.0", "extsprintf": "1.0.2", "json-schema": "0.2.3", "verror": "1.3.6" }, "engines": [ "node >=0.6.0" ], "license": "MIT" } node-jsprim-1.4.0/test/000077500000000000000000000000001306157206500147275ustar00rootroot00000000000000node-jsprim-1.4.0/test/basic.js000066400000000000000000000204311306157206500163460ustar00rootroot00000000000000/* * test/basic.js: tests jsprim functions */ var mod_assert = require('assert'); var jsprim = require('../lib/jsprim'); /* deepCopy */ var obj = { 'family': 'simpson', 'children': [ 'bart', 'lisa', 'maggie', 'hugo' ], 'home': true, 'income': undefined, 'dignity': null, 'nhomes': 1 }; var copy = jsprim.deepCopy(obj); mod_assert.deepEqual(copy, obj); copy['home'] = false; mod_assert.ok(obj['home'] === true); /* deepEqual */ var values = [ true, false, null, undefined, 0, 1, new Date(), { 'hello': 'world', 'goodbye': 'sky' }, { 'hello': 'world' }, { 'hello': 'world', 'goodbye': undefined }, [], [ 1, 2, 3 ], [ 1, 2, 3, 4 ], [ 1, { 'hello': 'world' }, false ] ]; values.forEach(function (o1, j) { values.forEach(function (o2, k) { if (j == k) { mod_assert.ok(jsprim.deepEqual(o1, o2)); mod_assert.ok( jsprim.deepEqual(o1, jsprim.deepCopy(o2))); } else { mod_assert.ok(!jsprim.deepEqual(o1, o2)); mod_assert.ok( !jsprim.deepEqual(o1, jsprim.deepCopy(o2))); } }); }); mod_assert.ok(!jsprim.deepEqual(NaN, NaN)); /* isEmpty */ mod_assert.ok(jsprim.isEmpty({})); mod_assert.ok(!jsprim.isEmpty({ 'foo': 'bar' })); /* hasKey */ mod_assert.ok(jsprim.hasKey(obj, 'family')); mod_assert.ok(jsprim.hasKey(obj, 'children')); mod_assert.ok(jsprim.hasKey(obj, 'home')); mod_assert.ok(jsprim.hasKey(obj, 'income')); mod_assert.ok(jsprim.hasKey(obj, 'dignity')); mod_assert.ok(jsprim.hasKey(obj, 'nhomes')); mod_assert.ok(obj.hasOwnProperty); mod_assert.ok(!jsprim.hasKey(obj, 'hasOwnProperty')); copy = Object.create(obj); copy.aprop = 'avalue'; mod_assert.ok(jsprim.hasKey(copy, 'aprop')); mod_assert.equal(copy.nhomes, 1); mod_assert.ok(!jsprim.hasKey(copy, 'nhomes')); copy.hasOwnProperty = null; mod_assert.ok(jsprim.hasKey(copy, 'aprop')); mod_assert.ok(!jsprim.hasKey(copy, 'nhomes')); /* forEachKey */ var keys = []; jsprim.forEachKey(obj, function (key, val) { mod_assert.deepEqual(obj[key], val); keys.push(key); }); keys.sort(); mod_assert.deepEqual(keys, [ 'children', 'dignity', 'family', 'home', 'income', 'nhomes' ]); /* * forEachKey skips non-own properties and works even on objects with a * hasOwnProperty key overridden. */ copy = jsprim.deepCopy(obj); Object.prototype.aprop = 'avalue'; mod_assert.equal(copy.aprop, 'avalue'); copy.hasOwnProperty = null; keys = []; jsprim.forEachKey(copy, function (key, val) { mod_assert.deepEqual(copy[key], val); keys.push(key); }); keys.sort(); mod_assert.deepEqual(keys, [ 'children', 'dignity', 'family', 'hasOwnProperty', 'home', 'income', 'nhomes' ]); delete (Object.prototype.aprop); /* startsWith */ mod_assert.ok(jsprim.startsWith('foobar', 'f')); mod_assert.ok(jsprim.startsWith('foobar', 'foo')); mod_assert.ok(jsprim.startsWith('foobar', 'foobar')); mod_assert.ok(jsprim.startsWith('foobars', 'foobar')); mod_assert.ok(!jsprim.startsWith('foobar', 'foobars')); mod_assert.ok(!jsprim.startsWith('hofoobar', 'foo')); mod_assert.ok(!jsprim.startsWith('hofoobar', 'bar')); /* endsWith */ mod_assert.ok(!jsprim.endsWith('foobar', 'f')); mod_assert.ok(jsprim.endsWith('foobar', 'r')); mod_assert.ok(jsprim.endsWith('foobar', 'bar')); mod_assert.ok(jsprim.endsWith('foobar', 'foobar')); mod_assert.ok(jsprim.endsWith('sfoobar', 'foobar')); mod_assert.ok(!jsprim.endsWith('foobar', 'foobars')); mod_assert.ok(!jsprim.endsWith('foobar', 'sfoobar')); mod_assert.ok(!jsprim.endsWith('hofoobar', 'foo')); /* iso8601 */ var d = new Date(1339194063451); mod_assert.equal(jsprim.iso8601(d), '2012-06-08T22:21:03.451Z'); /* rfc1123 */ mod_assert.equal(jsprim.rfc1123(d), 'Fri, 08 Jun 2012 22:21:03 GMT'); /* randElt */ var a = []; mod_assert.throws(function () { jsprim.randElt(a); }, /must be a non-empty array/); a = [ 10 ]; var r = jsprim.randElt(a); mod_assert.equal(r, 10); r = jsprim.randElt(a); mod_assert.equal(r, 10); var v = {}; a = [ 'alpha', 'bravo', 'charlie', 'oscar' ]; for (var i = 0; i < 10000; i++) { r = jsprim.randElt(a); if (!v.hasOwnProperty(r)) v[r] = 0; v[r]++; } mod_assert.deepEqual(Object.keys(v).sort(), [ 'alpha', 'bravo', 'charlie', 'oscar' ]); jsprim.forEachKey(v, function (_, value) { mod_assert.ok(value > 0); }); /* flatten */ v = jsprim.flattenObject({ 'I': { 'A': { 'i': { 'datum1': [ 1, 2 ], 'datum2': [ 3, 4 ] }, 'ii': { 'datum1': [ 3, 4 ] } }, 'B': { 'i': { 'datum1': [ 5, 6 ] }, 'ii': { 'datum1': [ 7, 8 ], 'datum2': [ 3, 4 ] }, 'iii': { } } }, 'II': { 'A': { 'i': { 'datum1': [ 1, 2 ], 'datum2': [ 3, 4 ] } } } }, 3); mod_assert.deepEqual(v, [ [ 'I', 'A', 'i', { 'datum1': [ 1, 2 ], 'datum2': [ 3, 4 ] } ], [ 'I', 'A', 'ii', { 'datum1': [ 3, 4 ] } ], [ 'I', 'B', 'i', { 'datum1': [ 5, 6 ] } ], [ 'I', 'B', 'ii', { 'datum1': [ 7, 8 ], 'datum2': [ 3, 4 ] } ], [ 'I', 'B', 'iii', {} ], [ 'II', 'A', 'i', { 'datum1': [ 1, 2 ], 'datum2': [ 3, 4 ] } ] ]); mod_assert.throws(function () { jsprim.flattenObject(null, 3); }); mod_assert.throws(function () { jsprim.flattenObject('hello', 3); }); mod_assert.throws(function () { jsprim.flattenObject(3, 3); }); mod_assert.throws(function () { jsprim.flattenObject({}, 'hello'); }); mod_assert.throws(function () { jsprim.flattenObject({}, -1); }); /* flattenIter */ v = { 'level1-A': { 'level2-Aa': { 'level3-Aai': 4, 'level3-Aaii': 7, 'level3-Aaiii': 2 }, 'level2-Ab': { 'level3-Abi': 51, 'level3-Abii': 31 }, 'level2-Ac': { 'level3-Aci': 1351, 'level3-Acii': 121 } }, 'level1-B': { 'level2-Ba': { 'level3-Bai': 8, 'level3-Baii': 7, 'level3-Baiii': 6 }, 'level2-Bb': { 'level3-Bbi': 5, 'level3-Bbii': 4 }, 'level2-Bc': { 'level3-Bci': 3, 'level3-Bcii': 2 } } }; var accum, unflattened; [ [ v, 1 ], [ v, 2 ], [ v, 3 ] ].forEach(function (testcase, j) { var flattened; accum = []; flattened = jsprim.flattenObject(testcase[0], testcase[1]); jsprim.flattenIter(testcase[0], testcase[1], function (entry) { accum.push(entry); }); console.error('test case %d', j, accum); mod_assert.deepEqual(accum, flattened); }); /* * It was arguably a mistake the way flatten with depth === 0 works. That's the * only case where the return value is not an array of arrays. flattenIter() * does the more sensible thing here. */ accum = []; jsprim.flattenIter(3, 0, function (entry) { accum.push(entry); }); mod_assert.deepEqual(accum, [ [ 3 ] ]); /* pluck */ mod_assert.equal('hello', jsprim.pluck({ 'world': 'hello' }, 'world')); mod_assert.equal('hello', jsprim.pluck({ 'world.bar': 'hello' }, 'world.bar')); mod_assert.equal('hello', jsprim.pluck({ 'world.bar': 'hello', 'world': { 'bar': 'junk' } }, 'world.bar')); mod_assert.equal('junk', jsprim.pluck({ 'world': { 'bar': 'junk' } }, 'world.bar')); mod_assert.ok(undefined === jsprim.pluck({ 'world': { 'bar': 'junk' } }, 'world.baz')); mod_assert.ok(undefined === jsprim.pluck(null, 'junk')); mod_assert.ok(undefined === jsprim.pluck(undefined, 'junk')); mod_assert.ok(undefined === jsprim.pluck(3, 'junk')); mod_assert.ok(undefined === jsprim.pluck('hello', 'junk')); mod_assert.ok(undefined === jsprim.pluck(true, 'junk')); mod_assert.ok(undefined === jsprim.pluck({}, '3')); mod_assert.throws(function () { jsprim.pluck({}, 3); }); mod_assert.throws(function () { jsprim.pluck({}, {}); }); mod_assert.throws(function () { jsprim.pluck({}, false); }); /* parseDateTime */ mod_assert.equal('2013-04-02T16:12:37.456Z', jsprim.iso8601( jsprim.parseDateTime('2013-04-02T16:12:37.456Z'))); mod_assert.equal('2013-04-02T16:12:37.000Z', jsprim.iso8601( jsprim.parseDateTime('2013-04-02T16:12:37Z'))); mod_assert.equal('2013-04-02T23:54:41.155Z', jsprim.iso8601( jsprim.parseDateTime('1364946881155'))); mod_assert.equal('2013-04-02T23:54:41.155Z', jsprim.iso8601( jsprim.parseDateTime(1364946881155))); mod_assert.equal('2013-04-02T23:54:41.000Z', jsprim.iso8601( jsprim.parseDateTime(new Date(1364946881000).toString()))); mod_assert.equal('1970-01-01T00:00:02.000Z', jsprim.iso8601(jsprim.parseDateTime('2000'))); mod_assert.equal('1970-01-01T00:00:00.001Z', jsprim.iso8601(jsprim.parseDateTime('1'))); mod_assert.equal('1970-01-01T00:00:00.000Z', jsprim.iso8601(jsprim.parseDateTime('0'))); console.log('basic tests okay'); node-jsprim-1.4.0/test/extraprops.js000066400000000000000000000033051306157206500174750ustar00rootroot00000000000000/* * extraprops.js: test extraProperties() function */ var mod_assert = require('assert'); var mod_util = require('util'); var mod_jsprim = require('../lib/jsprim'); var extraProperties = mod_jsprim.extraProperties; var test_cases = [ { obj: null, allowed: [ 'one', 'two' ], expectedThrow: 'obj argument must be a non-null object' }, { obj: { charlie: 'horse' }, allowed: [ 'charlie', 5 ], expectedThrow: 'allowed argument must be an array of strings' }, { obj: { charlie: 'horse' }, allowed: { charlie: true }, expectedThrow: 'allowed argument must be an array of strings' }, { obj: { strict: true, hapless: true, quality: -3 }, allowed: [ 'strict', 'advisable', 'decent', 'quality' ], expected: [ 'hapless' ] }, { obj: {}, allowed: [], expected: null }, { obj: { strict: true, quality: 100 }, allowed: [ 'strict', 'advisable', 'decent', 'quality' ], expected: [] }, { obj: { 'false': null }, allowed: [], expected: [ 'false' ] } ]; function printf() { process.stdout.write(mod_util.format.apply(null, arguments)); } test_cases.forEach(function (testcase, idx) { printf('test_cases[%d]:\n', idx); printf('\tobj: %j\n', testcase.obj); printf('\tallowed: %j\n', testcase.allowed); var actual; try { actual = extraProperties(testcase.obj, testcase.allowed); } catch (ex) { if (!testcase.expectedThrow) { throw (ex); } mod_assert.equal(ex.message, testcase.expectedThrow); return; } if (testcase.expectedThrow) { throw (new Error('expected an assertion failure')); } if (testcase.expected) { mod_assert.deepEqual(actual, testcase.expected); } }); node-jsprim-1.4.0/test/hrtimeadd.js000066400000000000000000000074321306157206500172340ustar00rootroot00000000000000/* * hrtimediff.js: test hrtimediff() function */ var mod_assert = require('assert'); var mod_util = require('util'); var deepCopy = require('../lib/jsprim').deepCopy; var deepEqual = require('../lib/jsprim').deepEqual; var hrtimeAccum = require('../lib/jsprim').hrtimeAccum; var hrtimeAdd = require('../lib/jsprim').hrtimeAdd; var hrtimeNanosec = require('../lib/jsprim').hrtimeNanosec; var test_cases = [ /* * Passing test cases: */ { ina: [ 0, 0 ], inb: [ 0, 0 ], out: [ 0, 0 ] }, { ina: [ 1000000000, 0 ], inb: [ 50, 0 ], out: [ 1000000050, 0 ] }, { ina: [ 0, 999999999 ], inb: [ 0, 1 ], out: [ 1, 0 ] }, { ina: [ 0, 999999999 ], inb: [ 0, 0 ], out: [ 0, 999999999 ] }, { ina: [ 0, 999999999 ], inb: [ 0, 999999999 ], out: [ 1, 999999998 ] }, { ina: [ 0, 999999999 ], inb: [ 0, 999999999 ], out: [ 1, 999999998 ] }, { ina: [ 50, 999999999 ], inb: [ 1000000000, 999999999 ], out: [ 1000000051, 999999998 ] }, /* * Failing test cases: */ { ina: null, inb: null, out: false }, { ina: null, inb: [ 0, 0 ], out: false }, { ina: [ 0, 0 ], inb: null, out: false }, { ina: [ -1, 0 ], inb: [ 0, 0 ], out: false }, { ina: [ 0, 0 ], inb: [ 0, -1 ], out: false }, { ina: [ 0, 1000000000 ], inb: [ 0, 0 ], out: false }, { ina: [ 0, 0 ], inb: [ 0, 1000000000 ], out: false } ]; /* * Test hrtimeAccum(): */ test_cases.forEach(function (tc) { var n = 'hrtimeAccum'; console.log('%s test case (%s):', tc.out === false ? 'failing' : 'passing', n); console.log('\ta = %j', tc.ina); console.log('\tb = %j', tc.inb); var accum = deepCopy(tc.ina); var interv = deepCopy(tc.inb); var rv; var failed = false; try { rv = hrtimeAccum(accum, interv); console.log('\taccum = %j', accum); } catch (ex) { console.log('\terror = %s', ex.toString()); failed = true; } /* * Ensure that we returned the accumulator array: */ if (!failed) { mod_assert.strictEqual(rv, accum); mod_assert.notStrictEqual(rv, interv); mod_assert.ok(deepEqual(rv, accum)); } /* * Cross-check result with regular addition: */ if (!failed) { var cross = hrtimeNanosec(tc.ina) + hrtimeNanosec(tc.inb); mod_assert.strictEqual(hrtimeNanosec(accum), cross); } /* * Ensure that we did not modify the second argument: */ mod_assert.ok(deepEqual(interv, tc.inb)); if (failed) { /* * We expected to fail; ensure we did so. */ mod_assert.strictEqual(tc.out, false, 'wanted throw'); } else { /* * Ensure that we returned the expected value: */ mod_assert.ok(deepEqual(accum, tc.out)); } }); /* * Test hrtimeAdd(): */ test_cases.forEach(function (tc) { var n = 'hrtimeAdd'; console.log('%s test case (%s):', tc.out === false ? 'failing' : 'passing', n); console.log('\ta = %j', tc.ina); console.log('\tb = %j', tc.inb); var copya = deepCopy(tc.ina); var copyb = deepCopy(tc.inb); var rv; var failed = false; try { rv = hrtimeAdd(copya, copyb); console.log('\tresult = %j', rv); } catch (ex) { console.log('\terror = %s', ex.toString()); failed = true; } /* * Ensure that we returned a _new_ array: */ if (!failed) { mod_assert.notStrictEqual(rv, copya); mod_assert.notStrictEqual(rv, copyb); } /* * Cross-check result with regular addition: */ if (!failed) { var cross = hrtimeNanosec(tc.ina) + hrtimeNanosec(tc.inb); mod_assert.strictEqual(hrtimeNanosec(rv), cross); } /* * Ensure that we did not modify either argument: */ mod_assert.ok(deepEqual(copya, tc.ina)); mod_assert.ok(deepEqual(copyb, tc.inb)); if (failed) { /* * We expected to fail; ensure we did so. */ mod_assert.strictEqual(tc.out, false, 'wanted throw'); } else { /* * Ensure that we returned the expected value: */ mod_assert.ok(deepEqual(rv, tc.out)); } }); node-jsprim-1.4.0/test/hrtimediff.js000066400000000000000000000032641306157206500174130ustar00rootroot00000000000000/* * hrtimediff.js: test hrtimediff() function */ var mod_assert = require('assert'); var mod_util = require('util'); var hrtimediff = require('../lib/jsprim').hrtimediff; var test_cases = [ /* A - B must equal C */ /* simple cases: straight component-wise subtraction */ [ [ 0, 900 ], [ 0, 800 ], [ 0, 100 ] ], [ [ 52, 0 ], [ 48, 0 ], [ 4, 0 ] ], [ [ 1, 900456789 ], [ 0, 800123456 ], [ 1, 100333333 ] ], [ [ 57, 123456789 ], [ 57, 123456789 ], [ 0, 0 ] ], [ [ 57, 123456789 ], [ 0, 0 ], [ 57, 123456789 ] ], /* wrap case */ [ [ 1, 200 ], [ 0, 400 ], [ 0, 999999800 ] ], /* illegal cases */ [ [ 0, 900 ], [ 0, -100 ], null ], /* negative ns */ [ [ 1, 100 ], [ -1, 0 ], null ], /* negative s */ [ [ 0, 0 ], [ 0, 1000000000 ], null ], /* ns too big */ [ [ 0, 300 ], [ 1, 100 ], null ], /* negative result */ [ [ 5, 300 ], [ 5, 400 ], null ] /* negative result */ ]; test_cases.forEach(function (testcase) { var result; if (testcase[2] !== null) { process.stdout.write(mod_util.format( '%j - %j = ', testcase[0], testcase[1])); result = hrtimediff(testcase[0], testcase[1]); process.stdout.write(mod_util.format('%j\n', result)); mod_assert.deepEqual(testcase[2], result); } else { process.stdout.write(mod_util.format('%j - %j (expect fail): ', testcase[0], testcase[1])); mod_assert.throws( function () { hrtimediff(testcase[0], testcase[1]); }, function (err) { console.log(err.message); return (true); }); } }); node-jsprim-1.4.0/test/hrtimesecs.js000066400000000000000000000036261306157206500174420ustar00rootroot00000000000000/* * hrtimediff.js: test hrtimediff() function */ var mod_assert = require('assert'); var mod_util = require('util'); var hrtimeNanosec = require('../lib/jsprim').hrtimeNanosec; var hrtimeMillisec = require('../lib/jsprim').hrtimeMillisec; var hrtimeMicrosec = require('../lib/jsprim').hrtimeMicrosec; var test_cases = [ /* * Passing test cases: */ { in: [ 0, 0 ], nano: 0, micro: 0, milli: 0 }, { in: [ 0, 1000 ], nano: 1000, micro: 1, milli: 0 }, { in: [ 0, 1999 ], nano: 1999, micro: 1, milli: 0 }, { in: [ 1000000000, 0 ], nano: 1000000000000000000, micro: 1000000000000000, milli: 1000000000000 }, { in: [ 5, 123456789 ], nano: 5123456789, micro: 5123456, milli: 5123 }, /* * Failing test cases: */ { in: null, out: false }, { in: [ -1, 0 ], out: false }, { in: [ 0, -1 ], out: false }, { in: [ 0, 1000000000 ], out: false } ]; test_cases.forEach(function (tc) { console.log('%s test case:', tc.out === false ? 'failing' : 'passing'); console.log('\tin = %j', tc.in); var nano, micro, milli; var failures = 0; try { nano = hrtimeNanosec(tc.in); console.log('\tnano = %j', nano); } catch (ex) { console.log('\tnano error = %s', ex.toString()); failures++; } try { micro = hrtimeMicrosec(tc.in); console.log('\tmicro = %j', micro); } catch (ex) { console.log('\tmicro error = %s', ex.toString()); failures++; } try { milli = hrtimeMillisec(tc.in); console.log('\tmilli = %j', milli); } catch (ex) { console.log('\tmilli error = %s', ex.toString()); failures++; } if (tc.out === false) { /* * We expected to fail; ensure we did so. */ mod_assert.strictEqual(failures, 3, 'wanted throw'); } else { /* * Ensure that we returned the expected value: */ mod_assert.strictEqual(nano, tc.nano); mod_assert.strictEqual(micro, tc.micro); mod_assert.strictEqual(milli, tc.milli); } }); node-jsprim-1.4.0/test/merge.js000066400000000000000000000022571306157206500163720ustar00rootroot00000000000000/* * merge.js: test mergeObjects() function */ var mod_assert = require('assert'); var mod_jsprim = require('../lib/jsprim'); var mergeObjects = mod_jsprim.mergeObjects; var test_cases = [ { 'name': 'null user, basic overrides and defaults', 'user': null, 'overrides': { 'a': 3 }, 'defaults': { 'b': 7 }, 'expected': { 'a': 3, 'b': 7 } }, { 'name': 'undefined user, basic overrides and defaults', 'user': undefined, 'overrides': { 'a': 3 }, 'defaults': { 'b': 7 }, 'expected': { 'a': 3, 'b': 7 } }, { 'name': 'empty user, basic overrides and defaults', 'user': {}, 'overrides': { 'a': 3 }, 'defaults': { 'b': 7 }, 'expected': { 'a': 3, 'b': 7 } }, { 'name': 'combination of user, overrides, defaults', 'user': { 'a': 3, 'b': 4, 'c': 5 }, 'overrides': { 'a': 9 }, 'defaults': { 'b': 7, 'd': 15 }, 'expected': { 'a': 9, 'b': 4, 'c': 5, 'd': 15 } } ]; test_cases.forEach(function runTestCase(tc) { var options; console.log('test case: %s', tc.name); options = mergeObjects(tc.user, tc.overrides, tc.defaults); console.log(options); mod_assert.deepEqual(options, tc.expected); }); console.log('TEST PASSED'); node-jsprim-1.4.0/test/parse-integer.js000066400000000000000000000310211306157206500200270ustar00rootroot00000000000000/* * test/parse-integer.js: tests parseInteger() */ var mod_assert = require('assert'); var mod_util = require('util'); var jsprim = require('../lib/jsprim'); // --- Globals var parseInteger = jsprim.parseInteger; /* parseIntJS is used in these tests to avoid confusion */ var parseIntJS = parseInt; /* Options for parseIntJS() compatible behaviour */ var COMPAT_OPTS = { allowSign: true, allowPrefix: true, allowTrailing: true, allowImprecise: true, trimWhitespace: true }; /* Characters trimmed by parseIntJS() */ var VALID_SPACE_CHARS = [ ' ', '\f', '\n', '\r', '\t', '\v', '\u00a0', '\u1680', '\u180e', '\u2000', '\u2001', '\u2002', '\u2003', '\u2004', '\u2005', '\u2006', '\u2007', '\u2008', '\u2009', '\u200a', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000', '\ufeff' ]; /* Characters not trimmed by parseIntJS() */ var INVALID_SPACE_CHARS = [ /* Some non-printable characters */ '\u0000', /* null (NUL) */ '\u0001', /* start of heading (SOH) */ '\u0002', /* start of text (STX) */ '\u0003', /* end of text (ETX) */ '\u0004', /* end of transmission (EOT) */ '\u0005', /* enquiry (ENQ) */ '\u0006', /* acknowledgement (ACK) */ '\u0007', /* bell (BEL) */ '\u0008', /* backspace (BS) */ '\u000E', /* shift out (SO) */ '\u000F', /* shift in (SI) */ /* Character before a whitespace range */ '\u1999', /* new tai lue letter low ma */ /* Some other white space characters */ '\u0085', /* next line (NEL) */ '\u200b', /* zero width space */ '\u200c', /* zero width non-joiner */ '\u200d', /* zero width joiner */ '\u2060' /* word joiner */ ]; /* Strings not valid before, after or inside the digit in any base. */ var INVALID_BASE_CHARS = [ '!', '=', '++', '--', '_', '\\', '}', ';', ':', '.', '.' ]; /* These are valid characters that are only okay before the number. */ var INVALID_TRAILING_CHARS = [ '+', '-' ]; /* Series of strings to try parsing in each base. */ var PI_BASES = [ /* Binary-only values */ '0', '1', '10', '100', '101', '1010', /* Ternary-only values */ '2', '12', '20', '21', '102', '1020', '2010', /* Octal-only values */ '4', '5', '6', '7', '225', '701', '605', '504', '65536', '7654321', /* Decimal-only values */ '8', '9', '812', '9001', '9437', '955512', '987654321', /* Hexadecimal-only values */ 'a', 'b', 'c', 'd', 'e', 'f', 'fd00', 'abcd', '1f2', '1f00', 'b00', /* Base 36-only values */ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1gkz', 'mno0', 'rq025', '6klmns5' ]; /* Generate a series of example input numbers */ var EXAMPLE_NUMS = PI_BASES.map(function (str) { return (parseIntJS(str, 36)); }); // --- Helpers function assertInvalidError(err) { mod_assert.ok(err instanceof Error); mod_assert.ok(jsprim.startsWith(err.message, 'invalid number')); } function assertTrailingError(err) { mod_assert.ok(err instanceof Error); mod_assert.ok(jsprim.startsWith(err.message, 'trailing characters')); } function assertPrecisionError(err) { mod_assert.ok(err instanceof Error); mod_assert.ok(jsprim.startsWith(err.message, 'number is outside of the supported range')); } function assertParseHelper(simpopts, num, str) { var whspopts = jsprim.mergeObjects(simpopts, { trimWhitespace: true }); var trailopts = jsprim.mergeObjects(simpopts, { allowTrailing: true }); /* Test normal parsing. */ mod_assert.equal(num, parseInteger(str, simpopts)); /* Test that whitespace trimmed by parseIntJS() is valid. */ VALID_SPACE_CHARS.forEach(function (sp) { var spBefore = sp + str; var spAfter = str + sp; var spBoth = sp + str + sp; /* Whitespace invalid by default */ assertInvalidError(parseInteger(spBefore, simpopts)); assertTrailingError(parseInteger(spAfter, simpopts)); assertInvalidError(parseInteger(spBoth, simpopts)); /* Adding in "trimWhitespace" makes it valid. */ mod_assert.equal(num, parseInteger(spBefore, whspopts)); mod_assert.equal(num, parseInteger(spAfter, whspopts)); mod_assert.equal(num, parseInteger(spBoth, whspopts)); /* Multiple whitespace chars is fine. */ mod_assert.equal(num, parseInteger(sp + spBefore, whspopts)); mod_assert.equal(num, parseInteger(spAfter + sp, whspopts)); mod_assert.equal(num, parseInteger(sp + spBoth + sp, whspopts)); }); /* Test that whitespace not trimmed by parseIntJS() is invalid. */ INVALID_SPACE_CHARS.forEach(function (sp) { assertInvalidError(parseInteger(sp + str, whspopts)); assertTrailingError(parseInteger(str + sp, whspopts)); assertInvalidError(parseInteger(sp + str + sp, whspopts)); mod_assert.equal(num, parseInteger(str + sp, trailopts)); }); /* Test trailing characters. */ INVALID_BASE_CHARS.forEach(function (c) { /* Trailing character is invalid without "allowTrailing". */ assertTrailingError(parseInteger(str + c, simpopts)); mod_assert.equal(num, parseInteger(str + c, trailopts)); /* Digits after an invalid character are still invalid. */ assertTrailingError(parseInteger(str + c + '0', simpopts)); assertTrailingError(parseInteger(str + c + '1', simpopts)); mod_assert.equal(num, parseInteger(str + c + '0', trailopts)); mod_assert.equal(num, parseInteger(str + c + '1', trailopts)); /* Leading character is invalid regardless of options. */ assertInvalidError(parseInteger(c + str, simpopts)); assertInvalidError(parseInteger(c + str, trailopts)); }); /* Some characters are only valid at the start of the number. */ INVALID_TRAILING_CHARS.forEach(function (c) { assertTrailingError(parseInteger(str + c, simpopts)); assertTrailingError(parseInteger(str + c + '0', simpopts)); mod_assert.equal(num, parseInteger(str + c, trailopts)); }); } function assertPosiNeg(opts, num, str) { /* Basic number parsing with the given options. */ assertParseHelper(opts, num, str); /* Leading '+' makes the number positive (a no-op). */ assertParseHelper(opts, num, '+' + str); /* Leading '-' makes the number negative. */ assertParseHelper(opts, -num, '-' + str); } // --- Tests /* parseInteger() compatibility with parseIntJS() */ var PI_COMPAT_VALUES = [ '123', '123x4', '-123', '0x123', '0x123x4', '-0x123x4', '+0x123x4', '-', '0x', '-0x' ]; PI_COMPAT_VALUES.forEach(function (str) { [ undefined, 10, 16 ].forEach(function (base) { var bopts = { base: base }; var copts = COMPAT_OPTS; if (base) { copts = jsprim.mergeObjects(COMPAT_OPTS, bopts); } var num = parseIntJS(str, base); if (isNaN(num)) { mod_assert.ok(isNaN(parseInteger(str, copts))); } else { mod_assert.equal(num, parseInteger(str, copts)); } }); }); /* parseInteger() with different combinations of bases and options */ [ 2, 3, 8, 10, 16, 36 ].forEach(function (base) { console.log('testing parsing in base %d', base); var sopts = { base: base }; var wopts = jsprim.mergeObjects(sopts, { trimWhitespace: true }); var topts = jsprim.mergeObjects(sopts, { allowTrailing: true }); var copts = jsprim.mergeObjects(sopts, COMPAT_OPTS); PI_BASES.forEach(function (str) { var num = parseIntJS(str, base); if (isNaN(num)) { /* Number cannot be parsed in our base. */ assertInvalidError(parseInteger(str, topts)); assertInvalidError(parseInteger('-' + str, topts)); return; } if (str === num.toString(base)) { /* Parse the number as-is. */ assertPosiNeg(sopts, num, str); /* Changing case shouldn't afect digits in base. */ assertPosiNeg(sopts, num, str.toUpperCase()); /* A leading zero doesn't change the value */ assertPosiNeg(sopts, num, '0' + str); } else { /* The input string had trailing characters. */ mod_assert.equal(num, parseInteger(str, copts)); mod_assert.equal(-num, parseInteger('-' + str, copts)); } }); INVALID_BASE_CHARS.forEach(function (str) { assertInvalidError(parseInteger(str, sopts)); assertInvalidError(parseInteger(str, topts)); assertInvalidError(parseInteger(str, wopts)); }); }); /* parseInteger() base prefixes */ var popts = { allowPrefix: true }; var PI_PREFIX_CHARS = [ { base: 2, chars: [ 'b', 'B' ], b17: 203, b36: 412 }, { base: 8, chars: [ 'o', 'O' ], b17: 0, b36: 880 }, { base: 10, chars: [ 't', 'T' ], b17: 0, b36: 1060 }, { base: 16, chars: [ 'x', 'X' ], b17: 0, b36: 1204 } ]; PI_PREFIX_CHARS.forEach(function (prefix) { console.log('test base %d prefixes: %j', prefix.base, prefix.chars); prefix.chars.forEach(function (pc) { var base = prefix.base; var ps = '0' + pc; function test(opts, num, str) { assertPosiNeg(opts, num, ps + str); assertPosiNeg(opts, num, ps + '0' + str); } /* * When the base and prefix match, the prefix is skipped. */ var bpopts = { base: base, allowPrefix: true }; [ popts, bpopts ].forEach(function (opts) { /* The prefix is invalid on its own. */ assertInvalidError(parseInteger(ps, opts)); /* Some basic numbers. */ test(opts, 0, '0'); test(opts, 1, '1'); test(opts, base, '10'); test(opts, base + 1, '11'); test(opts, base * base, '100'); test(opts, base * base + 1, '101'); /* Try a bunch of different values in this base. */ EXAMPLE_NUMS.forEach(function (num) { test(opts, num, num.toString(base)); }); }); /* It's okay to drop the prefix if the base is given. */ EXAMPLE_NUMS.forEach(function (num) { mod_assert.equal(num, parseInteger(num.toString(base), bpopts)); }); /* * When the base and prefix differ, the specified base takes * precedence. */ var b17opts = { base: 17, allowPrefix: true, allowTrailing: true }; var b36opts = { base: 36, allowPrefix: true, allowTrailing: true }; mod_assert.equal(prefix.b17, parseInteger(ps + 'g', b17opts)); mod_assert.equal(prefix.b36, parseInteger(ps + 'g', b36opts)); }); }); /* Try several invalid prefixes. */ [ 'i', 'q', 'v', 'z' ].forEach(function (pc) { var ps = '0' + pc + '0'; assertTrailingError(parseInteger(ps, { allowPrefix: true })); assertTrailingError(parseInteger(ps, { allowPrefix: false })); }); console.log('testing edge cases'); assertInvalidError(parseInteger('')); assertInvalidError(parseInteger('+')); assertInvalidError(parseInteger('-')); mod_assert.equal(0, parseInteger('0')); /* parseInteger() doesn't return negative zero */ mod_assert.equal('0', mod_util.inspect(parseInteger('-0'))); /* parseInteger() imprecise result tests */ var PI_BOUNDARIES = [ /* Positive imprecise numbers */ '9007199254740992', '9007199254740993', '9007199254740995', '9007199254740999', '9007199254741000', '27021597764223000', '27021597764223001', '27021597764223002', '27021597764223003', '27021597764223004', '27021597764223005', '27021597764223006', /* Negative imprecise numbers */ '-9007199254740992', '-9007199254740993', '-9007199254740995', '-9007199254740997', '-9007199254740999', '-27021597764223000', '-27021597764223001', '-27021597764223002', '-27021597764223003', '-27021597764223004', '-27021597764223005', '-27021597764223006' ]; console.log('testing imprecise parsing'); mod_assert.equal(9007199254740991, parseInteger('9007199254740991')); mod_assert.equal(-9007199254740991, parseInteger('-9007199254740991')); PI_BOUNDARIES.forEach(function (str) { var iopts = { allowImprecise: true }; var inum = parseIntJS(str, 10); assertPrecisionError(parseInteger(str)); assertParseHelper(iopts, inum, str); }); /* parseInteger() octal notation tests */ var oopts = { leadingZeroIsOctal: true }; var opopts = { leadingZeroIsOctal: true, allowPrefix: true }; console.log('testing octal notation'); [ oopts, opopts ].forEach(function (opts) { /* Leading zero parses in octal */ assertPosiNeg(opts, 0, '0'); assertPosiNeg(opts, 0, '00'); assertPosiNeg(opts, 1, '01'); assertPosiNeg(opts, 8, '010'); assertPosiNeg(opts, 9, '011'); assertPosiNeg(opts, 511, '0777'); /* No leading zero when option is enabled remains decimal */ assertPosiNeg(opts, 1, '1'); assertPosiNeg(opts, 10, '10'); assertPosiNeg(opts, 11, '11'); assertPosiNeg(opts, 777, '777'); }); /* parseInteger() invalid input tests */ console.log('testing invalid values'); mod_assert.throws(function () { parseInteger({}); }); mod_assert.throws(function () { parseInteger(true); }); var INVALID_OPTS = [ false, 16, '16', { base: 1 }, { base: 37 }, { allowSign: 0 }, { allowImprecise: {} }, { allowPrefix: 'please' }, { allowTrailing: 5 }, { trimWhitespace: [ true ] }, { leadingZeroIsOctal: 'yes' } ]; INVALID_OPTS.forEach(function (opts) { mod_assert.throws(function () { parseInteger('123', opts); }); }); /* parseInteger() with allowSign=false */ var sfopts = { allowSign: false }; mod_assert.equal(5, parseInteger('5', sfopts)); assertInvalidError(parseInteger('+5', sfopts)); assertInvalidError(parseInteger('-5', sfopts)); node-jsprim-1.4.0/test/validate-bench.js000066400000000000000000000101751306157206500201370ustar00rootroot00000000000000/* * test/validate.js: microbenchmark JSON object validation * * Using "JSV" implementation: * * 100 iterations * 6340 milliseconds * 63.400 ms per iteration on average * * Using "json-schema" implementation: * * 100 iterations * 31 milliseconds * 0.310 ms per iteration on average */ var sprintf = require('extsprintf').sprintf; var jsprim = require('../lib/jsprim'); var jsprimjsv = require('../lib/jsprim-jsv'); /* BEGIN JSSTYLED */ var schema = { "type": "object", "properties": { "gid": { "type": "string", "required": true, "minLength": 1 }, "uid": { "type": "string", "required": true, "minLength": 1 }, "ord": { "type": "integer", "required": true, "minimum": 0 }, "state": { "type": "string", "required": true, "enum": [ "dispatched", "running", "done", "cancelled", "aborted" ] }, "machine": { "type": "string" }, "zonename": { "type": "string" }, "server": { "type": "string" }, "result": { "type": "string", "enum": [ "ok", "fail" ] }, "error": { "type": "object", "properties": { "code": { "type": "string", "required": true, "minLength": 1 }, "message": { "type": "string", "required": true } } }, "crtime": { "type": "string", "format": "date-time", "required": true }, "qtime": { "type": "string", "format": "date-time" }, "mtime": { "type": "string", "format": "date-time" }, "atime": { "type": "string", "format": "date-time" }, "ctime": { "type": "string", "format": "date-time" }, "ptime": { "type": "string", "format": "date-time" }, "nresults": { "type": "integer", "minimum": 0 }, "results": { "type": "array", "items": { "type": "object", "properties": { "entry": { "type": "string", "required": true, "minLength": 1 }, "crtime": { "type": "string", "format": "date-time", "required": true } } } }, "entry": { "type": "string" }, "oid": { "type": "string" } } }; var template = { "gid": "ecf8bc81-a454-4e5d-ab30-c328df3f5fc9", "uid": "748a319d-65e4-49e4-b097-dbf87296bc9e", "oid": "e4515c50-3b57-44c5-895e-f93516132266", "server": "564d7268-5617-dba7-ad0d-ca714f0050c9", "zonename": "9e4e7c3d-dd4d-4cbd-b234-ffdfa3bc5fa2", "machine": "0c90171b-1830-469a-b42f-2fde8f9b7574", "crtime": "2012-09-05T22:00:58.112Z", "ctime": "2012-09-05T22:01:07.080Z", "mtime": "2012-09-05T22:01:04.534Z", "atime": "2012-09-05T22:01:04.730Z", "ord": 0, "state": "done", "entry": "wiggum", "result": "ok", "nresults": 1, "results": [ { "entry": "wiggum", "crtime": "2012-09-05T22:01:04.568Z" } ] }; /* END JSSTYLED */ var start = Date.now(); var count = 1000; var validate = jsprim.validateJsonObjectJS; /* Or use: var validate = jsprimjsv.validateJsonObjectJSV; */ var i; for (i = 0; i < count; i++) validate(schema, template); var done = Date.now(); console.log(sprintf('%6d iterations', count)); console.log(sprintf('%6d milliseconds', done - start)); console.log(sprintf('%6s ms per iteration on average', ((done - start) / count).toFixed(3))); node-jsprim-1.4.0/test/validate.js000066400000000000000000000153641306157206500170670ustar00rootroot00000000000000/* * test/validate.js: test JSON validation */ var assert = require('assert'); var sprintf = require('extsprintf').sprintf; var jsprim = require('../lib/jsprim'); /* BEGIN JSSTYLED */ var schema = { "type": "object", "properties": { "gid": { "type": "string", "required": true, "minLength": 1 }, "uid": { "type": "string", "required": true, "minLength": 1 }, "ord": { "type": "integer", "required": true, "minimum": 0 }, "state": { "type": "string", "required": true, "enum": [ "dispatched", "running", "done", "cancelled", "aborted" ] }, "machine": { "type": "string" }, "zonename": { "type": "string" }, "server": { "type": "string" }, "result": { "type": "string", "enum": [ "ok", "fail" ] }, "error": { "type": "object", "properties": { "code": { "type": "string", "required": true, "minLength": 1 }, "message": { "type": "string", "required": true } } }, "crtime": { "type": "string", "format": "date-time", "required": true }, "qtime": { "type": "string", "format": "date-time" }, "mtime": { "type": "string", "format": "date-time" }, "atime": { "type": "string", "format": "date-time" }, "ctime": { "type": "string", "format": "date-time" }, "ptime": { "type": "string", "format": "date-time" }, "nresults": { "type": "integer", "minimum": 0 }, "results": { "type": "array", "minItems": 1, "items": { "type": "object", 'additionalProperties': false, "properties": { "entry": { "type": "string", "required": true, "minLength": 1 }, "crtime": { "type": "string", "format": "date-time", "required": true } } } }, "entry": { "type": "string" }, "oid": { "type": "string" } } }; var template = { "gid": "ecf8bc81-a454-4e5d-ab30-c328df3f5fc9", "uid": "748a319d-65e4-49e4-b097-dbf87296bc9e", "oid": "e4515c50-3b57-44c5-895e-f93516132266", "server": "564d7268-5617-dba7-ad0d-ca714f0050c9", "zonename": "9e4e7c3d-dd4d-4cbd-b234-ffdfa3bc5fa2", "machine": "0c90171b-1830-469a-b42f-2fde8f9b7574", "crtime": "2012-09-05T22:00:58.112Z", "ctime": "2012-09-05T22:01:07.080Z", "mtime": "2012-09-05T22:01:04.534Z", "atime": "2012-09-05T22:01:04.730Z", "ord": 0, "state": "done", "entry": "wiggum", "result": "ok", "nresults": 1, "results": [ { "entry": "wiggum", "crtime": "2012-09-05T22:01:04.568Z" } ] }; /* END JSSTYLED */ var obj, err; var validate = jsprim.validateJsonObjectJS; /* accepts a valid object */ obj = jsprim.deepCopy(template); err = validate(schema, obj); assert(!err); /* rejects object with missing field */ obj = jsprim.deepCopy(template); delete (obj['gid']); err = validate(schema, obj); console.log(err.message); assert.ok(/required/.test(err.message)); /* JSSTYLED */ assert.ok(/"gid"/.test(err.message)); /* rejects object with wrong type for field */ obj = jsprim.deepCopy(template); obj['gid'] = 5; err = validate(schema, obj); console.log(err.message); assert.ok(/string/.test(err.message)); /* JSSTYLED */ assert.ok(/"gid"/.test(err.message)); obj['gid'] = {}; err = validate(schema, obj); console.log(err.message); assert.ok(/string/.test(err.message)); /* JSSTYLED */ assert.ok(/"gid"/.test(err.message)); /* rejects strings that are too short */ obj = jsprim.deepCopy(template); obj['gid'] = ''; err = validate(schema, obj); console.log(err.message); assert.ok(/long/.test(err.message) || /length/.test(err.message)); /* JSSTYLED */ assert.ok(/"gid"/.test(err.message)); /* rejects wrong type for integer fields */ obj = jsprim.deepCopy(template); obj['ord'] = 'food'; err = validate(schema, obj); console.log(err.message); assert.ok(/integer/.test(err.message)); /* JSSTYLED */ assert.ok(/"ord"/.test(err.message)); /* XXX json-schema accepts strings as integers */ obj = jsprim.deepCopy(template); obj['ord'] = '12'; err = validate(schema, obj); if (err) { console.log(err.message); assert.ok(/integer/.test(err.message)); /* JSSTYLED */ assert.ok(/"ord"/.test(err.message)); } else { console.error('WARNING: accepted string as integer'); } /* rejects floats for integers */ obj = jsprim.deepCopy(template); obj['ord'] = 3.582; err = validate(schema, obj); console.log(err.message); assert.ok(/integer/.test(err.message)); /* JSSTYLED */ assert.ok(/"ord"/.test(err.message)); /* rejects numbers too small */ obj = jsprim.deepCopy(template); obj['ord'] = -5; err = validate(schema, obj); console.log(err.message); assert.ok(/minimum/.test(err.message)); /* JSSTYLED */ assert.ok(/"ord"/.test(err.message)); /* rejects enum string not in the valid set */ obj = jsprim.deepCopy(template); obj['state'] = 'fubared'; err = validate(schema, obj); console.log(err.message); assert.ok(/enumeration/.test(err.message) || /possible/.test(err.message)); /* JSSTYLED */ assert.ok(/"state"/.test(err.message)); /* XXX both accept malformed date */ obj = jsprim.deepCopy(template); obj['crtime'] = 'fubared'; err = validate(schema, obj); if (err) { console.log(err.message); /* JSSTYLED */ assert.ok(/"crtime"/.test(err.message)); } else { console.error('WARNING: accepted malformed date'); } /* rejects array that is too short */ obj = jsprim.deepCopy(template); obj['results'] = []; err = validate(schema, obj); console.log(err.message); assert.ok(/minimum/.test(err.message)); /* JSSTYLED */ assert.ok(/"results"/.test(err.message)); /* rejects objects with extra properties */ obj = jsprim.deepCopy(template); obj['results'][0]['extra'] = 'hello'; err = validate(schema, obj); console.log(err.message); assert.ok(/unsupported property/.test(err.message)); /* BEGIN JSSTYLED */ assert.ok(/"results\[0\]"/.test(err.message) || /"results\[0\]\.extra"/.test(err.message) || /"results\/0"/.test(err.message)); /* END JSSTYLED */