diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..c66ad556 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,154 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2017 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# EditorConfig configuration file (see ). + +# Indicate that this file is a root-level configuration file: +root = true + +# Set properties for all files: +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +# Set properties for JavaScript files: +[*.js] +indent_style = tab + +# Set properties for TypeScript files: +[*.ts] +indent_style = tab + +# Set properties for Python files: +[*.py] +indent_style = space +indent_size = 4 + +# Set properties for Julia files: +[*.jl] +indent_style = tab + +# Set properties for R files: +[*.R] +indent_style = tab + +# Set properties for C files: +[*.c] +indent_style = tab + +# Set properties for C header files: +[*.h] +indent_style = tab + +# Set properties for C++ files: +[*.cpp] +indent_style = tab + +# Set properties for C++ header files: +[*.hpp] +indent_style = tab + +# Set properties for Fortran files: +[*.f] +indent_style = space +indent_size = 2 +insert_final_newline = false + +# Set properties for shell files: +[*.sh] +indent_style = tab + +# Set properties for AWK files: +[*.awk] +indent_style = tab + +# Set properties for HTML files: +[*.html] +indent_style = tab +tab_width = 2 + +# Set properties for CSS files: +[*.css] +indent_style = tab + +# Set properties for Makefiles: +[Makefile] +indent_style = tab + +[*.mk] +indent_style = tab + +# Set properties for Markdown files: +[*.md] +indent_style = space +indent_size = 4 +trim_trailing_whitespace = false + +# Set properties for `usage.txt` files: +[usage.txt] +indent_style = space +indent_size = 2 + +# Set properties for `repl.txt` files: +[repl.txt] +indent_style = space +indent_size = 4 + +# Set properties for `package.json` files: +[package.json] +indent_style = space +indent_size = 2 + +# Set properties for `datapackage.json` files: +[datapackage.json] +indent_style = space +indent_size = 2 + +# Set properties for `tslint.json` files: +[tslint.json] +indent_style = space +indent_size = 2 + +# Set properties for `tsconfig.json` files: +[tsconfig.json] +indent_style = space +indent_size = 2 + +# Set properties for LaTeX files: +[*.tex] +indent_style = tab + +# Set properties for LaTeX Bibliography files: +[*.bib] +indent_style = tab + +# Set properties for YAML files: +[*.yml] +indent_style = space +indent_size = 2 + +# Set properties for GYP files: +[binding.gyp] +indent_style = space +indent_size = 2 + +[*.gypi] +indent_style = space +indent_size = 2 diff --git a/.github/workflows/install.yml b/.github/workflows/install.yml new file mode 100644 index 00000000..53e0af7d --- /dev/null +++ b/.github/workflows/install.yml @@ -0,0 +1,32 @@ +name: Install dependencies + +on: + workflow_run: + workflows: ["Publish Package"] + types: [completed] + +jobs: + on-success: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'success' }} + steps: + - uses: actions/checkout@v1 + - uses: actions/setup-node@v1 + with: + node-version: 10 + - name: Install dependencies via npm + run: | + npm install + on-failure: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'failure' }} + steps: + - uses: actions/checkout@v1 + - uses: actions/setup-node@v1 + with: + node-version: 10 + - name: Delete tag of unpublished version + run: | + git config --local user.email "noreply@stdlib.io" + git config --local user.name "stdlib-bot" + git push origin --delete $(git tag -l | tail --lines=1) \ No newline at end of file diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 00000000..8e99fd5a --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,27 @@ +name: Publish Package + +on: push + +jobs: + publish: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions/setup-node@v1 + with: + node-version: 10 + - name: Increment version + run: | + git config --local user.email "noreply@stdlib.io" + git config --local user.name "stdlib-bot" + npm version patch + - name: Push changes + uses: ad-m/github-push-action@master + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + branch: ${{ github.ref }} + tags: true + - name: Publish package to npm + uses: JS-DevTools/npm-publish@v1 + with: + token: ${{ secrets.NPM_TOKEN }} \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 00000000..642016ec --- /dev/null +++ b/.npmignore @@ -0,0 +1,226 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2017 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# Files # +######### +TODO.md +ROADMAP.md +.postinstall.json + +# Directories # +############### +.circleci/ +.github/ +**/benchmark/ +**/build/ +**/examples/ +reports/ +support/ +**/tmp/ +workshops/ + +# Ignore test directories, except for testing dependency installation: +**/test/ +!/deps/test/ + +# Ignore top-level tools directory, except for scripts: +/tools/* +!/tools/scripts/ + +# Only top-level directories: +/etc/ +/docs/ + +# Compiled source # +################### +*.com +*.class +*.dll +*.o +*.so +*.slo +*.lo +*.obj +*.dylib +*.lai +*.la +*.a +*.lib +*.ko +*.elf +*.node + +# Precompiled headers # +####################### +*.gch +*.pch + +# Executables # +############### +*.exe +*.out +*.app + +# Packages # +############ +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip + +# Make an exception for compressed distributable files: +!dist/*.gz + +# Logs and databases # +###################### +*.log +*.sql +*.sqlite + +# OS generated files # +###################### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +Icon? +ehthumbs.db +Thumbs.db +Desktop.ini + +# Temporary files # +################### +*~ + +# Node.js # +########### +.npmignore + +# Only top-level node_modules: +/node_modules/ + +# TypeScript # +############## +tsconfig.json +tslint.json +*.tsbuildinfo + +# Matlab # +########## +*.asv +*.mex* + +# Fortran # +########### +*.mod + +# R # +##### +.Rhistory +.Rapp.history +.Rproj.user/ + +# Python # +########## +__pycache__/ +*.py[cod] +*$py.class +*.egg-info/ +.ipynb_checkpoints +setup.cfg +setup.py + +# TeX # +####### +*.aux +*.lof +*.log +*.lot +*.fls +*.out +*.toc +*.dvi +*-converted-to.* +*.bbl +*.bcf +*.blg +*-blx.aux +*-blx.bib +*.brf +*.run.xml +*.fdb_latexmk +*.synctex +*.synctex.gz +*.synctex.gz(busy) +*.pdfsync +*.alg +*.loa +acs-*.bib +*.thm +*.nav +*.snm +*.vrb +*.acn +*.acr +*.glg +*.glo +*.gls +*-concordance.tex +*.tikz +*-tikzDictionary +*.idx +*.ilg +*.ind +*.ist + +# Git # +####### +.git* +.mailmap + +# Visual Studio # +################# +.vscode/ +jsconfig.json + +# Utilities # +############# +.jshintrc +.jshintignore +.eslintrc* +.eslintignore + +.pylintrc +.pycodestyle +.pydocstyle + +.travis.yml +circle.yml +appveyor.yml +azure-pipelines.yml + +.editorconfig +.codeclimate.yml +.codecov.yml + +.rtlintrc diff --git a/.npmrc b/.npmrc new file mode 100644 index 00000000..36f5bef4 --- /dev/null +++ b/.npmrc @@ -0,0 +1,28 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2017 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# Configuration for [npm][1]. +# +# [1]: https://docs.npmjs.com/files/npmrc + +# Disable the creation of a lock file: +package-lock = false +shrinkwrap = false + +# Disable automatically "saving" dependencies on install: +save = false diff --git a/CONTRIBUTORS b/CONTRIBUTORS new file mode 100644 index 00000000..da469e5a --- /dev/null +++ b/CONTRIBUTORS @@ -0,0 +1,24 @@ +# This file is generated by tools/scripts/update_contributors. +# +# Contributors listed in alphabetical order. + +Athan Reines +Brendan Graetz +Bruno Fenzl +Christopher Dambamuromo +Dominik Moritz +Frank Kovacs +James +Jithin KS +Joey Reed +Joris Labie +Justin Dennison +Marcus +Matt Cochrane +Milan Raj +Ognjen Jevremović +Philipp Burckhardt +Ricky Reusser +Ryan Seal +Shraddheya Shendre +rei2hu diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..fcc99346 --- /dev/null +++ b/LICENSE @@ -0,0 +1,481 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + + +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by this +license (the "Software") to use, reproduce, display, distribute, execute, and +transmit the Software, and to prepare derivative works of the Software, and to +permit third-parties to whom the Software is furnished to do so, all subject to +the following: + +The copyright notices in the Software and this entire statement, including the +above license grant, this restriction and the following disclaimer, must be +included in all copies of the Software, in whole or in part, and all derivative +works of the Software, unless such copies or derivative works are solely in the +form of machine-executable object code generated by a source language processor. + +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, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES +OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + + + +DEPENDENCIES + +The library links against the following external libraries, which have their own +licenses: + +* OpenBLAS + +Copyright (c) 2011-2014, The OpenBLAS Project +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + 3. Neither the name of the OpenBLAS project nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +* Electron + +Copyright (c) 2013-2017 GitHub Inc. + +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. + + +* Boost + +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +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, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + + +* Cephes + +Copyright (c) 1984-2000 Stephen L. Moshier + +Some software in this archive may be from the book _Methods and Programs for +Mathematical Functions_ (Prentice-Hall or Simon & Schuster International, 1989) +or from the Cephes Mathematical Library, a commercial product. In either event, +it is copyrighted by the author. What you see here may be used freely but it +comes with no support or guarantee. + +Stephen L. Moshier +moshier@na-net.ornl.gov + + + +ATTRIBUTION + +The library contains implementations from the following external libraries, +which have their own licenses: + +* FreeBSD + +Copyright (C) 1993-2004 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + + +* FDLIBM + +Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + + +* Go + +Copyright (c) 2009 The Go Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +* SLATEC Common Mathematical Library + +Public domain. + + +* ESLint + +Copyright JS Foundation and other contributors, https://js.foundation + +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. + + +* StatsFuns.jl + +Copyright (c) 2015: Dahua Lin. + +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. + + +* SpecialFunctions.jl + +The MIT License (MIT) + +Copyright (c) 2017 Jeff Bezanson, Stefan Karpinski, Viral B. Shah, and others: + +https://github.com/JuliaMath/SpecialFunctions.jl/graphs/contributors + +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. + + +* MT19937 + +Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. The names of its contributors may not be used to endorse or promote + products derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/NOTICE b/NOTICE new file mode 100644 index 00000000..f5374f67 --- /dev/null +++ b/NOTICE @@ -0,0 +1 @@ +Copyright (c) 2016-2021 The Stdlib Authors. diff --git a/README.md b/README.md new file mode 100644 index 00000000..25e7a727 --- /dev/null +++ b/README.md @@ -0,0 +1,987 @@ + + +# Assert + +> Standard library assertion utilities. + +
+ +## Installation + +``` bash +$ npm install @stdlib/assert +``` + +
+ +
+ +## Usage + +```javascript +var assert = require( '@stdlib/assert' ); +``` + +#### assert + +Included in this namespace is a comprehensive suite of assertion utilities, such as utilities for testing for various data types and others for testing for JavaScript feature support. + +```javascript +var o = assert; +// returns {...} +``` + +To validate the built-in JavaScript data types, the namespace includes the following assertion utilities: + + + +
+ +- [`isArray( value )`][@stdlib/assert/is-array]: test if a value is an array. +- [`isBoolean( value )`][@stdlib/assert/is-boolean]: test if a value is a boolean. +- [`isDateObject( value )`][@stdlib/assert/is-date-object]: test if a value is a Date object. +- [`isFunction( value )`][@stdlib/assert/is-function]: test if a value is a function. +- [`isnan( value )`][@stdlib/assert/is-nan]: test if a value is NaN. +- [`isNull( value )`][@stdlib/assert/is-null]: test if a value is null. +- [`isNumber( value )`][@stdlib/assert/is-number]: test if a value is a number. +- [`isObject( value )`][@stdlib/assert/is-object]: test if a value is an object. +- [`isRegExp( value )`][@stdlib/assert/is-regexp]: test if a value is a regular expression. +- [`isString( value )`][@stdlib/assert/is-string]: test if a value is a string. +- [`isSymbol( value )`][@stdlib/assert/is-symbol]: test if a value is a symbol. +- [`isUndefined( value )`][@stdlib/assert/is-undefined]: test if a value is undefined. + +
+ + + +For primitive types having corresponding object wrappers, assertion utilities provide `isObject` and `isPrimitive` methods to test for either objects or primitives, respectively. + + + +```javascript +var isBoolean = require( '@stdlib/assert/is-boolean' ); + +var bool = isBoolean.isObject( new Boolean( false ) ); +// returns true + +bool = isBoolean.isObject( false ); +// returns false + +bool = isBoolean.isPrimitive( false ); +// returns true +``` + +Many of the assertion utilities have corresponding packages that test whether array elements are of the given data type: + + + +
+ +- [`isArrayArray( value )`][@stdlib/assert/is-array-array]: test if a value is an array of arrays. +- [`isBooleanArray( value )`][@stdlib/assert/is-boolean-array]: test if a value is an array-like object of booleans. +- [`isFunctionArray( value )`][@stdlib/assert/is-function-array]: test if a value is an array-like object containing only functions. +- [`isNaNArray( value )`][@stdlib/assert/is-nan-array]: test if a value is an array-like object containing only NaN values. +- [`isNullArray( value )`][@stdlib/assert/is-null-array]: test if a value is an array-like object containing only null values. +- [`isNumberArray( value )`][@stdlib/assert/is-number-array]: test if a value is an array-like object of numbers. +- [`isObjectArray( value )`][@stdlib/assert/is-object-array]: test if a value is an array-like object containing only objects. +- [`isStringArray( value )`][@stdlib/assert/is-string-array]: test if a value is an array of strings. +- [`isSymbolArray( value )`][@stdlib/assert/is-symbol-array]: test if a value is an array-like object containing only symbols. + +
+ + + +Where applicable, similar to the assertion utilities for built-in data types, array assertion utilities provides methods for testing for an array of primitives or objects. + + + +```javascript +var isStringArray = require( '@stdlib/assert/is-string-array' ); + +var bool = isStringArray( [ 'hello', 'world' ] ); +// returns true + +bool = isStringArray.primitives( [ 'hello', 'world' ] ); +// returns true + +bool = isStringArray.objects( [ 'hello', 'world' ] ); +// returns false + +bool = isStringArray.objects( [ new String( 'hello' ), new String( 'world' ) ] ); +// returns true +``` + +The namespace also contains utilities to test for numbers within a certain range or for numbers satisfying a particular "type": + + + +
+ +- [`isCubeNumber( value )`][@stdlib/assert/is-cube-number]: test if a value is a cube number. +- [`isIntegerArray( value )`][@stdlib/assert/is-integer-array]: test if a value is an array-like object containing only integers. +- [`isInteger( value )`][@stdlib/assert/is-integer]: test if a value is a number having an integer value. +- [`isNegativeIntegerArray( value )`][@stdlib/assert/is-negative-integer-array]: test if a value is an array-like object containing only negative integers. +- [`isNegativeInteger( value )`][@stdlib/assert/is-negative-integer]: test if a value is a number having a negative integer value. +- [`isNegativeNumberArray( value )`][@stdlib/assert/is-negative-number-array]: test if a value is an array-like object containing only negative numbers. +- [`isNegativeNumber( value )`][@stdlib/assert/is-negative-number]: test if a value is a number having a negative value. +- [`isNonNegativeIntegerArray( value )`][@stdlib/assert/is-nonnegative-integer-array]: test if a value is an array-like object containing only nonnegative integers. +- [`isNonNegativeInteger( value )`][@stdlib/assert/is-nonnegative-integer]: test if a value is a number having a nonnegative integer value. +- [`isNonNegativeNumberArray( value )`][@stdlib/assert/is-nonnegative-number-array]: test if a value is an array-like object containing only nonnegative numbers. +- [`isNonNegativeNumber( value )`][@stdlib/assert/is-nonnegative-number]: test if a value is a number having a nonnegative value. +- [`isNonPositiveIntegerArray( value )`][@stdlib/assert/is-nonpositive-integer-array]: test if a value is an array-like object containing only nonpositive integers. +- [`isNonPositiveInteger( value )`][@stdlib/assert/is-nonpositive-integer]: test if a value is a number having a nonpositive integer value. +- [`isNonPositiveNumberArray( value )`][@stdlib/assert/is-nonpositive-number-array]: test if a value is an array-like object containing only nonpositive numbers. +- [`isNonPositiveNumber( value )`][@stdlib/assert/is-nonpositive-number]: test if a value is a number having a nonpositive value. +- [`isPositiveIntegerArray( value )`][@stdlib/assert/is-positive-integer-array]: test if a value is an array-like object containing only positive integers. +- [`isPositiveInteger( value )`][@stdlib/assert/is-positive-integer]: test if a value is a number having a positive integer value. +- [`isPositiveNumberArray( value )`][@stdlib/assert/is-positive-number-array]: test if a value is an array-like object containing only positive numbers. +- [`isPositiveNumber( value )`][@stdlib/assert/is-positive-number]: test if a value is a number having a positive value. +- [`isSafeIntegerArray( value )`][@stdlib/assert/is-safe-integer-array]: test if a value is an array-like object containing only safe integers. +- [`isSafeInteger( value )`][@stdlib/assert/is-safe-integer]: test if a value is a number having a safe integer value. +- [`isSquareNumber( value )`][@stdlib/assert/is-square-number]: test if a value is a square number. +- [`isSquareTriangularNumber( value )`][@stdlib/assert/is-square-triangular-number]: test if a value is a square triangular number. +- [`isTriangularNumber( value )`][@stdlib/assert/is-triangular-number]: test if a value is a triangular number. + +
+ + + +The namespace provides various utilities for validating typed arrays: + + + +
+ +- [`isFloat32Array( value )`][@stdlib/assert/is-float32array]: test if a value is a Float32Array. +- [`isFloat64Array( value )`][@stdlib/assert/is-float64array]: test if a value is a Float64Array. +- [`isInt16Array( value )`][@stdlib/assert/is-int16array]: test if a value is an Int16Array. +- [`isInt32Array( value )`][@stdlib/assert/is-int32array]: test if a value is an Int32Array. +- [`isInt8Array( value )`][@stdlib/assert/is-int8array]: test if a value is an Int8Array. +- [`isUint16Array( value )`][@stdlib/assert/is-uint16array]: test if a value is a Uint16Array. +- [`isUint32Array( value )`][@stdlib/assert/is-uint32array]: test if a value is a Uint32Array. +- [`isUint8Array( value )`][@stdlib/assert/is-uint8array]: test if a value is a Uint8Array. +- [`isUint8ClampedArray( value )`][@stdlib/assert/is-uint8clampedarray]: test if a value is a Uint8ClampedArray. + +
+ + + +The namespace includes utilities for validating `ndarray`s (n-dimensional arrays). + + + +
+ +- [`isCentrosymmetricMatrix( value )`][@stdlib/assert/is-centrosymmetric-matrix]: test if a value is a centrosymmetric matrix. +- [`isFloat32MatrixLike( value )`][@stdlib/assert/is-float32matrix-like]: test if a value is a 2-dimensional ndarray-like object containing single-precision floating-point numbers. +- [`isFloat32ndarrayLike( value )`][@stdlib/assert/is-float32ndarray-like]: test if a value is an ndarray-like object containing single-precision floating-point numbers. +- [`isFloat32VectorLike( value )`][@stdlib/assert/is-float32vector-like]: test if a value is a 1-dimensional ndarray-like object containing single-precision floating-point numbers. +- [`isFloat64MatrixLike( value )`][@stdlib/assert/is-float64matrix-like]: test if a value is a 2-dimensional ndarray-like object containing double-precision floating-point numbers. +- [`isFloat64ndarrayLike( value )`][@stdlib/assert/is-float64ndarray-like]: test if a value is an ndarray-like object containing double-precision floating-point numbers. +- [`isFloat64VectorLike( value )`][@stdlib/assert/is-float64vector-like]: test if a value is a 1-dimensional ndarray-like object containing double-precision floating-point numbers. +- [`isMatrixLike( value )`][@stdlib/assert/is-matrix-like]: test if a value is 2-dimensional ndarray-like object. +- [`isndarrayLike( value )`][@stdlib/assert/is-ndarray-like]: test if a value is ndarray-like. +- [`isNonSymmetricMatrix( value )`][@stdlib/assert/is-nonsymmetric-matrix]: test if a value is a non-symmetric matrix. +- [`isPersymmetricMatrix( value )`][@stdlib/assert/is-persymmetric-matrix]: test if a value is a persymmetric matrix. +- [`isSkewCentrosymmetricMatrix( value )`][@stdlib/assert/is-skew-centrosymmetric-matrix]: test if a value is a skew-centrosymmetric matrix. +- [`isSkewPersymmetricMatrix( value )`][@stdlib/assert/is-skew-persymmetric-matrix]: test if a value is a skew-persymmetric matrix. +- [`isSkewSymmetricMatrix( value )`][@stdlib/assert/is-skew-symmetric-matrix]: test if a value is a skew-symmetric matrix. +- [`isSquareMatrix( value )`][@stdlib/assert/is-square-matrix]: test if a value is a 2-dimensional ndarray-like object having equal dimensions. +- [`isSymmetricMatrix( value )`][@stdlib/assert/is-symmetric-matrix]: test if a value is a symmetric matrix. +- [`isVectorLike( value )`][@stdlib/assert/is-vector-like]: test if a value is a 1-dimensional ndarray-like object. + +
+ + + +The namespace includes utilities for validating complex numbers and arrays of complex numbers: + + + +
+ +- [`isComplexLike( value )`][@stdlib/assert/is-complex-like]: test if a value is a complex number-like object. +- [`isComplexTypedArrayLike( value )`][@stdlib/assert/is-complex-typed-array-like]: test if a value is complex-typed-array-like. +- [`isComplexTypedArray( value )`][@stdlib/assert/is-complex-typed-array]: test if a value is a complex typed array. +- [`isComplex( value )`][@stdlib/assert/is-complex]: test if a value is a 64-bit or 128-bit complex number. +- [`isComplex128( value )`][@stdlib/assert/is-complex128]: test if a value is a 128-bit complex number. +- [`isComplex128Array( value )`][@stdlib/assert/is-complex128array]: test if a value is a Complex128Array. +- [`isComplex64( value )`][@stdlib/assert/is-complex64]: test if a value is a 64-bit complex number. +- [`isComplex64Array( value )`][@stdlib/assert/is-complex64array]: test if a value is a Complex64Array. + +
+ + + +The namespace includes utilities for validating other special arrays or buffers: + + + +
+ +- [`isArrayLength( value )`][@stdlib/assert/is-array-length]: test if a value is a valid array length. +- [`isArrayLikeObject( value )`][@stdlib/assert/is-array-like-object]: test if a value is an array-like object. +- [`isArrayLike( value )`][@stdlib/assert/is-array-like]: test if a value is array-like. +- [`isArrayBuffer( value )`][@stdlib/assert/is-arraybuffer]: test if a value is an ArrayBuffer. +- [`isBetweenArray( value, a, b[, left, right] )`][@stdlib/assert/is-between-array]: test if a value is an array-like object where every element is between two values. +- [`isCircularArray( value )`][@stdlib/assert/is-circular-array]: test if a value is an array containing a circular reference. +- [`isEmptyArray( value )`][@stdlib/assert/is-empty-array]: test if a value is an empty array. +- [`isFalsyArray( value )`][@stdlib/assert/is-falsy-array]: test if a value is an array-like object containing only falsy values. +- [`isFiniteArray( value )`][@stdlib/assert/is-finite-array]: test if a value is an array-like object containing only finite numbers. +- [`isNumericArray( value )`][@stdlib/assert/is-numeric-array]: test if a value is a numeric array. +- [`isPlainObjectArray( value )`][@stdlib/assert/is-plain-object-array]: test if a value is an array-like object containing only plain objects. +- [`isProbabilityArray( value )`][@stdlib/assert/is-probability-array]: test if a value is an array-like object containing only probabilities. +- [`isSharedArrayBuffer( value )`][@stdlib/assert/is-sharedarraybuffer]: test if a value is a SharedArrayBuffer. +- [`isTruthyArray( value )`][@stdlib/assert/is-truthy-array]: test if a value is an array-like object containing only truthy values. +- [`isTypedArrayLength( value )`][@stdlib/assert/is-typed-array-length]: test if a value is a valid typed array length. +- [`isTypedArrayLike( value )`][@stdlib/assert/is-typed-array-like]: test if a value is typed-array-like. +- [`isTypedArray( value )`][@stdlib/assert/is-typed-array]: test if a value is a typed array. +- [`isUnityProbabilityArray( value )`][@stdlib/assert/is-unity-probability-array]: test if a value is an array of probabilities that sum to one. + +
+ + + +To test for error objects, the namespace includes the following utilities: + + + +
+ +- [`isError( value )`][@stdlib/assert/is-error]: test if a value is an Error object. +- [`isEvalError( value )`][@stdlib/assert/is-eval-error]: test if a value is an EvalError object. +- [`isRangeError( value )`][@stdlib/assert/is-range-error]: test if a value is a RangeError object. +- [`isReferenceError( value )`][@stdlib/assert/is-reference-error]: test if a value is a ReferenceError object. +- [`isSyntaxError( value )`][@stdlib/assert/is-syntax-error]: test if a value is a SyntaxError object. +- [`isTypeError( value )`][@stdlib/assert/is-type-error]: test if a value is a TypeError object. +- [`isURIError( value )`][@stdlib/assert/is-uri-error]: test if a value is a URIError object. + +
+ + + +The namespace exposes the following constants concerning the current running process: + + + +
+ +- [`IS_BROWSER`][@stdlib/assert/is-browser]: check if the runtime is a web browser. +- [`IS_DARWIN`][@stdlib/assert/is-darwin]: boolean indicating if the current process is running on Darwin. +- [`IS_ELECTRON_MAIN`][@stdlib/assert/is-electron-main]: check if the runtime is the main Electron process. +- [`IS_ELECTRON_RENDERER`][@stdlib/assert/is-electron-renderer]: check if the runtime is the Electron renderer process. +- [`IS_ELECTRON`][@stdlib/assert/is-electron]: check if the runtime is Electron. +- [`IS_LITTLE_ENDIAN`][@stdlib/assert/is-little-endian]: check if an environment is little endian. +- [`IS_NODE`][@stdlib/assert/is-node]: check if the runtime is Node.js. +- [`IS_WEB_WORKER`][@stdlib/assert/is-web-worker]: check if the runtime is a web worker. +- [`IS_WINDOWS`][@stdlib/assert/is-windows]: boolean indicating if the current process is running on Windows. + +
+ + + +To test whether a runtime environment supports certain features, the namespace includes the following utilities: + + + +
+ +- [`hasArrayBufferSupport()`][@stdlib/assert/has-arraybuffer-support]: detect native `ArrayBuffer` support. +- [`hasAsyncAwaitSupport()`][@stdlib/assert/has-async-await-support]: detect native `async`/`await` support. +- [`hasAsyncIteratorSymbolSupport()`][@stdlib/assert/has-async-iterator-symbol-support]: detect native `Symbol.asyncIterator` support. +- [`hasClassSupport()`][@stdlib/assert/has-class-support]: detect native `class` support. +- [`hasDefinePropertiesSupport()`][@stdlib/assert/has-define-properties-support]: detect `Object.defineProperties` support. +- [`hasDefinePropertySupport()`][@stdlib/assert/has-define-property-support]: detect `Object.defineProperty` support. +- [`hasFloat32ArraySupport()`][@stdlib/assert/has-float32array-support]: detect native `Float32Array` support. +- [`hasFloat64ArraySupport()`][@stdlib/assert/has-float64array-support]: detect native `Float64Array` support. +- [`hasFunctionNameSupport()`][@stdlib/assert/has-function-name-support]: detect native function `name` support. +- [`hasGeneratorSupport()`][@stdlib/assert/has-generator-support]: detect native `generator function` support. +- [`hasGlobalThisSupport()`][@stdlib/assert/has-globalthis-support]: detect `globalThis` support. +- [`hasInt16ArraySupport()`][@stdlib/assert/has-int16array-support]: detect native `Int16Array` support. +- [`hasInt32ArraySupport()`][@stdlib/assert/has-int32array-support]: detect native `Int32Array` support. +- [`hasInt8ArraySupport()`][@stdlib/assert/has-int8array-support]: detect native `Int8Array` support. +- [`hasIteratorSymbolSupport()`][@stdlib/assert/has-iterator-symbol-support]: detect native `Symbol.iterator` support. +- [`hasMapSupport()`][@stdlib/assert/has-map-support]: detect native `Map` support. +- [`hasNodeBufferSupport()`][@stdlib/assert/has-node-buffer-support]: detect native `Buffer` support. +- [`hasProxySupport()`][@stdlib/assert/has-proxy-support]: detect native `Proxy` support. +- [`hasSetSupport()`][@stdlib/assert/has-set-support]: detect native `Set` support. +- [`hasSharedArrayBufferSupport()`][@stdlib/assert/has-sharedarraybuffer-support]: detect native `SharedArrayBuffer` support. +- [`hasSymbolSupport()`][@stdlib/assert/has-symbol-support]: detect native `Symbol` support. +- [`hasToStringTagSupport()`][@stdlib/assert/has-tostringtag-support]: detect native `Symbol.toStringTag` support. +- [`hasUint16ArraySupport()`][@stdlib/assert/has-uint16array-support]: detect native `Uint16Array` support. +- [`hasUint32ArraySupport()`][@stdlib/assert/has-uint32array-support]: detect native `Uint32Array` support. +- [`hasUint8ArraySupport()`][@stdlib/assert/has-uint8array-support]: detect native `Uint8Array` support. +- [`hasUint8ClampedArraySupport()`][@stdlib/assert/has-uint8clampedarray-support]: detect native `Uint8ClampedArray` support. +- [`hasWebAssemblySupport()`][@stdlib/assert/has-wasm-support]: detect native WebAssembly support. +- [`hasWeakMapSupport()`][@stdlib/assert/has-weakmap-support]: detect native `WeakMap` support. +- [`hasWeakSetSupport()`][@stdlib/assert/has-weakset-support]: detect native `WeakSet` support. + +
+ + + +The remaining namespace utilities are as follows: + + + +
+ +- [`contains( val, searchValue[, position] )`][@stdlib/assert/contains]: test if an array-like value contains a search value. +- [`deepEqual( a, b )`][@stdlib/assert/deep-equal]: test for deep equality between two values. +- [`deepHasOwnProp( value, path[, options] )`][@stdlib/assert/deep-has-own-property]: test whether an object contains a nested key path. +- [`deepHasProp( value, path[, options] )`][@stdlib/assert/deep-has-property]: test whether an object contains a nested key path, either own or inherited. +- [`hasOwnProp( value, property )`][@stdlib/assert/has-own-property]: test if an object has a specified property. +- [`hasProp( value, property )`][@stdlib/assert/has-property]: test if an object has a specified property, either own or inherited. +- [`hasUTF16SurrogatePairAt( string, position )`][@stdlib/assert/has-utf16-surrogate-pair-at]: test if a position in a string marks the start of a UTF-16 surrogate pair. +- [`instanceOf( value, constructor )`][@stdlib/assert/instance-of]: test whether a value has in its prototype chain a specified constructor as a prototype property. +- [`isAbsolutePath( value )`][@stdlib/assert/is-absolute-path]: test if a value is an absolute path. +- [`isAccessorPropertyIn( value, property )`][@stdlib/assert/is-accessor-property-in]: test if an object's own or inherited property has an accessor descriptor. +- [`isAccessorProperty( value, property )`][@stdlib/assert/is-accessor-property]: test if an object's own property has an accessor descriptor. +- [`isAlphagram( value )`][@stdlib/assert/is-alphagram]: test if a value is an alphagram. +- [`isAlphaNumeric( value )`][@stdlib/assert/is-alphanumeric]: test whether a string contains only alphanumeric characters. +- [`isAnagram( str, value )`][@stdlib/assert/is-anagram]: test if a value is an anagram. +- [`isArguments( value )`][@stdlib/assert/is-arguments]: test if a value is an arguments object. +- [`isASCII( value )`][@stdlib/assert/is-ascii]: test whether a character belongs to the ASCII character set and whether this is true for all characters in a provided string. +- [`isBetween( value, a, b[, left, right] )`][@stdlib/assert/is-between]: test if a value is between two values. +- [`IS_BIG_ENDIAN`][@stdlib/assert/is-big-endian]: check if an environment is big endian. +- [`isBinaryString( value )`][@stdlib/assert/is-binary-string]: test if a value is a binary string. +- [`isBoxedPrimitive( value )`][@stdlib/assert/is-boxed-primitive]: test if a value is a JavaScript boxed primitive. +- [`isBuffer( value )`][@stdlib/assert/is-buffer]: test if a value is a Buffer object. +- [`isCapitalized( value )`][@stdlib/assert/is-capitalized]: test if a value is a string having an uppercase first character. +- [`isCircular( value )`][@stdlib/assert/is-circular]: test if a value is a plain object containing a circular reference. +- [`isCircular( value )`][@stdlib/assert/is-circular]: test if an object-like value contains a circular reference. +- [`isCollection( value )`][@stdlib/assert/is-collection]: test if a value is a collection. +- [`isComposite( value )`][@stdlib/assert/is-composite]: test if a value is a composite number. +- [`isConfigurablePropertyIn( value, property )`][@stdlib/assert/is-configurable-property-in]: test if an object's own or inherited property is configurable. +- [`isConfigurableProperty( value, property )`][@stdlib/assert/is-configurable-property]: test if an object's own property is configurable. +- [`isDataPropertyIn( value, property )`][@stdlib/assert/is-data-property-in]: test if an object's own or inherited property has a data descriptor. +- [`isDataProperty( value, property )`][@stdlib/assert/is-data-property]: test if an object's own property has a data descriptor. +- [`isDigitString( value )`][@stdlib/assert/is-digit-string]: test whether a string contains only numeric digits. +- [`isEmailAddress( value )`][@stdlib/assert/is-email-address]: test if a value is an email address. +- [`isEmptyObject( value )`][@stdlib/assert/is-empty-object]: test if a value is an empty object. +- [`isEmptyString( value )`][@stdlib/assert/is-empty-string]: test if a value is an empty string. +- [`isEnumerablePropertyIn( value, property )`][@stdlib/assert/is-enumerable-property-in]: test if an object's own or inherited property is enumerable. +- [`isEnumerableProperty( value, property )`][@stdlib/assert/is-enumerable-property]: test if an object's own property is enumerable. +- [`isEven( value )`][@stdlib/assert/is-even]: test if a value is an even number. +- [`isFalsy( value )`][@stdlib/assert/is-falsy]: test if a value is falsy. +- [`isFinite( value )`][@stdlib/assert/is-finite]: test if a value is a finite number. +- [`isGeneratorObjectLike( value )`][@stdlib/assert/is-generator-object-like]: test if a value is `generator` object-like. +- [`isGeneratorObject( value )`][@stdlib/assert/is-generator-object]: test if a value is a `generator` object. +- [`isgzipBuffer( value )`][@stdlib/assert/is-gzip-buffer]: test if a value is a gzip buffer. +- [`isHexString( value )`][@stdlib/assert/is-hex-string]: test whether a string contains only hexadecimal digits. +- [`isInfinite( value )`][@stdlib/assert/is-infinite]: test if a value is an infinite number. +- [`isInheritedProperty( value, property )`][@stdlib/assert/is-inherited-property]: test if an object has an inherited property. +- [`isIterableLike( value )`][@stdlib/assert/is-iterable-like]: test if a value is `iterable`-like. +- [`isIteratorLike( value )`][@stdlib/assert/is-iterator-like]: test if a value is `iterator`-like. +- [`isJSON( value )`][@stdlib/assert/is-json]: test if a value is a parseable JSON string. +- [`isLeapYear( [value] )`][@stdlib/assert/is-leap-year]: test if a value corresponds to a leap year in the Gregorian calendar. +- [`isLowercase( value )`][@stdlib/assert/is-lowercase]: test if a value is a lowercase string. +- [`isMethodIn( value, property )`][@stdlib/assert/is-method-in]: test if an object has a specified method name, either own or inherited. +- [`isMethod( value, property )`][@stdlib/assert/is-method]: test if an object has a specified method name. +- [`isNamedTypedTupleLike( value )`][@stdlib/assert/is-named-typed-tuple-like]: test if a value is named typed tuple-like. +- [`isNativeFunction( value )`][@stdlib/assert/is-native-function]: test if a value is a native function. +- [`isNegativeZero( value )`][@stdlib/assert/is-negative-zero]: test if a value is a number equal to negative zero. +- [`isNodeBuiltin( value )`][@stdlib/assert/is-node-builtin]: test whether a string matches a Node.js built-in module name. +- [`isNodeDuplexStreamLike( value )`][@stdlib/assert/is-node-duplex-stream-like]: test if a value is Node duplex stream-like. +- [`isNodeReadableStreamLike( value )`][@stdlib/assert/is-node-readable-stream-like]: test if a value is Node readable stream-like. +- [`isNodeREPL()`][@stdlib/assert/is-node-repl]: check if running in a Node.js REPL environment. +- [`isNodeStreamLike( value )`][@stdlib/assert/is-node-stream-like]: test if a value is Node stream-like. +- [`isNodeTransformStreamLike( value )`][@stdlib/assert/is-node-transform-stream-like]: test if a value is Node transform stream-like. +- [`isNodeWritableStreamLike( value )`][@stdlib/assert/is-node-writable-stream-like]: test if a value is Node writable stream-like. +- [`isNonConfigurablePropertyIn( value, property )`][@stdlib/assert/is-nonconfigurable-property-in]: test if an object's own or inherited property is non-configurable. +- [`isNonConfigurableProperty( value, property )`][@stdlib/assert/is-nonconfigurable-property]: test if an object's own property is non-configurable. +- [`isNonEnumerablePropertyIn( value, property )`][@stdlib/assert/is-nonenumerable-property-in]: test if an object's own or inherited property is non-enumerable. +- [`isNonEnumerableProperty( value, property )`][@stdlib/assert/is-nonenumerable-property]: test if an object's own property is non-enumerable. +- [`isObjectLike( value )`][@stdlib/assert/is-object-like]: test if a value is object-like. +- [`isOdd( value )`][@stdlib/assert/is-odd]: test if a value is an odd number. +- [`isPlainObject( value )`][@stdlib/assert/is-plain-object]: test if a value is a plain object. +- [`isPositiveZero( value )`][@stdlib/assert/is-positive-zero]: test if a value is a number equal to positive zero. +- [`isPrime( value )`][@stdlib/assert/is-prime]: test if a value is a prime number. +- [`isPrimitive( value )`][@stdlib/assert/is-primitive]: test if a value is a JavaScript primitive. +- [`isPRNGLike( value )`][@stdlib/assert/is-prng-like]: test if a value is PRNG-like. +- [`isProbability( value )`][@stdlib/assert/is-probability]: test if a value is a probability. +- [`isPrototypeOf( obj, prototype )`][@stdlib/assert/is-prototype-of]: test if an object's prototype chain contains a provided prototype. +- [`isReadOnlyPropertyIn( value, property )`][@stdlib/assert/is-read-only-property-in]: test if an object's own or inherited property is read-only. +- [`isReadOnlyProperty( value, property )`][@stdlib/assert/is-read-only-property]: test if an object's own property is read-only. +- [`isReadWritePropertyIn( value, property )`][@stdlib/assert/is-read-write-property-in]: test if an object's own or inherited property is readable and writable. +- [`isReadWriteProperty( value, property )`][@stdlib/assert/is-read-write-property]: test if an object's own property is readable and writable. +- [`isReadablePropertyIn( value, property )`][@stdlib/assert/is-readable-property-in]: test if an object's own or inherited property is readable. +- [`isReadableProperty( value, property )`][@stdlib/assert/is-readable-property]: test if an object's own property is readable. +- [`isRegExpString( value )`][@stdlib/assert/is-regexp-string]: test if a value is a regular expression string. +- [`isRelativePath( value )`][@stdlib/assert/is-relative-path]: test if a value is a relative path. +- [`isSameValueZero( a, b )`][@stdlib/assert/is-same-value-zero]: test if two arguments are the same value. +- [`isSameValue( a, b )`][@stdlib/assert/is-same-value]: test if two arguments are the same value. +- [`isStrictEqual( a, b )`][@stdlib/assert/is-strict-equal]: test if two arguments are strictly equal. +- [`isTruthy( value )`][@stdlib/assert/is-truthy]: test if a value is truthy. +- [`isUNCPath( value )`][@stdlib/assert/is-unc-path]: test if a value is a UNC path. +- [`isUndefinedOrNull( value )`][@stdlib/assert/is-undefined-or-null]: test if a value is undefined or null. +- [`isUppercase( value )`][@stdlib/assert/is-uppercase]: test if a value is an uppercase string. +- [`isURI( value )`][@stdlib/assert/is-uri]: test if a value is a URI. +- [`isWhitespace( value )`][@stdlib/assert/is-whitespace]: test whether a string contains only white space characters. +- [`isWritablePropertyIn( value, property )`][@stdlib/assert/is-writable-property-in]: test if an object's own or inherited property is writable. +- [`isWritableProperty( value, property )`][@stdlib/assert/is-writable-property]: test if an object's own property is writable. +- [`isWriteOnlyPropertyIn( value, property )`][@stdlib/assert/is-write-only-property-in]: test if an object's own or inherited property is write-only. +- [`isWriteOnlyProperty( value, property )`][@stdlib/assert/is-write-only-property]: test if an object's own property is write-only. + +
+ + + +
+ + + +
+ +## Examples + + + + + +```javascript +var objectKeys = require( '@stdlib/utils/keys' ); +var assert = require( '@stdlib/assert' ); + +console.log( objectKeys( assert ) ); +``` + +
+ + + + +
+ +* * * + +## Notice + +This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. + +For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. + +--- + +## License + +See [LICENSE][stdlib-license]. + + +## Copyright + +Copyright © 2016-2021. The Stdlib [Authors][stdlib-authors]. + +
+ + + + + + + + diff --git a/contains/README.md b/contains/README.md new file mode 100644 index 00000000..5dbda8b8 --- /dev/null +++ b/contains/README.md @@ -0,0 +1,154 @@ + + +# Contains + +> Test if an array-like value contains a search value. + +
+ +
+ + + +
+ +## Usage + +```javascript +var contains = require( '@stdlib/assert/contains' ); +``` + +#### contains( val, searchValue\[, position] ) + +Tests if `val` contains a search value. When `val` is a `string`, the function checks whether the characters of a search string are found in the input string. + +```javascript +var v = contains( 'Hello World', 'World' ); +// returns true +``` + +When `val` is an `array-like` object, but not a `string`, the function checks whether the input value contains an element strictly equal to the specified search value. + +```javascript +var arr = [ null, NaN, 2, 'abc', {} ]; + +var v = contains( arr, NaN ); // NaNs are considered equal +// returns true + +v = contains( arr, {} ); +// returns false + +v = contains( arr, 'ab' ); +// returns false +``` + +Search is case-sensitive. + +```javascript +var v = contains( 'Hello World', 'world' ); +// returns false +``` + +To start searching at a specified index, provide a `position` argument. + +```javascript +var v = contains( 'Hello World', 'Hello', 6 ); +// returns false + +v = contains( [ true, NaN, false ], true, 1 ); +// returns false +``` + +If not provided an `array-like` object, the function throws an error. + + + +```javascript +var v = contains( false, 'abc' ); +// throws +``` + +If not provided an integer-valued `position` argument, the function throws an error. + + + +```javascript +var v = contains( 'hello', 'e', 2.5 ); +// throws +``` + +
+ + + +
+ +* * * + +## Notes + +- For `strings`, the function is modeled after [String.prototype.includes][mdn-includes], part of the ECMAScript 6 specification. This function is different from a call to `String.prototype.includes.call` insofar as type-checking is performed for all arguments. +- The function does **not** distinguish between positive and negative zero. +- If `position < 0`, the search is performed for the entire input array or string. + +
+ + + +
+ +## Examples + + + +```javascript +var contains = require( '@stdlib/assert/contains' ); + +var bool = contains( 'last man standing', 'stand' ); +// returns true + +bool = contains( [ 1, 2, 3, 4 ], 2 ); +// returns true + +bool = contains( 'presidential election', 'president' ); +// returns true + +bool = contains( [ NaN, 2, 3, 4 ], NaN ); +// returns true + +bool = contains( 'javaScript', 'js' ); +// returns false + +bool = contains( 'Hidden Treasures', '' ); +// returns true +``` + +
+ + + + + + diff --git a/contains/benchmark/benchmark.js b/contains/benchmark/benchmark.js new file mode 100644 index 00000000..4a6f4469 --- /dev/null +++ b/contains/benchmark/benchmark.js @@ -0,0 +1,75 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var contains = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::string', function benchmark( b ) { + var bool; + var str; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = 'abcdefghijklmnopqrstuvwxyz'+fromCodePoint( i%126 ); + bool = contains( str, 'z' ); + if ( bool === false ) { + b.fail( 'should not return false' ); + } + } + b.toc(); + if ( bool === false ) { + b.fail( 'should not return false' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array', function benchmark( b ) { + var bool; + var arr; + var i; + + arr = []; + for ( i = 0; i < 10; i++ ) { + arr.push( i ); + } + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr[ 0 ] = randu(); + bool = contains( arr, 9 ); + if ( bool === false ) { + b.fail( 'should not return false' ); + } + } + b.toc(); + if ( bool === false ) { + b.fail( 'should not return false' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/contains/benchmark/benchmark.length.js b/contains/benchmark/benchmark.length.js new file mode 100644 index 00000000..6363a903 --- /dev/null +++ b/contains/benchmark/benchmark.length.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var contains = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( i ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a value. + x[ len-2 ] = randu(); + bool = fcn( x, len-1 ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( contains, len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/contains/docs/repl.txt b/contains/docs/repl.txt new file mode 100644 index 00000000..5ba5b560 --- /dev/null +++ b/contains/docs/repl.txt @@ -0,0 +1,57 @@ + +{{alias}}( val, searchValue[, position] ) + Tests if an array-like value contains a search value. + + When `val` is a string, the function checks whether the characters of the + search string are found in the input string. The search is case-sensitive. + + When `val` is an array-like object, the function checks whether the input + array contains an element strictly equal to the specified search value. + + For strings, this function is modeled after `String.prototype.includes`, + part of the ECMAScript 6 specification. This function is different from a + call to `String.prototype.includes.call` insofar as type-checking is + performed for all arguments. + + The function does not distinguish between positive and negative zero. + + If `position < 0`, the search is performed for the entire input array or + string. + + + Parameters + ---------- + val: ArrayLike + Input value. + + searchValue: any + Value to search for. + + position: integer (optional) + Position at which to start searching for `searchValue`. Default: `0`. + + Returns + ------- + bool: boolean + Boolean indicating if an input value contains another value. + + Examples + -------- + > var bool = {{alias}}( 'Hello World', 'World' ) + true + > bool = {{alias}}( 'Hello World', 'world' ) + false + > bool = {{alias}}( [ 1, 2, 3, 4 ], 2 ) + true + > bool = {{alias}}( [ NaN, 2, 3, 4 ], NaN ) + true + + // Supply a position: + > bool = {{alias}}( 'Hello World', 'Hello', 6 ) + false + > bool = {{alias}}( [ true, NaN, false ], true, 1 ) + false + + See Also + -------- + diff --git a/contains/docs/types/index.d.ts b/contains/docs/types/index.d.ts new file mode 100644 index 00000000..1e4c144f --- /dev/null +++ b/contains/docs/types/index.d.ts @@ -0,0 +1,75 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/// + +import { ArrayLike } from '@stdlib/types/array'; + +/** +* Tests if an array-like value contains a search value. +* +* ## Notes +* +* - When `val` is a string, the function checks whether the characters of the search string are found in the input string. The search is case-sensitive. +* - When `val` is an array-like object, the function checks whether the input array contains an element strictly equal to the specified search value. +* - For strings, this function is modeled after `String.prototype.includes`, part of the ECMAScript 6 specification. This function is different from a call to `String.prototype.includes.call` insofar as type-checking is performed for all arguments. +* - The function does not distinguish between positive and negative zero. +* - If `position < 0`, the search is performed for the entire input array or string. +* +* @param val - input value +* @param searchValue - search value +* @param position - position at which to start searching for `searchValue` (default: 0) +* @throws second argument must be a primitive string primitive when the first argument is a string +* @returns boolean indicating whether one value contains another +* +* @example +* var bool = contains( 'last man standing', 'stand' ); +* // returns true +* +* @example +* var bool = contains( [ 1, 2, 3, 4 ], 2 ); +* // returns true +* +* @example +* var bool = contains( 'presidential election', 'president' ); +* // returns true +* +* @example +* var bool = contains( [ NaN, 2, 3, 4 ], NaN ); +* // returns true +* +* @example +* var bool = contains( 'javaScript', 'js' ); +* // returns false +* +* @example +* var bool = contains( [ 1, 2, 3, {} ], {} ); +* // returns false +* +* @example +* var bool = contains( 'Hidden Treasures', '' ); +* // returns true +*/ +declare function contains( val: ArrayLike, searchValue: any, position?: number ): boolean; // tslint:disable-line:max-line-length + + +// EXPORTS // + +export = contains; diff --git a/contains/docs/types/test.ts b/contains/docs/types/test.ts new file mode 100644 index 00000000..a1c8f3cc --- /dev/null +++ b/contains/docs/types/test.ts @@ -0,0 +1,51 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import contains = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + contains( 'abd', 'a' ); // $ExpectType boolean + contains( 'To be, or not to be, that is the question.', 'question', 10 ); // $ExpectType boolean + contains( 'abd', 'ab', 2 ); // $ExpectType boolean + contains( [ 1, 2, 3, 4, 5, 6 ], 2 ); // $ExpectType boolean + contains( [ 1, 2, 3, 4, 5, 6 ], 2, 3 ); // $ExpectType boolean +} + +// The function does not compile if provided arguments having invalid types... +{ + contains( true, 'd' ); // $ExpectError + contains( false, 'd' ); // $ExpectError + contains( 3, 'd', 3 ); // $ExpectError + contains( {}, 'd', 3 ); // $ExpectError + + contains( 'abd', 'a', true ); // $ExpectError + contains( 'abd', 'a', false ); // $ExpectError + contains( 'abd', 'a', [] ); // $ExpectError + contains( 'abd', 'a', {} ); // $ExpectError + contains( 'abd', 'a', ( x: number ): number => x ); // $ExpectError +} + +// The function does not compile if provided insufficient arguments... +{ + contains(); // $ExpectError + contains( 'abc' ); // $ExpectError +} diff --git a/contains/examples/index.js b/contains/examples/index.js new file mode 100644 index 00000000..0830aec8 --- /dev/null +++ b/contains/examples/index.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var contains = require( './../lib' ); + +console.log( contains( 'last man standing', 'stand' ) ); +// => true + +console.log( contains( [ 1, 2, 3, 4 ], 2 ) ); +// => true + +console.log( contains( 'presidential election', 'president' ) ); +// => true + +console.log( contains( 'javaScript', 'js' ) ); +// => false + +console.log( contains( [ NaN, 2, 3, 4 ], NaN ) ); +// => true + +console.log( contains( 'Hidden Treasures', '' ) ); +// => true diff --git a/contains/lib/contains.js b/contains/lib/contains.js new file mode 100644 index 00000000..44c5fe8b --- /dev/null +++ b/contains/lib/contains.js @@ -0,0 +1,118 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isCollection = require( '@stdlib/assert/is-collection' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isnan = require( '@stdlib/assert/is-nan' ).isPrimitive; + + +// MAIN // + +/** +* Tests if an array-like value contains a search value. +* +* @param {(Collection|string)} val - input value +* @param {*} searchValue - search value +* @param {integer} [position=0] - position at which to start searching for `searchValue` +* @throws {TypeError} first argument must be array-like +* @throws {Error} must provide a search value +* @throws {TypeError} second argument must be a primitive string primitive when the first argument is a string +* @throws {TypeError} third argument must be an integer +* @returns {boolean} boolean indicating whether one value contains another +* +* @example +* var bool = contains( 'last man standing', 'stand' ); +* // returns true +* +* @example +* var bool = contains( [ 1, 2, 3, 4 ], 2 ); +* // returns true +* +* @example +* var bool = contains( 'presidential election', 'president' ); +* // returns true +* +* @example +* var bool = contains( [ NaN, 2, 3, 4 ], NaN ); +* // returns true +* +* @example +* var bool = contains( 'javaScript', 'js' ); +* // returns false +* +* @example +* var bool = contains( [ 1, 2, 3, {} ], {} ); +* // returns false +* +* @example +* var bool = contains( 'Hidden Treasures', '' ); +* // returns true +*/ +function contains( val, searchValue, position ) { + var len; + var pos; + var i; + if ( !isCollection( val ) && !isString( val ) ) { + throw new TypeError( 'invalid argument. First argument must be array-like. Value: `' + val + '`.' ); + } + if ( arguments.length < 2 ) { + throw new Error( 'insufficient input arguments. Must provide a search value.' ); + } + if ( arguments.length > 2 ) { + if ( !isInteger( position ) ) { + throw new TypeError( 'invalid argument. Third argument must be an integer. Value: `' + position + '`.' ); + } + pos = position; + if ( pos < 0 ) { + pos = 0; + } + } else { + pos = 0; + } + if ( isString( val ) ) { + if ( !isString( searchValue ) ) { + throw new TypeError( 'invalid argument. Second argument must be a string primitive. Value: `' + searchValue + '`.' ); + } + return val.indexOf( searchValue, pos ) !== -1; + } + len = val.length; + if ( isnan( searchValue ) ) { + for ( i = pos; i < len; i++ ) { + if ( isnan( val[ i ] ) ) { + return true; + } + } + return false; + } + for ( i = pos; i < len; i++ ) { + if ( val[ i ] === searchValue ) { + return true; + } + } + return false; +} + + +// EXPORTS // + +module.exports = contains; diff --git a/contains/lib/index.js b/contains/lib/index.js new file mode 100644 index 00000000..c4f7e41e --- /dev/null +++ b/contains/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if an array-like value contains another value. +* +* @module @stdlib/assert/contains +* +* @example +* var contains = require( '@stdlib/assert/contains' ); +* +* var bool = contains( 'Hello World', 'World' ); +* // returns true +* +* bool = contains( 'Hello World', 'world' ); +* // returns false +* +* bool = contains( [ 1, 2, 3, 4 ], 2 ); +* // returns true +* +* bool = contains( [ NaN, 2, 3, 4 ], NaN ); +* // returns true +*/ + +// MODULES // + +var contains = require( './contains.js' ); + + +// EXPORTS // + +module.exports = contains; diff --git a/contains/package.json b/contains/package.json new file mode 100644 index 00000000..9c35c40d --- /dev/null +++ b/contains/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/assert/contains", + "version": "0.0.0", + "description": "Test if an array-like value contains a search value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "array-like", + "array", + "string", + "str", + "find", + "check", + "contains", + "has", + "includes" + ] +} diff --git a/contains/test/test.js b/contains/test/test.js new file mode 100644 index 00000000..952daf92 --- /dev/null +++ b/contains/test/test.js @@ -0,0 +1,236 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var contains = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof contains, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if the first argument is not array-like', function test( t ) { + var values; + var i; + + values = [ + 5, + null, + true, + false, + void 0, + NaN, + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + contains( value, '' ); + }; + } +}); + +tape( 'the function throws an error if not provided a second argument', function test( t ) { + t.throws( badValue, Error, 'throws an error when not provided a second argument' ); + t.end(); + + function badValue() { + return contains( 'abc' ); + } +}); + +tape( 'the function throws an error if the first argument is a string and the second argument is not a string', function test( t ) { + var values; + var i; + + values = [ + 5, + null, + true, + false, + void 0, + NaN, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + contains( 'abc', value ); + }; + } +}); + +tape( 'the function throws an error if the `position` argument is not an integer', function test( t ) { + var values; + var i; + + values = [ + 6.5, + '5', + null, + true, + false, + void 0, + NaN, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + contains( 'Hello World', 'Hello', value ); + }; + } +}); + +tape( 'the function returns `true` when a search string is contained in input string', function test( t ) { + var out; + + out = contains( 'Hello World', 'World' ); + t.equal( out, true, 'returns true' ); + + out = contains( 'Hello World', ' ' ); + t.equal( out, true, 'returns true' ); + + out = contains( 'Hello World', 'Hell' ); + t.equal( out, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` when a search string is not contained in input string', function test( t ) { + var out; + + out = contains( 'Hello World', 'world' ); + t.equal( out, false, 'returns false' ); + + out = contains( 'Hello World', '\t' ); + t.equal( out, false, 'returns false' ); + + out = contains( 'Hello World', 'Word' ); + t.equal( out, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `true` when a search value is contained in input array', function test( t ) { + var out; + + out = contains( [ NaN, null, 3, 'abc' ], 'abc' ); + t.equal( out, true, 'returns true' ); + + out = contains( [ NaN, null, 3, 'abc' ], NaN ); + t.equal( out, true, 'returns true' ); + + out = contains( [ NaN, null, 3, 'abc' ], null ); + t.equal( out, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` when search value is not contained in input array', function test( t ) { + var out; + + out = contains( [ NaN, null, 3, 'abc' ], 'ac' ); + t.equal( out, false, 'returns false' ); + + out = contains( [ NaN, null, 3, 'abc' ], false ); + t.equal( out, false, 'returns false' ); + + out = contains( [ NaN, null, 3, 'abc' ], 3.5 ); + t.equal( out, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function supports beginning a search at the specified position', function test( t ) { + var out; + + out = contains( 'ABCDEFG', 'A', 1 ); + t.equal( out, false, 'returns false' ); + + out = contains( 'ABCDEFG', 'B', 1 ); + t.equal( out, true, 'returns true' ); + + out = contains( 'ABCDEFG', 'A', -3 ); + t.equal( out, true, 'returns true' ); + + out = contains( [ null, NaN, 2 ], 3, 1 ); + t.equal( out, false, 'returns false' ); + + out = contains( [ null, NaN, 2 ], NaN, 1 ); + t.equal( out, true, 'returns true' ); + + out = contains( [ null, NaN, 2 ], NaN, -3 ); + t.equal( out, true, 'returns true' ); + + out = contains( [ null, NaN, 2 ], NaN, 2 ); + t.equal( out, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `true` if provided an input string and an empty string as the search value', function test( t ) { + t.equal( contains( 'abc', '' ), true, 'returns true' ); + t.equal( contains( '', '' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided an empty array-like value', function test( t ) { + t.equal( contains( [], '' ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function does not distinguish between positive and negative zero', function test( t ) { + t.equal( contains( [ -0.0 ], +0.0 ), true, 'returns true' ); + t.equal( contains( [ +0.0 ], -0.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a position which exceeds the maximum index', function test( t ) { + t.equal( contains( [ 1 ], 1, 1e5 ), false, 'returns false' ); + t.equal( contains( 'abc', 'a', 1e5 ), false, 'returns false' ); + t.end(); +}); diff --git a/deep-equal/README.md b/deep-equal/README.md new file mode 100644 index 00000000..3be6f07d --- /dev/null +++ b/deep-equal/README.md @@ -0,0 +1,113 @@ + + +# deepEqual + +> Test for deep equality between two values. + +
+ +## Usage + +```javascript +var deepEqual = require( '@stdlib/assert/deep-equal' ); +``` + +#### deepEqual( a, b ) + +Returns a `boolean` indicating if `a` is deep equal to `b`. + + + +```javascript +var bool = deepEqual( [ 1, 2, 3 ], [ 1, 2, 3 ] ); +// returns true + +bool = deepEqual( [ 1, 2, 3 ], [ 1, 2, '3' ] ); +// returns false + +bool = deepEqual( { 'a': 2 }, { 'a': [ 2 ] } ); +// returns false +``` + +
+ + + +
+ +## Notes + +- The function uses strict equality checks (`===`) and does not perform any type coercion. +- When given two objects, only enumerable own properties are recursively compared. + +
+ + + +
+ +## Examples + + + +```javascript +var deepEqual = require( '@stdlib/assert/deep-equal' ); +var bool; +var a; +var b; + +a = [ true, false, true ]; +b = [ true, false, true ]; +bool = deepEqual( a, b ); +// returns true + +b.pop(); +bool = deepEqual( a, b ); +// returns false + +a = { 'a': { 'b': { 'c': 'd' } } }; +b = { 'a': { 'b': { 'c': 'd' } } }; +bool = deepEqual( a, b ); +// returns true + +b.a.b.c = null; +bool = deepEqual( a, b ); +// returns false + +a = { 'a': [ { 'b': 0 }, { 'c': 1 } ] }; +b = { 'a': [ { 'b': 0 }, { 'c': 1 } ] }; +bool = deepEqual( a, b ); +// returns true + +b = { 'a': [ { 'b': [ 0 ] }, { 'c': '1' } ] }; +bool = deepEqual( a, b ); +// returns false +``` + +
+ + + + + + diff --git a/deep-equal/benchmark/benchmark.js b/deep-equal/benchmark/benchmark.js new file mode 100644 index 00000000..4afe57d8 --- /dev/null +++ b/deep-equal/benchmark/benchmark.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Buffer = require( '@stdlib/buffer/ctor' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var pkg = require( './../package.json' ).name; +var deepEqual = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var xi; + var yi; + var x; + var y; + var i; + + values = [ + 'beep', + 'boop', + 'baz', + 1, + '1', + 2.3, + '2.3', + 0, + -2.0, + true, + false, + 'true', + 'false', + null, + NaN, + void 0, + new Error( 'beep' ), + new Error( 'boop' ), + new TypeError( 'baz' ), + new RangeError( 'baz' ), + new SyntaxError(), + new Date( '2018-09-20T01:23:28.936Z' ), + new Date( 1537406608936 ), + new Buffer( 'xyz' ), // eslint-disable-line no-buffer-constructor + new Number( 0 ), // eslint-disable-line no-new-wrappers, + [ 1, 2, 3 ], + [ 1, 2 ], + { + '0': 0, + '1': 1, + 'length': 2 + }, + [ null, null, null ], + [ NaN, NaN, NaN ], + [ void 0, void 0, void 0 ] + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + xi = discreteUniform( 0, values.length ); + yi = discreteUniform( 0, values.length ); + x = values[ xi ]; + y = values[ yi ]; + out = deepEqual( x, y ); + if ( !isBoolean( out ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( out ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/deep-equal/docs/repl.txt b/deep-equal/docs/repl.txt new file mode 100644 index 00000000..87691c5d --- /dev/null +++ b/deep-equal/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( a, b ) + Tests for deep equality between two values. + + Parameters + ---------- + a: any + First comparison value. + + b: any + Second comparison value. + + Returns + ------- + out: bool + Boolean indicating if `a` is deep equal to `b`. + + Examples + -------- + > var bool = {{alias}}( [ 1, 2, 3 ], [ 1, 2, 3 ] ) + true + + > bool = {{alias}}( [ 1, 2, 3 ], [ 1, 2, '3' ] ) + false + + > bool = {{alias}}( { 'a': 2 }, { 'a': [ 2 ] } ) + false + + See Also + -------- + diff --git a/deep-equal/docs/types/index.d.ts b/deep-equal/docs/types/index.d.ts new file mode 100644 index 00000000..dc3295ee --- /dev/null +++ b/deep-equal/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for deep equality between two values. +* +* @param a - first comparison value +* @param b - second comparison value +* @returns boolean indicating if `a` is deep equal to `b` +* +* @example +* var bool = deepEqual( [ 1, 2, 3 ], [ 1, 2, 3 ] ); +* // returns true +* +* @example +* var bool = deepEqual( [ 1, 2, 3 ], [ 1, 2, '3' ] ); +* // returns false +* +* @example +* var bool = deepEqual( { 'a': 2 }, { 'a': [ 2 ] } ); +* // returns false +* +* @example +* var bool = deepEqual( [], {} ); +* // returns false +* +* @example +* var bool = deepEqual( null, null ); +* // returns true +*/ +declare function deepEqual( a: any, b: any ): boolean; + + +// EXPORTS // + +export = deepEqual; diff --git a/deep-equal/docs/types/test.ts b/deep-equal/docs/types/test.ts new file mode 100644 index 00000000..02c76c48 --- /dev/null +++ b/deep-equal/docs/types/test.ts @@ -0,0 +1,36 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import deepEqual = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + deepEqual( [ 1, 2, 3 ], [ 1, 2, 3 ] ); // $ExpectType boolean + deepEqual( null, null ); // $ExpectType boolean + deepEqual( 'beep', 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + deepEqual(); // $ExpectError + deepEqual( [ 1, 2, 3 ] ); // $ExpectError + deepEqual( 'beep', 'beep', [ 1, 2, 3 ] ); // $ExpectError +} diff --git a/deep-equal/examples/index.js b/deep-equal/examples/index.js new file mode 100644 index 00000000..223d0033 --- /dev/null +++ b/deep-equal/examples/index.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline, object-curly-spacing */ + +'use strict'; + +var deepEqual = require( './../lib' ); + +var bool; +var a; +var b; + +a = [ true, false, true ]; +b = [ true, false, true ]; +bool = deepEqual( a, b ); +console.log( bool ); +// => true + +b.pop(); +bool = deepEqual( a, b ); +console.log( bool ); +// => false + +a = { 'a': { 'b': { 'c': 'd' } } }; +b = { 'a': { 'b': { 'c': 'd' } } }; +bool = deepEqual( a, b ); +console.log( bool ); +// => true + +b.a.b.c = null; +bool = deepEqual( a, b ); +console.log( bool ); +// => false + +a = { 'a': [ { 'b': 0 }, { 'c': 1 } ] }; +b = { 'a': [ { 'b': 0 }, { 'c': 1 } ] }; +bool = deepEqual( a, b ); +console.log( bool ); +// => true + +b = { 'a': [ { 'b': [ 0 ] }, { 'c': '1' } ] }; +bool = deepEqual( a, b ); +console.log( bool ); +// => false diff --git a/deep-equal/lib/index.js b/deep-equal/lib/index.js new file mode 100644 index 00000000..9cc8dda6 --- /dev/null +++ b/deep-equal/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for deep equality between two values. +* +* @module @stdlib/assert/deep-equal +* +* @example +* var deepEqual = require( '@stdlib/assert/deep-equal' ); +* +* var bool = deepEqual( [ 1, 2, 3 ], [ 1, 2, 3 ] ); +* // returns true +* +* bool = deepEqual( [ 1, 2, 3 ], [ 1, 2, '3' ] ); +* // returns false +* +* bool = deepEqual( { 'a': 2 }, { 'a': [ 2 ] } ); +* // returns false +*/ + +// MODULES // + +var deepEqual = require( './main.js' ); + + +// EXPORTS // + +module.exports = deepEqual; diff --git a/deep-equal/lib/main.js b/deep-equal/lib/main.js new file mode 100644 index 00000000..7dc8774a --- /dev/null +++ b/deep-equal/lib/main.js @@ -0,0 +1,132 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var objectKeys = require( '@stdlib/utils/keys' ); +var getPrototypeOf = require( '@stdlib/utils/get-prototype-of' ); +var isDate = require( '@stdlib/assert/is-date-object' ); +var isError = require( '@stdlib/assert/is-error' ); +var isBuffer = require( '@stdlib/assert/is-buffer' ); +var isRegExp = require( '@stdlib/assert/is-regexp' ); + + +// MAIN // + +/** +* Tests for deep equality between two values. +* +* @param {*} a - first comparison value +* @param {*} b - second comparison value +* @returns {boolean} boolean indicating if `a` is deep equal to `b` +* +* @example +* var bool = deepEqual( [ 1, 2, 3 ], [ 1, 2, 3 ] ); +* // returns true +* +* @example +* var bool = deepEqual( [ 1, 2, 3 ], [ 1, 2, '3' ] ); +* // returns false +* +* @example +* var bool = deepEqual( { 'a': 2 }, { 'a': [ 2 ] } ); +* // returns false +* +* @example +* var bool = deepEqual( [], {} ); +* // returns false +* +* @example +* var bool = deepEqual( null, null ); +* // returns true +*/ +function deepEqual( a, b ) { + var aKeys; + var bKeys; + var typeA; + var typeB; + var key; + var i; + + typeA = typeof a; + typeB = typeof b; + if ( a === null || typeA !== 'object' ) { + if ( b === null || typeB !== 'object' ) { + return a === b; + } + return false; + } + // Case: `a` is of type 'object' + if ( typeB !== 'object' ) { + return false; + } + if ( getPrototypeOf( a ) !== getPrototypeOf( b ) ) { + return false; + } + if ( isDate( a ) ) { + return a.getTime() === b.getTime(); + } + if ( isRegExp( a ) ) { + return a.source === b.source && a.flags === b.flags; + } + if ( isError( a ) ) { + if ( a.message !== b.message || a.name !== b.name ) { + return false; + } + } + if ( isBuffer( a ) ) { + if ( a.length !== b.length ) { + return false; + } + for ( i = 0; i < a.length; i++ ) { + if ( a[ i ] !== b[ i ] ) { + return false; + } + } + return true; + } + aKeys = objectKeys( a ); + bKeys = objectKeys( b ); + if ( aKeys.length !== bKeys.length ) { + return false; + } + aKeys.sort(); + bKeys.sort(); + + // Cheap key test: + for ( i = 0; i < aKeys.length; i++ ) { + if ( aKeys[ i ] !== bKeys[ i ] ) { + return false; + } + } + // Possibly expensive deep equality test for each corresponding key: + for ( i = 0; i < aKeys.length; i++ ) { + key = aKeys[ i ]; + if ( !deepEqual( a[ key ], b[ key ] ) ) { + return false; + } + } + return typeA === typeB; +} + + +// EXPORTS // + +module.exports = deepEqual; diff --git a/deep-equal/package.json b/deep-equal/package.json new file mode 100644 index 00000000..887aadbe --- /dev/null +++ b/deep-equal/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/assert/deep-equal", + "version": "0.0.0", + "description": "Test for deep equality between two values.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "deepequal", + "equality", + "equal", + "deep", + "test", + "check", + "validate" + ] +} diff --git a/deep-equal/test/test.js b/deep-equal/test/test.js new file mode 100644 index 00000000..e9ba9976 --- /dev/null +++ b/deep-equal/test/test.js @@ -0,0 +1,356 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Buffer = require( '@stdlib/buffer/ctor' ); +var Number = require( '@stdlib/number/ctor' ); +var inherit = require( '@stdlib/utils/inherit' ); +var deepEqual = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof deepEqual, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function compares boolean, string, and number primitives using strict equality', function test( t ) { + var values; + var a; + var b; + var i; + + values = [ + 'beep', + 'boop', + 'baz', + 1, + '1', + 2.3, + '2.3', + 0, + -2.0, + true, + false, + 'true', + 'false' + ]; + for ( i = 1; i < values.length; i++ ) { + a = values[ i-1 ]; + b = values[ i ]; + t.strictEqual( deepEqual( a, a ), true, 'returns true' ); + t.strictEqual( deepEqual( b, b ), true, 'returns true' ); + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns `false` for two `NaN`s', function test( t ) { + t.strictEqual( deepEqual( NaN, NaN ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `true` for two `null`s', function test( t ) { + t.strictEqual( deepEqual( null, null ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` for two `undefined`s', function test( t ) { + t.strictEqual( deepEqual( void 0, void 0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function compares `arguments` objects', function test( t ) { + var a = returnArgs( 1, 2, 3 ); + var b = returnArgs( 1, 2, 3 ); + var c = returnArgs( 3, 2, 1 ); + + t.strictEqual( deepEqual( a, b ), true, 'returns true' ); + t.strictEqual( deepEqual( a, c ), false, 'returns false' ); + t.strictEqual( deepEqual( b, c ), false, 'returns false' ); + t.strictEqual( deepEqual( a, [ 1, 2, 3 ] ), false, 'returns false' ); + t.end(); + + function returnArgs() { + return arguments; + } +}); + +tape( 'the function distinguishes between `null`, `NaN`, and `undefined`', function test( t ) { + t.strictEqual( deepEqual( NaN, null ), false, 'returns false' ); + t.strictEqual( deepEqual( NaN, void 0 ), false, 'returns false' ); + t.strictEqual( deepEqual( null, void 0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive and an object type', function test( t ) { + var a; + var b; + + a = 'abc'; + b = new String( 'abc' ); // eslint-disable-line no-new-wrappers + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + + a = 'abc'; + b = new Buffer( 'abc' ); // eslint-disable-line no-buffer-constructor + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + t.strictEqual( deepEqual( b, a ), false, 'returns false' ); + + a = null; + b = []; + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + + a = true; + b = new Boolean( true ); // eslint-disable-line no-new-wrappers + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + + a = 3.12; + b = new Number( 3.12 ); // eslint-disable-line no-new-wrappers + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` for objects of different types', function test( t ) { + t.strictEqual( deepEqual( [], {} ), false, 'returns false' ); + t.strictEqual( deepEqual( [], new RegExp( '[0-9]' ) ), false, 'returns false' ); + t.strictEqual( deepEqual( new Date(), new RegExp( '[0-9]' ) ), false, 'returns false' ); + t.strictEqual( deepEqual( new Int8Array(), [] ), false, 'returns false' ); + + // eslint-disable-next-line no-buffer-constructor, no-new-wrappers + t.strictEqual( deepEqual( new Buffer( 'xyz' ), new String( 'xyz' ) ), false, 'returns false' ); + + // eslint-disable-next-line no-buffer-constructor + t.strictEqual( deepEqual( new Buffer( [ 1, 2, 3 ] ), [ 1, 2, 3 ] ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function checks deep equality of `Date` objects', function test( t ) { + var a = new Date( '2018-09-20T01:23:28.936Z' ); + var b = new Date( 1537406608936 ); + var c = new Date(); + + t.strictEqual( deepEqual( a, b ), true, 'returns true' ); + t.strictEqual( deepEqual( c, c ), true, 'returns true' ); + t.strictEqual( deepEqual( a, c ), false, 'returns false' ); + t.strictEqual( deepEqual( b, c ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function checks equality of `RegExp` objects', function test( t ) { + var a = new RegExp( '[0-9]+', 'gi' ); + var b = /[0-9]+/gi; + var c = /[0-9]+/; + + t.strictEqual( deepEqual( a, b ), true, 'returns true' ); + t.strictEqual( deepEqual( c, c ), true, 'returns true' ); + t.strictEqual( deepEqual( a, c ), false, 'returns false' ); + t.strictEqual( deepEqual( b, c ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function checks equality of `Error` objects', function test( t ) { + var values; + var a; + var b; + var i; + + values = [ + new Error( 'beep' ), + new Error( 'boop' ), + new TypeError( 'baz' ), + new RangeError( 'baz' ), + new SyntaxError() + ]; + for ( i = 1; i < values.length; i++ ) { + a = values[ i-1 ]; + b = values[ i ]; + t.strictEqual( deepEqual( a, a ), true, 'returns true' ); + t.strictEqual( deepEqual( b, b ), true, 'returns true' ); + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function checks deep equality of `Buffers`', function test( t ) { + /* eslint-disable no-buffer-constructor */ + var a = new Buffer( 'xyz' ); + var b = new Buffer( 'xyz' ); + var c = new Buffer( 'abcdef' ); + var d = new Buffer( 'abc' ); + + /* eslint-enable no-buffer-constructor */ + + t.strictEqual( deepEqual( a, b ), true, 'returns true' ); + t.strictEqual( deepEqual( a, c ), false, 'returns false' ); + t.strictEqual( deepEqual( b, c ), false, 'returns false' ); + t.strictEqual( deepEqual( c, d ), false, 'returns false' ); + t.strictEqual( deepEqual( a, d ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function compares arrays for deep equality', function test( t ) { + var a; + var b; + + a = [ 'beep', 'boop', 'baz' ]; + b = [ 'beep', 'boop', 'baz' ]; + t.strictEqual( deepEqual( a, b ), true, 'returns true' ); + b[ 1 ] = 'bap'; + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + b = []; + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + + a = [ true, false, true ]; + b = [ true, false, true ]; + t.strictEqual( deepEqual( a, b ), true, 'returns true' ); + b[ 1 ] = true; + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + + a = [ 0, 1, 0 ]; + b = [ 0, 1, 0 ]; + t.strictEqual( deepEqual( a, b ), true, 'returns true' ); + b = [ 1, 1, 1 ]; + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + + a = [ null, null, null ]; + b = [ null, null, null ]; + t.strictEqual( deepEqual( a, b ), true, 'returns true' ); + b = [ NaN, NaN, NaN ]; + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + b = [ void 0, void 0, void 0 ]; + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + + a = [ void 0, void 0, void 0 ]; + b = [ void 0, void 0, void 0 ]; + t.strictEqual( deepEqual( a, b ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function compares nested arrays for deep equality', function test( t ) { + var a; + var b; + + a = [ [ 1, 2 ], [ true, false ] ]; + b = [ [ 1, 2 ], [ true, false ] ]; + t.strictEqual( deepEqual( a, b ), true, 'returns true' ); + + b = [ [ true, false ], [ 1, 2 ] ]; + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function compares plain objects for deep equality', function test( t ) { + var a; + var b; + + a = { + 'beep': 'boop' + }; + b = { + 'beep': 'boop' + }; + t.strictEqual( deepEqual( a, b ), true, 'returns true' ); + b.beep = 'baz'; + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + + a = { + 'beep': [ 0 ] + }; + b = { + 'beep': 0 + }; + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + + a = { + 'beep': 'boop', + 'boz': 23 + }; + b = { + 'beep': 'boop', + 'baz': 23 + }; + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + t.strictEqual( deepEqual( b, a ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function distinguishes between array-like objects and arrays', function test( t ) { + var a = [ 0, 1 ]; + var b = { + '0': 0, + '1': 1, + 'length': 2 + }; + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function compares constructor instances for deep equality', function test( t ) { + var a; + var b; + + a = new Person( 'Methuselah', 969 ); + b = new Person( 'Methuselah', 969 ); + t.strictEqual( deepEqual( a, b ), true, 'returns true' ); + b.beep = 'boop'; + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + + t.end(); + + function Person( name, age ) { + this.name = name; + this.age = age; + return this; + } +}); + +tape( 'the function returns `false` for instances of constructors with different prototypes', function test( t ) { + var a; + var b; + + a = new Person( 'Methuselah', 969 ); + b = new Person( 'Methuselah', 969 ); + t.strictEqual( deepEqual( a, b ), true, 'returns true' ); + + inherit( Person, Foo ); + b = new Person( 'Methuselah', 969 ); + t.strictEqual( deepEqual( a, b ), false, 'returns false' ); + + t.end(); + + function Person( name, age ) { + this.name = name; + this.age = age; + return this; + } + function Foo() { + return this; + } +}); diff --git a/deep-has-own-property/README.md b/deep-has-own-property/README.md new file mode 100644 index 00000000..00e98dfa --- /dev/null +++ b/deep-has-own-property/README.md @@ -0,0 +1,253 @@ + + +# deepHasOwnProp + +> Test whether an object contains a nested key path. + +
+ +## Usage + +```javascript +var deepHasOwnProp = require( '@stdlib/assert/deep-has-own-property' ); +``` + +#### deepHasOwnProp( value, path\[, options] ) + +Returns a `boolean` indicating if a `value` has a specified `path`. + + + +```javascript +var obj = { 'a': { 'b': { 'c': 'd' } } }; + +var bool = deepHasOwnProp( obj, 'a.b.c' ); +// returns true + +bool = deepHasOwnProp( obj, 'a.b.c.d.e' ); +// returns false +``` + +If a key path includes an `array`, specify the numeric index. + + + +```javascript +var arr = [ + { + 'a': [ + { + 'b': [ + { 'c': 'd' }, + { 'e': 'f' } + ] + } + ] + } +]; + +var bool = deepHasOwnProp( arr, '0.a.0.b.0.c' ); +// returns true + +bool = deepHasOwnProp( arr, '0.a.1.b.0.c' ); +// returns false + +bool = deepHasOwnProp( arr, '0.a.0.b.1.c' ); +// returns false +``` + +The key path may be specified as either a delimited `string` or a key `array`. + + + +```javascript +var obj = { 'a': { 'b': { 'c': 'd' } } }; + +var bool = deepHasOwnProp( obj, [ 'a', 'b', 'c' ] ); +// returns true +``` + +The function accepts the following `options`: + +- **sep**: key path separator. Default: `'.'`. + +By default, the function assumes `.` separated key values. To specify an alternative separator, set the `sep` option. + + + +```javascript +var obj = { 'a': { 'b': { 'c': 'd' } } }; + +var bool = deepHasOwnProp( obj, 'a/b/c', { + 'sep': '/' +}); +// returns true +``` + +#### deepHasOwnProp.factory( path\[, options] ) + +Returns a `function` which tests whether a `value` contains a nested key `path`. + +```javascript +var has = deepHasOwnProp.factory( 'a/b/c', { + 'sep': '/' +}); +``` + +#### has( value ) + +Returns a `boolean` indicating whether a `value` contains a nested key `path`. + + + +```javascript +var has = deepHasOwnProp.factory( 'a.b.c' ); + +var obj = { 'a': { 'b': { 'c': 'd' } } }; + +var bool = has( obj ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Only **own** properties are tested. Paths matching inherited properties are not considered. + + ```javascript + function Foo() { + this.a = 'b'; + return this; + } + Foo.prototype.c = 'd'; + + var foo = new Foo(); + + var bool = deepHasOwnProp( foo, 'a' ); + // returns true + + bool = deepHasOwnProp( foo, 'c' ); + // returns false + ``` + +- When provided `null` or `undefined`, the function result is always `false`. + + ```javascript + var bool = deepHasOwnProp( null, 'a.b.c' ); + // returns false + + bool = deepHasOwnProp( void 0, 'a.b.c' ); + // returns false + ``` + +- Property values other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var obj = { + 'a': 'b' + }; + + var bool = deepHasOwnProp( obj, 'a.length' ); + // returns true + ``` + +- Key path `array` elements are coerced to `strings`. + + ```javascript + var obj = { + 'null': false + }; + var bool = deepHasOwnProp( obj, [ null ] ); + // returns true + + obj = { + '[object Object]': false + }; + bool = deepHasOwnProp( obj, [ {} ] ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var deepHasOwnProp = require( '@stdlib/assert/deep-has-own-property' ); + +var bool; +var has; + +bool = deepHasOwnProp( { 'a': { 'b': { 'c': 'd' } } }, 'a.b.c' ); +// returns true + +bool = deepHasOwnProp( { 'a': { 'b': { 'c': 'd' } } }, [ 'a', 'b', 'c' ] ); +// returns true + +bool = deepHasOwnProp( { 'a': { 'b': { 'c': 'd' } } }, 'a/b/c', { + 'sep': '/' +}); +// returns true + +bool = deepHasOwnProp( { 'a': { 'b': { 'c': 'd' } } }, 'a.b.c.d' ); +// returns false + +bool = deepHasOwnProp( { 'a': [ { 'b': { 'c': 'd' } } ] }, [ 'a', '0', 'b', 'c', 'd' ] ); +// returns false + +bool = deepHasOwnProp( { 'a': { 'b': { 'c': 'd' } } }, 'a/b/c/d/e', { + 'sep': '/' +}); +// returns false + +// Create a customized function: +has = deepHasOwnProp.factory( 'a_b_c', { + 'sep': '_' +}); + +bool = has( { 'a': { 'b': { 'c': 'd' } } } ); +// returns true + +bool = has( { 'a': [ { 'b': { 'c': 'd' } } ] } ); +// returns false +``` + +
+ + + + + + diff --git a/deep-has-own-property/benchmark/benchmark.factory.js b/deep-has-own-property/benchmark/benchmark.factory.js new file mode 100644 index 00000000..266fbe42 --- /dev/null +++ b/deep-has-own-property/benchmark/benchmark.factory.js @@ -0,0 +1,176 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var isFunction = require( '@stdlib/assert/is-function' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ).factory; + + +// MAIN // + +bench( pkg+':factory', function benchmark( b ) { + var path; + var has; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + path = 'a.b.' + i.toString(); + has = factory( path ); + if ( typeof has !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( !isFunction( has ) ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::options:factory', function benchmark( b ) { + var opts; + var has; + var i; + + opts = { + 'sep': '|' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + opts.sep = i.toString(); + has = factory( 'a.b.c', opts ); + if ( typeof has !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( !isFunction( has ) ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::delimited-string:factory', function benchmark( b ) { + var bool; + var obj; + var has; + var i; + + obj = { + 'a': { + 'b': { + 'c': randu() + } + } + }; + has = factory( 'a.b.c' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = has( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::key-array:factory', function benchmark( b ) { + var bool; + var path; + var obj; + var has; + var i; + + obj = { + 'a': { + 'b': { + 'c': randu() + } + } + }; + path = [ 'a', 'b', 'c' ]; + has = factory( path ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = has( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::custom-delimiter:factory', function benchmark( b ) { + var bool; + var opts; + var obj; + var has; + var i; + + obj = { + 'a': { + 'b': { + 'c': randu() + } + } + }; + opts = { + 'sep': '-|-' + }; + has = factory( 'a-|-b-|-c', opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = has( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/deep-has-own-property/benchmark/benchmark.js b/deep-has-own-property/benchmark/benchmark.js new file mode 100644 index 00000000..51335e1b --- /dev/null +++ b/deep-has-own-property/benchmark/benchmark.js @@ -0,0 +1,149 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var deepHasOwnProp = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var obj; + var i; + + obj = { + 'a': { + 'b': { + 'c': randu() + } + } + }; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = deepHasOwnProp( obj, 'a.b.c' ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::delimited-string', function benchmark( b ) { + var bool; + var obj; + var i; + + obj = { + 'a': { + 'b': { + 'c': randu() + } + } + }; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = deepHasOwnProp( obj, 'a.b.c' ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::key-array', function benchmark( b ) { + var bool; + var path; + var obj; + var i; + + obj = { + 'a': { + 'b': { + 'c': randu() + } + } + }; + path = [ 'a', 'b', 'c' ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = deepHasOwnProp( obj, path ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::custom-delimiter', function benchmark( b ) { + var bool; + var opts; + var obj; + var i; + + obj = { + 'a': { + 'b': { + 'c': randu() + } + } + }; + opts = { + 'sep': '-|-' + }; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = deepHasOwnProp( obj, 'a-|-b-|-c', opts ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/deep-has-own-property/docs/repl.txt b/deep-has-own-property/docs/repl.txt new file mode 100644 index 00000000..83745372 --- /dev/null +++ b/deep-has-own-property/docs/repl.txt @@ -0,0 +1,73 @@ + +{{alias}}( value, path[, options] ) + Returns a boolean indicating whether an object contains a nested key path. + + The function tests for "own" properties and will return `false` for + inherited properties. + + Value arguments other than `null` or `undefined` are coerced to objects. + + Key path array elements are coerced to strings. + + Parameters + ---------- + value: any + Value to test. + + path: string|Array + Key path. + + options: Object (optional) + Options. + + options.sep: string (optional) + Key path separator. Default: '.'. + + Returns + ------- + bool: boolean + Boolean indicating if an object has a specified path. + + Examples + -------- + > var obj = { 'a': { 'b': { 'c': 'd' } } }; + > var bool = {{alias}}( obj, 'a.b.c' ) + true + + // Specify a custom separator via the `sep` option: + > obj = { 'a': { 'b': { 'c': 'd' } } }; + > bool = {{alias}}( obj, 'a/b/c', { 'sep': '/' } ) + true + +{{alias}}.factory( path[, options] ) + Returns a function which tests whether an object contains a nested key path. + + The returned function tests for "own" properties and will return `false` for + inherited properties. + + Parameters + ---------- + path: string|Array + Key path. + + options: Object (optional) + Options. + + options.sep: string (optional) + Key path separator. Default: '.'. + + Returns + ------- + out: Function + Function which tests whether an object contains a nested key path. + + Examples + -------- + > var has = {{alias}}.factory( 'a/b/c', { 'sep': '/' } ); + > var obj = { 'a': { 'b': { 'c': 'd' } } }; + > var bool = has( obj ) + true + + See Also + -------- + diff --git a/deep-has-own-property/docs/types/index.d.ts b/deep-has-own-property/docs/types/index.d.ts new file mode 100644 index 00000000..947ab530 --- /dev/null +++ b/deep-has-own-property/docs/types/index.d.ts @@ -0,0 +1,178 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining function options. +*/ +interface Options { + /** + * Key path separator (default: '.'). + */ + sep?: string; +} + +/** +* Returns a boolean indicating whether an object has a nested key path. +* +* @param value - value to test +* @returns boolean indicating whether an object has a nested property +* +* @example +* var obj = { 'a': { 'b': { 'c': 'd' } } }; +* var bool = deepHasOwnProp( obj ); +*/ +type HasFunction = ( value: any ) => boolean; + +/** +* Interface for testing whether an object contains a nested key path. +*/ +interface DeepHasOwnProp { + /** + * Tests whether an object contains a nested key path. + * + * ## Notes + * + * - The function tests for "own" properties and will return `false` for inherited properties. + * - Value arguments other than `null` or `undefined` are coerced to objects. + * - Key path array elements are coerced to strings. + * + * @param value - value to test + * @param path - key path + * @param options - function options + * @param options.sep - key path separator (default: '.') + * @throws must provide valid options + * @returns boolean indicating whether an object has a nested property + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var bool = deepHasOwnProp( obj, 'a.b.c' ); + * // returns true + * + * @example + * var arr = [ + * { + * 'a': [ + * { + * 'b': [ + * { 'c': 'd' }, + * { 'e': 'f' } + * ] + * } + * ] + * } + * ]; + * var bool = deepHasOwnProp( arr, '0.a.0.b.0.c' ); + * // returns true + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var bool = deepHasOwnProp( obj, [ 'a', 'b', 'c' ] ); + * // returns true + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var bool = deepHasOwnProp( obj, 'a/b/c', { + * 'sep': '/' + * }); + * // returns true + */ + ( value: any, path: string | Array, options?: Options ): boolean; + + /** + * Returns a function which tests whether an object has a nested key path, either own or inherited. + * + * ## Notes + * + * - The returned function tests for "own" properties and will return `false` for inherited properties. + * + * @param path - key path + * @param options - function options + * @param options.sep - key path separator (default: '.') + * @throws must provide valid options + * @returns function which tests whether an object has a nested key path + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var has = deepHasOwnProp.factory( 'a/b/c', { + * 'sep': '/' + * }); + * var bool = has( obj ); + * // returns true + */ + factory( path: string | Array, options?: Options ): HasFunction; +} + +/** +* Tests whether an object contains a nested key path. +* +* @param value - value to test +* @param path - key path +* @param options - function options +* @param options.sep - key path separator (default: '.') +* @throws must provide valid options +* @returns boolean indicating whether an object has a nested property +* +* @example +* var obj = { 'a': { 'b': { 'c': 'd' } } }; +* var bool = deepHasOwnProp( obj, 'a.b.c' ); +* // returns true +* +* @example +* var arr = [ +* { +* 'a': [ +* { +* 'b': [ +* { 'c': 'd' }, +* { 'e': 'f' } +* ] +* } +* ] +* } +* ]; +* var bool = deepHasOwnProp( arr, '0.a.0.b.0.c' ); +* // returns true +* +* @example +* var obj = { 'a': { 'b': { 'c': 'd' } } }; +* var bool = deepHasOwnProp( obj, [ 'a', 'b', 'c' ] ); +* // returns true +* +* @example +* var obj = { 'a': { 'b': { 'c': 'd' } } }; +* var bool = deepHasOwnProp( obj, 'a/b/c', { +* 'sep': '/' +* }); +* // returns true +* +* @example +* var obj = { 'a': { 'b': { 'c': 'd' } } }; +* var has = deepHasOwnProp.factory( 'a/b/c', { +* 'sep': '/' +* }); +* var bool = has( obj ); +* // returns true +*/ +declare var deepHasOwnProp: DeepHasOwnProp; + + +// EXPORTS // + +export = deepHasOwnProp; diff --git a/deep-has-own-property/docs/types/test.ts b/deep-has-own-property/docs/types/test.ts new file mode 100644 index 00000000..ce5f037c --- /dev/null +++ b/deep-has-own-property/docs/types/test.ts @@ -0,0 +1,111 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import deepHasOwnProp = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { 'a': { 'b': { 'c': 'd' } } }; + deepHasOwnProp( obj, 'a.b.c' ); // $ExpectType boolean + deepHasOwnProp( obj, 'a.b.d' ); // $ExpectType boolean + deepHasOwnProp( obj, 'a/b/c', { 'sep': '/' } ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided a second argument which is neither a string nor an array of strings... +{ + const obj = { 'a': { 'b': { 'c': 'd' } } }; + deepHasOwnProp( obj, {} ); // $ExpectError + deepHasOwnProp( obj, false ); // $ExpectError + deepHasOwnProp( obj, true ); // $ExpectError + deepHasOwnProp( obj, null ); // $ExpectError + deepHasOwnProp( obj, 123 ); // $ExpectError + deepHasOwnProp( obj, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not an object... +{ + const obj = { 'a': { 'b': { 'c': 'd' } } }; + deepHasOwnProp( obj, 'a.b.c', 'abc' ); // $ExpectError + deepHasOwnProp( obj, 'a.b.c', false ); // $ExpectError + deepHasOwnProp( obj, 'a.b.c', true ); // $ExpectError + deepHasOwnProp( obj, 'a.b.c', null ); // $ExpectError + deepHasOwnProp( obj, 'a.b.c', 123 ); // $ExpectError + deepHasOwnProp( obj, 'a.b.c', [] ); // $ExpectError + deepHasOwnProp( obj, 'a.b.c', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `sep` option which is not a string... +{ + const obj = { 'a': { 'b': { 'c': 'd' } } }; + deepHasOwnProp( obj, 'a/b/c', { 'sep': 123 } ); // $ExpectError + deepHasOwnProp( obj, 'a/b/c', { 'sep': true } ); // $ExpectError + deepHasOwnProp( obj, 'a/b/c', { 'sep': false } ); // $ExpectError + deepHasOwnProp( obj, 'a/b/c', { 'sep': {} } ); // $ExpectError + deepHasOwnProp( obj, 'a/b/c', { 'sep': [] } ); // $ExpectError + deepHasOwnProp( obj, 'a/b/c', { 'sep': ( x: number ): number => x } ); // $ExpectError +} + +// Attached to main export is a `factory` method which returns a function... +{ + deepHasOwnProp.factory( 'a.b.c' ); // $ExpectType HasFunction +} + +// The compiler throws an error if the `factory` method is provided an unsupported number of arguments... +{ + deepHasOwnProp.factory(); // $ExpectError + deepHasOwnProp.factory( 'a.b.c', {}, {} ); // $ExpectError +} + +// The `factory` method returns a function which returns a boolean... +{ + const obj = { 'a': { 'b': { 'c': 'd' } } }; + const has = deepHasOwnProp.factory( 'a.b.c' ); // $ExpectType HasFunction + has( obj ); // $ExpectType boolean + has( {} ); // $ExpectType boolean +} + +// The compiler throws an error if the `factory` method is provided a first argument which is neither a string nor an array of strings... +{ + deepHasOwnProp.factory( {} ); // $ExpectError + deepHasOwnProp.factory( false ); // $ExpectError + deepHasOwnProp.factory( true ); // $ExpectError + deepHasOwnProp.factory( null ); // $ExpectError + deepHasOwnProp.factory( 123 ); // $ExpectError + deepHasOwnProp.factory( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `factory` method is provided a `sep` option which is not a string... +{ + deepHasOwnProp.factory( 'a/b/c', { 'sep': true } ); // $ExpectError + deepHasOwnProp.factory( 'a/b/c', { 'sep': false } ); // $ExpectError + deepHasOwnProp.factory( 'a/b/c', { 'sep': 123 } ); // $ExpectError + deepHasOwnProp.factory( 'a/b/c', { 'sep': [] } ); // $ExpectError + deepHasOwnProp.factory( 'a/b/c', { 'sep': {} } ); // $ExpectError + deepHasOwnProp.factory( 'a/b/c', { 'sep': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const obj = { 'a': { 'b': { 'c': 'd' } } }; + deepHasOwnProp(); // $ExpectError + deepHasOwnProp( obj ); // $ExpectError + deepHasOwnProp( obj, 'a.b.c', {}, 123 ); // $ExpectError +} diff --git a/deep-has-own-property/examples/index.js b/deep-has-own-property/examples/index.js new file mode 100644 index 00000000..fdf9a9e9 --- /dev/null +++ b/deep-has-own-property/examples/index.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline, object-curly-spacing */ + +'use strict'; + +var deepHasOwnProp = require( './../lib' ); + +var bool; +var has; +var obj; + +obj = { 'a': { 'b': { 'c': 'd' } } }; +bool = deepHasOwnProp( obj, 'a.b.c' ); +console.log( bool ); +// => true + +obj = { 'a': { 'b': { 'c': 'd' } } }; +bool = deepHasOwnProp( obj, [ 'a', 'b', 'c' ] ); +console.log( bool ); +// => true + +obj = { 'a': { 'b': { 'c': 'd' } } }; +bool = deepHasOwnProp( obj, 'a/b/c', { + 'sep': '/' +}); +console.log( bool ); +// => true + +obj = { 'a': { 'b': { 'c': 'd' } } }; +bool = deepHasOwnProp( obj, 'a.b.c.d' ); +console.log( bool ); +// => false + +obj = { 'a': [ { 'b': { 'c': 'd' } } ] }; +bool = deepHasOwnProp( obj, [ 'a', '0', 'b', 'c', 'd' ] ); +console.log( bool ); +// => false + +obj = { 'a': { 'b': { 'c': 'd' } } }; +bool = deepHasOwnProp( obj, 'a/b/c/d/e', { + 'sep': '/' +}); +console.log( bool ); +// => false + +// Create a customized function: +has = deepHasOwnProp.factory( 'a_b_c', { + 'sep': '_' +}); + +obj = { 'a': { 'b': { 'c': 'd' } } }; +bool = has( obj ); +console.log( bool ); +// => true + +obj = { 'a': [ { 'b': { 'c': 'd' } } ] }; +bool = has( obj ); +console.log( bool ); +// => false diff --git a/deep-has-own-property/lib/defaults.json b/deep-has-own-property/lib/defaults.json new file mode 100644 index 00000000..e2ca187c --- /dev/null +++ b/deep-has-own-property/lib/defaults.json @@ -0,0 +1,3 @@ +{ + "sep": "." +} diff --git a/deep-has-own-property/lib/factory.js b/deep-has-own-property/lib/factory.js new file mode 100644 index 00000000..ad46ba95 --- /dev/null +++ b/deep-has-own-property/lib/factory.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isArray = require( '@stdlib/assert/is-array' ); +var copy = require( '@stdlib/utils/copy' ); +var validate = require( './validate.js' ); +var defaults = require( './defaults.json' ); +var has = require( './has.js' ); + + +// MAIN // + +/** +* Returns a function which tests whether an object has a nested key path. +* +* @param {(string|Array)} path - key path +* @param {Options} [options] - function options +* @param {string} [options.sep='.'] - key path separator +* @throws {TypeError} first argument must be a string primitive or key array +* @throws {TypeError} options argument must be an object +* @throws {TypeError} must provide valid options +* @returns {Function} function which tests whether an object has a nested key path +* +* @example +* var has = factory( 'a/b/c', { +* 'sep': '/' +* }); +*/ +function factory( path, options ) { + var isStr; + var props; + var opts; + var err; + isStr = isString( path ); + if ( !isStr && !isArray( path ) ) { + throw new TypeError( 'invalid argument. Key path must be a string primitive or a key array. Value: `' + path + '`.' ); + } + opts = copy( defaults ); + if ( arguments.length > 1 ) { + err = validate( opts, options ); + if ( err ) { + throw err; + } + } + if ( isStr ) { + props = path.split( opts.sep ); + } else { + props = path; + } + return deepHasOwnProp; + + /** + * Returns a boolean indicating whether an object has a nested key path. + * + * @private + * @param {*} value - value to test + * @returns {boolean} boolean indicating whether an object has a nested property + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var bool = deepHasOwnProp( obj ); + */ + function deepHasOwnProp( value ) { + if ( value === void 0 || value === null ) { + return false; + } + return has( value, props ); + } +} + + +// EXPORTS // + +module.exports = factory; diff --git a/deep-has-own-property/lib/has.js b/deep-has-own-property/lib/has.js new file mode 100644 index 00000000..b4a68b8c --- /dev/null +++ b/deep-has-own-property/lib/has.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); + + +// MAIN // + +/** +* Returns a boolean indicating whether an object has a nested property. +* +* @private +* @param {*} v - value to test +* @param {Array} props - list of properties defining a key path +* @returns {boolean} boolean indicating whether an object has a nested property +*/ +function deepHasOwnProp( v, props ) { + var len = props.length; + var i; + if ( len === 0 ) { + return false; + } + for ( i = 0; i < len; i++ ) { + if ( hasOwnProp( v, props[ i ] ) ) { + v = v[ props[ i ] ]; + } else { + return false; + } + } + return true; +} + + +// EXPORTS // + +module.exports = deepHasOwnProp; diff --git a/deep-has-own-property/lib/index.js b/deep-has-own-property/lib/index.js new file mode 100644 index 00000000..2e23d6fe --- /dev/null +++ b/deep-has-own-property/lib/index.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object contains a nested key path. +* +* @module @stdlib/assert/deep-has-own-property +* +* @example +* var deepHasOwnProp = require( '@stdlib/assert/deep-has-own-property' ); +* +* var obj = { +* 'a': { +* 'b': { +* 'c': 'd' +* } +* } +* }; +* +* var bool = deepHasOwnProp( obj, 'a.b.c' ); +* // returns true +* +* bool = deepHasOwnProp( obj, [ 'a', 'b', 'c' ] ); +* // returns true +* +* @example +* var factory = require( '@stdlib/assert/deep-has-own-property' ).factory; +* +* var has = factory( 'a/b/c', { +* 'sep': '/' +* }); +* +* var obj = { +* 'a': { +* 'b': { +* 'c': 'd' +* } +* } +* }; +* +* var bool = has( obj ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var deepHasOwnProp = require( './main.js' ); +var factory = require( './factory.js' ); + + +// MAIN // + +setReadOnly( deepHasOwnProp, 'factory', factory ); + + +// EXPORTS // + +module.exports = deepHasOwnProp; diff --git a/deep-has-own-property/lib/main.js b/deep-has-own-property/lib/main.js new file mode 100644 index 00000000..35323a6b --- /dev/null +++ b/deep-has-own-property/lib/main.js @@ -0,0 +1,109 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isArray = require( '@stdlib/assert/is-array' ); +var copy = require( '@stdlib/utils/copy' ); +var validate = require( './validate.js' ); +var defaults = require( './defaults.json' ); +var has = require( './has.js' ); + + +// MAIN // + +/** +* Tests whether an object contains a nested key path. +* +* @param {*} value - value to test +* @param {(string|Array)} path - key path +* @param {Options} [options] - function options +* @param {string} [options.sep='.'] - key path separator +* @throws {TypeError} second argument must be a string primitive or key array +* @throws {TypeError} options argument must be an object +* @throws {TypeError} must provide valid options +* @returns {boolean} boolean indicating whether an object has a nested property +* +* @example +* var obj = { 'a': { 'b': { 'c': 'd' } } }; +* var bool = deepHasOwnProp( obj, 'a.b.c' ); +* // returns true +* +* @example +* var arr = [ +* { +* 'a': [ +* { +* 'b': [ +* { 'c': 'd' }, +* { 'e': 'f' } +* ] +* } +* ] +* } +* ]; +* var bool = deepHasOwnProp( arr, '0.a.0.b.0.c' ); +* // returns true +* +* @example +* var obj = { 'a': { 'b': { 'c': 'd' } } }; +* var bool = deepHasOwnProp( obj, [ 'a', 'b', 'c' ] ); +* // returns true +* +* @example +* var obj = { 'a': { 'b': { 'c': 'd' } } }; +* var bool = deepHasOwnProp( obj, 'a/b/c', { +* 'sep': '/' +* }); +* // returns true +*/ +function deepHasOwnProp( value, path, options ) { + var isStr; + var props; + var opts; + var err; + + isStr = isString( path ); + if ( !isStr && !isArray( path ) ) { + throw new TypeError( 'invalid argument. Key path must be a string primitive or a key array. Value: `' + path + '`.' ); + } + opts = copy( defaults ); + if ( arguments.length > 2 ) { + err = validate( opts, options ); + if ( err ) { + throw err; + } + } + if ( value === void 0 || value === null ) { + return false; + } + if ( isStr ) { + props = path.split( opts.sep ); + } else { + props = path; + } + return has( value, props ); +} + + +// EXPORTS // + +module.exports = deepHasOwnProp; diff --git a/deep-has-own-property/lib/validate.js b/deep-has-own-property/lib/validate.js new file mode 100644 index 00000000..bf1fa535 --- /dev/null +++ b/deep-has-own-property/lib/validate.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isObject = require( '@stdlib/assert/is-plain-object' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); + + +// MAIN // + +/** +* Validates function options. +* +* @private +* @param {Object} opts - destination for function options +* @param {Options} options - function options +* @param {string} [options.sep] - key path separator +* @returns {(Error|null)} error or null +* +* @example +* var opts = {}; +* var options = { +* 'sep': '/' +* }; +* var err = validate( opts, options ); +* if ( err ) { +* throw err; +* } +*/ +function validate( opts, options ) { + if ( !isObject( options ) ) { + return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); + } + if ( hasOwnProp( options, 'sep' ) ) { + opts.sep = options.sep; + if ( !isString( opts.sep ) ) { + return new TypeError( 'invalid option. `sep` option must be a string primitive. Option: `' + opts.sep + '`.' ); + } + } + return null; +} + + +// EXPORTS // + +module.exports = validate; diff --git a/deep-has-own-property/package.json b/deep-has-own-property/package.json new file mode 100644 index 00000000..05d37655 --- /dev/null +++ b/deep-has-own-property/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/deep-has-own-property", + "version": "0.0.0", + "description": "Test whether an object contains a nested key path.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "deephas", + "deephasown", + "deephasownproperty", + "property", + "prop", + "test", + "check", + "validate", + "has", + "own", + "hasown", + "hasownprop", + "hasownproperty" + ] +} diff --git a/deep-has-own-property/test/test.factory.js b/deep-has-own-property/test/test.factory.js new file mode 100644 index 00000000..350390ad --- /dev/null +++ b/deep-has-own-property/test/test.factory.js @@ -0,0 +1,454 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var factory = require( './../lib/factory.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof factory, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a key path argument which is neither a string primitive or a key array', function test( t ) { + var values; + var i; + + values = [ + 3, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + factory( value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + '3', + 3, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + factory( 'a.b', value ); + }; + } +}); + +tape( 'the function throws an error if an invalid option', function test( t ) { + var values; + var i; + + values = [ + 3, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var opts = { + 'sep': value + }; + factory( 'a.b', opts ); + }; + } +}); + +tape( 'the function returns a function (no options)', function test( t ) { + var has = factory( 'a.b.c' ); + t.strictEqual( typeof has, 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns a function (options)', function test( t ) { + var opts; + var has; + + opts = { + 'sep': '/' + }; + has = factory( 'a/b/c', opts ); + + t.strictEqual( typeof has, 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the returned function returns `false` if provided a `null` or `undefined` (no options)', function test( t ) { + var values; + var has; + var i; + + values = [ + null, + void 0 + ]; + + has = factory( 'a.b.c' ); + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( has( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the returned function returns `false` if provided a `null` or `undefined` (options)', function test( t ) { + var values; + var opts; + var has; + var i; + + values = [ + null, + void 0 + ]; + + opts = { + 'sep': '_' + }; + has = factory( 'a_b_c', opts ); + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( has( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the returned function returns `true` if an object has a specified path (delimited string)', function test( t ) { + var bool; + var has; + var obj; + + has = factory( 'a.b.0.c' ); + obj = { + 'a': { + 'b': [ + { + 'c': 'd' + } + ] + } + }; + bool = has( obj ); + + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the returned function returns `true` if an object has a specified path (key array)', function test( t ) { + var bool; + var path; + var has; + var obj; + + path = [ 'a', 'b', '0', 'c' ]; + has = factory( path ); + + obj = { + 'a': { + 'b': [ + { + 'c': 'd' + } + ] + } + }; + bool = has( obj ); + + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the returned function returns `true` if an object has a specified path (custom delimiter)', function test( t ) { + var bool; + var opts; + var has; + var obj; + + opts = { + 'sep': '/' + }; + has = factory( 'a/b/0/c', opts ); + + obj = { + 'a': { + 'b': [ + { + 'c': 'd' + } + ] + } + }; + bool = has( obj ); + + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the returned function returns `false` if an object does not have a specified path (delimited string)', function test( t ) { + var bool; + var has; + var obj; + + has = factory( 'a.b.c.0.d' ); + obj = { + 'a': { + 'b': [ + { + 'c': 'd' + } + ] + } + }; + bool = has( obj ); + + t.strictEqual( bool, false, 'returns false' ); + t.end(); +}); + +tape( 'the returned function returns `false` if an object does not have a specified path (key array)', function test( t ) { + var bool; + var path; + var has; + var obj; + + path = [ 'a', 'b', 'c', '0', 'd' ]; + has = factory( path ); + + obj = { + 'a': { + 'b': [ + { + 'c': 'd' + } + ] + } + }; + bool = has( obj ); + + t.strictEqual( bool, false, 'returns false' ); + t.end(); +}); + +tape( 'the returned function returns `false` if an object does not have a specified path (custom delimiter)', function test( t ) { + var bool; + var opts; + var has; + var obj; + + opts = { + 'sep': '/' + }; + has = factory( 'a/b/c/0/d', opts ); + + obj = { + 'a': { + 'b': [ + { + 'c': 'd' + } + ] + } + }; + bool = has( obj ); + + t.strictEqual( bool, false, 'returns false' ); + t.end(); +}); + +tape( 'the returned function returns `false` if provided an empty object (no options)', function test( t ) { + var bool; + var path; + var has; + + has = factory( 'a.b.c' ); + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + path = [ 'a', 'b', 'c' ]; + has = factory( path ); + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + has = factory( '' ); + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the returned function returns `false` if provided an empty object (options)', function test( t ) { + var bool; + var opts; + var path; + var has; + + opts = { + 'sep': '/' + }; + + has = factory( 'a/b/c', opts ); + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + path = [ 'a', 'b', 'c' ]; + has = factory( path, opts ); + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + has = factory( '', opts ); + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the returned function can return `true` if provided an empty key path (string; no options)', function test( t ) { + var bool; + var obj; + var has; + + has = factory( '' ); + obj = { + '': 'b' + }; + bool = has( obj ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the returned function can return `true` if provided an empty key path (string; options)', function test( t ) { + var bool; + var opts; + var has; + var obj; + + opts = { + 'sep': '/' + }; + + has = factory( '', opts ); + obj = { + '': 'b' + }; + bool = has( obj ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the returned function always returns `false` if provided an empty key path array (string; no options)', function test( t ) { + var bool; + var obj; + var has; + + has = factory( [] ); + obj = { + '': 'b' + }; + bool = has( obj ); + t.strictEqual( bool, false, 'returns false' ); + + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the returned function only returns `true` for "own" properties', function test( t ) { + var bool; + var foo; + var has; + + function Foo() { + this.a = 'b'; + return this; + } + Foo.prototype.c = 'd'; + + foo = new Foo(); + + has = factory( 'a' ); + bool = has( foo ); + t.strictEqual( bool, true, 'returns true' ); + + has = factory( 'c' ); + bool = has( foo ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the returned function coerces primitives to objects', function test( t ) { + var bool; + var has; + + has = factory( 'length' ); + bool = has( 'beep' ); + + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); diff --git a/deep-has-own-property/test/test.has.js b/deep-has-own-property/test/test.has.js new file mode 100644 index 00000000..897bd704 --- /dev/null +++ b/deep-has-own-property/test/test.has.js @@ -0,0 +1,195 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var has = require( './../lib/has.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof has, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object has a specified path', function test( t ) { + var bool; + var path; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + path = [ 'a', 'b', 'c' ]; + + bool = has( obj, path ); + t.strictEqual( bool, true, 'returns true' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + path = [ 'a', '0', 'b', '0', 'c' ]; + + bool = has( obj, path ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object does not have a specified path', function test( t ) { + var bool; + var path; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + path = [ 'a', 'b', 'c', 'd', 'e' ]; + + bool = has( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + path = [ '0', 'a', '0', 'b', '0', 'e' ]; + + bool = has( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an empty object', function test( t ) { + var bool; + var path; + var obj; + + path = [ 'a', 'b', 'c' ]; + obj = {}; + + bool = has( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + path = []; + obj = {}; + + bool = has( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an empty key path', function test( t ) { + var bool; + var path; + var obj; + + path = []; + obj = { + 'a': 'b' + }; + + bool = has( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + path = []; + obj = {}; + + bool = has( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function only returns `true` for "own" properties', function test( t ) { + var bool; + var path; + var foo; + + function Foo() { + this.a = 'b'; + return this; + } + Foo.prototype.c = 'd'; + + foo = new Foo(); + + path = [ 'a' ]; + bool = has( foo, path ); + t.strictEqual( bool, true, 'returns true' ); + + path = [ 'c' ]; + bool = has( foo, path ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function coerces path values to strings', function test( t ) { + var bool; + var path; + var obj; + + obj = { + 'a': { + '[object Object]': 'c' + } + }; + + path = [ 'a', {} ]; + bool = has( obj, path ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/deep-has-own-property/test/test.js b/deep-has-own-property/test/test.js new file mode 100644 index 00000000..e98c15f7 --- /dev/null +++ b/deep-has-own-property/test/test.js @@ -0,0 +1,503 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var deepHasOwnProp = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof deepHasOwnProp, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function exports a factory function', function test( t ) { + t.strictEqual( typeof deepHasOwnProp.factory, 'function', 'exports a factory function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a key path argument which is neither a string primitive or a key array', function test( t ) { + var values; + var i; + + values = [ + 3, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var obj = { + 'a': { + 'b': 'c' + } + }; + deepHasOwnProp( obj, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + '3', + 3, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var obj = { + 'a': { + 'b': 'c' + } + }; + deepHasOwnProp( obj, 'a.b', value ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid option', function test( t ) { + var values; + var i; + + values = [ + 3, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var opts; + var obj; + + obj = { + 'a': { + 'b': 'c' + } + }; + opts = { + 'sep': value + }; + deepHasOwnProp( obj, 'a.b', opts ); + }; + } +}); + +tape( 'the function returns `false` if provided a first argument which is either `null` or `undefined`', function test( t ) { + var values; + var i; + + values = [ + null, + void 0 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( deepHasOwnProp( values[ i ], 'a.b.c' ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns `true` if an object has a specified path (delimited string)', function test( t ) { + var bool; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + bool = deepHasOwnProp( obj, 'a.b.c' ); + t.strictEqual( bool, true, 'returns true' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + bool = deepHasOwnProp( obj, 'a.0.b.0.c' ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if an object has a specified path (key array)', function test( t ) { + var bool; + var path; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + path = [ 'a', 'b', 'c' ]; + bool = deepHasOwnProp( obj, path ); + t.strictEqual( bool, true, 'returns true' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + path = [ 'a', '0', 'b', '0', 'c' ]; + bool = deepHasOwnProp( obj, path ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if an object has a specified path (custom delimiter)', function test( t ) { + var bool; + var opts; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + opts = { + 'sep': '/' + }; + bool = deepHasOwnProp( obj, 'a/b/c', opts ); + t.strictEqual( bool, true, 'returns true' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + opts = { + 'sep': '/' + }; + bool = deepHasOwnProp( obj, 'a/0/b/0/c', opts ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object does not have a specified path (delimited string)', function test( t ) { + var bool; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + bool = deepHasOwnProp( obj, 'a.b.c.d.e' ); + t.strictEqual( bool, false, 'returns false' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + bool = deepHasOwnProp( obj, '0.a.0.b.0.e' ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object does not have a specified path (key array)', function test( t ) { + var bool; + var path; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + path = [ 'a', 'b', 'c', 'd', 'e' ]; + bool = deepHasOwnProp( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + path = [ '0', 'a', '0', 'b', '0', 'e' ]; + bool = deepHasOwnProp( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object does not have a specified path (custom delimiter)', function test( t ) { + var bool; + var opts; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + opts = { + 'sep': '/' + }; + bool = deepHasOwnProp( obj, 'a/b/c/d/e', opts ); + t.strictEqual( bool, false, 'returns false' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + opts = { + 'sep': '/' + }; + bool = deepHasOwnProp( obj, '0/a/0/b/0/e', opts ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an empty object (no options)', function test( t ) { + var bool; + + bool = deepHasOwnProp( {}, 'a.b.c' ); + t.strictEqual( bool, false, 'returns false' ); + + bool = deepHasOwnProp( {}, '' ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an empty object (options)', function test( t ) { + var bool; + var opts; + + opts = { + 'sep': '/' + }; + + bool = deepHasOwnProp( {}, 'a/b/c', opts ); + t.strictEqual( bool, false, 'returns false' ); + + bool = deepHasOwnProp( {}, '', opts ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an empty path array (no options)', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + bool = deepHasOwnProp( obj, [] ); + t.strictEqual( bool, false, 'returns false' ); + + bool = deepHasOwnProp( {}, [] ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an empty path array (options)', function test( t ) { + var bool; + var opts; + var obj; + + opts = { + 'sep': '/' + }; + obj = { + 'a': 'b' + }; + bool = deepHasOwnProp( obj, [], opts ); + t.strictEqual( bool, false, 'returns false' ); + + bool = deepHasOwnProp( {}, [], opts ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function can return `true` if provided an empty key path string (no options)', function test( t ) { + var bool; + var obj; + + obj = { + '': 'b' + }; + bool = deepHasOwnProp( obj, '' ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function can return `true` if provided an empty key path string (options)', function test( t ) { + var bool; + var opts; + var obj; + + obj = { + '': 'b' + }; + opts = { + 'sep': '/' + }; + bool = deepHasOwnProp( obj, '', opts ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function only returns `true` for "own" properties', function test( t ) { + var bool; + var foo; + + function Foo() { + this.a = 'b'; + return this; + } + Foo.prototype.c = 'd'; + + foo = new Foo(); + + bool = deepHasOwnProp( foo, 'a' ); + t.strictEqual( bool, true, 'returns true' ); + + bool = deepHasOwnProp( foo, 'c' ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function coerces primitives to objects', function test( t ) { + var bool = deepHasOwnProp( 'beep', 'length' ); + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); diff --git a/deep-has-own-property/test/test.validate.js b/deep-has-own-property/test/test.validate.js new file mode 100644 index 00000000..6985e0df --- /dev/null +++ b/deep-has-own-property/test/test.validate.js @@ -0,0 +1,125 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var validate = require( './../lib/validate.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof validate, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns an error if provided an options argument which is not an object', function test( t ) { + var values; + var err; + var i; + + values = [ + '3', + 3, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + err = validate( {}, values[ i ] ); + t.strictEqual( err instanceof TypeError, true, 'returns a TypeError when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns an error if provided a `sep` option argument which is not a string primitive', function test( t ) { + var values; + var opts; + var err; + var i; + + values = [ + new String( '3' ), // eslint-disable-line no-new-wrappers + 3, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + opts = { + 'sep': values[ i ] + }; + err = validate( {}, opts ); + t.strictEqual( err instanceof TypeError, true, 'returns a TypeError when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function will ignore unrecognized properties', function test( t ) { + var opts; + var obj; + + opts = { + 'foo': true, + 'bar': 'baz' + }; + obj = {}; + + t.strictEqual( validate( obj, opts ), null, 'returns null' ); + t.deepEqual( obj, {}, 'does not set any properties' ); + + t.end(); +}); + +tape( 'the function returns `null` if all options are valid', function test( t ) { + var opts; + var obj; + + opts = { + 'sep': '_' + }; + obj = {}; + + t.strictEqual( validate( obj, opts ), null, 'returns null' ); + t.strictEqual( obj.sep, '_', 'sets option value' ); + + opts = { + 'sep': '/' + }; + obj = {}; + + t.strictEqual( validate( obj, opts ), null, 'returns null' ); + t.strictEqual( obj.sep, '/', 'sets option value' ); + + t.end(); +}); diff --git a/deep-has-property/README.md b/deep-has-property/README.md new file mode 100644 index 00000000..43782cc4 --- /dev/null +++ b/deep-has-property/README.md @@ -0,0 +1,249 @@ + + +# deepHasProp + +> Test whether an object contains a nested key path, either own or inherited. + +
+ +## Usage + +```javascript +var deepHasProp = require( '@stdlib/assert/deep-has-property' ); +``` + +#### deepHasProp( value, path\[, options] ) + +Returns a `boolean` indicating if a `value` has a specified `path`, either own or inherited. + + + +```javascript +function Foo() { + return this; +} +Foo.prototype.b = { + 'c': 'd' +}; + +var obj = { 'a': new Foo() }; + +var bool = deepHasProp( obj, 'a.b.c' ); +// returns true + +bool = deepHasProp( obj, 'a.b.c.d.e' ); +// returns false +``` + +If a key path includes an `array`, specify the numeric index. + + + +```javascript +var arr = [ + { + 'a': [ + { + 'b': [ + { 'c': 'd' }, + { 'e': 'f' } + ] + } + ] + } +]; + +var bool = deepHasProp( arr, '0.a.0.b.0.c' ); +// returns true + +bool = deepHasProp( arr, '0.a.1.b.0.c' ); +// returns false + +bool = deepHasProp( arr, '0.a.0.b.1.c' ); +// returns false +``` + +The key path may be specified as either a delimited `string` or a key `array`. + + + +```javascript +var obj = { 'a': { 'b': { 'c': 'd' } } }; + +var bool = deepHasProp( obj, [ 'a', 'b', 'c' ] ); +// returns true +``` + +The function accepts the following `options`: + +- **sep**: key path separator. Default: `'.'`. + +By default, the function assumes `.` separated key values. To specify an alternative separator, set the `sep` option. + + + +```javascript +var obj = { 'a': { 'b': { 'c': 'd' } } }; + +var bool = deepHasProp( obj, 'a/b/c', { + 'sep': '/' +}); +// returns true +``` + +#### deepHasProp.factory( path\[, options] ) + +Returns a `function` which tests whether a `value` contains a nested key `path`, either own or inherited. + +```javascript +var has = deepHasProp.factory( 'a/b/c', { + 'sep': '/' +}); +``` + +#### has( value ) + +Returns a `boolean` indicating whether a `value` contains a nested key `path`, either own or inherited. + + + +```javascript +var has = deepHasProp.factory( 'a.b.c' ); + +function Foo() { + return this; +} +Foo.prototype.b = { + 'c': 'd' +}; + +var obj = { 'a': new Foo() }; + +var bool = has( obj ); +// returns true +``` + +
+ + + +
+ +## Notes + +- When provided `null` or `undefined`, the function result is always `false`. + + ```javascript + var bool = deepHasProp( null, 'a.b.c' ); + // returns false + + bool = deepHasProp( void 0, 'a.b.c' ); + // returns false + ``` + +- Property values other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var obj = { + 'a': 'b' + }; + + var bool = deepHasProp( obj, 'a.length' ); + // returns true + ``` + +- Key path `array` elements are coerced to `strings`. + + ```javascript + var obj = { + 'null': false + }; + var bool = deepHasProp( obj, [ null ] ); + // returns true + + obj = { + '[object Object]': false + }; + bool = deepHasProp( obj, [ {} ] ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var deepHasProp = require( '@stdlib/assert/deep-has-property' ); + +var bool; +var has; + +bool = deepHasProp( { 'a': { 'b': { 'c': 'd' } } }, 'a.b.c' ); +// returns true + +bool = deepHasProp( { 'a': { 'b': { 'c': 'd' } } }, [ 'a', 'b', 'hasOwnProperty' ] ); +// returns true + +bool = deepHasProp( { 'a': { 'b': { 'c': 'd' } } }, 'a/b/c', { + 'sep': '/' +}); +// returns true + +bool = deepHasProp( { 'a': { 'b': { 'c': 'd' } } }, 'a.b.c.d' ); +// returns false + +bool = deepHasProp( { 'a': [ { 'b': { 'c': 'd' } } ] }, [ 'a', '0', 'b', 'c', 'd' ] ); +// returns false + +bool = deepHasProp( { 'a': { 'b': { 'c': 'd' } } }, 'a/b/c/d/e', { + 'sep': '/' +}); +// returns false + +// Create a customized function: +has = deepHasProp.factory( 'a_b_c', { + 'sep': '_' +}); + +bool = has( { 'a': { 'b': { 'c': 'd' } } } ); +// returns true + +bool = has( { 'a': [ { 'b': { 'c': 'd' } } ] } ); +// returns false +``` + +
+ + + + + + diff --git a/deep-has-property/benchmark/benchmark.factory.js b/deep-has-property/benchmark/benchmark.factory.js new file mode 100644 index 00000000..6d93decd --- /dev/null +++ b/deep-has-property/benchmark/benchmark.factory.js @@ -0,0 +1,185 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var isFunction = require( '@stdlib/assert/is-function' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ).factory; + + +// MAIN // + +bench( pkg+':factory', function benchmark( b ) { + var path; + var has; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + path = 'a.b.' + i.toString(); + has = factory( path ); + if ( typeof has !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( !isFunction( has ) ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::options:factory', function benchmark( b ) { + var opts; + var has; + var i; + + opts = { + 'sep': '|' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + opts.sep = i.toString(); + has = factory( 'a.b.c', opts ); + if ( typeof has !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( !isFunction( has ) ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::delimited-string:factory', function benchmark( b ) { + var bool; + var obj; + var has; + var i; + + function Foo() { + return this; + } + Foo.prototype.b = { + 'c': randu() + }; + + obj = { + 'a': new Foo() + }; + has = factory( 'a.b.c' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = has( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::key-array:factory', function benchmark( b ) { + var bool; + var path; + var obj; + var has; + var i; + + function Foo() { + return this; + } + Foo.prototype.b = { + 'c': randu() + }; + + obj = { + 'a': new Foo() + }; + path = [ 'a', 'b', 'c' ]; + has = factory( path ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = has( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::custom-delimiter:factory', function benchmark( b ) { + var bool; + var opts; + var obj; + var has; + var i; + + function Foo() { + return this; + } + Foo.prototype.b = { + 'c': randu() + }; + + obj = { + 'a': new Foo() + }; + opts = { + 'sep': '-|-' + }; + has = factory( 'a-|-b-|-c', opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = has( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/deep-has-property/benchmark/benchmark.js b/deep-has-property/benchmark/benchmark.js new file mode 100644 index 00000000..c6ca4962 --- /dev/null +++ b/deep-has-property/benchmark/benchmark.js @@ -0,0 +1,161 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var deepHasProp = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var obj; + var i; + + function Foo() { + return this; + } + Foo.prototype.b = { + 'c': randu() + }; + + obj = { + 'a': new Foo() + }; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = deepHasProp( obj, 'a.b.c' ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::delimited-string', function benchmark( b ) { + var bool; + var obj; + var i; + + function Foo() { + return this; + } + Foo.prototype.b = { + 'c': randu() + }; + + obj = { + 'a': new Foo() + }; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = deepHasProp( obj, 'a.b.c' ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::key-array', function benchmark( b ) { + var bool; + var path; + var obj; + var i; + + function Foo() { + return this; + } + Foo.prototype.b = { + 'c': randu() + }; + + obj = { + 'a': new Foo() + }; + path = [ 'a', 'b', 'c' ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = deepHasProp( obj, path ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::custom-delimiter', function benchmark( b ) { + var bool; + var opts; + var obj; + var i; + + function Foo() { + return this; + } + Foo.prototype.b = { + 'c': randu() + }; + + obj = { + 'a': new Foo() + }; + opts = { + 'sep': '-|-' + }; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj.a.b.c = randu(); + bool = deepHasProp( obj, 'a-|-b-|-c', opts ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/deep-has-property/docs/repl.txt b/deep-has-property/docs/repl.txt new file mode 100644 index 00000000..05e1de11 --- /dev/null +++ b/deep-has-property/docs/repl.txt @@ -0,0 +1,72 @@ + +{{alias}}( value, path[, options] ) + Returns a boolean indicating whether an object contains a nested key path, + either own or inherited. + + Value arguments other than `null` or `undefined` are coerced to objects. + + Key path array elements are coerced to strings. + + Parameters + ---------- + value: any + Value to test. + + path: string|Array + Key path. + + options: Object (optional) + Options. + + options.sep: string (optional) + Key path separator. Default: '.'. + + Returns + ------- + bool: boolean + Boolean indicating if an object has a specified path. + + Examples + -------- + > function Foo() { return this; }; + > Foo.prototype.b = { 'c': 'd' }; + > var obj = { 'a': new Foo() }; + > var bool = {{alias}}( obj, 'a.b.c' ) + true + + // Specify a custom separator via the `sep` option: + > bool = {{alias}}( obj, 'a/b/c', { 'sep': '/' } ) + true + +{{alias}}.factory( path[, options] ) + Returns a function which tests whether an object contains a nested key path, + either own or inherited. + + Parameters + ---------- + path: string|Array + Key path. + + options: Object (optional) + Options. + + options.sep: string (optional) + Key path separator. Default: '.'. + + Returns + ------- + out: Function + Function which tests whether an object contains a nested key path. + + Examples + -------- + > function Foo() { return this; }; + > Foo.prototype.b = { 'c': 'd' }; + > var has = {{alias}}.factory( 'a/b/c', { 'sep': '/' } ); + > var obj = { 'a': new Foo() }; + > var bool = has( obj ) + true + + See Also + -------- + diff --git a/deep-has-property/docs/types/index.d.ts b/deep-has-property/docs/types/index.d.ts new file mode 100644 index 00000000..15422d01 --- /dev/null +++ b/deep-has-property/docs/types/index.d.ts @@ -0,0 +1,182 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining function options. +*/ +interface Options { + /** + * Key path separator (default: '.'). + */ + sep?: string; +} + +/** +* Returns a boolean indicating whether an object has a nested key path, either own or inherited. +* +* @param value - value to test +* @returns boolean indicating whether an object has a nested property +* +* @example +* var obj = { 'a': { 'b': { 'c': 'd' } } }; +* var bool = deepHasProp( obj ); +*/ +type HasFunction = ( value: any ) => boolean; + +/** +* Interface for testing whether an object contains a nested key path. +*/ +interface DeepHasProp { + /** + * Tests whether an object contains a nested key path, either own or inherited. + * + * ## Notes + * + * - Value arguments other than `null` or `undefined` are coerced to objects. + * - Key path array elements are coerced to strings. + * + * @param value - value to test + * @param path - key path + * @param options - function options + * @param options.sep - key path separator (default: '.') + * @throws must provide valid options + * @returns boolean indicating whether an object has a nested property + * + * @example + * function Foo() { + * return this; + * } + * Foo.prototype.b = { + * 'c': 'd' + * }; + * + * var obj = { + * 'a': new Foo() + * }; + * + * var bool = deepHasProp( obj, 'a.b.c' ); + * // returns true + * + * @example + * var arr = [ + * { + * 'a': [ + * { + * 'b': [ + * { 'c': 'd' }, + * { 'e': 'f' } + * ] + * } + * ] + * } + * ]; + * var bool = deepHasProp( arr, '0.a.0.b.0.c' ); + * // returns true + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var bool = deepHasProp( obj, [ 'a', 'b', 'c' ] ); + * // returns true + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var bool = deepHasProp( obj, 'a/b/c', { + * 'sep': '/' + * }); + * // returns true + */ + ( value: any, path: string | Array, options?: Options ): boolean; + + /** + * Returns a function which tests whether an object has a nested key path, either own or inherited. + * + * @param path - key path + * @param options - function options + * @param options.sep - key path separator (default: '.') + * @throws must provide valid options + * @returns function which tests whether an object has a nested key path + * + * @example + * var has = factory( 'a/b/c', { + * 'sep': '/' + * }); + */ + factory( path: string | Array, options?: Options ): HasFunction; +} + +/** +* Tests whether an object contains a nested key path, either own or inherited. +* +* @param value - value to test +* @param path - key path +* @param options - function options +* @param options.sep - key path separator (default: '.') +* @throws must provide valid options +* @returns boolean indicating whether an object has a nested property +* +* @example +* function Foo() { +* return this; +* } +* Foo.prototype.b = { +* 'c': 'd' +* }; +* +* var obj = { +* 'a': new Foo() +* }; +* +* var bool = deepHasProp( obj, 'a.b.c' ); +* // returns true +* +* @example +* var arr = [ +* { +* 'a': [ +* { +* 'b': [ +* { 'c': 'd' }, +* { 'e': 'f' } +* ] +* } +* ] +* } +* ]; +* var bool = deepHasProp( arr, '0.a.0.b.0.c' ); +* // returns true +* +* @example +* var obj = { 'a': { 'b': { 'c': 'd' } } }; +* var bool = deepHasProp( obj, [ 'a', 'b', 'c' ] ); +* // returns true +* +* @example +* var obj = { 'a': { 'b': { 'c': 'd' } } }; +* var bool = deepHasProp( obj, 'a/b/c', { +* 'sep': '/' +* }); +* // returns true +*/ +declare var deepHasProp: DeepHasProp; + + +// EXPORTS // + +export = deepHasProp; diff --git a/deep-has-property/docs/types/test.ts b/deep-has-property/docs/types/test.ts new file mode 100644 index 00000000..e7b3a06c --- /dev/null +++ b/deep-has-property/docs/types/test.ts @@ -0,0 +1,111 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import deepHasProp = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { 'a': { 'b': { 'c': 'd' } } }; + deepHasProp( obj, 'a.b.c' ); // $ExpectType boolean + deepHasProp( obj, 'a.b.d' ); // $ExpectType boolean + deepHasProp( obj, 'a/b/c', { 'sep': '/' } ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided a second argument which is neither a string nor an array of strings... +{ + const obj = { 'a': { 'b': { 'c': 'd' } } }; + deepHasProp( obj, {} ); // $ExpectError + deepHasProp( obj, false ); // $ExpectError + deepHasProp( obj, true ); // $ExpectError + deepHasProp( obj, null ); // $ExpectError + deepHasProp( obj, 123 ); // $ExpectError + deepHasProp( obj, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not an object... +{ + const obj = { 'a': { 'b': { 'c': 'd' } } }; + deepHasProp( obj, 'a.b.c', 'abc' ); // $ExpectError + deepHasProp( obj, 'a.b.c', false ); // $ExpectError + deepHasProp( obj, 'a.b.c', true ); // $ExpectError + deepHasProp( obj, 'a.b.c', null ); // $ExpectError + deepHasProp( obj, 'a.b.c', 123 ); // $ExpectError + deepHasProp( obj, 'a.b.c', [] ); // $ExpectError + deepHasProp( obj, 'a.b.c', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `sep` option which is not a string... +{ + const obj = { 'a': { 'b': { 'c': 'd' } } }; + deepHasProp( obj, 'a/b/c', { 'sep': 123 } ); // $ExpectError + deepHasProp( obj, 'a/b/c', { 'sep': true } ); // $ExpectError + deepHasProp( obj, 'a/b/c', { 'sep': false } ); // $ExpectError + deepHasProp( obj, 'a/b/c', { 'sep': {} } ); // $ExpectError + deepHasProp( obj, 'a/b/c', { 'sep': [] } ); // $ExpectError + deepHasProp( obj, 'a/b/c', { 'sep': ( x: number ): number => x } ); // $ExpectError +} + +// Attached to main export is a `factory` method which returns a function... +{ + deepHasProp.factory( 'a.b.c' ); // $ExpectType HasFunction +} + +// The compiler throws an error if the `factory` method is provided an unsupported number of arguments... +{ + deepHasProp.factory(); // $ExpectError + deepHasProp.factory( 'a.b.c', {}, {} ); // $ExpectError +} + +// The `factory` method returns a function which returns a boolean... +{ + const obj = { 'a': { 'b': { 'c': 'd' } } }; + const has = deepHasProp.factory( 'a.b.c' ); // $ExpectType HasFunction + has( obj ); // $ExpectType boolean + has( {} ); // $ExpectType boolean +} + +// The compiler throws an error if the `factory` method is provided a first argument which is neither a string nor an array of strings... +{ + deepHasProp.factory( {} ); // $ExpectError + deepHasProp.factory( false ); // $ExpectError + deepHasProp.factory( true ); // $ExpectError + deepHasProp.factory( null ); // $ExpectError + deepHasProp.factory( 123 ); // $ExpectError + deepHasProp.factory( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `factory` method is provided a `sep` option which is not a string... +{ + deepHasProp.factory( 'a/b/c', { 'sep': true } ); // $ExpectError + deepHasProp.factory( 'a/b/c', { 'sep': false } ); // $ExpectError + deepHasProp.factory( 'a/b/c', { 'sep': 123 } ); // $ExpectError + deepHasProp.factory( 'a/b/c', { 'sep': [] } ); // $ExpectError + deepHasProp.factory( 'a/b/c', { 'sep': {} } ); // $ExpectError + deepHasProp.factory( 'a/b/c', { 'sep': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const obj = { 'a': { 'b': { 'c': 'd' } } }; + deepHasProp(); // $ExpectError + deepHasProp( obj ); // $ExpectError + deepHasProp( obj, 'a.b.c', {}, 123 ); // $ExpectError +} diff --git a/deep-has-property/examples/index.js b/deep-has-property/examples/index.js new file mode 100644 index 00000000..c1a516de --- /dev/null +++ b/deep-has-property/examples/index.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline, object-curly-spacing */ + +'use strict'; + +var deepHasProp = require( './../lib' ); + +var bool; +var has; +var obj; + +obj = { 'a': { 'b': { 'c': 'd' } } }; +bool = deepHasProp( obj, 'a.b.c' ); +console.log( bool ); +// => true + +obj = { 'a': { 'b': { 'c': 'd' } } }; +bool = deepHasProp( obj, [ 'a', 'b', 'hasOwnProperty' ] ); +console.log( bool ); +// => true + +obj = { 'a': { 'b': { 'c': 'd' } } }; +bool = deepHasProp( obj, 'a/b/c', { + 'sep': '/' +}); +console.log( bool ); +// => true + +obj = { 'a': { 'b': { 'c': 'd' } } }; +bool = deepHasProp( obj, 'a.b.c.d' ); +console.log( bool ); +// => false + +obj = { 'a': [ { 'b': { 'c': 'd' } } ] }; +bool = deepHasProp( obj, [ 'a', '0', 'b', 'c', 'd' ] ); +console.log( bool ); +// => false + +obj = { 'a': { 'b': { 'c': 'd' } } }; +bool = deepHasProp( obj, 'a/b/c/d/e', { + 'sep': '/' +}); +console.log( bool ); +// => false + +// Create a customized function: +has = deepHasProp.factory( 'a_b_c', { + 'sep': '_' +}); + +obj = { 'a': { 'b': { 'c': 'd' } } }; +bool = has( obj ); +console.log( bool ); +// => true + +obj = { 'a': [ { 'b': { 'c': 'd' } } ] }; +bool = has( obj ); +console.log( bool ); +// => false diff --git a/deep-has-property/lib/defaults.json b/deep-has-property/lib/defaults.json new file mode 100644 index 00000000..e2ca187c --- /dev/null +++ b/deep-has-property/lib/defaults.json @@ -0,0 +1,3 @@ +{ + "sep": "." +} diff --git a/deep-has-property/lib/factory.js b/deep-has-property/lib/factory.js new file mode 100644 index 00000000..417634e7 --- /dev/null +++ b/deep-has-property/lib/factory.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isArray = require( '@stdlib/assert/is-array' ); +var copy = require( '@stdlib/utils/copy' ); +var validate = require( './validate.js' ); +var defaults = require( './defaults.json' ); +var has = require( './has.js' ); + + +// MAIN // + +/** +* Returns a function which tests whether an object has a nested key path, either own or inherited. +* +* @param {(string|Array)} path - key path +* @param {Options} [options] - function options +* @param {string} [options.sep='.'] - key path separator +* @throws {TypeError} first argument must be a string primitive or key array +* @throws {TypeError} options argument must be an object +* @throws {TypeError} must provide valid options +* @returns {Function} function which tests whether an object has a nested key path +* +* @example +* var has = factory( 'a/b/c', { +* 'sep': '/' +* }); +*/ +function factory( path, options ) { + var isStr; + var props; + var opts; + var err; + isStr = isString( path ); + if ( !isStr && !isArray( path ) ) { + throw new TypeError( 'invalid argument. Key path must be a string primitive or a key array. Value: `' + path + '`.' ); + } + opts = copy( defaults ); + if ( arguments.length > 1 ) { + err = validate( opts, options ); + if ( err ) { + throw err; + } + } + if ( isStr ) { + props = path.split( opts.sep ); + } else { + props = path; + } + return deepHasProp; + + /** + * Returns a boolean indicating whether an object has a nested key path, either own or inherited. + * + * @private + * @param {*} value - value to test + * @returns {boolean} boolean indicating whether an object has a nested property + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var bool = deepHasProp( obj ); + */ + function deepHasProp( value ) { + if ( value === void 0 || value === null ) { + return false; + } + return has( value, props ); + } +} + + +// EXPORTS // + +module.exports = factory; diff --git a/deep-has-property/lib/has.js b/deep-has-property/lib/has.js new file mode 100644 index 00000000..8f6fb87e --- /dev/null +++ b/deep-has-property/lib/has.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasProp = require( '@stdlib/assert/has-property' ); + + +// MAIN // + +/** +* Returns a boolean indicating whether an object has a nested property. +* +* @private +* @param {*} v - value to test +* @param {Array} props - list of properties defining a key path +* @returns {boolean} boolean indicating whether an object has a nested property +*/ +function deepHasProp( v, props ) { + var len = props.length; + var i; + if ( len === 0 ) { + return false; + } + for ( i = 0; i < len; i++ ) { + if ( hasProp( v, props[i] ) ) { + v = v[ props[ i ] ]; + } else { + return false; + } + } + return true; +} + + +// EXPORTS // + +module.exports = deepHasProp; diff --git a/deep-has-property/lib/index.js b/deep-has-property/lib/index.js new file mode 100644 index 00000000..2a758464 --- /dev/null +++ b/deep-has-property/lib/index.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object contains a nested key path, either own or inherited. +* +* @module @stdlib/assert/deep-has-property +* +* @example +* var deepHasProp = require( '@stdlib/assert/deep-has-property' ); +* +* function Foo() { +* return this; +* } +* Foo.prototype.b = { +* 'c': 'd' +* }; +* +* var obj = { +* 'a': new Foo() +* }; +* +* var bool = deepHasProp( obj, 'a.b.c' ); +* // returns true +* +* bool = deepHasProp( obj, [ 'a', 'b', 'c' ] ); +* // returns true +* +* @example +* var factory = require( '@stdlib/assert/deep-has-property' ).factory; +* +* var has = factory( 'a/b/c', { +* 'sep': '/' +* }); +* +* function Foo() { +* return this; +* } +* Foo.prototype.b = { +* 'c': 'd' +* }; +* +* var obj = { +* 'a': new Foo() +* }; +* +* var bool = has( obj ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var deepHasProp = require( './main.js' ); +var factory = require( './factory.js' ); + + +// MAIN // + +setReadOnly( deepHasProp, 'factory', factory ); + + +// EXPORTS // + +module.exports = deepHasProp; diff --git a/deep-has-property/lib/main.js b/deep-has-property/lib/main.js new file mode 100644 index 00000000..ce44cbda --- /dev/null +++ b/deep-has-property/lib/main.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isArray = require( '@stdlib/assert/is-array' ); +var copy = require( '@stdlib/utils/copy' ); +var validate = require( './validate.js' ); +var defaults = require( './defaults.json' ); +var has = require( './has.js' ); + + +// MAIN // + +/** +* Tests whether an object contains a nested key path, either own or inherited. +* +* @param {*} value - value to test +* @param {(string|Array)} path - key path +* @param {Options} [options] - function options +* @param {string} [options.sep='.'] - key path separator +* @throws {TypeError} second argument must be a string primitive or key array +* @throws {TypeError} options argument must be an object +* @throws {TypeError} must provide valid options +* @returns {boolean} boolean indicating whether an object has a nested property +* +* @example +* function Foo() { +* return this; +* } +* Foo.prototype.b = { +* 'c': 'd' +* }; +* +* var obj = { +* 'a': new Foo() +* }; +* +* var bool = deepHasProp( obj, 'a.b.c' ); +* // returns true +* +* @example +* var arr = [ +* { +* 'a': [ +* { +* 'b': [ +* { 'c': 'd' }, +* { 'e': 'f' } +* ] +* } +* ] +* } +* ]; +* var bool = deepHasProp( arr, '0.a.0.b.0.c' ); +* // returns true +* +* @example +* var obj = { 'a': { 'b': { 'c': 'd' } } }; +* var bool = deepHasProp( obj, [ 'a', 'b', 'c' ] ); +* // returns true +* +* @example +* var obj = { 'a': { 'b': { 'c': 'd' } } }; +* var bool = deepHasProp( obj, 'a/b/c', { +* 'sep': '/' +* }); +* // returns true +*/ +function deepHasProp( value, path, options ) { + var isStr; + var props; + var opts; + var err; + + isStr = isString( path ); + if ( !isStr && !isArray( path ) ) { + throw new TypeError( 'invalid argument. Key path must be a string primitive or a key array. Value: `' + path + '`.' ); + } + opts = copy( defaults ); + if ( arguments.length > 2 ) { + err = validate( opts, options ); + if ( err ) { + throw err; + } + } + if ( value === void 0 || value === null ) { + return false; + } + if ( isStr ) { + props = path.split( opts.sep ); + } else { + props = path; + } + return has( value, props ); +} + + +// EXPORTS // + +module.exports = deepHasProp; diff --git a/deep-has-property/lib/validate.js b/deep-has-property/lib/validate.js new file mode 100644 index 00000000..bf1fa535 --- /dev/null +++ b/deep-has-property/lib/validate.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isObject = require( '@stdlib/assert/is-plain-object' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); + + +// MAIN // + +/** +* Validates function options. +* +* @private +* @param {Object} opts - destination for function options +* @param {Options} options - function options +* @param {string} [options.sep] - key path separator +* @returns {(Error|null)} error or null +* +* @example +* var opts = {}; +* var options = { +* 'sep': '/' +* }; +* var err = validate( opts, options ); +* if ( err ) { +* throw err; +* } +*/ +function validate( opts, options ) { + if ( !isObject( options ) ) { + return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); + } + if ( hasOwnProp( options, 'sep' ) ) { + opts.sep = options.sep; + if ( !isString( opts.sep ) ) { + return new TypeError( 'invalid option. `sep` option must be a string primitive. Option: `' + opts.sep + '`.' ); + } + } + return null; +} + + +// EXPORTS // + +module.exports = validate; diff --git a/deep-has-property/package.json b/deep-has-property/package.json new file mode 100644 index 00000000..1e786784 --- /dev/null +++ b/deep-has-property/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/deep-has-property", + "version": "0.0.0", + "description": "Test whether an object contains a nested key path, either own or inherited.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "deephas", + "deephasown", + "deephasproperty", + "deephasownproperty", + "property", + "prop", + "test", + "check", + "validate", + "has", + "own", + "hasown", + "hasprop", + "hasproperty", + "hasownproperty" + ] +} diff --git a/deep-has-property/test/test.factory.js b/deep-has-property/test/test.factory.js new file mode 100644 index 00000000..5d15cf08 --- /dev/null +++ b/deep-has-property/test/test.factory.js @@ -0,0 +1,454 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var factory = require( './../lib/factory.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof factory, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a key path argument which is neither a string primitive or a key array', function test( t ) { + var values; + var i; + + values = [ + 3, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + factory( value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + '3', + 3, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + factory( 'a.b', value ); + }; + } +}); + +tape( 'the function throws an error if an invalid option', function test( t ) { + var values; + var i; + + values = [ + 3, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var opts = { + 'sep': value + }; + factory( 'a.b', opts ); + }; + } +}); + +tape( 'the function returns a function (no options)', function test( t ) { + var has = factory( 'a.b.c' ); + t.strictEqual( typeof has, 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the function returns a function (options)', function test( t ) { + var opts; + var has; + + opts = { + 'sep': '/' + }; + has = factory( 'a/b/c', opts ); + + t.strictEqual( typeof has, 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the returned function returns `false` if provided a `null` or `undefined` (no options)', function test( t ) { + var values; + var has; + var i; + + values = [ + null, + void 0 + ]; + + has = factory( 'a.b.c' ); + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( has( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the returned function returns `false` if provided a `null` or `undefined` (options)', function test( t ) { + var values; + var opts; + var has; + var i; + + values = [ + null, + void 0 + ]; + + opts = { + 'sep': '_' + }; + has = factory( 'a_b_c', opts ); + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( has( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the returned function returns `true` if an object has a specified path (delimited string)', function test( t ) { + var bool; + var has; + var obj; + + has = factory( 'a.b.0.c' ); + obj = { + 'a': { + 'b': [ + { + 'c': 'd' + } + ] + } + }; + bool = has( obj ); + + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the returned function returns `true` if an object has a specified path (key array)', function test( t ) { + var bool; + var path; + var has; + var obj; + + path = [ 'a', 'b', '0', 'c' ]; + has = factory( path ); + + obj = { + 'a': { + 'b': [ + { + 'c': 'd' + } + ] + } + }; + bool = has( obj ); + + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the returned function returns `true` if an object has a specified path (custom delimiter)', function test( t ) { + var bool; + var opts; + var has; + var obj; + + opts = { + 'sep': '/' + }; + has = factory( 'a/b/0/c', opts ); + + obj = { + 'a': { + 'b': [ + { + 'c': 'd' + } + ] + } + }; + bool = has( obj ); + + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the returned function returns `false` if an object does not have a specified path (delimited string)', function test( t ) { + var bool; + var has; + var obj; + + has = factory( 'a.b.c.0.d' ); + obj = { + 'a': { + 'b': [ + { + 'c': 'd' + } + ] + } + }; + bool = has( obj ); + + t.strictEqual( bool, false, 'returns false' ); + t.end(); +}); + +tape( 'the returned function returns `false` if an object does not have a specified path (key array)', function test( t ) { + var bool; + var path; + var has; + var obj; + + path = [ 'a', 'b', 'c', '0', 'd' ]; + has = factory( path ); + + obj = { + 'a': { + 'b': [ + { + 'c': 'd' + } + ] + } + }; + bool = has( obj ); + + t.strictEqual( bool, false, 'returns false' ); + t.end(); +}); + +tape( 'the returned function returns `false` if an object does not have a specified path (custom delimiter)', function test( t ) { + var bool; + var opts; + var has; + var obj; + + opts = { + 'sep': '/' + }; + has = factory( 'a/b/c/0/d', opts ); + + obj = { + 'a': { + 'b': [ + { + 'c': 'd' + } + ] + } + }; + bool = has( obj ); + + t.strictEqual( bool, false, 'returns false' ); + t.end(); +}); + +tape( 'the returned function returns `false` if provided an empty object (no options)', function test( t ) { + var bool; + var path; + var has; + + has = factory( 'a.b.c' ); + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + path = [ 'a', 'b', 'c' ]; + has = factory( path ); + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + has = factory( '' ); + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the returned function returns `false` if provided an empty object (options)', function test( t ) { + var bool; + var opts; + var path; + var has; + + opts = { + 'sep': '/' + }; + + has = factory( 'a/b/c', opts ); + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + path = [ 'a', 'b', 'c' ]; + has = factory( path, opts ); + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + has = factory( '', opts ); + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the returned function can return `true` if provided an empty key path (string; no options)', function test( t ) { + var bool; + var obj; + var has; + + has = factory( '' ); + obj = { + '': 'b' + }; + bool = has( obj ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the returned function can return `true` if provided an empty key path (string; options)', function test( t ) { + var bool; + var opts; + var has; + var obj; + + opts = { + 'sep': '/' + }; + + has = factory( '', opts ); + obj = { + '': 'b' + }; + bool = has( obj ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the returned function always returns `false` if provided an empty key path array (string; no options)', function test( t ) { + var bool; + var obj; + var has; + + has = factory( [] ); + obj = { + '': 'b' + }; + bool = has( obj ); + t.strictEqual( bool, false, 'returns false' ); + + bool = has( {} ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the returned function supports paths containing inherited properties', function test( t ) { + var bool; + var foo; + var has; + + function Foo() { + this.a = 'b'; + return this; + } + Foo.prototype.c = 'd'; + + foo = new Foo(); + + has = factory( 'a' ); + bool = has( foo ); + t.strictEqual( bool, true, 'returns true' ); + + has = factory( 'c' ); + bool = has( foo ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the returned function coerces primitives to objects', function test( t ) { + var bool; + var has; + + has = factory( 'length' ); + bool = has( 'beep' ); + + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); diff --git a/deep-has-property/test/test.has.js b/deep-has-property/test/test.has.js new file mode 100644 index 00000000..8c7c1da5 --- /dev/null +++ b/deep-has-property/test/test.has.js @@ -0,0 +1,195 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var has = require( './../lib/has.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof has, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object has a specified path', function test( t ) { + var bool; + var path; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + path = [ 'a', 'b', 'c' ]; + + bool = has( obj, path ); + t.strictEqual( bool, true, 'returns true' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + path = [ 'a', '0', 'b', '0', 'c' ]; + + bool = has( obj, path ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object does not have a specified path', function test( t ) { + var bool; + var path; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + path = [ 'a', 'b', 'c', 'd', 'e' ]; + + bool = has( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + path = [ '0', 'a', '0', 'b', '0', 'e' ]; + + bool = has( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an empty object', function test( t ) { + var bool; + var path; + var obj; + + path = [ 'a', 'b', 'c' ]; + obj = {}; + + bool = has( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + path = []; + obj = {}; + + bool = has( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an empty key path', function test( t ) { + var bool; + var path; + var obj; + + path = []; + obj = { + 'a': 'b' + }; + + bool = has( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + path = []; + obj = {}; + + bool = has( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function supports paths including inherited properties', function test( t ) { + var bool; + var path; + var foo; + + function Foo() { + this.a = 'b'; + return this; + } + Foo.prototype.c = 'd'; + + foo = new Foo(); + + path = [ 'a' ]; + bool = has( foo, path ); + t.strictEqual( bool, true, 'returns true' ); + + path = [ 'c' ]; + bool = has( foo, path ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function coerces path values to strings', function test( t ) { + var bool; + var path; + var obj; + + obj = { + 'a': { + '[object Object]': 'c' + } + }; + + path = [ 'a', {} ]; + bool = has( obj, path ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/deep-has-property/test/test.js b/deep-has-property/test/test.js new file mode 100644 index 00000000..971331d6 --- /dev/null +++ b/deep-has-property/test/test.js @@ -0,0 +1,505 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var deepHasProp = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof deepHasProp, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function exports a factory function', function test( t ) { + t.strictEqual( typeof deepHasProp.factory, 'function', 'exports a factory function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a key path argument which is neither a string primitive or a key array', function test( t ) { + var values; + var i; + + values = [ + 3, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var obj = { + 'a': { + 'b': 'c' + } + }; + deepHasProp( obj, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + '3', + 3, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var obj = { + 'a': { + 'b': 'c' + } + }; + deepHasProp( obj, 'a.b', value ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid option', function test( t ) { + var values; + var i; + + values = [ + 3, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var opts; + var obj; + + obj = { + 'a': { + 'b': 'c' + } + }; + opts = { + 'sep': value + }; + deepHasProp( obj, 'a.b', opts ); + }; + } +}); + +tape( 'the function returns `false` if provided a first argument which is either `null` or `undefined`', function test( t ) { + var values; + var i; + + values = [ + null, + void 0 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( deepHasProp( values[ i ], 'a.b.c' ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns `true` if an object has a specified path (delimited string)', function test( t ) { + var bool; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + bool = deepHasProp( obj, 'a.b.c' ); + t.strictEqual( bool, true, 'returns true' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + bool = deepHasProp( obj, 'a.0.b.0.c' ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if an object has a specified path (key array)', function test( t ) { + var bool; + var path; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + path = [ 'a', 'b', 'c' ]; + bool = deepHasProp( obj, path ); + t.strictEqual( bool, true, 'returns true' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + path = [ 'a', '0', 'b', '0', 'c' ]; + bool = deepHasProp( obj, path ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if an object has a specified path (custom delimiter)', function test( t ) { + var bool; + var opts; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + opts = { + 'sep': '/' + }; + bool = deepHasProp( obj, 'a/b/c', opts ); + t.strictEqual( bool, true, 'returns true' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + opts = { + 'sep': '/' + }; + bool = deepHasProp( obj, 'a/0/b/0/c', opts ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object does not have a specified path (delimited string)', function test( t ) { + var bool; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + bool = deepHasProp( obj, 'a.b.c.d.e' ); + t.strictEqual( bool, false, 'returns false' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + bool = deepHasProp( obj, '0.a.0.b.0.e' ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object does not have a specified path (key array)', function test( t ) { + var bool; + var path; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + path = [ 'a', 'b', 'c', 'd', 'e' ]; + bool = deepHasProp( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + path = [ '0', 'a', '0', 'b', '0', 'e' ]; + bool = deepHasProp( obj, path ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object does not have a specified path (custom delimiter)', function test( t ) { + var bool; + var opts; + var obj; + + obj = { + 'a': { + 'b': { + 'c': 'd' + } + } + }; + opts = { + 'sep': '/' + }; + bool = deepHasProp( obj, 'a/b/c/d/e', opts ); + t.strictEqual( bool, false, 'returns false' ); + + obj = { + 'a': [ + { + 'b': [ + { + 'c': 'd' + }, + { + 'e': 'f' + } + ] + } + ] + }; + opts = { + 'sep': '/' + }; + bool = deepHasProp( obj, '0/a/0/b/0/e', opts ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an empty object (no options)', function test( t ) { + var bool; + + bool = deepHasProp( {}, 'a.b.c' ); + t.strictEqual( bool, false, 'returns false' ); + + bool = deepHasProp( {}, '' ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an empty object (options)', function test( t ) { + var bool; + var opts; + + opts = { + 'sep': '/' + }; + + bool = deepHasProp( {}, 'a/b/c', opts ); + t.strictEqual( bool, false, 'returns false' ); + + bool = deepHasProp( {}, '', opts ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an empty path array (no options)', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + bool = deepHasProp( obj, [] ); + t.strictEqual( bool, false, 'returns false' ); + + bool = deepHasProp( {}, [] ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an empty path array (options)', function test( t ) { + var bool; + var opts; + var obj; + + opts = { + 'sep': '/' + }; + obj = { + 'a': 'b' + }; + bool = deepHasProp( obj, [], opts ); + t.strictEqual( bool, false, 'returns false' ); + + bool = deepHasProp( {}, [], opts ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function can return `true` if provided an empty key path string (no options)', function test( t ) { + var bool; + var obj; + + obj = { + '': 'b' + }; + bool = deepHasProp( obj, '' ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function can return `true` if provided an empty key path string (options)', function test( t ) { + var bool; + var opts; + var obj; + + obj = { + '': 'b' + }; + opts = { + 'sep': '/' + }; + bool = deepHasProp( obj, '', opts ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function supports paths including inherited properties', function test( t ) { + var bool; + var foo; + + function Foo() { + this.a = 'b'; + return this; + } + Foo.prototype.c = 'd'; + + foo = { + 'a': new Foo() + }; + + bool = deepHasProp( foo, 'a.a' ); + t.strictEqual( bool, true, 'returns true' ); + + bool = deepHasProp( foo, 'a.c' ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function coerces primitives to objects', function test( t ) { + var bool = deepHasProp( 'beep', 'length' ); + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); diff --git a/deep-has-property/test/test.validate.js b/deep-has-property/test/test.validate.js new file mode 100644 index 00000000..6985e0df --- /dev/null +++ b/deep-has-property/test/test.validate.js @@ -0,0 +1,125 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var validate = require( './../lib/validate.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof validate, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns an error if provided an options argument which is not an object', function test( t ) { + var values; + var err; + var i; + + values = [ + '3', + 3, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + err = validate( {}, values[ i ] ); + t.strictEqual( err instanceof TypeError, true, 'returns a TypeError when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns an error if provided a `sep` option argument which is not a string primitive', function test( t ) { + var values; + var opts; + var err; + var i; + + values = [ + new String( '3' ), // eslint-disable-line no-new-wrappers + 3, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + opts = { + 'sep': values[ i ] + }; + err = validate( {}, opts ); + t.strictEqual( err instanceof TypeError, true, 'returns a TypeError when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function will ignore unrecognized properties', function test( t ) { + var opts; + var obj; + + opts = { + 'foo': true, + 'bar': 'baz' + }; + obj = {}; + + t.strictEqual( validate( obj, opts ), null, 'returns null' ); + t.deepEqual( obj, {}, 'does not set any properties' ); + + t.end(); +}); + +tape( 'the function returns `null` if all options are valid', function test( t ) { + var opts; + var obj; + + opts = { + 'sep': '_' + }; + obj = {}; + + t.strictEqual( validate( obj, opts ), null, 'returns null' ); + t.strictEqual( obj.sep, '_', 'sets option value' ); + + opts = { + 'sep': '/' + }; + obj = {}; + + t.strictEqual( validate( obj, opts ), null, 'returns null' ); + t.strictEqual( obj.sep, '/', 'sets option value' ); + + t.end(); +}); diff --git a/docs/types/index.d.ts b/docs/types/index.d.ts new file mode 100644 index 00000000..d49eebf3 --- /dev/null +++ b/docs/types/index.d.ts @@ -0,0 +1,6056 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/* tslint:disable:max-line-length */ +/* tslint:disable:max-file-line-count */ + +import contains = require( '@stdlib/assert/contains' ); +import deepEqual = require( '@stdlib/assert/deep-equal' ); +import deepHasOwnProp = require( '@stdlib/assert/deep-has-own-property' ); +import deepHasProp = require( '@stdlib/assert/deep-has-property' ); +import hasArrayBufferSupport = require( '@stdlib/assert/has-arraybuffer-support' ); +import hasAsyncAwaitSupport = require( '@stdlib/assert/has-async-await-support' ); +import hasAsyncIteratorSymbolSupport = require( '@stdlib/assert/has-async-iterator-symbol-support' ); +import hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' ); +import hasClassSupport = require( '@stdlib/assert/has-class-support' ); +import hasDataViewSupport = require( '@stdlib/assert/has-dataview-support' ); +import hasDefinePropertiesSupport = require( '@stdlib/assert/has-define-properties-support' ); +import hasDefinePropertySupport = require( '@stdlib/assert/has-define-property-support' ); +import hasFloat32ArraySupport = require( '@stdlib/assert/has-float32array-support' ); +import hasFloat64ArraySupport = require( '@stdlib/assert/has-float64array-support' ); +import hasFunctionNameSupport = require( '@stdlib/assert/has-function-name-support' ); +import hasGeneratorSupport = require( '@stdlib/assert/has-generator-support' ); +import hasGlobalThisSupport = require( '@stdlib/assert/has-globalthis-support' ); +import hasInt8ArraySupport = require( '@stdlib/assert/has-int8array-support' ); +import hasInt16ArraySupport = require( '@stdlib/assert/has-int16array-support' ); +import hasInt32ArraySupport = require( '@stdlib/assert/has-int32array-support' ); +import hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' ); +import hasMapSupport = require( '@stdlib/assert/has-map-support' ); +import hasNodeBufferSupport = require( '@stdlib/assert/has-node-buffer-support' ); +import hasOwnProp = require( '@stdlib/assert/has-own-property' ); +import hasProp = require( '@stdlib/assert/has-property' ); +import hasProxySupport = require( '@stdlib/assert/has-proxy-support' ); +import hasSetSupport = require( '@stdlib/assert/has-set-support' ); +import hasSharedArrayBufferSupport = require( '@stdlib/assert/has-sharedarraybuffer-support' ); +import hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +import hasToStringTagSupport = require( '@stdlib/assert/has-tostringtag-support' ); +import hasUint8ArraySupport = require( '@stdlib/assert/has-uint8array-support' ); +import hasUint8ClampedArraySupport = require( '@stdlib/assert/has-uint8clampedarray-support' ); +import hasUint16ArraySupport = require( '@stdlib/assert/has-uint16array-support' ); +import hasUint32ArraySupport = require( '@stdlib/assert/has-uint32array-support' ); +import hasUTF16SurrogatePairAt = require( '@stdlib/assert/has-utf16-surrogate-pair-at' ); +import hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' ); +import hasWeakMapSupport = require( '@stdlib/assert/has-weakmap-support' ); +import hasWeakSetSupport = require( '@stdlib/assert/has-weakset-support' ); +import instanceOf = require( '@stdlib/assert/instance-of' ); +import isAbsolutePath = require( '@stdlib/assert/is-absolute-path' ); +import isAccessorProperty = require( '@stdlib/assert/is-accessor-property' ); +import isAccessorPropertyIn = require( '@stdlib/assert/is-accessor-property-in' ); +import isAlphagram = require( '@stdlib/assert/is-alphagram' ); +import isAlphaNumeric = require( '@stdlib/assert/is-alphanumeric' ); +import isAnagram = require( '@stdlib/assert/is-anagram' ); +import isArguments = require( '@stdlib/assert/is-arguments' ); +import isArray = require( '@stdlib/assert/is-array' ); +import isArrayArray = require( '@stdlib/assert/is-array-array' ); +import isArrayLength = require( '@stdlib/assert/is-array-length' ); +import isArrayLike = require( '@stdlib/assert/is-array-like' ); +import isArrayLikeObject = require( '@stdlib/assert/is-array-like-object' ); +import isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); +import isASCII = require( '@stdlib/assert/is-ascii' ); +import isBetween = require( '@stdlib/assert/is-between' ); +import isBetweenArray = require( '@stdlib/assert/is-between-array' ); +import IS_BIG_ENDIAN = require( '@stdlib/assert/is-big-endian' ); +import isBigInt = require( '@stdlib/assert/is-bigint' ); +import isBinaryString = require( '@stdlib/assert/is-binary-string' ); +import isBoolean = require( '@stdlib/assert/is-boolean' ); +import isBooleanArray = require( '@stdlib/assert/is-boolean-array' ); +import isBoxedPrimitive = require( '@stdlib/assert/is-boxed-primitive' ); +import IS_BROWSER = require( '@stdlib/assert/is-browser' ); +import isBuffer = require( '@stdlib/assert/is-buffer' ); +import isCapitalized = require( '@stdlib/assert/is-capitalized' ); +import isCentrosymmetricMatrix = require( '@stdlib/assert/is-centrosymmetric-matrix' ); +import isCircular = require( '@stdlib/assert/is-circular' ); +import isCircularArray = require( '@stdlib/assert/is-circular-array' ); +import isCircularPlainObject = require( '@stdlib/assert/is-circular-plain-object' ); +import isCollection = require( '@stdlib/assert/is-collection' ); +import isComplex = require( '@stdlib/assert/is-complex' ); +import isComplexLike = require( '@stdlib/assert/is-complex-like' ); +import isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' ); +import isComplexTypedArrayLike = require( '@stdlib/assert/is-complex-typed-array-like' ); +import isComplex64 = require( '@stdlib/assert/is-complex64' ); +import isComplex64Array = require( '@stdlib/assert/is-complex64array' ); +import isComplex128 = require( '@stdlib/assert/is-complex128' ); +import isComplex128Array = require( '@stdlib/assert/is-complex128array' ); +import isComposite = require( '@stdlib/assert/is-composite' ); +import isConfigurableProperty = require( '@stdlib/assert/is-configurable-property' ); +import isConfigurablePropertyIn = require( '@stdlib/assert/is-configurable-property-in' ); +import isCubeNumber = require( '@stdlib/assert/is-cube-number' ); +import IS_DARWIN = require( '@stdlib/assert/is-darwin' ); +import isDataProperty = require( '@stdlib/assert/is-data-property' ); +import isDataPropertyIn = require( '@stdlib/assert/is-data-property-in' ); +import isDataView = require( '@stdlib/assert/is-dataview' ); +import isDateObject = require( '@stdlib/assert/is-date-object' ); +import isDigitString = require( '@stdlib/assert/is-digit-string' ); +import IS_ELECTRON = require( '@stdlib/assert/is-electron' ); +import IS_ELECTRON_MAIN = require( '@stdlib/assert/is-electron-main' ); +import IS_ELECTRON_RENDERER = require( '@stdlib/assert/is-electron-renderer' ); +import isEmailAddress = require( '@stdlib/assert/is-email-address' ); +import isEmptyArray = require( '@stdlib/assert/is-empty-array' ); +import isEmptyObject = require( '@stdlib/assert/is-empty-object' ); +import isEmptyString = require( '@stdlib/assert/is-empty-string' ); +import isEnumerableProperty = require( '@stdlib/assert/is-enumerable-property' ); +import isEnumerablePropertyIn = require( '@stdlib/assert/is-enumerable-property-in' ); +import isError = require( '@stdlib/assert/is-error' ); +import isEvalError = require( '@stdlib/assert/is-eval-error' ); +import isEven = require( '@stdlib/assert/is-even' ); +import isFalsy = require( '@stdlib/assert/is-falsy' ); +import isFalsyArray = require( '@stdlib/assert/is-falsy-array' ); +import isFinite = require( '@stdlib/assert/is-finite' ); +import isFiniteArray = require( '@stdlib/assert/is-finite-array' ); +import isFloat32Array = require( '@stdlib/assert/is-float32array' ); +import isFloat32MatrixLike = require( '@stdlib/assert/is-float32matrix-like' ); +import isFloat32ndarrayLike = require( '@stdlib/assert/is-float32ndarray-like' ); +import isFloat32VectorLike = require( '@stdlib/assert/is-float32vector-like' ); +import isFloat64Array = require( '@stdlib/assert/is-float64array' ); +import isFloat64MatrixLike = require( '@stdlib/assert/is-float64matrix-like' ); +import isFloat64ndarrayLike = require( '@stdlib/assert/is-float64ndarray-like' ); +import isFloat64VectorLike = require( '@stdlib/assert/is-float64vector-like' ); +import isFunction = require( '@stdlib/assert/is-function' ); +import isFunctionArray = require( '@stdlib/assert/is-function-array' ); +import isGeneratorObject = require( '@stdlib/assert/is-generator-object' ); +import isGeneratorObjectLike = require( '@stdlib/assert/is-generator-object-like' ); +import isgzipBuffer = require( '@stdlib/assert/is-gzip-buffer' ); +import isHexString = require( '@stdlib/assert/is-hex-string' ); +import isInfinite = require( '@stdlib/assert/is-infinite' ); +import isInheritedProperty = require( '@stdlib/assert/is-inherited-property' ); +import isInt8Array = require( '@stdlib/assert/is-int8array' ); +import isInt16Array = require( '@stdlib/assert/is-int16array' ); +import isInt32Array = require( '@stdlib/assert/is-int32array' ); +import isInteger = require( '@stdlib/assert/is-integer' ); +import isIntegerArray = require( '@stdlib/assert/is-integer-array' ); +import isIterableLike = require( '@stdlib/assert/is-iterable-like' ); +import isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +import isJSON = require( '@stdlib/assert/is-json' ); +import isLeapYear = require( '@stdlib/assert/is-leap-year' ); +import IS_LITTLE_ENDIAN = require( '@stdlib/assert/is-little-endian' ); +import isLowercase = require( '@stdlib/assert/is-lowercase' ); +import isMatrixLike = require( '@stdlib/assert/is-matrix-like' ); +import isMethod = require( '@stdlib/assert/is-method' ); +import isMethodIn = require( '@stdlib/assert/is-method-in' ); +import isNamedTypedTupleLike = require( '@stdlib/assert/is-named-typed-tuple-like' ); +import isnan = require( '@stdlib/assert/is-nan' ); +import isNaNArray = require( '@stdlib/assert/is-nan-array' ); +import isNativeFunction = require( '@stdlib/assert/is-native-function' ); +import isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +import isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ); +import isNegativeIntegerArray = require( '@stdlib/assert/is-negative-integer-array' ); +import isNegativeNumber = require( '@stdlib/assert/is-negative-number' ); +import isNegativeNumberArray = require( '@stdlib/assert/is-negative-number-array' ); +import isNegativeZero = require( '@stdlib/assert/is-negative-zero' ); +import IS_NODE = require( '@stdlib/assert/is-node' ); +import isNodeBuiltin = require( '@stdlib/assert/is-node-builtin' ); +import isNodeDuplexStreamLike = require( '@stdlib/assert/is-node-duplex-stream-like' ); +import isNodeReadableStreamLike = require( '@stdlib/assert/is-node-readable-stream-like' ); +import isNodeREPL = require( '@stdlib/assert/is-node-repl' ); +import isNodeStreamLike = require( '@stdlib/assert/is-node-stream-like' ); +import isNodeTransformStreamLike = require( '@stdlib/assert/is-node-transform-stream-like' ); +import isNodeWritableStreamLike = require( '@stdlib/assert/is-node-writable-stream-like' ); +import isNonConfigurableProperty = require( '@stdlib/assert/is-nonconfigurable-property' ); +import isNonConfigurablePropertyIn = require( '@stdlib/assert/is-nonconfigurable-property-in' ); +import isNonEnumerableProperty = require( '@stdlib/assert/is-nonenumerable-property' ); +import isNonEnumerablePropertyIn = require( '@stdlib/assert/is-nonenumerable-property-in' ); +import isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ); +import isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ); +import isNonNegativeNumber = require( '@stdlib/assert/is-nonnegative-number' ); +import isNonNegativeNumberArray = require( '@stdlib/assert/is-nonnegative-number-array' ); +import isNonPositiveInteger = require( '@stdlib/assert/is-nonpositive-integer' ); +import isNonPositiveIntegerArray = require( '@stdlib/assert/is-nonpositive-integer-array' ); +import isNonPositiveNumber = require( '@stdlib/assert/is-nonpositive-number' ); +import isNonPositiveNumberArray = require( '@stdlib/assert/is-nonpositive-number-array' ); +import isNonSymmetricMatrix = require( '@stdlib/assert/is-nonsymmetric-matrix' ); +import isNull = require( '@stdlib/assert/is-null' ); +import isNullArray = require( '@stdlib/assert/is-null-array' ); +import isNumber = require( '@stdlib/assert/is-number' ); +import isNumberArray = require( '@stdlib/assert/is-number-array' ); +import isNumericArray = require( '@stdlib/assert/is-numeric-array' ); +import isObject = require( '@stdlib/assert/is-object' ); +import isObjectArray = require( '@stdlib/assert/is-object-array' ); +import isObjectLike = require( '@stdlib/assert/is-object-like' ); +import isOdd = require( '@stdlib/assert/is-odd' ); +import isPersymmetricMatrix = require( '@stdlib/assert/is-persymmetric-matrix' ); +import isPlainObject = require( '@stdlib/assert/is-plain-object' ); +import isPlainObjectArray = require( '@stdlib/assert/is-plain-object-array' ); +import isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ); +import isPositiveIntegerArray = require( '@stdlib/assert/is-positive-integer-array' ); +import isPositiveNumber = require( '@stdlib/assert/is-positive-number' ); +import isPositiveNumberArray = require( '@stdlib/assert/is-positive-number-array' ); +import isPositiveZero = require( '@stdlib/assert/is-positive-zero' ); +import isPrime = require( '@stdlib/assert/is-prime' ); +import isPrimitive = require( '@stdlib/assert/is-primitive' ); +import isPrimitiveArray = require( '@stdlib/assert/is-primitive-array' ); +import isPRNGLike = require( '@stdlib/assert/is-prng-like' ); +import isProbability = require( '@stdlib/assert/is-probability' ); +import isProbabilityArray = require( '@stdlib/assert/is-probability-array' ); +import isPrototypeOf = require( '@stdlib/assert/is-prototype-of' ); +import isRangeError = require( '@stdlib/assert/is-range-error' ); +import isReadOnlyProperty = require( '@stdlib/assert/is-read-only-property' ); +import isReadOnlyPropertyIn = require( '@stdlib/assert/is-read-only-property-in' ); +import isReadWriteProperty = require( '@stdlib/assert/is-read-write-property' ); +import isReadWritePropertyIn = require( '@stdlib/assert/is-read-write-property-in' ); +import isReadableProperty = require( '@stdlib/assert/is-readable-property' ); +import isReadablePropertyIn = require( '@stdlib/assert/is-readable-property-in' ); +import isReferenceError = require( '@stdlib/assert/is-reference-error' ); +import isRegExp = require( '@stdlib/assert/is-regexp' ); +import isRegExpString = require( '@stdlib/assert/is-regexp-string' ); +import isRelativePath = require( '@stdlib/assert/is-relative-path' ); +import isSafeInteger = require( '@stdlib/assert/is-safe-integer' ); +import isSafeIntegerArray = require( '@stdlib/assert/is-safe-integer-array' ); +import isSameValue = require( '@stdlib/assert/is-same-value' ); +import isSameValueZero = require( '@stdlib/assert/is-same-value-zero' ); +import isSharedArrayBuffer = require( '@stdlib/assert/is-sharedarraybuffer' ); +import isSkewCentrosymmetricMatrix = require( '@stdlib/assert/is-skew-centrosymmetric-matrix' ); +import isSkewPersymmetricMatrix = require( '@stdlib/assert/is-skew-persymmetric-matrix' ); +import isSkewSymmetricMatrix = require( '@stdlib/assert/is-skew-symmetric-matrix' ); +import isSquareMatrix = require( '@stdlib/assert/is-square-matrix' ); +import isSquareNumber = require( '@stdlib/assert/is-square-number' ); +import isSquareTriangularNumber = require( '@stdlib/assert/is-square-triangular-number' ); +import isStrictEqual = require( '@stdlib/assert/is-strict-equal' ); +import isString = require( '@stdlib/assert/is-string' ); +import isStringArray = require( '@stdlib/assert/is-string-array' ); +import isSymbol = require( '@stdlib/assert/is-symbol' ); +import isSymbolArray = require( '@stdlib/assert/is-symbol-array' ); +import isSymmetricMatrix = require( '@stdlib/assert/is-symmetric-matrix' ); +import isSyntaxError = require( '@stdlib/assert/is-syntax-error' ); +import isTriangularNumber = require( '@stdlib/assert/is-triangular-number' ); +import isTruthy = require( '@stdlib/assert/is-truthy' ); +import isTruthyArray = require( '@stdlib/assert/is-truthy-array' ); +import isTypeError = require( '@stdlib/assert/is-type-error' ); +import isTypedArray = require( '@stdlib/assert/is-typed-array' ); +import isTypedArrayLength = require( '@stdlib/assert/is-typed-array-length' ); +import isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' ); +import isUint8Array = require( '@stdlib/assert/is-uint8array' ); +import isUint8ClampedArray = require( '@stdlib/assert/is-uint8clampedarray' ); +import isUint16Array = require( '@stdlib/assert/is-uint16array' ); +import isUint32Array = require( '@stdlib/assert/is-uint32array' ); +import isUNCPath = require( '@stdlib/assert/is-unc-path' ); +import isUndefined = require( '@stdlib/assert/is-undefined' ); +import isUndefinedOrNull = require( '@stdlib/assert/is-undefined-or-null' ); +import isUnityProbabilityArray = require( '@stdlib/assert/is-unity-probability-array' ); +import isUppercase = require( '@stdlib/assert/is-uppercase' ); +import isURI = require( '@stdlib/assert/is-uri' ); +import isURIError = require( '@stdlib/assert/is-uri-error' ); +import isVectorLike = require( '@stdlib/assert/is-vector-like' ); +import IS_WEB_WORKER = require( '@stdlib/assert/is-web-worker' ); +import isWhitespace = require( '@stdlib/assert/is-whitespace' ); +import IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +import isWritableProperty = require( '@stdlib/assert/is-writable-property' ); +import isWritablePropertyIn = require( '@stdlib/assert/is-writable-property-in' ); +import isWriteOnlyProperty = require( '@stdlib/assert/is-write-only-property' ); +import isWriteOnlyPropertyIn = require( '@stdlib/assert/is-write-only-property-in' ); + +/** +* Interface describing the `assert` namespace. +*/ +interface Namespace { + /** + * Tests if an array-like value contains a search value. + * + * ## Notes + * + * - When `val` is a string, the function checks whether the characters of the search string are found in the input string. The search is case-sensitive. + * - When `val` is an array-like object, the function checks whether the input array contains an element strictly equal to the specified search value. + * - For strings, this function is modeled after `String.prototype.includes`, part of the ECMAScript 6 specification. This function is different from a call to `String.prototype.includes.call` insofar as type-checking is performed for all arguments. + * - The function does not distinguish between positive and negative zero. + * - If `position < 0`, the search is performed for the entire input array or string. + * + * @param val - input value + * @param searchValue - search value + * @param position - position at which to start searching for `searchValue` (default: 0) + * @throws second argument must be a primitive string primitive when the first argument is a string + * @returns boolean indicating whether one value contains another + * + * @example + * var bool = ns.contains( 'last man standing', 'stand' ); + * // returns true + * + * @example + * var bool = ns.contains( [ 1, 2, 3, 4 ], 2 ); + * // returns true + * + * @example + * var bool = ns.contains( 'presidential election', 'president' ); + * // returns true + * + * @example + * var bool = ns.contains( [ NaN, 2, 3, 4 ], NaN ); + * // returns true + * + * @example + * var bool = ns.contains( 'javaScript', 'js' ); + * // returns false + * + * @example + * var bool = ns.contains( [ 1, 2, 3, {} ], {} ); + * // returns false + * + * @example + * var bool = ns.contains( 'Hidden Treasures', '' ); + * // returns true + */ + contains: typeof contains; + + /** + * Tests for deep equality between two values. + * + * @param a - first comparison value + * @param b - second comparison value + * @returns boolean indicating if `a` is deep equal to `b` + * + * @example + * var bool = ns.deepEqual( [ 1, 2, 3 ], [ 1, 2, 3 ] ); + * // returns true + * + * @example + * var bool = ns.deepEqual( [ 1, 2, 3 ], [ 1, 2, '3' ] ); + * // returns false + * + * @example + * var bool = ns.deepEqual( { 'a': 2 }, { 'a': [ 2 ] } ); + * // returns false + * + * @example + * var bool = ns.deepEqual( [], {} ); + * // returns false + * + * @example + * var bool = ns.deepEqual( null, null ); + * // returns true + */ + deepEqual: typeof deepEqual; + + /** + * Tests whether an object contains a nested key path. + * + * @param value - value to test + * @param path - key path + * @param options - function options + * @param options.sep - key path separator (default: '.') + * @throws must provide valid options + * @returns boolean indicating whether an object has a nested property + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var bool = ns.deepHasOwnProp( obj, 'a.b.c' ); + * // returns true + * + * @example + * var arr = [ + * { + * 'a': [ + * { + * 'b': [ + * { 'c': 'd' }, + * { 'e': 'f' } + * ] + * } + * ] + * } + * ]; + * var bool = ns.deepHasOwnProp( arr, '0.a.0.b.0.c' ); + * // returns true + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var bool = ns.deepHasOwnProp( obj, [ 'a', 'b', 'c' ] ); + * // returns true + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var bool = ns.deepHasOwnProp( obj, 'a/b/c', { + * 'sep': '/' + * }); + * // returns true + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var has = ns.deepHasOwnProp.factory( 'a/b/c', { + * 'sep': '/' + * }); + * var bool = has( obj ); + * // returns true + */ + deepHasOwnProp: typeof deepHasOwnProp; + + /** + * Tests whether an object contains a nested key path, either own or inherited. + * + * @param value - value to test + * @param path - key path + * @param options - function options + * @param options.sep - key path separator (default: '.') + * @throws must provide valid options + * @returns boolean indicating whether an object has a nested property + * + * @example + * function Foo() { + * return this; + * } + * Foo.prototype.b = { + * 'c': 'd' + * }; + * + * var obj = { + * 'a': new Foo() + * }; + * + * var bool = ns.deepHasProp( obj, 'a.b.c' ); + * // returns true + * + * @example + * var arr = [ + * { + * 'a': [ + * { + * 'b': [ + * { 'c': 'd' }, + * { 'e': 'f' } + * ] + * } + * ] + * } + * ]; + * var bool = ns.deepHasProp( arr, '0.a.0.b.0.c' ); + * // returns true + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var bool = ns.deepHasProp( obj, [ 'a', 'b', 'c' ] ); + * // returns true + * + * @example + * var obj = { 'a': { 'b': { 'c': 'd' } } }; + * var bool = ns.deepHasProp( obj, 'a/b/c', { + * 'sep': '/' + * }); + * // returns true + */ + deepHasProp: typeof deepHasProp; + + /** + * Tests for native `ArrayBuffer` support. + * + * @returns boolean indicating if an environment has `ArrayBuffer` support + * + * @example + * var bool = ns.hasArrayBufferSupport(); + * // returns + */ + hasArrayBufferSupport: typeof hasArrayBufferSupport; + + /** + * Tests for native `async/await` support. + * + * @returns boolean indicating if an environment has native `async`/`await` support + * + * @example + * var bool = ns.hasAsyncAwaitSupport(); + * if ( bool ) { + * console.log( 'Environment has native async/await support.' ); + * } else { + * console.log( 'Environment lacks native async/await support.' ); + * } + */ + hasAsyncAwaitSupport: typeof hasAsyncAwaitSupport; + + /** + * Tests for native `Symbol.asyncIterator` support. + * + * @returns boolean indicating if an environment has `Symbol.asyncIterator` support + * + * @example + * var bool = ns.hasAsyncIteratorSymbolSupport(); + * // returns + */ + hasAsyncIteratorSymbolSupport: typeof hasAsyncIteratorSymbolSupport; + + /** + * Tests for native `BigInt` support. + * + * @returns boolean indicating if an environment has `BigInt` support + * + * @example + * var bool = ns.hasBigIntSupport(); + * // returns + */ + hasBigIntSupport: typeof hasBigIntSupport; + + /** + * Tests for native `class` support. + * + * @returns boolean indicating if an environment has native `class` support + * + * @example + * var bool = ns.hasClassSupport(); + * if ( bool ) { + * console.log( 'Environment has native class support.' ); + * } else { + * console.log( 'Environment lacks native class support.' ); + * } + */ + hasClassSupport: typeof hasClassSupport; + + /** + * Tests for native `DataView` support. + * + * @returns boolean indicating if an environment has `DataView` support + * + * @example + * var bool = ns.hasDataViewSupport(); + * // returns + */ + hasDataViewSupport: typeof hasDataViewSupport; + + /** + * Tests for `Object.defineProperties` support. + * + * @returns boolean indicating if an environment has `Object.defineProperties` support + * + * @example + * var bool = ns.hasDefinePropertiesSupport(); + * // returns + */ + hasDefinePropertiesSupport: typeof hasDefinePropertiesSupport; + + /** + * Tests for `Object.defineProperty` support. + * + * @returns boolean indicating if an environment has `Object.defineProperty` support + * + * @example + * var bool = ns.hasDefinePropertySupport(); + * // returns + */ + hasDefinePropertySupport: typeof hasDefinePropertySupport; + + /** + * Tests for native `Float32Array` support. + * + * @returns boolean indicating if an environment has `Float32Array` support + * + * @example + * var bool = ns.hasFloat32ArraySupport(); + * // returns + */ + hasFloat32ArraySupport: typeof hasFloat32ArraySupport; + + /** + * Tests for native `Float64Array` support. + * + * @returns boolean indicating if an environment has `Float64Array` support + * + * @example + * var bool = ns.hasFloat64ArraySupport(); + * // returns + */ + hasFloat64ArraySupport: typeof hasFloat64ArraySupport; + + /** + * Tests for native function `name` support. + * + * @returns boolean indicating if an environment has function `name` support + * + * @example + * var bool = ns.hasFunctionNameSupport(); + * // returns + */ + hasFunctionNameSupport: typeof hasFunctionNameSupport; + + /** + * Tests for native `function*()` support. + * + * @returns boolean indicating if an environment has native `function*()` support + * + * @example + * var bool = ns.hasGeneratorSupport(); + * // returns + */ + hasGeneratorSupport: typeof hasGeneratorSupport; + + /** + * Tests for `globalThis` support. + * + * @returns boolean indicating if an environment has `globalThis` support + * + * @example + * var bool = ns.hasGlobalThisSupport(); + * // returns + */ + hasGlobalThisSupport: typeof hasGlobalThisSupport; + + /** + * Tests for native `Int8Array` support. + * + * @returns boolean indicating if an environment has `Int8Array` support + * + * @example + * var bool = ns.hasInt8ArraySupport(); + * // returns + */ + hasInt8ArraySupport: typeof hasInt8ArraySupport; + + /** + * Tests for native `Int16Array` support. + * + * @returns boolean indicating if an environment has `Int16Array` support + * + * @example + * var bool = ns.hasInt16ArraySupport(); + * // returns + */ + hasInt16ArraySupport: typeof hasInt16ArraySupport; + + /** + * Tests for native `Int32Array` support. + * + * @returns boolean indicating if an environment has `Int32Array` support + * + * @example + * var bool = ns.hasInt32ArraySupport(); + * // returns + */ + hasInt32ArraySupport: typeof hasInt32ArraySupport; + + /** + * Tests for native `Symbol.iterator` support. + * + * @returns boolean indicating if an environment has `Symbol.iterator` support + * + * @example + * var bool = ns.hasIteratorSymbolSupport(); + * // returns + */ + hasIteratorSymbolSupport: typeof hasIteratorSymbolSupport; + + /** + * Tests for native `Map` support. + * + * @returns boolean indicating if an environment has `Map` support + * + * @example + * var bool = ns.hasMapSupport(); + * // returns + */ + hasMapSupport: typeof hasMapSupport; + + /** + * Tests for native `Buffer` support. + * + * @returns boolean indicating if an environment has `Buffer` support + * + * @example + * var bool = ns.hasNodeBufferSupport(); + * // returns + */ + hasNodeBufferSupport: typeof hasNodeBufferSupport; + + /** + * Tests if an object has a specified property. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object has a specified property + * + * @example + * var beep = { + * 'boop': true + * }; + * + * var bool = ns.hasOwnProp( beep, 'boop' ); + * // returns true + * + * @example + * var beep = { + * 'boop': true + * }; + * + * var bool = ns.hasOwnProp( beep, 'bap' ); + * // returns false + */ + hasOwnProp: typeof hasOwnProp; + + /** + * Tests if an object has a specified property, either own or inherited. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object has a specified property + * + * @example + * var beep = { + * 'boop': true + * }; + * + * var bool = ns.hasProp( beep, 'boop' ); + * // returns true + * + * @example + * var beep = { + * 'boop': true + * }; + * + * var bool = ns.hasProp( beep, 'bap' ); + * // returns false + */ + hasProp: typeof hasProp; + + /** + * Tests for native `Proxy` support. + * + * @returns boolean indicating if an environment has native `Proxy` support + * + * @example + * var bool = ns.hasProxySupport(); + * // returns + */ + hasProxySupport: typeof hasProxySupport; + + /** + * Tests for native `Set` support. + * + * @returns boolean indicating if an environment has `Set` support + * + * @example + * var bool = ns.hasSetSupport(); + * // returns + */ + hasSetSupport: typeof hasSetSupport; + + /** + * Tests for native `SharedArrayBuffer` support. + * + * @returns boolean indicating if an environment has `SharedArrayBuffer` support + * + * @example + * var bool = ns.hasSharedArrayBufferSupport(); + * // returns + */ + hasSharedArrayBufferSupport: typeof hasSharedArrayBufferSupport; + + /** + * Tests for native `Symbol` support. + * + * @returns boolean indicating if an environment has `Symbol` support + * + * @example + * var bool = ns.hasSymbolSupport(); + * // returns + */ + hasSymbolSupport: typeof hasSymbolSupport; + + /** + * Tests for native `toStringTag` support. + * + * @returns boolean indicating if an environment has `toStringTag` support + * + * @example + * var bool = ns.hasToStringTagSupport(); + * // returns + */ + hasToStringTagSupport: typeof hasToStringTagSupport; + + /** + * Tests for native `Uint8Array` support. + * + * @returns boolean indicating if an environment has `Uint8Array` support + * + * @example + * var bool = ns.hasUint8ArraySupport(); + * // returns + */ + hasUint8ArraySupport: typeof hasUint8ArraySupport; + + /** + * Tests for native `Uint8ClampedArray` support. + * + * @returns boolean indicating if an environment has `Uint8ClampedArray` support + * + * @example + * var bool = ns.hasUint8ClampedArraySupport(); + * // returns + */ + hasUint8ClampedArraySupport: typeof hasUint8ClampedArraySupport; + + /** + * Tests for native `Uint16Array` support. + * + * @returns boolean indicating if an environment has `Uint16Array` support + * + * @example + * var bool = ns.hasUint16ArraySupport(); + * // returns + */ + hasUint16ArraySupport: typeof hasUint16ArraySupport; + + /** + * Tests for native `Uint32Array` support. + * + * @returns boolean indicating if an environment has `Uint32Array` support + * + * @example + * var bool = ns.hasUint32ArraySupport(); + * // returns + */ + hasUint32ArraySupport: typeof hasUint32ArraySupport; + + /** + * Tests if a position in a string marks the start of a UTF-16 surrogate pair. + * + * @param str - input string + * @param pos - position + * @throws first argument must be a string + * @throws second argument must be a nonnegative integer + * @throws position must be a valid index in string + * @returns boolean indicating if a position in a string marks the start of a UTF-16 surrogate pair + * + * @example + * var bool = ns.hasUTF16SurrogatePairAt( '🌷', 0 ); + * // returns true + * + * @example + * var bool = ns.hasUTF16SurrogatePairAt( '🌷', 1 ); + * // returns false + */ + hasUTF16SurrogatePairAt: typeof hasUTF16SurrogatePairAt; + + /** + * Tests for native WebAssembly support. + * + * @returns boolean indicating if an environment has native WebAssembly support + * + * @example + * var bool = ns.hasWebAssemblySupport(); + * // returns + */ + hasWebAssemblySupport: typeof hasWebAssemblySupport; + + /** + * Tests for native `WeakMap` support. + * + * @returns boolean indicating if an environment has `WeakMap` support + * + * @example + * var bool = ns.hasWeakMapSupport(); + * // returns + */ + hasWeakMapSupport: typeof hasWeakMapSupport; + + /** + * Tests for native `WeakSet` support. + * + * @returns boolean indicating if an environment has `WeakSet` support + * + * @example + * var bool = ns.hasWeakSetSupport(); + * // returns + */ + hasWeakSetSupport: typeof hasWeakSetSupport; + + /** + * Tests whether a value has in its prototype chain a specified constructor as a prototype property. + * + * @param value - value to test + * @param constructor - constructor to test against + * @throws constructor must be callable + * @returns boolean indicating whether a value is an instance of a provided constructor + * + * @example + * var bool = ns.instanceOf( [], Array ); + * // returns true + * + * @example + * var bool = ns.instanceOf( {}, Object ); // exception + * // returns true + * + * @example + * var bool = ns.instanceOf( 'beep', String ); + * // returns false + * + * @example + * var bool = ns.instanceOf( null, Object ); + * // returns false + * + * @example + * var bool = ns.instanceOf( 5, Object ); + * // returns false + */ + instanceOf: typeof instanceOf; + + /** + * Tests if a value is an absolute path. + * + * ## Notes + * + * - Function behavior is platform-specific. On Windows platforms, the function is equal to `.win32()`. On POSIX platforms, the function is equal to `.posix()`. + * + * @param value - value to test + * @returns boolean indicating whether value is an absolute path + * + * @example + * var IS_WINDOWS = require( `@stdlib/assert/is-windows` ); + * var bool; + * if ( IS_WINDOWS ) { + * bool = ns.isAbsolutePath( 'C:\\foo\\bar\\baz' ); + * // returns true + * } else { + * bool = ns.isAbsolutePath( '/foo/bar/baz' ); + * // returns true + * } + * + * @example + * var bool = ns.isAbsolutePath.posix( '/foo/bar/baz' ); + * // returns true + * + * @example + * var bool = ns.isAbsolutePath.posix( 'foo/bar/baz' ); + * // returns false + * + * @example + * var bool = ns.isAbsolutePath.win32( 'C:\\foo\\bar\\baz' ); + * // returns true + * + * @example + * var bool = ns.isAbsolutePath.win32( 'foo\\bar\\baz' ); + * // returns false + */ + isAbsolutePath: typeof isAbsolutePath; + + /** + * Tests if an object's own property has an accessor descriptor. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property has an accessor descriptor + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * function getter() { + * return 'beep'; + * } + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': false, + * 'get': getter + * }); + * + * var bool = ns.isAccessorProperty( obj, 'boop' ); + * // returns false + * + * bool = ns.isAccessorProperty( obj, 'beep' ); + * // returns true + */ + isAccessorProperty: typeof isAccessorProperty; + + /** + * Tests if an object's own or inherited property has an accessor descriptor. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property has an accessor descriptor + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * function getter() { + * return 'beep'; + * } + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': false, + * 'get': getter + * }); + * + * var bool = ns.isAccessorPropertyIn( obj, 'boop' ); + * // returns false + * + * bool = ns.isAccessorPropertyIn( obj, 'beep' ); + * // returns true + */ + isAccessorPropertyIn: typeof isAccessorPropertyIn; + + /** + * Tests if a value is an alphagram (i.e., a sequence of characters arranged in alphabetical order). + * + * ## Notes + * + * - The function first checks that an input value is a string before validating that the value is an alphagram. For non-string values, the function returns `false`. + * + * @param value - value to test + * @returns boolean indicating if a value is an alphagram + * + * @example + * var out = ns.isAlphagram( 'beep' ); + * // returns true + * + * @example + * var out = ns.isAlphagram( new String( 'beep' ) ); + * // returns true + * + * @example + * var out = ns.isAlphagram( 'zba' ); + * // returns false + * + * @example + * var out = ns.isAlphagram( '' ); + * // returns false + * + * @example + * var out = ns.isAlphagram( 123 ); + * // returns false + */ + isAlphagram: typeof isAlphagram; + + /** + * Tests whether a string contains only alphanumeric characters. + * + * @param value - value to test + * @returns boolean indicating if a string contains only alphanumeric characters + * + * @example + * var out = ns.isAlphaNumeric( 'abc123def456' ); + * // returns true + * + * @example + * var out = ns.isAlphaNumeric( '0xffffff' ); + * // returns true + * + * @example + * var out = ns.isAlphaNumeric( '123' ); + * // returns true + * + * @example + * var out = ns.isAlphaNumeric( '' ); + * // returns false + * + * @example + * var out = ns.isAlphaNumeric( 123 ); + * // returns false + */ + isAlphaNumeric: typeof isAlphaNumeric; + + /** + * Tests if a value is an anagram. + * + * @param str - comparison string + * @param x - value to test + * @returns boolean indicating if a value is an anagram + * + * @example + * var bool = ns.isAnagram( 'I am a weakish speller', 'William Shakespeare' ); + * // returns true + * + * @example + * var bool = ns.isAnagram( 'bat', 'tabba' ); + * // returns false + */ + isAnagram: typeof isAnagram; + + /** + * Tests whether a value is an `arguments` object. + * + * @param value - value to test + * @returns boolean indicating whether a value is an `arguments` object + * + * @example + * function foo() { + * return arguments; + * } + * + * var bool = ns.isArguments( foo() ); + * // returns true + * + * @example + * var bool = ns.isArguments( [] ); + * // returns false + */ + isArguments: typeof isArguments; + + /** + * Tests if a value is an array. + * + * @param value - value to test + * @returns boolean indicating whether value is an array + * + * @example + * var bool = ns.isArray( [] ); + * // returns true + * + * @example + * var bool = ns.isArray( {} ); + * // returns false + */ + isArray: typeof isArray; + + /** + * Tests if a value is an array of arrays. + * + * @param value - value to test + * @returns boolean indicating whether a value is an array of arrays + * + * @example + * var bool = ns.isArrayArray( [ [], [] ] ); + * // returns true + * + * bool = ns.isArrayArray( [ {}, {} ] ); + * // returns false + * + * bool = ns.isArrayArray( [] ); + * // returns false + */ + isArrayArray: typeof isArrayArray; + + /** + * Tests if a value is a valid array length. + * + * ## Notes + * + * - A valid length property for an Array instance is any integer value on the interval `[0, 2^32-1]`. + * + * @param value - value to test + * @returns boolean indicating if a value is a valid array length + * + * @example + * var bool = ns.isArrayLength( 3 ); + * // returns true + * + * @example + * var bool = ns.isArrayLength( 3.14 ); + * // returns false + */ + isArrayLength: typeof isArrayLength; + + /** + * Tests if a value is array-like. + * + * ## Notes + * + * - If provided a string, the function returns `true`. + * + * @param value - value to test + * @returns boolean indicating if a value is array-like + * + * @example + * var bool = ns.isArrayLike( [] ); + * // returns true + * + * @example + * var bool = ns.isArrayLike( {'length':10} ); + * // returns true + */ + isArrayLike: typeof isArrayLike; + + /** + * Tests if a value is an array-like object. + * + * ## Notes + * + * - If provided a string, the function returns `false`. + * + * @param value - value to test + * @returns boolean indicating if a value is an array-like object + * + * @example + * var bool = ns.isArrayLikeObjectObject( [] ); + * // returns true + * + * @example + * var bool = ns.isArrayLikeObjectObject( { 'length':10 } ); + * // returns true + * + * @example + * var bool = ns.isArrayLikeObjectObject( 'beep' ); + * // returns false + */ + isArrayLikeObject: typeof isArrayLikeObject; + + /** + * Tests if a value is an ArrayBuffer. + * + * @param value - value to test + * @returns boolean indicating whether value is an ArrayBuffer + * + * @example + * var ArrayBuffer = require( `@stdlib/array/buffer` ); + * + * var bool = ns.isArrayBuffer( new ArrayBuffer( 10 ) ); + * // returns true + * + * @example + * var bool = ns.isArrayBuffer( [] ); + * // returns false + */ + isArrayBuffer: typeof isArrayBuffer; + + /** + * Tests whether a character belongs to the ASCII character set and whether this is true for all characters in a provided string. + * + * @param value - value to test + * @returns boolean indicating if a string has all ASCII characters + * + * @example + * var out = ns.isASCII( 'beep' ); + * // returns true + * + * @example + * var out = ns.isASCII( 'È' ); + * // returns false + * + * @example + * var out = ns.isASCII( '' ); + * // returns false + * + * @example + * var out = ns.isASCII( 123 ); + * // returns false + */ + isASCII: typeof isASCII; + + /** + * Tests if a value is between two values. + * + * @param value - value to test + * @param a - left comparison value + * @param b - right comparison value + * @param left - indicates whether the left comparison value is inclusive (default: 'closed') + * @param right - indicates whether the right comparison value is inclusive (default: 'closed') + * @returns boolean indicating whether a value is between two values + * + * @example + * var bool = ns.isBetween( 3.14, 3.0, 4.0 ); + * // returns true + * + * @example + * var bool = ns.isBetween( 4.5, 3.0, 4.0 ); + * // returns false + * + * @example + * var bool = ns.isBetween( 3.14, 3.14, 4.0 ); + * // returns true + * + * @example + * var bool = ns.isBetween( 3.14, 3.14, 4.0, 'open', 'closed' ); + * // returns false + * + * @example + * var bool = ns.isBetween( 3.14, 3.0, 3.14 ); + * // returns true + * + * @example + * var bool = ns.isBetween( 3.14, 3.0, 3.14, 'closed', 'open' ); + * // returns false + */ + isBetween: typeof isBetween; + + /** + * Tests if a value is an array-like object where every element is between two values. + * + * @param value - value to test + * @param a - left comparison value + * @param b - right comparison value + * @param left - indicates whether the left comparison value is inclusive (default: 'closed') + * @param right - indicates whether the right comparison value is inclusive (default: 'closed') + * @returns boolean indicating whether a value is an array-like object where every element is between two values + * + * @example + * var arr = [ 3.0, 3.14, 4.0 ]; + * var bool = ns.isBetweenArray( arr, 3.0, 4.0 ); + * // returns true + * + * @example + * var arr = [ 3.0, 3.14, 4.0 ]; + * var bool = ns.isBetweenArray( arr, 3.14, 4.0 ); + * // returns false + * + * @example + * var arr = [ 3.0, 3.14, 4.0 ]; + * var bool = ns.isBetweenArray( arr, 3.0, 3.14 ); + * // returns false + * + * @example + * var arr = [ 3.0, 3.14, 4.0 ]; + * var bool = ns.isBetweenArray( arr, 3.0, 4.0, 'open', 'closed' ); + * // returns false + * + * @example + * var arr = [ 3.0, 3.14, 4.0 ]; + * var bool = ns.isBetweenArray( arr, 3.0, 4.0, 'closed', 'open' ); + * // returns false + */ + isBetweenArray: typeof isBetweenArray; + + /** + * Boolean indicating if the environment is big endian. + * + * @example + * var bool = ns.IS_BIG_ENDIAN; + * // returns + */ + IS_BIG_ENDIAN: typeof IS_BIG_ENDIAN; + + /** + * Tests if a value is a BigInt. + * + * @param value - value to test + * @returns boolean indicating whether value is a BigInt + * + * @example + * var BigInt = require( `@stdlib/bigint/ctor` ); + * + * var bool = ns.isBigInt.isPrimitive( BigInt( '1' ) ); + * // returns true + * + * @example + * var BigInt = require( `@stdlib/bigint/ctor` ); + * + * var bool = ns.isBigInt.isPrimitive( Object( BigInt( '1' ) ) ); + * // returns false + * + * @example + * var BigInt = require( `@stdlib/bigint/ctor` ); + * + * var bool = ns.isBigInt.isPrimitive( BigInt( '1' ) ); + * // returns true + * + * @example + * var BigInt = require( `@stdlib/bigint/ctor` ); + * + * var bool = ns.isBigInt.isPrimitive( Object( BigInt( '1' ) ) ); + * // returns false + * + * @example + * var BigInt = require( `@stdlib/bigint/ctor` ); + * + * var bool = ns.isBigInt.isObject( BigInt( '1' ) ); + * // returns false + * + * @example + * var BigInt = require( `@stdlib/bigint/ctor` ); + * + * var bool = ns.isBigInt.isObject( Object( BigInt( '1' ) ) ); + * // returns true + */ + isBigInt: typeof isBigInt; + + /** + * Tests if a value is a binary string. + * + * @param value - value to test + * @returns boolean indicating if an input value is a binary string + * + * @example + * var bool = ns.isBinaryString( '1000101' ); + * // returns true + * + * @example + * var bool = ns.isBinaryString( 'beep' ); + * // returns false + * + * @example + * var bool = ns.isBinaryString( '' ); + * // returns false + */ + isBinaryString: typeof isBinaryString; + + /** + * Tests if a value is a boolean. + * + * @param value - value to test + * @returns boolean indicating whether value is a boolean + * + * @example + * var bool = ns.isBoolean( false ); + * // returns true + * + * @example + * var bool = ns.isBoolean( true ); + * // returns true + * + * @example + * var bool = ns.isBoolean.isPrimitive( new Boolean( true ) ); + * // returns false + * + * @example + * var bool = ns.isBoolean.isObject( new Boolean( false ) ); + * // returns true + */ + isBoolean: typeof isBoolean; + + /** + * Tests if a value is an array-like object of booleans. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object of booleans + * + * @example + * var bool = ns.isBooleanArray( [ true, false, true ] ); + * // returns true + * + * @example + * var bool = ns.isBooleanArray( [ true, 'abc', false ] ); + * // returns false + * + * @example + * var bool = ns.isBooleanArray.primitives( [ true, false ] ); + * // returns true + * + * @example + * var bool = ns.isBooleanArray.objects( [ new Boolean( false ), new Boolean( true ) ] ); + * // returns true + */ + isBooleanArray: typeof isBooleanArray; + + /** + * Tests if a value is a JavaScript boxed primitive. + * + * @param value - value to test + * @returns boolean indicating if a value is a JavaScript boxed primitive + * + * @example + * var bool = ns.isBoxedPrimitive( new String( 'beep' ) ); + * // returns true + * + * @example + * var bool = ns.isBoxedPrimitive( new Number( 3.21 ) ); + * // returns true + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * var bool = ns.isBoxedPrimitive( Object( Symbol( 'beep' ) ) ); + * // returns true + * + * @example + * var bool = ns.isBoxedPrimitive( true ); + * // returns false + * + * @example + * var bool = ns.isBoxedPrimitive( {} ); + * // returns false + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * var bool = ns.isBoxedPrimitive( Symbol( 'beep' ) ); + * // returns false + */ + isBoxedPrimitive: typeof isBoxedPrimitive; + + /** + * Boolean indicating if the runtime is a web browser. + * + * @example + * var bool = ns.IS_BROWSER; + * // returns + */ + IS_BROWSER: typeof IS_BROWSER; + + /** + * Tests if a value is a Buffer instance. + * + * @param value - value to validate + * @returns boolean indicating if a value is a Buffer instance + * + * @example + * var v = ns.isBuffer( new Buffer( 'beep' ) ); + * // returns true + * + * @example + * var v = ns.isBuffer( new Buffer( [1,2,3,4] ) ); + * // returns true + * + * @example + * var v = ns.isBuffer( {} ); + * // returns false + * + * @example + * var v = ns.isBuffer( [] ); + * // returns false + */ + isBuffer: typeof isBuffer; + + /** + * Tests if a value is a string having an uppercase first character. + * + * @param value - value to test + * @returns boolean indicating whether a value is a string with an uppercase first character + * + * @example + * var bool = ns.isCapitalized( 'Hello' ); + * // returns true + * + * @example + * var bool = ns.isCapitalized( 'WORLD' ); + * // returns true + * + * @example + * var bool = ns.isCapitalized( '!' ); + * // returns false + * + * @example + * var bool = ns.isCapitalized( 'salt and light' ); + * // returns false + */ + isCapitalized: typeof isCapitalized; + + /** + * Tests if a value is a centrosymmetric matrix. + * + * ## Notes + * + * - The implementation must rely on manually checking that \\(M_{ij} = M_{N-i-1,N-j-1}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. + * - Worst case scenario: O(N^2). + * + * @param v - value to test + * @returns boolean indicating if a value is a centrosymmetric matrix + * + * @example + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'generic', [ 2, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * + * var bool = ns.isCentrosymmetricMatrix( arr ); + * // returns true + * + * bool = ns.isCentrosymmetricMatrix( [] ); + * // returns false + */ + isCentrosymmetricMatrix: typeof isCentrosymmetricMatrix; + + /** + * Tests if an object-like value contains a circular reference. + * + * @param value - value to test + * @returns boolean indicating whether value is object-like and contains a circular reference + * + * @example + * var obj = { + * 'a': 'beep', + * 'b': { + * 'c': 'boop' + * } + * }; + * obj.b.self = obj; + * var bool = ns.isCircular( obj ); + * // returns true + * + * @example + * var arr = [ 1, 2, 3 ]; + * arr.push( arr ); + * var bool = ns.isCircular( arr ); + * // returns true + * + * @example + * var bool = ns.isCircular( {} ); + * // returns false + * + * @example + * var bool = ns.isCircular( null ); + * // returns false + */ + isCircular: typeof isCircular; + + /** + * Tests if a value is an array containing a circular reference. + * + * @param value - value to test + * @returns boolean indicating whether value is an array containing a circular reference + * + * @example + * var arr = [ 1, 2, 3 ]; + * arr.push( arr ); + * var bool = ns.isCircularArray( arr ); + * // returns true + * + * @example + * var obj = { + * 'a': 'beep', + * 'b': { + * 'c': 'boop' + * } + * }; + * obj.b.self = obj; + * var bool = ns.isCircularArray( obj ); + * // returns false + * + * @example + * var bool = ns.isCircularArray( [] ); + * // returns false + * + * @example + * var bool = ns.isCircularArray( null ); + * // returns false + */ + isCircularArray: typeof isCircularArray; + + /** + * Tests if a value is a plain object containing a circular reference. + * + * @param value - value to test + * @returns boolean indicating whether value is a plain object containing a circular reference + * + * @example + * var obj = { + * 'a': 'beep', + * 'b': { + * 'c': 'boop' + * } + * }; + * obj.b.self = obj; + * var bool = ns.isCircularPlainObject( obj ); + * // returns true + * + * @example + * var arr = [ 1, 2, 3 ]; + * arr.push( arr ); + * var bool = ns.isCircularPlainObject( arr ); + * // returns false + * + * @example + * var bool = ns.isCircularPlainObject( {} ); + * // returns false + * + * @example + * var bool = ns.isCircularPlainObject( null ); + * // returns false + */ + isCircularPlainObject: typeof isCircularPlainObject; + + /** + * Tests if a value is a collection. + * + * @param value - value to test + * @returns boolean indicating whether a value is a collection + * + * @example + * var bool = ns.isCollection( [] ); + * // returns true + * + * @example + * var bool = ns.isCollection( {} ); + * // returns false + */ + isCollection: typeof isCollection; + + /** + * Tests if a value is a 64-bit or 128-bit complex number. + * + * @param value - value to test + * @returns boolean indicating if a value is a 64-bit or 128-bit complex number + * + * @example + * var Complex128 = require( `@stdlib/complex/float64` ); + * var Complex64 = require( `@stdlib/complex/float32` ); + * + * var x = new Complex128( 4.0, 2.0 ); + * var bool = ns.isComplex( x ); + * // returns true + * + * x = new Complex64( 4.0, 2.0 ); + * bool = ns.isComplex( x ); + * // returns true + */ + isComplex: typeof isComplex; + + /** + * Tests if a value is a complex number-like object. + * + * @param value - value to test + * @returns boolean indicating if a value is a complex number-like object. + * + * @example + * var Complex128 = require( `@stdlib/complex/float64` ); + * var Complex64 = require( `@stdlib/complex/float32` ); + * + * var x = new Complex128( 4.0, 2.0 ); + * var bool = ns.isComplexLike( x ); + * // returns true + * + * x = new Complex64( 4.0, 2.0 ); + * bool = ns.isComplexLike( x ); + * // returns true + */ + isComplexLike: typeof isComplexLike; + + /** + * Tests if a value is a complex typed array. + * + * @param value - value to test + * @returns boolean indicating if a value is a complex typed array + * + * @example + * var Complex128Array = require( `@stdlib/array/complex128` ); + * + * var bool = ns.isComplexTypedArray( new Complex128Array( 10 ) ); + * // returns true + */ + isComplexTypedArray: typeof isComplexTypedArray; + + /** + * Tests if a value is complex-typed-array-like. + * + * @param value - value to test + * @returns boolean indicating if a value is complex-typed-array-like + * + * @example + * var arr = { + * 'BYTES_PER_ELEMENT': 8, + * 'length': 10, + * 'byteOffset': 0, + * 'byteLength': 10, + * 'get': function get() {}, + * 'set': function set() {} + * }; + * var val = ns.isComplexTypedArrayLike( arr ); + * // returns true + * + * @example + * var Complex128Array = require( `@stdlib/array/complex128` ); + * + * var val = ns.isComplexTypedArrayLike( new Complex128Array( 4 ) ); + * // returns true + * + * @example + * var val = ns.isComplexTypedArrayLike( [] ); + * // returns false + * + * @example + * var val = ns.isComplexTypedArrayLike( {} ); + * // returns false + * + * @example + * var val = ns.isComplexTypedArrayLike( null ); + * // returns false + * + * @example + * var val = ns.isComplexTypedArrayLike( 'beep' ); + * // returns false + */ + isComplexTypedArrayLike: typeof isComplexTypedArrayLike; + + /** + * Tests if a value is a 64-bit complex number. + * + * @param value - value to test + * @returns boolean indicating if a value is a 64-bit complex number + * + * @example + * var Complex64 = require( `@stdlib/complex/float32` ); + * + * var x = new Complex64( 4.0, 2.0 ); + * + * var bool = ns.isComplex64( x ); + * // returns true + */ + isComplex64: typeof isComplex64; + + /** + * Tests if a value is a Complex64Array. + * + * @param value - value to test + * @returns boolean indicating whether value is a Complex64Array + * + * @example + * var bool = ns.isComplex64Array( new Complex64Array( 10 ) ); + * // returns true + * + * @example + * var bool = ns.isComplex64Array( [] ); + * // returns false + */ + isComplex64Array: typeof isComplex64Array; + + /** + * Tests if a value is a 128-bit complex number. + * + * @param value - value to test + * @returns boolean indicating if a value is a 128-bit complex number + * + * @example + * var Complex128 = require( `@stdlib/complex/float64` ); + * + * var x = new Complex128( 4.0, 2.0 ); + * + * var bool = ns.isComplex128( x ); + * // returns true + */ + isComplex128: typeof isComplex128; + + /** + * Tests if a value is a Complex128Array. + * + * @param value - value to test + * @returns boolean indicating whether value is a Complex128Array + * + * @example + * var bool = ns.isComplex128Array( new Complex128Array( 10 ) ); + * // returns true + * + * @example + * var bool = ns.isComplex128Array( [] ); + * // returns false + */ + isComplex128Array: typeof isComplex128Array; + + /** + * Tests if a value is a composite number. + * + * @param value - value to test + * @returns boolean indicating whether value is a composite number + * + * @example + * var bool = ns.isComposite( 4.0 ); + * // returns true + * + * @example + * var bool = ns.isComposite( new Number( 4.0 ) ); + * // returns true + * + * @example + * var bool = ns.isComposite( 3.14 ); + * // returns false + * + * @example + * var bool = ns.isComposite( -4.0 ); + * // returns false + * + * @example + * var bool = ns.isComposite( null ); + * // returns false + * + * @example + * var bool = ns.isComposite.isPrimitive( 4.0 ); + * // returns true + * + * @example + * var bool = ns.isComposite.isObject( new Number( 4.0 ) ); + * // returns true + */ + isComposite: typeof isComposite; + + /** + * Tests if an object's own property is configurable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is configurable + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': true, + * 'writable': true, + * 'value': true + * }); + * + * var bool = ns.isConfigurableProperty( obj, 'boop' ); + * // returns true + * + * bool = ns.isConfigurableProperty( obj, 'beep' ); + * // returns false + */ + isConfigurableProperty: typeof isConfigurableProperty; + + /** + * Tests if an object's own or inherited property is configurable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is configurable + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': true, + * 'writable': true, + * 'value': true + * }); + * + * var bool = ns.isConfigurablePropertyIn( obj, 'boop' ); + * // returns true + * + * bool = ns.isConfigurablePropertyIn( obj, 'beep' ); + * // returns false + */ + isConfigurablePropertyIn: typeof isConfigurablePropertyIn; + + /** + * Tests if a value is a cube number. + * + * @param value - value to test + * @returns boolean indicating whether value is a cube number + * + * @example + * var bool = ns.isCubeNumber( 8.0 ); + * // returns true + * + * @example + * var bool = ns.isCubeNumber( new Number( 8.0 ) ); + * // returns true + * + * @example + * var bool = ns.isCubeNumber( 3.14 ); + * // returns false + * + * @example + * var bool = ns.isCubeNumber( -5.0 ); + * // returns false + * + * @example + * var bool = ns.isCubeNumber( null ); + * // returns false + * + * @example + * var bool = ns.isCubeNumber.isPrimitive( 8.0 ); + * // returns true + * + * @example + * var bool = ns.isCubeNumber.isObject( new Number( 8.0 ) ); + * // returns true + */ + isCubeNumber: typeof isCubeNumber; + + /** + * Boolean indicating if the current process is running on Darwin. + * + * @example + * var PLATFORM = require( `@stdlib/os/platform` ); + * + * if ( ns.IS_DARWIN ) { + * console.log( 'Running on Darwin...' ); + * } else { + * console.log( 'Running on %s...', PLATFORM ); + * } + */ + IS_DARWIN: typeof IS_DARWIN; + + /** + * Tests if an object's own property has a data descriptor. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property has a data descriptor + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * function getter() { + * return 'beep'; + * } + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': false, + * 'get': getter + * }); + * + * var bool = ns.isDataProperty( obj, 'boop' ); + * // returns true + * + * bool = ns.isDataProperty( obj, 'beep' ); + * // returns false + */ + isDataProperty: typeof isDataProperty; + + /** + * Tests if an object's own or inherited property has a data descriptor. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property has a data descriptor + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * function getter() { + * return 'beep'; + * } + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': false, + * 'get': getter + * }); + * + * var bool = ns.isDataPropertyIn( obj, 'boop' ); + * // returns true + * + * bool = ns.isDataPropertyIn( obj, 'beep' ); + * // returns false + */ + isDataPropertyIn: typeof isDataPropertyIn; + + /** + * Tests if a value is a `DataView`. + * + * @param value - value to test + * @returns boolean indicating whether value is a DataView + * + * @example + * var ArrayBuffer = require( `@stdlib/array/buffer` ); + * var DataView = require( `@stdlib/array/dataview` ); + * + * var bool = ns.isDataView( new DataView( new ArrayBuffer( 10 ) ) ); + * // returns true + * + * @example + * var bool = ns.isDataView( [] ); + * // returns false + */ + isDataView: typeof isDataView; + + /** + * Tests if a value is a `Date` object. + * + * @param value - value to test + * @returns boolean indicating if a value is a `Date` object + * + * @example + * var bool = ns.isDateObject( new Date() ); + * // returns true + * + * @example + * var bool = ns.isDateObject( '2017-01-01' ); + * // returns false + */ + isDateObject: typeof isDateObject; + + /** + * Tests whether a string contains only numeric digits. + * + * @param value - value to test + * @returns boolean indicating if a string contains only numeric digits + * + * @example + * var out = ns.isDigitString( '0123456789' ); + * // returns true + * + * @example + * var out = ns.isDigitString( '0xffffff' ); + * // returns false + * + * @example + * var out = ns.isDigitString( '' ); + * // returns false + * + * @example + * var out = ns.isDigitString( 123 ); + * // returns false + */ + isDigitString: typeof isDigitString; + + /** + * Boolean indicating if the runtime is Electron. + * + * @example + * var bool = ns.IS_ELECTRON; + * // returns + */ + IS_ELECTRON: typeof IS_ELECTRON; + + /** + * Boolean indicating if the runtime is the main Electron process. + * + * @example + * var bool = ns.IS_ELECTRON_MAIN; + * // returns + */ + IS_ELECTRON_MAIN: typeof IS_ELECTRON_MAIN; + + /** + * Boolean indicating if the runtime is the Electron renderer process. + * + * @example + * var bool = ns.IS_ELECTRON_RENDERER; + * // returns + */ + IS_ELECTRON_RENDERER: typeof IS_ELECTRON_RENDERER; + + /** + * Tests if a value is a valid(ish) email address. + * + * @param value - value to test + * @returns boolean indicating whether a value is a valid(ish) email address + * + * @example + * var bool = ns.isEmailAddress( 'beep@boop.com' ); + * // returns true + * + * @example + * var bool = ns.isEmailAddress( 'beep' ); + * // returns false + * + * @example + * var bool = ns.isEmailAddress( 'beep.com' ); + * // returns false + * + * @example + * var bool = ns.isEmailAddress( null ); + * // returns false + */ + isEmailAddress: typeof isEmailAddress; + + /** + * Tests if a value is an empty array. + * + * @param value - value to test + * @returns boolean indicating whether value is an empty array + * + * @example + * var bool = ns.isEmptyArray( [] ); + * // returns true + * + * @example + * var bool = ns.isEmptyArray( [ 1, 2, 3 ] ); + * // returns false + * + * @example + * var bool = ns.isEmptyArray( {} ); + * // returns false + */ + isEmptyArray: typeof isEmptyArray; + + /** + * Tests if a value is an empty object. + * + * @param value - value to test + * @returns boolean indicating whether value is an empty object + * + * @example + * var bool = ns.isEmptyObject( {} ); + * // returns true + * + * @example + * var bool = ns.isEmptyObject( { 'beep': 'boop' } ); + * // returns false + * + * @example + * var bool = ns.isEmptyObject( [] ); + * // returns false + */ + isEmptyObject: typeof isEmptyObject; + + /** + * Tests if a value is an empty string. + * + * @param value - value to test + * @returns boolean indicating whether value is an empty string + * + * @example + * var bool = ns.isEmptyString( '' ); + * // returns true + * + * @example + * var bool = ns.isEmptyString( 'beep' ); + * // returns false + * + * @example + * var bool = ns.isEmptyString( [] ); + * // returns false + * + * @example + * var bool = ns.isEmptyString.isPrimitive( '' ); + * // returns true + * + * @example + * var bool = ns.isEmptyString.isObject( '' ); + * // returns false + */ + isEmptyString: typeof isEmptyString; + + /** + * Tests if an object's own property is enumerable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is enumerable + * + * @example + * var beep = { + * 'boop': true + * }; + * + * var bool = ns.isEnumerableProperty( beep, 'boop' ); + * // returns true + * + * @example + * var beep = { + * 'boop': true + * }; + * + * var bool = ns.isEnumerableProperty( beep, 'hasOwnProperty' ); + * // returns false + */ + isEnumerableProperty: typeof isEnumerableProperty; + + /** + * Tests if an object's own or inherited property is enumerable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is enumerable + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * defineProperty( obj, 'beep', { + * 'configurable': true, + * 'enumerable': false, + * 'writable': true, + * 'value': true + * }); + * + * var bool = ns.isEnumerablePropertyIn( obj, 'boop' ); + * // returns true + * + * bool = ns.isEnumerablePropertyIn( obj, 'beep' ); + * // returns false + */ + isEnumerablePropertyIn: typeof isEnumerablePropertyIn; + + /** + * Tests if a value is an `Error` object. + * + * @param value - value to test + * @returns boolean indicating whether a value is an `Error` object + * + * @example + * var bool = ns.isError( new Error( 'beep' ) ); + * // returns true + * + * @example + * var bool = ns.isError( {} ); + * // returns false + */ + isError: typeof isError; + + /** + * Tests if a value is an `EvalError` object. + * + * ## Notes + * + * - This function should **not** be considered robust. While the function should always return `true` if provided an EvalError (or a descendant) object, false positives may occur due to the fact that the EvalError constructor inherits from Error and has no internal class of its own. Hence, EvalError impersonation is possible. + * + * @param value - value to test + * @returns boolean indicating whether a value is an `EvalError` object + * + * @example + * var bool = ns.isEvalError( new EvalError( 'beep' ) ); + * // returns true + * + * @example + * var bool = ns.isEvalError( {} ); + * // returns false + */ + isEvalError: typeof isEvalError; + + /** + * Tests if a value is an even number. + * + * @param value - value to test + * @returns boolean indicating whether value is an even number + * + * @example + * var bool = ns.isEven( 4.0 ); + * // returns true + * + * @example + * var bool = ns.isEven( new Number( 4.0 ) ); + * // returns true + * + * @example + * var bool = ns.isEven( 3.0 ); + * // returns false + * + * @example + * var bool = ns.isEven.isPrimitive( new Number( 4.0 ) ); + * // returns false + * + * @example + * var bool = ns.isEven.isObject( 4.0 ); + * // returns false + * + * @example + * var bool = ns.isEven( null ); + * // returns false + */ + isEven: typeof isEven; + + /** + * Tests if a value is falsy. + * + * @param value - value to test + * @returns boolean indicating whether a value is falsy + * + * @example + * var bool = ns.isFalsy( false ); + * // returns true + * + * @example + * var bool = ns.isFalsy( null ); + * // returns true + * + * @example + * var bool = ns.isFalsy( '' ); + * // returns true + * + * @example + * var bool = ns.isFalsy( 0 ); + * // returns true + * + * @example + * var bool = ns.isFalsy( void 0 ); + * // returns true + * + * @example + * var bool = ns.isFalsy( NaN ); + * // returns true + * + * @example + * var bool = ns.isFalsy( [] ); + * // returns false + */ + isFalsy: typeof isFalsy; + + /** + * Test if a value is an array-like object containing only falsy values. + * + * @param value - value to test + * @returns boolean indicating whether a value is an array-like object containing only falsy values + * + * @example + * var bool = ns.isFalsyArray( [ false, null, void 0, '', 0, NaN ] ); + * // returns true + * + * bool = ns.isFalsyArray( [ {}, [] ] ); + * // returns false + * + * bool = ns.isFalsyArray( [] ); + * // returns false + */ + isFalsyArray: typeof isFalsyArray; + + /** + * Tests if a value is a finite number. + * + * @param value - value to test + * @returns boolean indicating whether a value is a finite number + * + * @example + * var bool = ns.isFinite( 5.0 ); + * // returns true + * + * @example + * var bool = ns.isFinite( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = ns.isFinite( 1.0/0.0 ); + * // returns false + * + * @example + * var bool = ns.isFinite( null ); + * // returns false + * + * @example + * var bool = ns.isFinite.isPrimitive( new Number( -3.0 ) ); + * // returns false + * + * @example + * var bool = ns.isFinite.isObject( new Number( 3.0 ) ); + * // returns true + */ + isFinite: typeof isFinite; + + /** + * Tests if a value is an array-like object of finite numbers. + * + * @param value - value to test + * @returns boolean indicating whether a value is an array-like object of finite numbers + * + * @example + * var bool = ns.isFiniteArray( [ -3.0, new Number(0.0), 2.0 ] ); + * // returns true + * + * @example + * var bool = ns.isFiniteArray( [ -3.0, 1.0/0.0 ] ); + * // returns false + * + * @example + * var bool = ns.isFiniteArray.primitives( [ -1.0, 10.0 ] ); + * // returns true + * + * @example + * var bool = ns.isFiniteArray.primitives( [ -1.5, 0.0, 5.0 ] ); + * // returns true + * + * @example + * var bool = ns.isFiniteArray.primitives( [ -3.0, new Number(-1.0) ] ); + * // returns false + * + * @example + * var bool = ns.isFiniteArray.objects( [ new Number(1.0), new Number(3.0) ] ); + * // returns true + * + * @example + * var bool = ns.isFiniteArray.objects( [ -1.0, 0.0, 3.0 ] ); + * // returns false + * + * @example + * var bool = ns.isFiniteArray.objects( [ 3.0, new Number(-1.0) ] ); + * // returns false + */ + isFiniteArray: typeof isFiniteArray; + + /** + * Tests if a value is a Float32Array. + * + * @param value - value to test + * @returns boolean indicating whether value is a Float32Array + * + * @example + * var bool = ns.isFloat32Array( new Float32Array( 10 ) ); + * // returns true + * + * @example + * var bool = ns.isFloat32Array( [] ); + * // returns false + */ + isFloat32Array: typeof isFloat32Array; + + /** + * Tests if a value is a 2-dimensional ndarray-like object whose underlying data type is `float32`. + * + * @param v - value to test + * @returns boolean indicating if a value is a 2-dimensional ndarray-like object whose underlying data type is `float32` + * + * @example + * var Float32Array = require( `@stdlib/array/float32` ); + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * + * var bool = ns.isFloat32MatrixLike( arr ); + * // returns true + * + * bool = ns.isFloat32MatrixLike( [] ); + * // returns false + */ + isFloat32MatrixLike: typeof isFloat32MatrixLike; + + /** + * Tests if a value is an ndarray-like object whose underlying data type is `float32`. + * + * @param v - value to test + * @returns boolean indicating if a value is an ndarray-like object whose underlying data type is `float32` + * + * @example + * var Float32Array = require( `@stdlib/array/float32` ); + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * + * var bool = ns.isFloat32ndarrayLike( arr ); + * // returns true + * + * bool = ns.isFloat32ndarrayLike( [] ); + * // returns false + */ + isFloat32ndarrayLike: typeof isFloat32ndarrayLike; + + /** + * Tests if a value is a 1-dimensional ndarray-like object whose underlying data type is `float32`. + * + * @param v - value to test + * @returns boolean indicating if a value is a 1-dimensional ndarray-like object whose underlying data type is `float32` + * + * @example + * var Float32Array = require( `@stdlib/array/float32` ); + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + * + * var bool = ns.isFloat32VectorLike( arr ); + * // returns true + * + * bool = ns.isFloat32VectorLike( [] ); + * // returns false + */ + isFloat32VectorLike: typeof isFloat32VectorLike; + + /** + * Tests if a value is a Float64Array. + * + * @param value - value to test + * @returns boolean indicating whether value is a Float64Array + * + * @example + * var Float64Array = require( `@stdlib/array/float64` ); + * + * var bool = ns.isFloat64Array( new Float64Array( 10 ) ); + * // returns true + * + * @example + * var bool = ns.isFloat64Array( [] ); + * // returns false + */ + isFloat64Array: typeof isFloat64Array; + + /** + * Tests if a value is a 2-dimensional ndarray-like object whose underlying data type is `float64`. + * + * @param v - value to test + * @returns boolean indicating if a value is a 2-dimensional ndarray-like object whose underlying data type is `float64` + * + * @example + * var Float64Array = require( `@stdlib/array/float64` ); + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'float64', new Float64Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * + * var bool = ns.isFloat64MatrixLike( arr ); + * // returns true + * + * bool = ns.isFloat64MatrixLike( [] ); + * // returns false + */ + isFloat64MatrixLike: typeof isFloat64MatrixLike; + + /** + * Tests if a value is an ndarray-like object whose underlying data type is `float64`. + * + * @param v - value to test + * @returns boolean indicating if a value is an ndarray-like object whose underlying data type is `float64` + * + * @example + * var Float64Array = require( `@stdlib/array/float64` ); + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'float64', new Float64Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * + * var bool = ns.isFloat64ndarrayLike( arr ); + * // returns true + * + * bool = ns.isFloat64ndarrayLike( [] ); + * // returns false + */ + isFloat64ndarrayLike: typeof isFloat64ndarrayLike; + + /** + * Tests if a value is a 1-dimensional ndarray-like object whose underlying data type is `float64`. + * + * @param v - value to test + * @returns boolean indicating if a value is a 1-dimensional ndarray-like object whose underlying data type is `float64` + * + * @example + * var Float64Array = require( `@stdlib/array/float64` ); + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'float64', new Float64Array( [ 0, 0, 0, 0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + * + * var bool = ns.isFloat64VectorLike( arr ); + * // returns true + * + * bool = ns.isFloat64VectorLike( [] ); + * // returns false + */ + isFloat64VectorLike: typeof isFloat64VectorLike; + + /** + * Tests if a value is a function. + * + * @param value - value to test + * @returns boolean indicating whether value is a function + * + * @example + * function beep() { + * return 'beep'; + * } + * + * var bool = ns.isFunction( beep ); + * // returns true + */ + isFunction: typeof isFunction; + + /** + * Test if a value is an array-like object containing only functions. + * + * @param value - value to test + * @returns boolean indicating whether an input value is an array-like object containing only functions + * + * @example + * function beep() {} + * + * function boop() {} + * + * var bool = ns.isFunctionArray( [ beep, boop ] ); + * // returns true + * + * bool = ns.isFunctionArray( [ {}, beep ] ); + * // returns false + * + * bool = ns.isFunctionArray( [] ); + * // returns false + */ + isFunctionArray: typeof isFunctionArray; + + /** + * Tests if a value is a generator object. + * + * @param value - value to test + * @returns boolean indicating whether value is a generator object + * + * @example + * function* generateID() { + * var idx = 0; + * while ( idx < idx+1 ) { + * yield idx; + * idx += 1; + * } + * } + * var bool = ns.isGeneratorObject( generateID() ); + * // returns true + * + * @example + * var bool = ns.isGeneratorObject( {} ); + * // returns false + * + * @example + * var bool = ns.isGeneratorObject( null ); + * // returns false + */ + isGeneratorObject: typeof isGeneratorObject; + + /** + * Tests if a value is generator object-like. + * + * @param value - value to test + * @returns boolean indicating whether value is generator object-like + * + * @example + * var gen = { + * 'next': function noop() {}, + * 'return': function noop() {}, + * 'throw': function noop() {} + * }; + * var bool = ns.isGeneratorObjectLike( gen ); + * // returns true + * + * @example + * function* generateID() { + * var idx = 0; + * while ( idx < idx+1 ) { + * yield idx; + * idx += 1; + * } + * } + * var bool = ns.isGeneratorObjectLike( generateID() ); + * // returns true + * + * @example + * var bool = ns.isGeneratorObjectLike( {} ); + * // returns false + * + * @example + * var bool = ns.isGeneratorObjectLike( null ); + * // returns false + */ + isGeneratorObjectLike: typeof isGeneratorObjectLike; + + /** + * Tests if a value is a gzip buffer (or Uint8Array). + * + * ## Notes + * + * - A gzip buffer is defined as either a Node.js Buffer or Uint8Array which contains a 10-byte header, a body containing the compressed payload, and an 8-byte footer containing a CRC-32 checksum and the length of the original uncompressed data, modulo 2^32. + * - This function only examines the 10-byte header to ensure the header includes the expected magic number and compression method. The function does not perform an integrity check. + * + * @param value - value to test + * @returns boolean indicating whether a value is a gzip buffer + * + * @example + * var Uint8Array = require( `@stdlib/array/uint8` ); + * + * var buf = new Uint8Array( 20 ); + * buf[ 0 ] = 31; // 0x1f => magic number + * buf[ 1 ] = 139; // 0x8b + * buf[ 2 ] = 8; // 0x08 => compression method + * + * var bool = ns.isgzipBuffer( buf ); + * // returns true + * + * @example + * var bool = ns.isgzipBuffer( [] ); + * // returns false + */ + isgzipBuffer: typeof isgzipBuffer; + + /** + * Tests whether a string contains only hexadecimal digits. + * + * @param x - value to test + * @returns boolean indicating if a string contains only hexadecimal digits + * + * @example + * var out = ns.isHexString( '0123456789abcdefABCDEF' ); + * // returns true + * + * @example + * var out = ns.isHexString( '0xffffff' ); + * // returns false + * + * @example + * var out = ns.isHexString( '' ); + * // returns false + * + * @example + * var out = ns.isHexString( 123 ); + * // returns false + */ + isHexString: typeof isHexString; + + /** + * Tests if a value is an infinite number. + * + * @param value - value to test + * @returns boolean indicating whether a value is an infinite number + * + * @example + * var bool = ns.isInfinite( 1.0/0.0 ); + * // returns true + * + * @example + * var bool = ns.isInfinite( new Number( 1.0/0.0 ) ); + * // returns true + * + * @example + * var bool = ns.isInfinite( 5.0 ); + * // returns false + * + * @example + * var bool = ns.isInfinite( null ); + * // returns false + */ + isInfinite: typeof isInfinite; + + /** + * Tests if an object has an inherited property. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object has an inherited property + * + * @example + * var obj = { + * 'boop': true + * }; + * + * var bool = ns.isInheritedProperty( obj, 'toString' ); + * // returns true + * + * @example + * var obj = { + * 'boop': true + * }; + * + * var bool = ns.isInheritedProperty( obj, 'boop' ); + * // returns false + * + * @example + * var obj = { + * 'boop': true + * }; + * + * var bool = ns.isInheritedProperty( obj, 'bap' ); + * // returns false + */ + isInheritedProperty: typeof isInheritedProperty; + + /** + * Tests if a value is an Int8Array. + * + * @param value - value to test + * @returns boolean indicating whether value is an Int8Array + * + * @example + * var bool = ns.isInt8Array( new Int8Array( 10 ) ); + * // returns true + * + * @example + * var bool = ns.isInt8Array( [] ); + * // returns false + */ + isInt8Array: typeof isInt8Array; + + /** + * Tests if a value is an Int16Array. + * + * @param value - value to test + * @returns boolean indicating whether value is an Int16Array + * + * @example + * var bool = ns.isInt16Array( new Int16Array( 10 ) ); + * // returns true + * + * @example + * var bool = ns.isInt16Array( [] ); + * // returns false + */ + isInt16Array: typeof isInt16Array; + + /** + * Tests if a value is an Int32Array. + * + * @param value - value to test + * @returns boolean indicating whether value is an Int32Array + * + * @example + * var bool = ns.isInt32Array( new Int32Array( 10 ) ); + * // returns true + * + * @example + * var bool = ns.isInt32Array( [] ); + * // returns false + */ + isInt32Array: typeof isInt32Array; + + /** + * Tests if a value is an integer. + * + * @param value - value to test + * @returns boolean indicating whether value is an integer + * + * @example + * var bool = ns.isInteger( 5.0 ); + * // returns true + * + * @example + * var bool = ns.isInteger( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = ns.isInteger( -3.14 ); + * // returns false + * + * @example + * var bool = ns.isInteger( null ); + * // returns false + * + * @example + * var bool = ns.isInteger.isPrimitive( -3.0 ); + * // returns true + * + * @example + * var bool = ns.isInteger.isObject( new Number( 3.0 ) ); + * // returns true + */ + isInteger: typeof isInteger; + + /** + * Tests if a value is an array-like object containing only integers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only integers + * + * @example + * var bool = ns.isIntegerArray( [ -3.0, new Number(0.0), 2.0 ] ); + * // returns true + * + * @example + * var bool = ns.isIntegerArray( [ -3.0, '3.0' ] ); + * // returns false + * @example + * var bool = ns.isIntegerArray.primitives( [ -1.0, 0.0, 5.0 ] ); + * // returns true + * + * @example + * var bool = ns.isIntegerArray.objects( [ new Number(1.0), new Number(3.0) ] ); + * // returns true + */ + isIntegerArray: typeof isIntegerArray; + + /** + * Tests if a value is iterable-like. + * + * ## Notes + * + * - In order to be iterable, an object must implement the @@iterator method, which, when called, returns an iterator protocol-compliant object. + * - An iterator protocol-compliant object is an object having a `next` method which adheres to the iterator protocol. + * - As full iterator compliance is impossible to achieve without evaluating an iterator, this function checks *only* for interface compliance. + * - In environments lacking Symbol.iterator support, this function always returns `false`. + * + * @param value - value to test + * @returns boolean indicating whether a value is iterable-like + * + * @example + * var bool = ns.isIterableLike( [] ); + * // returns + * + * @example + * var bool = ns.isIterableLike( {} ); + * // returns false + * + * @example + * var bool = ns.isIterableLike( null ); + * // returns false + */ + isIterableLike: typeof isIterableLike; + + /** + * Tests if a value is iterator-like. + * + * @param value - value to test + * @returns boolean indicating whether value is iterator-like + * + * @example + * var it = { + * 'next': function noop() {} + * }; + * var bool = ns.isIteratorLike( it ); + * // returns true + * + * @example + * var bool = ns.isIteratorLike( {} ); + * // returns false + * + * @example + * var bool = ns.isIteratorLike( null ); + * // returns false + */ + isIteratorLike: typeof isIteratorLike; + + /** + * Tests if a value is a parseable JSON string. + * + * @param value - value to test + * @returns boolean indicating if a value is a parseable JSON string + * + * @example + * var v = ns.isJSON( '{"a":5}' ); + * // returns true + * + * @example + * var v = ns.isJSON( '{a":5}' ); + * // returns false + */ + isJSON: typeof isJSON; + + /** + * Tests whether a value corresponds to a leap year in the Gregorian calendar. + * + * ## Notes + * + * - According to the Gregorian calendar, every year that is exactly divisible by `4` is a leap year, except those years which are also divisible by `100` and not by `400` (e.g., `1900`). + * + * @param value - input value + * @returns boolean whether a value corresponds to a leap year + * + * @example + * var bool = ns.isLeapYear(); + * // returns + * + * @example + * var bool = ns.isLeapYear( new Date() ); + * // returns + * + * @example + * var bool = ns.isLeapYear( 1996 ); + * // returns true + * + * @example + * var bool = ns.isLeapYear( 2001 ); + * // returns false + */ + isLeapYear: typeof isLeapYear; + + /** + * Return a boolean indicating if an environment is little endian. + * + * @example + * var bool = ns.IS_LITTLE_ENDIAN; + * // returns + */ + IS_LITTLE_ENDIAN: typeof IS_LITTLE_ENDIAN; + + /** + * Tests if a value is a lowercase string. + * + * @param value - value to test + * @returns boolean indicating whether value is a lowercase string + * + * @example + * var bool = ns.isLowercase( 'salt and light' ); + * // returns true + * + * @example + * var bool = ns.isLowercase( 'HELLO' ); + * // returns false + * + * @example + * var bool = ns.isLowercase( 'World' ); + * // returns false + * + * @example + * var bool = ns.isLowercase( '!' ); + * // returns false + */ + isLowercase: typeof isLowercase; + + /** + * Tests if a value is a 2-dimensional ndarray-like object. + * + * @param v - value to test + * @returns boolean indicating if a value is a 2-dimensional ndarray-like object + * + * @example + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * + * var bool = ns.isMatrixLike( arr ); + * // returns true + * + * bool = ns.isMatrixLike( [] ); + * // returns false + */ + isMatrixLike: typeof isMatrixLike; + + /** + * Tests if an object has a specified method name. + * + * ## Notes + * + * - Value arguments other than `null` or `undefined` are coerced to objects. + * - Property arguments are coerced to strings. + * - The function only searches own properties. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object has a specified method name + * + * @example + * var beep = { + * 'boop': ns.isMethod + * }; + * + * var bool = ns.isMethod( beep, 'boop' ); + * // returns true + * + * var bool = ns.isMethod( beep, 'toString' ); + * // returns false + */ + isMethod: typeof isMethod; + + /** + * Tests if an object has a specified method name, either own or inherited. + * + * ## Notes + * + * - Value arguments other than `null` or `undefined` are coerced to objects. + * - Non-symbol property arguments are coerced to strings. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object has a specified method name + * + * @example + * var beep = { + * 'boop': true + * }; + * + * var bool = ns.isMethodIn( beep, 'toString' ); + * // returns true + * + * bool = ns.isMethodIn( beep, 'boop' ); + * // returns false + */ + isMethodIn: typeof isMethodIn; + + /** + * Tests if a value is named typed tuple-like. + * + * @param v - value to test + * @returns boolean indicating if a value is named typed tuple-like + * + * @example + * var namedtypedtuple = require( `@stdlib/utils/named-typed-tuple` ); + * + * var Point = namedtypedtuple( [ 'x', 'y' ] ); + * + * var p = new Point(); + * + * var bool = ns.isNamedTypedTupleLike( p ); + * // returns true + * + * bool = ns.isNamedTypedTupleLike( [] ); + * // returns false + */ + isNamedTypedTupleLike: typeof isNamedTypedTupleLike; + + /** + * Tests if a value is `NaN`. + * + * @param value - value to test + * @returns boolean indicating whether value is `NaN` + * + * @example + * var bool = ns.isnan( NaN ); + * // returns true + * + * @example + * var bool = ns.isnan( new Number( NaN ) ); + * // returns true + * + * @example + * var bool = ns.isnan( 3.14 ); + * // returns false + * + * @example + * var bool = ns.isnan( null ); + * // returns false + * + * @example + * var bool = ns.isnan.isPrimitive( NaN ); + * // returns true + * + * @example + * var bool = ns.isnan.isObject( new Number( NaN ) ); + * // returns true + */ + isnan: typeof isnan; + + /** + * Tests if a value is an array-like object containing only NaN values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only NaN values + * + * @example + * var bool = ns.isNaNArray( [NaN,NaN,NaN] ); + * // returns true + * + * @example + * var bool = ns.isNaNArray( [NaN,2] ); + * // returns false + * + * @example + * var bool = ns.isNaNArray.primitives( [NaN,NaN,NaN] ); + * // returns true + * + * @example + * var bool = ns.isNaNArray.objects( [new Number( NaN ),new Number( NaN )] ); + * // returns true + */ + isNaNArray: typeof isNaNArray; + + /** + * Tests if a value is a native function. + * + * @param value - value to test + * @returns boolean indicating whether value is a native function + * + * @example + * function beep() { + * console.log( 'boop' ); + * } + * + * var bool = ns.isNativeFunction( beep ); + * // returns false + * + * bool = ns.isNativeFunction( Date ); + * // returns true + */ + isNativeFunction: typeof isNativeFunction; + + /** + * Tests if a value is ndarray-like. + * + * @param v - value to test + * @returns boolean indicating if a value is ndarray-like + * + * @example + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * + * var bool = ns.isndarrayLike( arr ); + * // returns true + * + * bool = ns.isndarrayLike( [] ); + * // returns false + */ + isndarrayLike: typeof isndarrayLike; + + /** + * Tests if a value is a negative integer. + * + * @param value - value to test + * @returns boolean indicating whether value is a negative integer + * + * @example + * var bool = ns.isNegativeInteger( -5.0 ); + * // returns true + * + * @example + * var bool = ns.isNegativeInteger( new Number( -5.0 ) ); + * // returns true + * + * @example + * var bool = ns.isNegativeInteger( 5.0 ); + * // returns false + * + * @example + * var bool = ns.isNegativeInteger( -3.14 ); + * // returns false + * + * @example + * var bool = ns.isNegativeInteger( null ); + * // returns false + * + * @example + * var bool = ns.isNegativeInteger.isPrimitive( -3.0 ); + * // returns true + * + * @example + * var bool = ns.isNegativeInteger.isObject( new Number( -3.0 ) ); + * // returns true + */ + isNegativeInteger: typeof isNegativeInteger; + + /** + * Tests if a value is an array-like object containing only negative integers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only negative integers + * + * @example + * var bool = ns.isNegativeIntegerArray( [ -3.0, new Number(-3.0) ] ); + * // returns true + * + * @example + * var bool = ns.isNegativeIntegerArray( [ -3.0, '-3.0' ] ); + * // returns false + * + * @example + * var bool = ns.isNegativeIntegerArray.primitives( [ -1.0, -10.0 ] ); + * // returns true + * + * @example + * var bool = ns.isNegativeIntegerArray.objects( [ new Number(-1.0), new Number(-10.0) ] ); + * // returns true + */ + isNegativeIntegerArray: typeof isNegativeIntegerArray; + + /** + * Tests if a value is a negative number. + * + * @param value - value to test + * @returns boolean indicating whether value is a negative number + * + * @example + * var bool = ns.isNegativeNumber( -5.0 ); + * // returns true + * + * @example + * var bool = ns.isNegativeNumber( new Number( -5.0 ) ); + * // returns true + * + * @example + * var bool = ns.isNegativeNumber( -3.14 ); + * // returns true + * + * @example + * var bool = ns.isNegativeNumber( 5.0 ); + * // returns false + * + * @example + * var bool = ns.isNegativeNumber( null ); + * // returns false + * + * @example + * var bool = ns.isNegativeNumber.isPrimitive( -3.0 ); + * // returns true + * + * @example + * var bool = ns.isNegativeNumber.isObject( new Number( -3.0 ) ); + * // returns true + */ + isNegativeNumber: typeof isNegativeNumber; + + /** + * Tests if a value is an array-like object containing only negative numbers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only negative numbers + * + * @example + * var bool = ns.isNegativeNumberArray( [ -3.7, new Number(-3.0) ] ); + * // returns true + * + * @example + * var bool = ns.isNegativeNumberArray( [ -3.7, '-3.0' ] ); + * // returns false + * + * @example + * var bool = ns.isNegativeNumberArray.primitives( [ -1.3, -10.0 ] ); + * // returns true + * + * @example + * var bool = ns.isNegativeNumberArray.objects( [ new Number(-1.3), new Number(-10.0) ] ); + * // returns true + */ + isNegativeNumberArray: typeof isNegativeNumberArray; + + /** + * Tests if a value is equal to negative zero. + * + * @param value - value to test + * @returns boolean indicating whether value is equal to negative zero + * + * @example + * var bool = ns.isNegativeZero( -0.0 ); + * // returns true + * + * @example + * var bool = ns.isNegativeZero( new Number( -0.0 ) ); + * // returns true + * + * @example + * var bool = ns.isNegativeZero( -3.14 ); + * // returns false + * + * @example + * var bool = ns.isNegativeZero( 5.0 ); + * // returns false + * + * @example + * var bool = ns.isNegativeZero( 0.0 ); + * // returns false + * + * @example + * var bool = ns.isNegativeZero( null ); + * // returns false + * + * @example + * var bool = ns.isNegativeZero.isPrimitive( -0.0 ); + * // returns true + * + * @example + * var bool = ns.isNegativeZero.isObject( new Number( -0.0 ) ); + * // returns true + */ + isNegativeZero: typeof isNegativeZero; + + /** + * Boolean indicating if the runtime is Node.js. + * + * @example + * var bool = ns.IS_NODE; + * // returns + */ + IS_NODE: typeof IS_NODE; + + /** + * Tests whether a string matches a Node.js built-in module name. + * + * @param x - value to test + * @returns boolean indicating whether a string matches a Node.js built-in module name + * + * @example + * var out = ns.isNodeBuiltin( 'cluster' ); + * // returns true + * + * @example + * var out = ns.isNodeBuiltin( 'crypto' ); + * // returns true + * + * @example + * var out = ns.isNodeBuiltin( 'fs-extra' ); + * // returns false + * + * @example + * var out = ns.isNodeBuiltin( '' ); + * // returns false + */ + isNodeBuiltin: typeof isNodeBuiltin; + + /** + * Tests if a value is Node duplex stream-like. + * + * @param value - value to test + * @returns boolean indicating if a value is Node duplex stream-like + * + * @example + * var transformStream = require( `@stdlib/streams/node/transform` ); + * + * var stream = transformStream(); + * + * var bool = ns.isNodeDuplexStreamLike( stream ); + * // returns true + * + * bool = ns.isNodeDuplexStreamLike( {} ); + * // returns false + */ + isNodeDuplexStreamLike: typeof isNodeDuplexStreamLike; + + /** + * Tests if a value is Node readable stream-like. + * + * @param value - value to test + * @returns boolean indicating if a value is Node readable stream-like + * + * @example + * var transformStream = require( `@stdlib/streams/node/transform` ); + * + * var stream = transformStream(); + * + * var bool = ns.isNodeReadableStreamLike( stream ); + * // returns true + * + * bool = ns.isNodeReadableStreamLike( {} ); + * // returns false + */ + isNodeReadableStreamLike: typeof isNodeReadableStreamLike; + + /** + * Returns a boolean indicating if the function is called from a Node.js REPL environment. + * + * ## Notes + * + * - False positives are possible due to the existence of a userland package having the same module `id` (see [repl][repl-template-lib]) as the builtin Node.js `repl`. + * + * [repl-template-lib]: https://www.npmjs.com/package/repl + * + * @returns boolean indicating if the function is called from a Node.js REPL environment + * + * @example + * var bool = ns.isNodeREPL(); + * // returns + */ + isNodeREPL: typeof isNodeREPL; + + /** + * Tests if a value is Node stream-like. + * + * @param value - value to test + * @returns boolean indicating if a value is Node stream-like + * + * @example + * var transformStream = require( `@stdlib/streams/node/transform` ); + * + * var stream = transformStream(); + * + * var bool = ns.isNodeStreamLike( stream ); + * // returns true + * + * bool = ns.isNodeStreamLike( {} ); + * // returns false + */ + isNodeStreamLike: typeof isNodeStreamLike; + + /** + * Tests if a value is Node transform stream-like. + * + * @param value - value to test + * @returns boolean indicating if a value is Node transform stream-like + * + * @example + * var transformStream = require( `@stdlib/streams/node/transform` ); + * + * var stream = transformStream(); + * + * var bool = ns.isNodeTransformStreamLike( stream ); + * // returns true + * + * bool = ns.isNodeTransformStreamLike( {} ); + * // returns false + */ + isNodeTransformStreamLike: typeof isNodeTransformStreamLike; + + /** + * Tests if a value is Node writable stream-like. + * + * @param value - value to test + * @returns boolean indicating if a value is Node writable stream-like + * + * @example + * var transformStream = require( `@stdlib/streams/node/transform` ); + * + * var stream = transformStream(); + * + * var bool = ns.isNodeWritableStreamLike( stream ); + * // returns true + * + * bool = ns.isNodeWritableStreamLike( {} ); + * // returns false + */ + isNodeWritableStreamLike: typeof isNodeWritableStreamLike; + + /** + * Tests if an object's own property is non-configurable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is non-configurable + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': true, + * 'writable': true, + * 'value': 'beep' + * }); + * + * var bool = ns.isNonConfigurableProperty( obj, 'boop' ); + * // returns false + * + * bool = ns.isNonConfigurableProperty( obj, 'beep' ); + * // returns true + */ + isNonConfigurableProperty: typeof isNonConfigurableProperty; + + /** + * Tests if an object's own or inherited property is non-configurable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is non-configurable + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': true, + * 'writable': true, + * 'value': true + * }); + * + * var bool = ns.isNonConfigurablePropertyIn( obj, 'boop' ); + * // returns false + * + * bool = ns.isNonConfigurablePropertyIn( obj, 'beep' ); + * // returns true + */ + isNonConfigurablePropertyIn: typeof isNonConfigurablePropertyIn; + + /** + * Tests if an object's own property is non-enumerable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is non-enumerable + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': false, + * 'writable': true, + * 'value': 'beep' + * }); + * + * var bool = ns.isNonEnumerableProperty( obj, 'boop' ); + * // returns false + * + * bool = ns.isNonEnumerableProperty( obj, 'beep' ); + * // returns true + */ + isNonEnumerableProperty: typeof isNonEnumerableProperty; + + /** + * Tests if an object's own or inherited property is non-enumerable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is non-enumerable + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * defineProperty( obj, 'beep', { + * 'configurable': true, + * 'enumerable': false, + * 'writable': true, + * 'value': true + * }); + * + * var bool = ns.isNonEnumerablePropertyIn( obj, 'boop' ); + * // returns false + * + * bool = ns.isNonEnumerablePropertyIn( obj, 'beep' ); + * // returns true + */ + isNonEnumerablePropertyIn: typeof isNonEnumerablePropertyIn; + + /** + * Tests if a value is a nonnegative integer. + * + * @param value - value to test + * @returns boolean indicating whether value is a nonnegative integer + * + * @example + * var bool = ns.isNonNegativeInteger( 5.0 ); + * // returns true + * + * @example + * var bool = ns.isNonNegativeInteger( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = ns.isNonNegativeInteger( -5.0 ); + * // returns false + * + * @example + * var bool = ns.isNonNegativeInteger( 3.14 ); + * // returns false + * + * @example + * var bool = ns.isNonNegativeInteger( null ); + * // returns false + * + * @example + * var bool = ns.isNonNegativeInteger.isPrimitive( 3.0 ); + * // returns true + * + * @example + * var bool = ns.isNonNegativeInteger.isObject( new Number( 3.0 ) ); + * // returns true + */ + isNonNegativeInteger: typeof isNonNegativeInteger; + + /** + * Tests if a value is an array-like object containing only nonnegative integers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only nonnegative integers + * + * @example + * var bool = ns.isNonNegativeIntegerArray( [ 3.0, new Number(3.0) ] ); + * // returns true + * + * @example + * var bool = ns.isNonNegativeIntegerArray( [ 3.0, '3.0' ] ); + * // returns false + * + * @example + * var bool = ns.isNonNegativeIntegerArray.primitives( [ 1.0, 0.0, 10.0 ] ); + * // returns true + * + * @example + * var bool = ns.isNonNegativeIntegerArray.objects( [ new Number(3.0), new Number(1.0) ] ); + * // returns true + */ + isNonNegativeIntegerArray: typeof isNonNegativeIntegerArray; + + /** + * Tests if a value is a nonnegative number. + * + * @param value - value to test + * @returns boolean indicating whether value is a nonnegative number + * + * @example + * var bool = ns.isNonNegativeNumber( 5.0 ); + * // returns true + * + * @example + * var bool = ns.isNonNegativeNumber( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = ns.isNonNegativeNumber( 3.14 ); + * // returns true + * + * @example + * var bool = ns.isNonNegativeNumber( -5.0 ); + * // returns false + * + * @example + * var bool = ns.isNonNegativeNumber( null ); + * // returns false + * + * @example + * var bool = ns.isNonNegativeNumber.isPrimitive( 3.0 ); + * // returns true + * + * @example + * var bool = ns.isNonNegativeNumber.isObject( new Number( 3.0 ) ); + * // returns true + */ + isNonNegativeNumber: typeof isNonNegativeNumber; + + /** + * Tests if a value is an array-like object containing only nonnegative numbers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only nonnegative numbers + * + * @example + * var bool = ns.isNonNegativeNumberArray( [ 3.0, new Number(3.0) ] ); + * // returns true + * + * @example + * var bool = ns.isNonNegativeNumberArray( [ 3.0, '3.0' ] ); + * // returns false + * + * @example + * var bool = ns.isNonNegativeNumberArray.primitives( [ 1.0, 0.0, 10.0 ] ); + * // returns true + * + * @example + * var bool = ns.isNonNegativeNumberArray.objects( [ new Number(3.0), new Number(1.0) ] ); + * // returns true + */ + isNonNegativeNumberArray: typeof isNonNegativeNumberArray; + + /** + * Tests if a value is a nonpositive integer. + * + * @param value - value to test + * @returns boolean indicating whether value is a nonpositive integer + * + * @example + * var bool = ns.isNonPositiveInteger( -5.0 ); + * // returns true + * + * @example + * var bool = ns.isNonPositiveInteger( new Number( -5.0 ) ); + * // returns true + * + * @example + * var bool = ns.isNonPositiveInteger( 5.0 ); + * // returns false + * + * @example + * var bool = ns.isNonPositiveInteger( -3.14 ); + * // returns false + * + * @example + * var bool = ns.isNonPositiveInteger( null ); + * // returns false + * + * @example + * var bool = ns.isNonPositiveInteger.isPrimitive( -3.0 ); + * // returns true + * + * @example + * var bool = ns.isNonPositiveInteger.isObject( new Number( -3.0 ) ); + * // returns true + */ + isNonPositiveInteger: typeof isNonPositiveInteger; + + /** + * Tests if a value is an array-like object containing only nonpositive integers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only nonpositive integers + * + * @example + * var bool = ns.isNonPositiveIntegerArray( [ -3.0, new Number(-3.0) ] ); + * // returns true + * + * @example + * var bool = ns.isNonPositiveIntegerArray( [ -3.0, '-3.0' ] ); + * // returns false + * + * @example + * var bool = ns.isNonPositiveIntegerArray.primitives( [ -1.0, -10.0 ] ); + * // returns true + * + * @example + * var bool = ns.isNonPositiveIntegerArray.objects( [ new Number(-1.0), new Number(-10.0) ] ); + * // returns true + */ + isNonPositiveIntegerArray: typeof isNonPositiveIntegerArray; + + /** + * Tests if a value is a nonpositive number. + * + * @param value - value to test + * @returns boolean indicating whether value is a nonpositive number + * + * @example + * var bool = ns.isNonPositiveNumber( -5.0 ); + * // returns true + * + * @example + * var bool = ns.isNonPositiveNumber( new Number( -5.0 ) ); + * // returns true + * + * @example + * var bool = ns.isNonPositiveNumber( -3.14 ); + * // returns true + * + * @example + * var bool = ns.isNonPositiveNumber( 5.0 ); + * // returns false + * + * @example + * var bool = ns.isNonPositiveNumber( null ); + * // returns false + * + * @example + * var bool = ns.isNonPositiveNumber.isPrimitive( -3.0 ); + * // returns true + * + * @example + * var bool = ns.isNonPositiveNumber.isObject( new Number( -3.0 ) ); + * // returns true + */ + isNonPositiveNumber: typeof isNonPositiveNumber; + + /** + * Tests if a value is an array-like object containing only nonpositive numbers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only nonpositive numbers + * + * @example + * var bool = ns.isNonPositiveNumberArray( [ -3.0, new Number(-3.0) ] ); + * // returns true + * + * @example + * var bool = ns.isNonPositiveNumberArray( [ -3.0, '-3.0' ] ); + * // returns false + * + * @example + * var bool = ns.isNonPositiveNumberArray.primitives( [ -1.0, -10.0 ] ); + * // returns true + * + * @example + * var bool = ns.isNonPositiveNumberArray.objects( [ new Number(-1.0), new Number(-10.0) ] ); + * // returns true + */ + isNonPositiveNumberArray: typeof isNonPositiveNumberArray; + + /** + * Tests if a value is a non-symmetric matrix. + * + * ## Notes + * + * - The implementation must rely on manually checking that \\(M_{ij} \neq M_{ji}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. + * - Worst case scenario: O(N^2). + * + * @param v - value to test + * @returns boolean indicating if a value is a non-symmetric matrix + * + * @example + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * var bool = ns.isNonSymmetricMatrix( arr ); + * // returns true + * + * bool = ns.isNonSymmetricMatrix( [] ); + * // returns false + */ + isNonSymmetricMatrix: typeof isNonSymmetricMatrix; + + /** + * Tests if a value is `null`. + * + * @param value - value to test + * @returns boolean indicating whether value is null + * + * @example + * var bool = ns.isNull( null ); + * // returns true + * + * bool = ns.isNull( true ); + * // returns false + */ + isNull: typeof isNull; + + /** + * Tests if a value is an array-like object containing only `null` values. + * + * @param value - value to test + * @returns boolean indicating whether an input value is an array-like object containing only `null` values + * + * @example + * var bool = ns.isNullArray( [null,null,null] ); + * // returns true + * + * bool = ns.isNullArray( [NaN,2,null] ); + * // returns false + */ + isNullArray: typeof isNullArray; + + /** + * Tests if a value is a number. + * + * @param value - value to test + * @returns boolean indicating whether value is a number + * + * @example + * var bool = ns.isNumber( 3.14 ); + * // returns true + * + * @example + * var bool = ns.isNumber( new Number( 3.14 ) ); + * // returns true + * + * @example + * var bool = ns.isNumber( NaN ); + * // returns true + * + * @example + * var bool = ns.isNumber( null ); + * // returns false + * + * @example + * var bool = ns.isNumber.isPrimitive( 3.14 ); + * // returns true + * + * @example + * var bool = ns.isNumber.isObject( new Number( 3.14 ) ); + * // returns true + */ + isNumber: typeof isNumber; + + /** + * Tests if a value is an array-like object containing only numbers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only numbers + * + * @example + * var bool = ns.isNumberArray( [ -3.0, new Number(0.0), 2.0 ] ); + * // returns true + * + * @example + * var bool = ns.isNumberArray( [ -3.0, '3.0' ] ); + * // returns false + * @example + * var bool = ns.isNumberArray.primitives( [ -1.0, 0.0, 5.0 ] ); + * // returns true + * + * @example + * var bool = ns.isNumberArray.objects( [ new Number(1.0), new Number(3.0) ] ); + * // returns true + */ + isNumberArray: typeof isNumberArray; + + /** + * Tests if a value is a numeric array. + * + * @param v - value to test + * @returns boolean indicating if a value is a numeric array + * + * @example + * var bool = ns.isNumericArray( new Int8Array( 10 ) ); + * // returns true + * + * bool = ns.isNumericArray( [ 1, 2, 3 ] ); + * // returns true + * + * bool = ns.isNumericArray( [ '1', '2', '3' ] ); + * // returns false + */ + isNumericArray: typeof isNumericArray; + + /** + * Tests if a value is an object; e.g., `{}`. + * + * @param value - value to test + * @returns boolean indicating whether value is an object + * + * @example + * var bool = ns.isObject( {} ); + * // returns true + * + * @example + * var bool = ns.isObject( null ); + * // returns false + */ + isObject: typeof isObject; + + /** + * Test if a value is an array-like object containing only objects. + * + * @param value - value to test + * @returns boolean indicating whether an input value is an array-like object containing only objects + * + * @example + * var bool = ns.isObjectArray( [ {}, new Number(3.0) ] ); + * // returns true + * + * bool = ns.isObjectArray( [ {}, { 'beep': 'boop' } ] ); + * // returns true + * + * bool = ns.isObjectArray( [ {}, '3.0' ] ); + * // returns false + */ + isObjectArray: typeof isObjectArray; + + /** + * Tests if a value is object-like. + * + * ## Notes + * + * - Return values are the same as would be obtained using the built-in `typeof` operator except that `null` is not considered an object. + * + * @param value - value to test + * @returns boolean indicating whether a value is object-like + * + * @example + * var bool = ns.isObjectLike( {} ); + * // returns true + * + * @example + * var bool = ns.isObjectLike( [] ); + * // returns true + * + * @example + * var bool = ns.isObjectLike( null ); + * // returns false + */ + isObjectLike: typeof isObjectLike; + + /** + * Tests if a value is an odd number. + * + * @param value - value to test + * @returns boolean indicating whether value is an odd number + * + * @example + * var bool = ns.isOdd( 5.0 ); + * // returns true + * + * @example + * var bool = ns.isOdd( new Number( 6.0 ) ); + * // returns true + * + * @example + * var bool = ns.isOdd( 3.0 ); + * // returns false + * + * @example + * var bool = ns.isOdd.isPrimitive( new Number( 5.0 ) ); + * // returns false + * + * @example + * var bool = ns.isOdd.isObject( 5.0 ); + * // returns false + * + * @example + * var bool = ns.isOdd( null ); + * // returns false + */ + isOdd: typeof isOdd; + + /** + * Tests if a value is a persymmetric matrix. + * + * ## Notes + * + * - The implementation must rely on manually checking that \\(M_{ij} = M_{N-j-1,N-i-1}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. + * - Worst case scenario: O(N^2). + * + * @param v - value to test + * @returns boolean indicating if a value is a persymmetric matrix + * + * @example + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'generic', [ 1, 2, 3, 1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * var bool = ns.isPersymmetricMatrix( arr ); + * // returns true + * + * bool = ns.isPersymmetricMatrix( [] ); + * // returns false + */ + isPersymmetricMatrix: typeof isPersymmetricMatrix; + + /** + * Tests if a value is a plain object. + * + * @param value - value to test + * @returns boolean indicating whether value is a plain object + * + * @example + * var bool = ns.isPlainObject( {} ); + * // returns true + * + * @example + * var bool = ns.isPlainObject( null ); + * // returns false + */ + isPlainObject: typeof isPlainObject; + + /** + * Tests if a value is an array-like object containing only plain objects. + * + * @param value - value to test + * @returns boolean indicating whether an input value is an array-like object containing only plain objects + * + * @example + * var bool = ns.isPlainObjectArray( [ {}, { 'beep': 'boop' } ] ); + * // returns true + * + * bool = ns.isPlainObjectArray( [ {}, new Number(3.0) ] ); + * // returns false + * + * bool = ns.isPlainObjectArray( [ {}, '3.0' ] ); + * // returns false + */ + isPlainObjectArray: typeof isPlainObjectArray; + + /** + * Tests if a value is a positive integer. + * + * @param value - value to test + * @returns boolean indicating whether value is a positive integer + * + * @example + * var bool = ns.isPositiveInteger( 5.0 ); + * // returns true + * + * @example + * var bool = ns.isPositiveInteger( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = ns.isPositiveInteger( 0.0 ); + * // returns false + * + * @example + * var bool = ns.isPositiveInteger( -5.0 ); + * // returns false + * + * @example + * var bool = ns.isPositiveInteger( 3.14 ); + * // returns false + * + * @example + * var bool = ns.isPositiveInteger( null ); + * // returns false + * + * @example + * var bool = ns.isPositiveInteger.isPrimitive( 3.0 ); + * // returns true + * + * @example + * var bool = ns.isPositiveInteger.isObject( 3.0 ); + * // returns false + */ + isPositiveInteger: typeof isPositiveInteger; + + /** + * Tests if a value is an array-like object containing only positive integers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only positive integers + * + * @example + * var bool = ns.isPositiveIntegerArray( [ 3.0, new Number(3.0) ] ); + * // returns true + * + * @example + * var bool = ns.isPositiveIntegerArray( [ 3.0, '3.0' ] ); + * // returns false + * + * @example + * var bool = ns.isPositiveIntegerArray.primitives( [ 1.0, 2.0, 10.0 ] ); + * // returns true + * + * @example + * var bool = ns.isPositiveIntegerArray.objects( [ new Number(3.0), new Number(1.0) ] ); + * // returns true + */ + isPositiveIntegerArray: typeof isPositiveIntegerArray; + + /** + * Tests if a value is a positive number. + * + * @param value - value to test + * @returns boolean indicating whether value is a positive number + * + * @example + * var bool = ns.isPositiveNumber( 5.0 ); + * // returns true + * + * @example + * var bool = ns.isPositiveNumber( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = ns.isPositiveNumber( 3.14 ); + * // returns true + * + * @example + * var bool = ns.isPositiveNumber( -5.0 ); + * // returns false + * + * @example + * var bool = ns.isPositiveNumber( null ); + * // returns false + * + * @example + * var bool = ns.isPositiveNumber.isPrimitive( 3.0 ); + * // returns true + * + * @example + * var bool = ns.isPositiveNumber.isObject( 3.0 ); + * // returns false + */ + isPositiveNumber: typeof isPositiveNumber; + + /** + * Tests if a value is an array-like object containing only positive numbers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only positive numbers + * + * @example + * var bool = ns.isPositiveNumberArray( [ 2.7, new Number(3.0) ] ); + * // returns true + * + * @example + * var bool = ns.isPositiveNumberArray( [ 2.7, '3.0' ] ); + * // returns false + * + * @example + * var bool = ns.isPositiveNumberArray.primitives( [ 1.0, 2.0, 10.1 ] ); + * // returns true + * + * @example + * var bool = ns.isPositiveNumberArray.objects( [ new Number(3.0), new Number(1.0) ] ); + * // returns true + */ + isPositiveNumberArray: typeof isPositiveNumberArray; + + /** + * Tests if a value is equal to positive zero. + * + * @param value - value to test + * @returns boolean indicating whether value is equal to positive zero + * + * @example + * var bool = ns.isPositiveZero( 0.0 ); + * // returns true + * + * @example + * var bool = ns.isPositiveZero( new Number( 0.0 ) ); + * // returns true + * + * @example + * var bool = ns.isPositiveZero( -3.14 ); + * // returns false + * + * @example + * var bool = ns.isPositiveZero( 5.0 ); + * // returns false + * + * @example + * var bool = ns.isPositiveZero( -0.0 ); + * // returns false + * + * @example + * var bool = ns.isPositiveZero( null ); + * // returns false + * + * @example + * var bool = ns.isPositiveZero.isPrimitive( 0.0 ); + * // returns true + * + * @example + * var bool = ns.isPositiveZero.isObject( new Number( 0.0 ) ); + * // returns true + */ + isPositiveZero: typeof isPositiveZero; + + /** + * Tests if a value is a prime number. + * + * @param value - value to test + * @returns boolean indicating whether value is a prime number + * + * @example + * var bool = ns.isPrime( 5.0 ); + * // returns true + * + * @example + * var bool = ns.isPrime( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = ns.isPrime( 3.14 ); + * // returns false + * + * @example + * var bool = ns.isPrime( -5.0 ); + * // returns false + * + * @example + * var bool = ns.isPrime( null ); + * // returns false + * + * @example + * var bool = ns.isPrime.isPrimitive( 5.0 ); + * // returns true + * + * @example + * var bool = ns.isPrime.isObject( new Number( 5.0 ) ); + * // returns true + */ + isPrime: typeof isPrime; + + /** + * Tests if a value is a JavaScript primitive. + * + * @param value - value to test + * @returns boolean indicating if a value is a JavaScript primitive + * + * @example + * var bool = ns.isPrimitive( true ); + * // returns true + * + * @example + * var bool = ns.isPrimitive( {} ); + * // returns false + */ + isPrimitive: typeof isPrimitive; + + /** + * Tests if a value is an array-like object containing only JavaScript primitives. + * + * @param value - value to test + * @returns boolean indicating whether an input value is an array-like object containing only JavaScript primitives + * + * @example + * var bool = ns.isPrimitiveArray( [ '3', 2, null ] ); + * // returns true + * + * bool = ns.isPrimitiveArray( [ {}, 2, 1 ] ); + * // returns false + * + * bool = ns.isPrimitiveArray( [ new String('abc'), '3.0' ] ); + * // returns false + */ + isPrimitiveArray: typeof isPrimitiveArray; + + /** + * Tests if a value is PRNG-like. + * + * @param v - value to test + * @returns boolean indicating if a value is PRNG-like + * + * @example + * var randu = require( `@stdlib/random/base/randu` ); + * + * var bool = ns.isPRNGLike( randu ); + * // returns true + * + * bool = ns.isPRNGLike( [] ); + * // returns false + */ + isPRNGLike: typeof isPRNGLike; + + /** + * Tests if a value is a probability. + * + * @param value - value to test + * @returns boolean indicating whether value is a probability + * + * @example + * var bool = ns.isProbability( 0.5 ); + * // returns true + * + * @example + * var bool = ns.isProbability( new Number( 0.5 ) ); + * // returns true + * + * @example + * var bool = ns.isProbability( 3.14 ); + * // returns false + * + * @example + * var bool = ns.isProbability( -5.0 ); + * // returns false + * + * @example + * var bool = ns.isProbability( null ); + * // returns false + * + * @example + * var bool = ns.isProbability.isPrimitive( 0.66 ); + * // returns true + * + * @example + * var bool = ns.isProbability.isObject( new Number( 0.5 ) ); + * // returns true + */ + isProbability: typeof isProbability; + + /** + * Test if a value is an array-like object containing only probabilities. + * + * @param value - value to test + * @returns boolean indicating whether value is a positive integer + * + * @example + * var bool = ns.isProbabilityArray( [ 0.3, new Number(0.3) ] ); + * // returns true + * + * @example + * var bool = ns.isProbabilityArray( [ 0.3, '0.3' ] ); + * // returns false + * + * @example + * var bool = ns.isProbabilityArray.primitives( [ 0.1, 0.2, 0.3 ] ); + * // returns true + * + * @example + * var bool = ns.isProbabilityArray.objects( [ new Number(0.3), new Number(0.9) ] ); + * // returns true + */ + isProbabilityArray: typeof isProbabilityArray; + + /** + * Tests if an object's prototype chain contains a provided prototype. + * + * @param value - value to test + * @param proto - prototype + * @throws second argument must be an object and not null + * @returns boolean indicating if a provided prototype exists in a prototype chain + * + * @example + * var inherit = require( `@stdlib/utils/inherit` ); + * + * function Foo() { + * return this; + * } + * + * function Bar() { + * return this; + * } + * inherit( Bar, Foo ); + * + * var bar = new Bar(); + * + * var bool = ns.isPrototypeOf( bar, Foo.prototype ); + * // returns true + */ + isPrototypeOf: typeof isPrototypeOf; + + /** + * Tests if a value is a `RangeError` object. + * + * ## Notes + * + * - This function should **not** be considered robust. While the function should always return `true` if provided a RangeError (or a descendant) object, false positives may occur due to the fact that the RangeError constructor inherits from Error and has no internal class of its own. Hence, RangeError impersonation is possible. + * + * @param value - value to test + * @returns boolean indicating whether a value is a `RangeError` object + * + * @example + * var bool = ns.isRangeError( new RangeError( 'beep' ) ); + * // returns true + * + * @example + * var bool = ns.isRangeError( {} ); + * // returns false + */ + isRangeError: typeof isRangeError; + + /** + * Tests if an object's own property is read-only. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is read-only + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': true, + * 'writable': false, + * 'value': true + * }); + * + * var bool = ns.isReadOnlyProperty( obj, 'boop' ); + * // returns false + * + * bool = ns.isReadOnlyProperty( obj, 'beep' ); + * // returns true + */ + isReadOnlyProperty: typeof isReadOnlyProperty; + + /** + * Tests if an object's own or inherited property is read-only. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is read-only + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': true, + * 'writable': false, + * 'value': true + * }); + * + * var bool = ns.isReadOnlyPropertyIn( obj, 'boop' ); + * // returns false + * + * bool = ns.isReadOnlyPropertyIn( obj, 'beep' ); + * // returns true + */ + isReadOnlyPropertyIn: typeof isReadOnlyPropertyIn; + + /** + * Tests if an object's own property is readable and writable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is readable and writable + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * function setter( v ) { + * obj.boop = v; + * } + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': false, + * 'set': setter + * }); + * + * var bool = ns.isReadWriteProperty( obj, 'boop' ); + * // returns true + * + * bool = ns.isReadWriteProperty( obj, 'beep' ); + * // returns false + */ + isReadWriteProperty: typeof isReadWriteProperty; + + /** + * Tests if an object's own or inherited property is readable and writable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is readable and writable + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * function setter( v ) { + * obj.boop = v; + * } + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': false, + * 'set': setter + * }); + * + * var bool = ns.isReadWritePropertyIn( obj, 'boop' ); + * // returns true + * + * bool = ns.isReadWritePropertyIn( obj, 'beep' ); + * // returns false + */ + isReadWritePropertyIn: typeof isReadWritePropertyIn; + + /** + * Tests if an object's own property is readable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is readable + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * function setter( v ) { + * obj.boop = v; + * } + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': false, + * 'set': setter + * }); + * + * var bool = ns.isReadableProperty( obj, 'boop' ); + * // returns true + * + * bool = ns.isReadableProperty( obj, 'beep' ); + * // returns false + */ + isReadableProperty: typeof isReadableProperty; + + /** + * Tests if an object's own or inherited property is readable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is readable + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * function setter( v ) { + * obj.boop = v; + * } + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': false, + * 'set': setter + * }); + * + * var bool = ns.isReadablePropertyIn( obj, 'boop' ); + * // returns true + * + * bool = ns.isReadablePropertyIn( obj, 'beep' ); + * // returns false + */ + isReadablePropertyIn: typeof isReadablePropertyIn; + + /** + * Tests if a value is a `ReferenceError` object. + * + * ## Notes + * + * - This function should **not** be considered robust. While the function should always return `true` if provided a ReferenceError (or a descendant) object, false positives may occur due to the fact that the ReferenceError constructor inherits from Error and has no internal class of its own. Hence, ReferenceError impersonation is possible. + * + * @param value - value to test + * @returns boolean indicating whether a value is a `ReferenceError` object + * + * @example + * var bool = ns.isReferenceError( new ReferenceError( 'beep' ) ); + * // returns true + * + * @example + * var bool = ns.isReferenceError( {} ); + * // returns false + */ + isReferenceError: typeof isReferenceError; + + /** + * Tests if a value is a regular expression. + * + * @param value - value to test + * @returns boolean indicating whether value is a regular expression + * + * @example + * var bool = ns.isRegExp( /\.+/ ); + * // returns true + * + * @example + * var bool = ns.isRegExp( {} ); + * // returns false + */ + isRegExp: typeof isRegExp; + + /** + * Tests if a value is a regular expression string. + * + * @param value - value to test + * @returns boolean indicating whether a value is a regular expression string + * + * @example + * var bool = ns.isRegExpString( '/beep/' ); + * // returns true + * + * @example + * var bool = ns.isRegExpString( '/beep/gim' ); + * // returns true + * + * @example + * var bool = ns.isRegExpString( 'beep' ); + * // returns false + * + * @example + * var bool = ns.isRegExpString( '' ); + * // returns false + * + * @example + * var bool = ns.isRegExpString( null ); + * // returns false + */ + isRegExpString: typeof isRegExpString; + + /** + * Tests if a value is a relative path. + * + * ## Notes + * + * - Function behavior is platform-specific. On Windows platforms, the function is equal to `.win32()`. On POSIX platforms, the function is equal to `.posix()`. + * + * @param value - value to test + * @returns boolean indicating whether value is a relative path + * + * @example + * var IS_WINDOWS = require( `@stdlib/assert/is-windows` ); + * var bool; + * if ( IS_WINDOWS ) { + * bool = ns.isRelativePath( 'foo\\bar\\baz' ); + * // returns true + * + * bool = ns.isRelativePath( 'C:\\foo\\..\\bar\\baz' ); + * // returns false + * } else { + * bool = ns.isRelativePath( './foo/bar/baz' ); + * // returns true + * + * bool = ns.isRelativePath( '/foo/../bar/baz' ); + * // returns false + * } + * + * @example + * var bool = ns.isRelativePath.posix( './foo/bar/baz' ); + * // returns true + * + * @example + * var bool = ns.isRelativePath.posix( '/foo/../bar/baz' ); + * // returns false + * + * @example + * var bool = ns.isRelativePath.win32( 'foo\\bar\\baz' ); + * // returns true + * + * @example + * var bool = ns.isRelativePath.win32( 'C:\\foo\\..\\bar\\baz' ); + * // returns false + */ + isRelativePath: typeof isRelativePath; + + /** + * Tests if a value is a safe integer. + * + * ## Notes + * + * - An integer valued number is "safe" when the number can be exactly represented as a double-precision floating-point number. + * + * @param value - value to test + * @returns boolean indicating whether value is a safe integer + * + * @example + * var bool = ns.isSafeInteger( 5.0 ); + * // returns true + * + * @example + * var bool = ns.isSafeInteger( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = ns.isSafeInteger( 2.0e300 ); + * // returns false + * + * @example + * var bool = ns.isSafeInteger( -3.14 ); + * // returns false + * + * @example + * var bool = ns.isSafeInteger( null ); + * // returns false + * + * @example + * var bool = ns.isSafeInteger.isPrimitive( -3.0 ); + * // returns true + * + * @example + * var bool = ns.isSafeInteger.isObject( new Number( 3.0 ) ); + * // returns true + */ + isSafeInteger: typeof isSafeInteger; + + /** + * Tests if a value is an array-like object containing only safe integers. + * + * ## Notes + * + * - An integer valued number is "safe" when the number can be exactly represented as a double-precision floating-point number. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only safe integers + * + * @example + * var bool = ns.isSafeIntegerArray( [ -3.0, new Number(0.0), 2.0 ] ); + * // returns true + * + * @example + * var bool = ns.isSafeIntegerArray( [ -3.0, '3.0' ] ); + * // returns false + * + * @example + * var bool = ns.isSafeIntegerArray.primitives( [ -1.0, 10.0 ] ); + * // returns true + * + * @example + * var bool = ns.isSafeIntegerArray.primitives( [ -1.0, 0.0, 5.0 ] ); + * // returns true + * + * @example + * var bool = ns.isSafeIntegerArray.primitives( [ -3.0, new Number(-1.0) ] ); + * // returns false + * + * @example + * var bool = ns.isSafeIntegerArray.objects( [ new Number(1.0), new Number(3.0) ] ); + * // returns true + * + * @example + * var bool = ns.isSafeIntegerArray.objects( [ -1.0, 0.0, 3.0 ] ); + * // returns false + * + * @example + * var bool = ns.isSafeIntegerArray.objects( [ 3.0, new Number(-1.0) ] ); + * // returns false + */ + isSafeIntegerArray: typeof isSafeIntegerArray; + + /** + * Tests if two arguments are the same value. + * + * ## Notes + * + * - The function differs from the `===` operator in that the function treats `-0` and `+0` as distinct and `NaNs` as the same. + * + * @param a - first input value + * @param b - second input value + * @returns boolean indicating whether two arguments are the same value + * + * @example + * var bool = ns.isSameValue( true, true ); + * // returns true + * + * @example + * var bool = ns.isSameValue( 3.14, 3.14 ); + * // returns true + * + * @example + * var bool = ns.isSameValue( {}, {} ); + * // returns false + * + * @example + * var bool = ns.isSameValue( -0.0, -0.0 ); + * // returns true + * + * @example + * var bool = ns.isSameValue( -0.0, 0.0 ); + * // returns false + * + * @example + * var bool = ns.isSameValue( NaN, NaN ); + * // returns true + * + * @example + * var bool = ns.isSameValue( [], [] ); + * // returns false + */ + isSameValue: typeof isSameValue; + + /** + * Tests if two arguments are the same value. + * + * ## Notes + * + * - The function differs from the `===` operator in that the function treats `NaNs` as the same. + * + * @param a - first input value + * @param b - second input value + * @returns boolean indicating whether two arguments are the same value + * + * @example + * var bool = ns.isSameValueZero( true, true ); + * // returns true + * + * @example + * var bool = ns.isSameValueZero( 3.14, 3.14 ); + * // returns true + * + * @example + * var bool = ns.isSameValueZero( {}, {} ); + * // returns false + * + * @example + * var bool = ns.isSameValueZero( -0.0, -0.0 ); + * // returns true + * + * @example + * var bool = ns.isSameValueZero( -0.0, 0.0 ); + * // returns true + * + * @example + * var bool = ns.isSameValueZero( NaN, NaN ); + * // returns true + * + * @example + * var bool = ns.isSameValueZero( [], [] ); + * // returns false + */ + isSameValueZero: typeof isSameValueZero; + + /** + * Tests if a value is a `SharedArrayBuffer`. + * + * @param value - value to test + * @returns boolean indicating whether value is a `SharedArrayBuffer` + * + * @example + * var SharedArrayBuffer = require( `@stdlib/array/shared-buffer` ); + * + * try { + * var bool = ns.isSharedArrayBuffer( new SharedArrayBuffer( 10 ) ); + * // returns true + * } catch ( err ) { + * console.log( 'Environment does not support SharedArrayBuffers.' ); + * } + * + * @example + * var bool = ns.isSharedArrayBuffer( [] ); + * // returns false + */ + isSharedArrayBuffer: typeof isSharedArrayBuffer; + + /** + * Tests if a value is a skew-centrosymmetric matrix. + * + * ## Notes + * + * - The implementation must rely on manually checking that \\(M_{ij} = -M_{N-i-1,N-j-1}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. + * - Worst case scenario: O(N^2). + * + * @param v - value to test + * @returns boolean indicating if a value is a skew-centrosymmetric matrix + * + * @example + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'generic', [ 2, 1, -1, -2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * var bool = ns.isSkewCentrosymmetricMatrix( arr ); + * // returns true + * + * bool = ns.isSkewCentrosymmetricMatrix( [] ); + * // returns false + */ + isSkewCentrosymmetricMatrix: typeof isSkewCentrosymmetricMatrix; + + /** + * Tests if a value is a skew-persymmetric matrix. + * + * ## Notes + * + * - The implementation must rely on manually checking that \\(M_{ij} = -M_{N-j-1,N-i-1}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. + * - Worst case scenario: O(N^2). + * + * @param v - value to test + * @returns boolean indicating if a value is a skew-persymmetric matrix + * + * @example + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'generic', [ 1, 0, 0, -1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * var bool = ns.isSkewPersymmetricMatrix( arr ); + * // returns true + * + * bool = ns.isSkewPersymmetricMatrix( [] ); + * // returns false + */ + isSkewPersymmetricMatrix: typeof isSkewPersymmetricMatrix; + + /** + * Tests if a value is a skew-symmetric matrix. + * + * ## Notes + * + * - The implementation must rely on manually checking that \\(M_{ij} = -M_{ji}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. + * - Worst case scenario: O(N^2). + * + * @param v - value to test + * @returns boolean indicating if a value is a skew-symmetric matrix + * + * @example + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'generic', [ 0, -1, 1, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * var bool = ns.isSkewSymmetricMatrix( arr ); + * // returns true + * + * bool = ns.isSkewSymmetricMatrix( [] ); + * // returns false + */ + isSkewSymmetricMatrix: typeof isSkewSymmetricMatrix; + + /** + * Tests if a value is a 2-dimensional ndarray-like object having equal dimensions. + * + * @param v - value to test + * @returns boolean indicating if a value is a 2-dimensional ndarray-like object having equal dimensions + * + * @example + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * var bool = ns.isSquareMatrix( arr ); + * // returns true + * + * bool = ns.isSquareMatrix( [] ); + * // returns false + */ + isSquareMatrix: typeof isSquareMatrix; + + /** + * Tests if a value is a square number. + * + * @param value - value to test + * @returns boolean indicating whether value is a square number + * + * @example + * var bool = ns.isSquareNumber( 4.0 ); + * // returns true + * + * @example + * var bool = ns.isSquareNumber( new Number( 4.0 ) ); + * // returns true + * + * @example + * var bool = ns.isSquareNumber( 3.14 ); + * // returns false + * + * @example + * var bool = ns.isSquareNumber( -5.0 ); + * // returns false + * + * @example + * var bool = ns.isSquareNumber( null ); + * // returns false + * + * @example + * var bool = ns.isSquareNumber.isPrimitive( 4.0 ); + * // returns true + * + * @example + * var bool = ns.isSquareNumber.isObject( new Number( 4.0 ) ); + * // returns true + */ + isSquareNumber: typeof isSquareNumber; + + /** + * Tests if a value is a square triangular number. + * + * ## Notes + * + * - Return values are not reliable for numbers greater than `1125899906842624`. + * + * @param value - value to test + * @returns boolean indicating whether value is a square triangular number + * + * @example + * var bool = ns.isSquareTriangularNumber( 36.0 ); + * // returns true + * + * @example + * var bool = ns.isSquareTriangularNumber( new Number( 36.0 ) ); + * // returns true + * + * @example + * var bool = ns.isSquareTriangularNumber( 3.14 ); + * // returns false + * + * @example + * var bool = ns.isSquareTriangularNumber( -5.0 ); + * // returns false + * + * @example + * var bool = ns.isSquareTriangularNumber( null ); + * // returns false + * + * @example + * var bool = ns.isSquareTriangularNumber.isPrimitive( 36.0 ); + * // returns true + * + * @example + * var bool = ns.isSquareTriangularNumber.isObject( new Number( 36.0 ) ); + * // returns true + */ + isSquareTriangularNumber: typeof isSquareTriangularNumber; + + /** + * Tests if two arguments are strictly equal. + * + * ## Notes + * + * - The function differs from the `===` operator in that the function treats `-0` and `+0` as distinct. + * + * @param a - first input value + * @param b - second input value + * @returns boolean indicating whether two arguments are strictly equal + * + * @example + * var bool = ns.isStrictEqual( true, true ); + * // returns true + * + * @example + * var bool = ns.isStrictEqual( 3.14, 3.14 ); + * // returns true + * + * @example + * var bool = ns.isStrictEqual( {}, {} ); + * // returns false + * + * @example + * var bool = ns.isStrictEqual( -0.0, -0.0 ); + * // returns true + * + * @example + * var bool = ns.isStrictEqual( -0.0, 0.0 ); + * // returns false + * + * @example + * var bool = ns.isStrictEqual( NaN, NaN ); + * // returns false + * + * @example + * var bool = ns.isStrictEqual( [], [] ); + * // returns false + */ + isStrictEqual: typeof isStrictEqual; + + /** + * Tests if a value is a string. + * + * @param value - value to test + * @returns boolean indicating whether value is a string + * + * @example + * var bool = ns.isString( new String( 'beep' ) ); + * // returns true + * + * @example + * var bool = ns.isString( 'beep' ); + * // returns true + * + * @example + * var bool = ns.isString.isPrimitive( 'beep' ); + * // returns true + * + * @example + * var bool = ns.isString.isObject( 'beep' ); + * // returns false + */ + isString: typeof isString; + + /** + * Tests if a value is an array of strings. + * + * @param value - value to test + * @returns boolean indicating whether value is an array of strings + * + * @example + * var bool = ns.isStringArray( [ 'abc', 'def' ] ); + * // returns true + * + * @example + * var bool = ns.isStringArray( [ 'abc', 123 ] ); + * // returns false + * + * @example + * var bool = ns.isStringArray.primitives( [ 'abc', 'def' ] ); + * // returns true + * + * @example + * var bool = ns.isStringArray.objects( [ new String( 'abc' ), new String( 'def' ) ] ); + * // returns true + */ + isStringArray: typeof isStringArray; + + /** + * Tests if a value is a symbol. + * + * @param value - value to test + * @returns boolean indicating whether value is a symbol + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * + * var bool = ns.isSymbol.isPrimitive( Symbol( 'beep' ) ); + * // returns true + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * + * var bool = ns.isSymbol.isPrimitive( Object( Symbol( 'beep' ) ) ); + * // returns false + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * + * var bool = ns.isSymbol.isPrimitive( Symbol( 'beep' ) ); + * // returns true + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * + * var bool = ns.isSymbol.isPrimitive( Object( Symbol( 'beep' ) ) ); + * // returns false + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * + * var bool = ns.isSymbol.isObject( Symbol( 'beep' ) ); + * // returns false + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * + * var bool = ns.isSymbol.isObject( Object( Symbol( 'beep' ) ) ); + * // returns true + */ + isSymbol: typeof isSymbol; + + /** + * Tests if a value is an array-like object containing only symbols. + * + * ## Notes + * + * - In pre-ES2015 environments, the function always returns `false`. + * + * @param value - value to test + * @returns boolean indicating whether value is an array of strings + * + * @example + * var bool = ns.isSymbolArray( [ Symbol( 'abc' ), Symbol( 'def' ) ] ); + * // returns true + * + * @example + * var bool = ns.isSymbolArray( [ Symbol( 'abc' ), 'def' ] ); + * // returns false + * + * @example + * var bool = ns.isSymbolArray.primitives( [ Symbol( 'abc' ), Symbol( 'def' ) ] ); + * // returns true + * + * @example + * var bool = ns.isSymbolArray.objects( [ Object( Symbol( 'abc' ) ), Object( Symbol( 'def' ) ) ] ); + * // returns true + */ + isSymbolArray: typeof isSymbolArray; + + /** + * Tests if a value is a symmetric matrix. + * + * ## Notes + * + * - The implementation must rely on manually checking that \\(M_{ij} = M_{ji}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. + * - Worst case scenario: O(N^2). + * + * @param v - value to test + * @returns boolean indicating if a value is a symmetric matrix + * + * @example + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'generic', [ 0, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + * var bool = ns.isSymmetricMatrix( arr ); + * // returns true + * + * bool = ns.isSymmetricMatrix( [] ); + * // returns false + */ + isSymmetricMatrix: typeof isSymmetricMatrix; + + /** + * Tests if a value is a `SyntaxError` object. + * + * ## Notes + * + * - This function should **not** be considered robust. While the function should always return `true` if provided a SyntaxError (or a descendant) object, false positives may occur due to the fact that the SyntaxError constructor inherits from Error and has no internal class of its own. Hence, SyntaxError impersonation is possible. + * + * @param value - value to test + * @returns boolean indicating whether a value is a `SyntaxError` object + * + * @example + * var bool = ns.isSyntaxError( new SyntaxError( 'beep' ) ); + * // returns true + * + * @example + * var bool = ns.isSyntaxError( {} ); + * // returns false + */ + isSyntaxError: typeof isSyntaxError; + + /** + * Tests if a value is a triangular number. + * + * ## Notes + * + * - Return values are not reliable for numbers greater than `1125899906842624`. + * + * @param value - value to test + * @returns boolean indicating whether value is a triangular number + * + * @example + * var bool = ns.isTriangularNumber( 36.0 ); + * // returns true + * + * @example + * var bool = ns.isTriangularNumber( new Number( 36.0 ) ); + * // returns true + * + * @example + * var bool = ns.isTriangularNumber( 3.14 ); + * // returns false + * + * @example + * var bool = ns.isTriangularNumber( -5.0 ); + * // returns false + * + * @example + * var bool = ns.isTriangularNumber( null ); + * // returns false + * + * @example + * var bool = ns.isTriangularNumber.isPrimitive( 36.0 ); + * // returns true + * + * @example + * var bool = ns.isTriangularNumber.isObject( new Number( 36.0 ) ); + * // returns true + */ + isTriangularNumber: typeof isTriangularNumber; + + /** + * Tests if a value is truthy. + * + * @param value - value to test + * @returns boolean indicating whether a value is truthy + * + * @example + * var bool = ns.isTruthy( true ); + * // returns true + * + * @example + * var bool = ns.isTruthy( [] ); + * // returns true + * + * @example + * var bool = ns.isTruthy( false ); + * // returns false + * + * @example + * var bool = ns.isTruthy( null ); + * // returns false + * + * @example + * var bool = ns.isTruthy( '' ); + * // returns false + * + * @example + * var bool = ns.isTruthy( 0 ); + * // returns false + * + * @example + * var bool = ns.isTruthy( void 0 ); + * // returns false + * + * @example + * var bool = ns.isTruthy( NaN ); + * // returns false + */ + isTruthy: typeof isTruthy; + + /** + * Tests if a value is an array-like object containing only truthy values. + * + * @param value - value to test + * @returns boolean indicating whether an input value is an array-like object containing only truthy values + * + * @example + * var bool = ns.isTruthyArray( [ {}, [] ] ); + * // returns true + * + * bool = ns.isTruthyArray( [ false, null, void 0, '', 0, NaN ] ); + * // returns false + * + * bool = ns.isTruthyArray( [] ); + * // returns false + */ + isTruthyArray: typeof isTruthyArray; + + /** + * Tests if a value is a `TypeError` object. + * + * ## Notes + * + * - This function should **not** be considered robust. While the function should always return `true` if provided a TypeError (or a descendant) object, false positives may occur due to the fact that the TypeError constructor inherits from Error and has no internal class of its own. Hence, TypeError impersonation is possible. + * + * @param value - value to test + * @returns boolean indicating whether a value is a `TypeError` object + * + * @example + * var bool = ns.isTypeError( new TypeError( 'beep' ) ); + * // returns true + * + * @example + * var bool = ns.isTypeError( {} ); + * // returns false + */ + isTypeError: typeof isTypeError; + + /** + * Tests if a value is a typed array. + * + * @param value - value to test + * @returns boolean indicating if a value is a typed array + * + * @example + * var Int8Array = require( `@stdlib/array/int8` ); + * + * var bool = ns.isTypedArray( new Int8Array( 10 ) ); + * // returns true + */ + isTypedArray: typeof isTypedArray; + + /** + * Tests if a value is a valid typed array length. + * + * ## Notes + * + * - A valid length property for a typed array instance is any integer value on the interval `[0, 2^53-1]`. + * + * @param value - value to test + * @returns boolean indicating if a value is a valid typed array length + * + * @example + * var bool = ns.isTypedArrayLength( 3 ); + * // returns true + * + * @example + * var bool = ns.isTypedArrayLength( 3.14 ); + * // returns false + */ + isTypedArrayLength: typeof isTypedArrayLength; + + /** + * Tests if a value is typed-array-like. + * + * @param value - value to test + * @returns boolean indicating if a value is typed-array-like + * + * @example + * var arr = { + * 'BYTES_PER_ELEMENT': 8, + * 'length': 10, + * 'byteOffset': 0, + * 'byteLength': 10 + * }; + * var val = ns.isTypedArrayLike( arr ); + * // returns true + * + * @example + * var Int8Array = require( `@stdlib/array/int8` ); + * + * var val = ns.isTypedArrayLike( new Int8Array( 4 ) ); + * // returns true + * + * @example + * var val = ns.isTypedArrayLike( [] ); + * // returns false + * + * @example + * var val = ns.isTypedArrayLike( {} ); + * // returns false + * + * @example + * var val = ns.isTypedArrayLike( null ); + * // returns false + * + * @example + * var val = ns.isTypedArrayLike( 'beep' ); + * // returns false + */ + isTypedArrayLike: typeof isTypedArrayLike; + + /** + * Tests if a value is a Uint8Array. + * + * @param value - value to test + * @returns boolean indicating whether value is a Uint8Array + * + * @example + * var bool = ns.isUint8Array( new Uint8Array( 10 ) ); + * // returns true + * + * @example + * var bool = ns.isUint8Array( [] ); + * // returns false + */ + isUint8Array: typeof isUint8Array; + + /** + * Tests if a value is a Uint8ClampedArray. + * + * @param value - value to test + * @returns boolean indicating whether value is a Uint8ClampedArray + * + * @example + * var bool = ns.isUint8ClampedArray( new Uint8ClampedArray( 10 ) ); + * // returns true + * + * @example + * var bool = ns.isUint8ClampedArray( [] ); + * // returns false + */ + isUint8ClampedArray: typeof isUint8ClampedArray; + + /** + * Tests if a value is a Uint16Array. + * + * @param value - value to test + * @returns boolean indicating whether value is a Uint16Array + * + * @example + * var bool = ns.isUint16Array( new Uint16Array( 10 ) ); + * // returns true + * + * @example + * var bool = ns.isUint16Array( [] ); + * // returns false + */ + isUint16Array: typeof isUint16Array; + + /** + * Tests if a value is a Uint32Array. + * + * @param value - value to test + * @returns boolean indicating whether value is a Uint32Array + * + * @example + * var bool = ns.isUint32Array( new Uint32Array( 10 ) ); + * // returns true + * + * @example + * var bool = ns.isUint32Array( [] ); + * // returns false + */ + isUint32Array: typeof isUint32Array; + + /** + * Tests if a value is a UNC path. + * + * @param value - value to test + * @returns boolean indicating if a value is a UNC path + * + * @example + * var bool = ns.isUNCPath( '\\\\server\\share\\foo\\bar\\baz' ); + * // returns true + * + * @example + * var bool = ns.isUNCPath( '/foo/bar/baz' ); + * // returns false + */ + isUNCPath: typeof isUNCPath; + + /** + * Tests if a value is `undefined`. + * + * @param value - value to test + * @returns boolean indicating whether value is undefined + * + * @example + * var bool = ns.isUndefined( undefined ); + * // returns true + * + * bool = ns.isUndefined( null ); + * // returns false + */ + isUndefined: typeof isUndefined; + + /** + * Tests if a value is `undefined` or `null`. + * + * @param value - value to test + * @returns boolean indicating whether value is undefined + * + * @example + * var bool = ns.isUndefinedOrNull( undefined ); + * // returns true + * + * bool = ns.isUndefinedOrNull( null ); + * // returns true + * + * bool = ns.isUndefinedOrNull( false ); + * // returns false + */ + isUndefinedOrNull: typeof isUndefinedOrNull; + + /** + * Tests if a value is an array of probabilities that sum to one. + * + * @param v - value to test + * @returns boolean indicating if a value is a probability array + * + * @example + * var bool = ns.isUnityProbabilityArray( [ 0.25, 0.5, 0.25 ] ); + * // returns true + * + * @example + * var bool = ns.isUnityProbabilityArray( new Uint8Array( [ 0, 1 ] ) ); + * // returns true + * + * @example + * var bool = ns.isUnityProbabilityArray( [ 0.4, 0.4, 0.4 ] ); + * // returns false + * + * @example + * var bool = ns.isUnityProbabilityArray( [ 3.14, 0.0 ] ); + * // returns false + */ + isUnityProbabilityArray: typeof isUnityProbabilityArray; + + /** + * Tests if a value is an uppercase string. + * + * @param value - value to test + * @returns boolean indicating whether value is an uppercase string + * + * @example + * var bool = ns.isUppercase( 'HELLO' ); + * // returns true + * + * @example + * var bool = ns.isUppercase( 'World' ); + * // returns false + * + * @example + * var bool = ns.isUppercase( '!' ); + * // returns false + * + * @example + * var bool = ns.isUppercase( 'salt and light' ); + * // returns false + */ + isUppercase: typeof isUppercase; + + /** + * Tests if a value is a URI. + * + * @param value - value to test + * @returns boolean indicating if a value is a URI + * + * @example + * var bool = ns.isURI( 'http://google.com' ); + * // returns true + * + * @example + * var bool = ns.isURI( 'http://localhost/' ); + * // returns true + * + * @example + * var bool = ns.isURI( 'http://example.w3.org/path%20with%20spaces.html' ); + * // returns true + * + * @example + * var bool = ns.isURI( 'http://example.w3.org/%20' ); + * // returns true + * + * @example + * var bool = ns.isURI( 'ftp://ftp.is.co.za/rfc/rfc1808.txt' ); + * // returns true + * + * @example + * var bool = ns.isURI( 'ftp://ftp.is.co.za/../../../rfc/rfc1808.txt' ); + * // returns true + * + * @example + * var bool = ns.isURI( 'http://www.ietf.org/rfc/rfc2396.txt' ); + * // returns true + * + * @example + * var bool = ns.isURI( 'ldap://[2001:db8::7]/c=GB?objectClass?one' ); + * // returns true + * + * @example + * var bool = ns.isURI( 'mailto:John.Doe@example.com' ); + * // returns true + * + * @example + * var bool = ns.isURI( 'news:comp.infosystems.www.servers.unix' ); + * // returns true + * + * @example + * var bool = ns.isURI( 'tel:+1-816-555-1212' ); + * // returns true + * + * @example + * var bool = ns.isURI( 'telnet://192.0.2.16:80/' ); + * // returns true + * + * @example + * var bool = ns.isURI( 'urn:oasis:names:specification:docbook:dtd:xml:4.1.2' ); + * // returns true + * + * @example + * // No scheme: + * var bool = ns.isURI( '' ); + * // returns false + * + * @example + * // No scheme: + * var bool = ns.isURI( 'foo' ); + * // returns false + * + * @example + * // No scheme: + * var bool = ns.isURI( 'foo@bar' ); + * // returns false + * + * @example + * // No scheme: + * var bool = ns.isURI( '://foo/' ); + * // returns false + * + * @example + * // Illegal characters: + * var bool = ns.isURI( 'http://' ); + * // returns false + * + * @example + * // Invalid path: + * var bool = ns.isURI( 'http:////foo.html' ); + * // returns false + * + * @example + * // Incomplete hex escapes... + * var bool = ns.isURI( 'http://example.w3.org/%a' ); + * // returns false + * + * @example + * var bool = ns.isURI( 'http://example.w3.org/%a/foo' ); + * // returns false + * + * @example + * var bool = ns.isURI( 'http://example.w3.org/%at' ); + * // returns false + */ + isURI: typeof isURI; + + /** + * Tests if a value is a `URIError` object. + * + * ## Notes + * + * - This function should **not** be considered robust. While the function should always return `true` if provided a URIError (or a descendant) object, false positives may occur due to the fact that the URIError constructor inherits from Error and has no internal class of its own. Hence, URIError impersonation is possible. + * + * @param value - value to test + * @returns boolean indicating whether a value is a `URIError` object + * + * @example + * var bool = ns.isURIError( new URIError( 'beep' ) ); + * // returns true + * + * @example + * var bool = ns.isURIError( {} ); + * // returns false + */ + isURIError: typeof isURIError; + + /** + * Tests if a value is a 1-dimensional ndarray-like object. + * + * @param v - value to test + * @returns boolean indicating if a value is a 1-dimensional ndarray-like object + * + * @example + * var ndarray = require( `@stdlib/ndarray/ctor` ); + * + * var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 4 ], [ 1 ], 0, 'row-major' ); + * var bool = ns.isVectorLike( arr ); + * // returns true + * + * bool = ns.isVectorLike( [] ); + * // returns false + */ + isVectorLike: typeof isVectorLike; + + /** + * Boolean indicating if the runtime is a web worker. + * + * @example + * var bool = ns.IS_WEB_WORKER; + * // returns + */ + IS_WEB_WORKER: typeof IS_WEB_WORKER; + + /** + * Tests whether a string contains only white space characters. + * + * @param x - value to test + * @returns boolean indicating if a string contains only white space characters + * + * @example + * var out = ns.isWhitespace( ' ' ); + * // returns true + * + * @example + * var out = ns.isWhitespace( 'beep boop' ); + * // returns false + * + * @example + * var out = ns.isWhitespace( '' ); + * // returns false + * + * @example + * var out = ns.isWhitespace( 123 ); + * // returns false + */ + isWhitespace: typeof isWhitespace; + + /** + * Boolean indicating if the current process is running on Windows. + * + * @example + * var PLATFORM = require( `@stdlib/os/platform` ); + * + * if ( ns.IS_WINDOWS ) { + * console.log( 'Running on Windows...' ); + * } else { + * console.log( 'Running on %s...', PLATFORM ); + * } + */ + IS_WINDOWS: typeof IS_WINDOWS; + + /** + * Tests if an object's own property is writable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is writable + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': false, + * 'writable': false, + * 'value': 'beep' + * }); + * + * var bool = ns.isWritableProperty( obj, 'boop' ); + * // returns true + * + * bool = ns.isWritableProperty( obj, 'beep' ); + * // returns false + */ + isWritableProperty: typeof isWritableProperty; + + /** + * Tests if an object's own or inherited property is writable. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is writable + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': false, + * 'writable': false, + * 'value': 'beep' + * }); + * + * var bool = ns.isWritablePropertyIn( obj, 'boop' ); + * // returns true + * + * bool = ns.isWritablePropertyIn( obj, 'beep' ); + * // returns false + */ + isWritablePropertyIn: typeof isWritablePropertyIn; + + /** + * Tests if an object's own property is write-only. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is write-only + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * function setter( v ) { + * obj.boop = v; + * } + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': true, + * 'set': setter + * }); + * + * var bool = ns.isWriteOnlyProperty( obj, 'boop' ); + * // returns false + * + * bool = ns.isWriteOnlyProperty( obj, 'beep' ); + * // returns true + */ + isWriteOnlyProperty: typeof isWriteOnlyProperty; + + /** + * Tests if an object's own or inherited property is write-only. + * + * @param value - value to test + * @param property - property to test + * @returns boolean indicating if an object property is write-only + * + * @example + * var defineProperty = require( `@stdlib/utils/define-property` ); + * + * var obj = { + * 'boop': true + * }; + * + * function setter( v ) { + * obj.boop = v; + * } + * + * defineProperty( obj, 'beep', { + * 'configurable': false, + * 'enumerable': true, + * 'set': setter + * }); + * + * var bool = ns.isWriteOnlyPropertyIn( obj, 'boop' ); + * // returns false + * + * bool = ns.isWriteOnlyPropertyIn( obj, 'beep' ); + * // returns true + */ + isWriteOnlyPropertyIn: typeof isWriteOnlyPropertyIn; +} + +/** +* Standard assertion utilities. +*/ +declare var ns: Namespace; + + +// EXPORTS // + +export = ns; diff --git a/docs/types/test.ts b/docs/types/test.ts new file mode 100644 index 00000000..ea40c37b --- /dev/null +++ b/docs/types/test.ts @@ -0,0 +1,29 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* tslint:disable:no-unused-expression */ + +import assert = require( './index' ); + + +// TESTS // + +// The exported value is the expected interface... +{ + assert; // $ExpectType Namespace +} diff --git a/examples/index.js b/examples/index.js new file mode 100644 index 00000000..c7e940d8 --- /dev/null +++ b/examples/index.js @@ -0,0 +1,24 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var objectKeys = require( '@stdlib/utils/keys' ); +var ns = require( './../lib' ); + +console.log( objectKeys( ns ) ); diff --git a/has-arraybuffer-support/README.md b/has-arraybuffer-support/README.md new file mode 100644 index 00000000..becbfcdf --- /dev/null +++ b/has-arraybuffer-support/README.md @@ -0,0 +1,113 @@ + + +# ArrayBuffer Support + +> Detect native [`ArrayBuffer`][mdn-arraybuffer] support. + +
+ +## Usage + +```javascript +var hasArrayBufferSupport = require( '@stdlib/assert/has-arraybuffer-support' ); +``` + +#### hasArrayBufferSupport() + +Detects if a runtime environment supports [`ArrayBuffer`][mdn-arraybuffer]. + +```javascript +var bool = hasArrayBufferSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasArrayBufferSupport = require( '@stdlib/assert/has-arraybuffer-support' ); + +var bool = hasArrayBufferSupport(); +if ( bool ) { + console.log( 'Environment has ArrayBuffer support.' ); +} else { + console.log( 'Environment lacks ArrayBuffer support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-arraybuffer-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-arraybuffer-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-arraybuffer-support/benchmark/benchmark.js b/has-arraybuffer-support/benchmark/benchmark.js new file mode 100644 index 00000000..309803f3 --- /dev/null +++ b/has-arraybuffer-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasArrayBufferSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasArrayBufferSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-arraybuffer-support/bin/cli b/has-arraybuffer-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-arraybuffer-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-arraybuffer-support/docs/repl.txt b/has-arraybuffer-support/docs/repl.txt new file mode 100644 index 00000000..eb096d71 --- /dev/null +++ b/has-arraybuffer-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `ArrayBuffer` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `ArrayBuffer` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-arraybuffer-support/docs/types/index.d.ts b/has-arraybuffer-support/docs/types/index.d.ts new file mode 100644 index 00000000..68501389 --- /dev/null +++ b/has-arraybuffer-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `ArrayBuffer` support. +* +* @returns boolean indicating if an environment has `ArrayBuffer` support +* +* @example +* var bool = hasArrayBufferSupport(); +* // returns +*/ +declare function hasArrayBufferSupport(): boolean; + + +// EXPORTS // + +export = hasArrayBufferSupport; diff --git a/has-arraybuffer-support/docs/types/test.ts b/has-arraybuffer-support/docs/types/test.ts new file mode 100644 index 00000000..a667b4bf --- /dev/null +++ b/has-arraybuffer-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasArrayBufferSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasArrayBufferSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasArrayBufferSupport( true ); // $ExpectError + hasArrayBufferSupport( [], 123 ); // $ExpectError +} diff --git a/has-arraybuffer-support/docs/usage.txt b/has-arraybuffer-support/docs/usage.txt new file mode 100644 index 00000000..c37ac1eb --- /dev/null +++ b/has-arraybuffer-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-arraybuffer-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-arraybuffer-support/etc/cli_opts.json b/has-arraybuffer-support/etc/cli_opts.json new file mode 100644 index 00000000..92119c45 --- /dev/null +++ b/has-arraybuffer-support/etc/cli_opts.json @@ -0,0 +1,15 @@ +{ + "string": [], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-arraybuffer-support/examples/index.js b/has-arraybuffer-support/examples/index.js new file mode 100644 index 00000000..51ed114c --- /dev/null +++ b/has-arraybuffer-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasArrayBufferSupport = require( './../lib' ); + +var bool = hasArrayBufferSupport(); +if ( bool ) { + console.log( 'Environment has ArrayBuffer support.' ); +} else { + console.log( 'Environment lacks ArrayBuffer support.' ); +} diff --git a/has-arraybuffer-support/lib/arraybuffer.js b/has-arraybuffer-support/lib/arraybuffer.js new file mode 100644 index 00000000..89029c67 --- /dev/null +++ b/has-arraybuffer-support/lib/arraybuffer.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof ArrayBuffer === 'function' ) ? ArrayBuffer : null; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = main; diff --git a/has-arraybuffer-support/lib/index.js b/has-arraybuffer-support/lib/index.js new file mode 100644 index 00000000..8458e593 --- /dev/null +++ b/has-arraybuffer-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `ArrayBuffer` support. +* +* @module @stdlib/assert/has-arraybuffer-support +* +* @example +* var hasArrayBufferSupport = require( '@stdlib/assert/has-arraybuffer-support' ); +* +* var bool = hasArrayBufferSupport(); +* // returns +*/ + +// MODULES // + +var hasArrayBufferSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasArrayBufferSupport; diff --git a/has-arraybuffer-support/lib/main.js b/has-arraybuffer-support/lib/main.js new file mode 100644 index 00000000..cb950b7f --- /dev/null +++ b/has-arraybuffer-support/lib/main.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); +var Float64Array = require( '@stdlib/array/float64' ); +var GlobalArrayBuffer = require( './arraybuffer.js' ); + + +// MAIN // + +/** +* Tests for native `ArrayBuffer` support. +* +* @returns {boolean} boolean indicating if an environment has `ArrayBuffer` support +* +* @example +* var bool = hasArrayBufferSupport(); +* // returns +*/ +function hasArrayBufferSupport() { + var bool; + var view; + var buf; + + if ( typeof GlobalArrayBuffer !== 'function' ) { + return false; + } + // Test basic support... + try { + buf = new GlobalArrayBuffer( 16 ); + bool = ( isArrayBuffer( buf ) && typeof GlobalArrayBuffer.isView === 'function' ); + if ( bool ) { + view = new Float64Array( buf ); + view[ 0 ] = -3.14; + view[ 1 ] = NaN; + bool = ( + bool && + GlobalArrayBuffer.isView( view ) && + buf.byteLength === 16 && + view[ 0 ] === -3.14 && + view[ 1 ] !== view[ 1 ] + ); + } + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasArrayBufferSupport; diff --git a/has-arraybuffer-support/package.json b/has-arraybuffer-support/package.json new file mode 100644 index 00000000..6e389cb1 --- /dev/null +++ b/has-arraybuffer-support/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/assert/has-arraybuffer-support", + "version": "0.0.0", + "description": "Detect native ArrayBuffer support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-arraybuffer-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "arraybuffer", + "es2015", + "es6", + "array", + "buffer", + "buf", + "binary", + "typed array", + "typed-array", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-arraybuffer-support/test/test.cli.js b/has-arraybuffer-support/test/test.cli.js new file mode 100644 index 00000000..004125f5 --- /dev/null +++ b/has-arraybuffer-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var EXEC_PATH = require( '@stdlib/process/exec-path' ); + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `ArrayBuffer` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-arraybuffer-support/test/test.js b/has-arraybuffer-support/test/test.js new file mode 100644 index 00000000..55fd8d46 --- /dev/null +++ b/has-arraybuffer-support/test/test.js @@ -0,0 +1,157 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasArrayBuffer = ( typeof ArrayBuffer === 'function' ); // eslint-disable-line stdlib/require-globals + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `ArrayBuffer` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasArrayBuffer ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + function Mock( len ) { + var out; + var i; + + out = new Array( len/8 ); // we assume evenly divisible + for ( i = 0; i < out.length; i++ ) { + out[ i ] = 0; + } + out.byteLength = len; + return out; + } + + Mock.isView = isView; + + mocked = proxyquire( './../lib/main.js', { + './arraybuffer.js': Mock, + '@stdlib/assert/is-arraybuffer': isBuffer + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function isBuffer() { + return true; + } + + function isView() { + return true; + } +}); + +tape( 'if `ArrayBuffer` is not supported, detection result is `false` (no ArrayBuffer global function)', function test( t ) { + var mocked; + if ( hasArrayBuffer ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './arraybuffer.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); +}); + +tape( 'if `ArrayBuffer` is not supported, detected result is `false` (constructor throws)', function test( t ) { + var mocked; + if ( hasArrayBuffer ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './arraybuffer.js': mock + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); + + function mock() { + throw new Error( 'beep' ); + } +}); + +tape( 'if `ArrayBuffer` is not supported, detected result is `false` (constructor has no static `isView` method)', function test( t ) { + var mocked; + if ( hasArrayBuffer ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + function mock( len ) { + return new ArrayBuffer( len ); // eslint-disable-line stdlib/require-globals + } + + mock.isView = null; + + mocked = proxyquire( './../lib/main.js', { + './arraybuffer.js': mock + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); +}); + +tape( 'if `ArrayBuffer` is not supported, detected result is `false` (isView)', function test( t ) { + var mocked; + if ( hasArrayBuffer ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + function mock( len ) { + return new ArrayBuffer( len ); // eslint-disable-line stdlib/require-globals + } + mock.isView = isView; + + mocked = proxyquire( './../lib/main.js', { + './arraybuffer.js': mock + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); + + function isView() { + return false; + } +}); diff --git a/has-async-await-support/README.md b/has-async-await-support/README.md new file mode 100644 index 00000000..d174ffb9 --- /dev/null +++ b/has-async-await-support/README.md @@ -0,0 +1,127 @@ + + +# Async/Await Support + +> Detect native [`async`][mdn-async]/[`await`][mdn-await] support. + +
+ +## Usage + +```javascript +var hasAsyncAwaitSupport = require( '@stdlib/assert/has-async-await-support' ); +``` + +#### hasAsyncAwaitSupport() + +Detects if a runtime environment supports ES2017 [`async`][mdn-async]/[`await`][mdn-await]. + +```javascript +var bool = hasAsyncAwaitSupport(); +// returns +``` + +
+ + + +
+ +## Notes + +- The implementation uses code evaluation, which may be problematic in browser contexts enforcing a strict [content security policy][mdn-csp] (CSP). + +
+ + + +
+ +## Examples + + + +```javascript +var hasAsyncAwaitSupport = require( '@stdlib/assert/has-async-await-support' ); + +var bool = hasAsyncAwaitSupport(); +if ( bool ) { + console.log( 'Environment has native async/await support.' ); +} else { + console.log( 'Environment lacks native async/await support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-async-await-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-async-await-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-async-await-support/benchmark/benchmark.js b/has-async-await-support/benchmark/benchmark.js new file mode 100644 index 00000000..5c839153 --- /dev/null +++ b/has-async-await-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasAsyncAwaitSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasAsyncAwaitSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-async-await-support/bin/cli b/has-async-await-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-async-await-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-async-await-support/docs/repl.txt b/has-async-await-support/docs/repl.txt new file mode 100644 index 00000000..6eef456a --- /dev/null +++ b/has-async-await-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `async`/`await` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has native `async`/`await` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-async-await-support/docs/types/index.d.ts b/has-async-await-support/docs/types/index.d.ts new file mode 100644 index 00000000..b4d3c552 --- /dev/null +++ b/has-async-await-support/docs/types/index.d.ts @@ -0,0 +1,39 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `async/await` support. +* +* @returns boolean indicating if an environment has native `async`/`await` support +* +* @example +* var bool = hasAsyncAwaitSupport(); +* if ( bool ) { +* console.log( 'Environment has native async/await support.' ); +* } else { +* console.log( 'Environment lacks native async/await support.' ); +* } +*/ +declare function hasAsyncAwaitSupport(): boolean; + + +// EXPORTS // + +export = hasAsyncAwaitSupport; diff --git a/has-async-await-support/docs/types/test.ts b/has-async-await-support/docs/types/test.ts new file mode 100644 index 00000000..b955e3c8 --- /dev/null +++ b/has-async-await-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasAsyncAwaitSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasAsyncAwaitSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasAsyncAwaitSupport( true ); // $ExpectError + hasAsyncAwaitSupport( [], 123 ); // $ExpectError +} diff --git a/has-async-await-support/docs/usage.txt b/has-async-await-support/docs/usage.txt new file mode 100644 index 00000000..2829e04a --- /dev/null +++ b/has-async-await-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-async-await-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-async-await-support/etc/cli_opts.json b/has-async-await-support/etc/cli_opts.json new file mode 100644 index 00000000..92119c45 --- /dev/null +++ b/has-async-await-support/etc/cli_opts.json @@ -0,0 +1,15 @@ +{ + "string": [], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-async-await-support/examples/index.js b/has-async-await-support/examples/index.js new file mode 100644 index 00000000..5ef4c03e --- /dev/null +++ b/has-async-await-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasAsyncAwaitSupport = require( './../lib' ); + +var bool = hasAsyncAwaitSupport(); +if ( bool ) { + console.log( 'Environment has native async/await support.' ); +} else { + console.log( 'Environment lacks native async/await support.' ); +} diff --git a/has-async-await-support/lib/index.js b/has-async-await-support/lib/index.js new file mode 100644 index 00000000..a4cbc394 --- /dev/null +++ b/has-async-await-support/lib/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `async`/`await` support. +* +* @module @stdlib/assert/has-async-await-support +* +* @example +* var hasAsyncAwaitSupport = require( '@stdlib/assert/has-async-await-support' ); +* +* var bool = hasAsyncAwaitSupport(); +* if ( bool ) { +* console.log( 'Environment has native async/await support.' ); +* } else { +* console.log( 'Environment lacks native async/await support.' ); +* } +*/ + +// MODULES // + +var hasAsyncAwaitSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasAsyncAwaitSupport; diff --git a/has-async-await-support/lib/main.js b/has-async-await-support/lib/main.js new file mode 100644 index 00000000..4686b5cc --- /dev/null +++ b/has-async-await-support/lib/main.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var evil = require( '@stdlib/utils/eval' ); + + +// MAIN // + +/** +* Tests for native `async/await` support. +* +* @returns {boolean} boolean indicating if an environment has native `async`/`await` support +* +* @example +* var bool = hasAsyncAwaitSupport(); +* if ( bool ) { +* console.log( 'Environment has native async/await support.' ); +* } else { +* console.log( 'Environment lacks native async/await support.' ); +* } +*/ +function hasAsyncAwaitSupport() { + var bool; + try { + evil( '"use strict"; async () => {}' ); + bool = true; + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasAsyncAwaitSupport; diff --git a/has-async-await-support/package.json b/has-async-await-support/package.json new file mode 100644 index 00000000..2fb1d980 --- /dev/null +++ b/has-async-await-support/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/has-async-await-support", + "version": "0.0.0", + "description": "Detect native async/await support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-async-await-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "async", + "await", + "es2017", + "es7", + "support", + "has", + "hasasync", + "hasawait", + "native", + "issupported", + "asynchronous", + "cli" + ] +} diff --git a/has-async-await-support/test/test.cli.js b/has-async-await-support/test/test.cli.js new file mode 100644 index 00000000..662232bd --- /dev/null +++ b/has-async-await-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `async`/`await` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-async-await-support/test/test.js b/has-async-await-support/test/test.js new file mode 100644 index 00000000..0e2bf441 --- /dev/null +++ b/has-async-await-support/test/test.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var detect = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `async`/`await` is supported, detection result is `true`', function test( t ) { + var detect = proxyquire( './../lib/main.js', { + '@stdlib/utils/eval': stub + }); + + t.strictEqual( detect(), true, 'detection result is `true`' ); + t.end(); + + function stub() { + return 'beep'; + } +}); + +tape( 'if `async`/`await` is not supported, detection result is `false`', function test( t ) { + var detect = proxyquire( './../lib/main.js', { + '@stdlib/utils/eval': stub + }); + + t.strictEqual( detect(), false, 'detection result is `false`' ); + t.end(); + + function stub() { + throw new Error( 'boop' ); + } +}); diff --git a/has-async-iterator-symbol-support/README.md b/has-async-iterator-symbol-support/README.md new file mode 100644 index 00000000..62585fc6 --- /dev/null +++ b/has-async-iterator-symbol-support/README.md @@ -0,0 +1,119 @@ + + +# Async Iterator Symbol Support + +> Detect native [`Symbol.asyncIterator`][mdn-symbol] support. + +
+ +## Usage + + + +```javascript +var hasAsyncIteratorSymbolSupport = require( '@stdlib/assert/has-async-iterator-symbol-support' ); +``` + +#### hasAsyncIteratorSymbolSupport() + +Detects if a runtime environment supports ES2018 [`Symbol.asyncIterator`][mdn-symbol]. + + + +```javascript +var bool = hasAsyncIteratorSymbolSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var hasAsyncIteratorSymbolSupport = require( '@stdlib/assert/has-async-iterator-symbol-support' ); + +var bool = hasAsyncIteratorSymbolSupport(); +if ( bool ) { + console.log( 'Environment has Symbol.asyncIterator support.' ); +} else { + console.log( 'Environment lacks Symbol.asyncIterator support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-async-iterator-symbol-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-async-iterator-symbol-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-async-iterator-symbol-support/benchmark/benchmark.js b/has-async-iterator-symbol-support/benchmark/benchmark.js new file mode 100644 index 00000000..8aef83d9 --- /dev/null +++ b/has-async-iterator-symbol-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasAsyncIteratorSymbolSupport = require( './../lib' ); // eslint-disable-line id-length + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasAsyncIteratorSymbolSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-async-iterator-symbol-support/bin/cli b/has-async-iterator-symbol-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-async-iterator-symbol-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-async-iterator-symbol-support/docs/repl.txt b/has-async-iterator-symbol-support/docs/repl.txt new file mode 100644 index 00000000..df8e7baf --- /dev/null +++ b/has-async-iterator-symbol-support/docs/repl.txt @@ -0,0 +1,18 @@ + +{{alias}}() + Tests for native `Symbol.asyncIterator` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has native `Symbol.asyncIterator` + support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-async-iterator-symbol-support/docs/types/index.d.ts b/has-async-iterator-symbol-support/docs/types/index.d.ts new file mode 100644 index 00000000..8c837ae0 --- /dev/null +++ b/has-async-iterator-symbol-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Symbol.asyncIterator` support. +* +* @returns boolean indicating if an environment has `Symbol.asyncIterator` support +* +* @example +* var bool = hasAsyncIteratorSymbolSupport(); +* // returns +*/ +declare function hasAsyncIteratorSymbolSupport(): boolean; + + +// EXPORTS // + +export = hasAsyncIteratorSymbolSupport; diff --git a/has-async-iterator-symbol-support/docs/types/test.ts b/has-async-iterator-symbol-support/docs/types/test.ts new file mode 100644 index 00000000..7690b6b7 --- /dev/null +++ b/has-async-iterator-symbol-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasAsyncIteratorSymbolSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasAsyncIteratorSymbolSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasAsyncIteratorSymbolSupport( true ); // $ExpectError + hasAsyncIteratorSymbolSupport( [], 123 ); // $ExpectError +} diff --git a/has-async-iterator-symbol-support/docs/usage.txt b/has-async-iterator-symbol-support/docs/usage.txt new file mode 100644 index 00000000..ebabf9c3 --- /dev/null +++ b/has-async-iterator-symbol-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-async-iterator-symbol-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-async-iterator-symbol-support/etc/cli_opts.json b/has-async-iterator-symbol-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-async-iterator-symbol-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-async-iterator-symbol-support/examples/index.js b/has-async-iterator-symbol-support/examples/index.js new file mode 100644 index 00000000..4ae86f7c --- /dev/null +++ b/has-async-iterator-symbol-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasAsyncIteratorSymbolSupport = require( './../lib' ); // eslint-disable-line id-length + +var bool = hasAsyncIteratorSymbolSupport(); +if ( bool ) { + console.log( 'Environment has Symbol.asyncIterator support.' ); +} else { + console.log( 'Environment lacks Symbol.asyncIterator support.' ); +} diff --git a/has-async-iterator-symbol-support/lib/index.js b/has-async-iterator-symbol-support/lib/index.js new file mode 100644 index 00000000..8522bb6c --- /dev/null +++ b/has-async-iterator-symbol-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Symbol.asyncIterator` support. +* +* @module @stdlib/assert/has-async-iterator-symbol-support +* +* @example +* var hasAsyncIteratorSymbolSupport = require( '@stdlib/assert/has-async-iterator-symbol-support' ); +* +* var bool = hasAsyncIteratorSymbolSupport(); +* // returns +*/ + +// MODULES // + +var hasAsyncIteratorSymbolSupport = require( './main.js' ); // eslint-disable-line id-length + + +// EXPORTS // + +module.exports = hasAsyncIteratorSymbolSupport; diff --git a/has-async-iterator-symbol-support/lib/main.js b/has-async-iterator-symbol-support/lib/main.js new file mode 100644 index 00000000..701dffff --- /dev/null +++ b/has-async-iterator-symbol-support/lib/main.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable stdlib/require-globals */ + +'use strict'; + +// MODULES // + +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); + + +// MAIN // + +/** +* Tests for native `Symbol.asyncIterator` support. +* +* @returns {boolean} boolean indicating if an environment has `Symbol.asyncIterator` support +* +* @example +* var bool = hasAsyncIteratorSymbolSupport(); +* // returns +*/ +function hasAsyncIteratorSymbolSupport() { // eslint-disable-line id-length + return ( + typeof Symbol === 'function' && + typeof Symbol( 'foo' ) === 'symbol' && + hasOwnProp( Symbol, 'asyncIterator' ) && + typeof Symbol.asyncIterator === 'symbol' + ); +} + + +// EXPORTS // + +module.exports = hasAsyncIteratorSymbolSupport; diff --git a/has-async-iterator-symbol-support/package.json b/has-async-iterator-symbol-support/package.json new file mode 100644 index 00000000..c8d46317 --- /dev/null +++ b/has-async-iterator-symbol-support/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/has-async-iterator-symbol-support", + "version": "0.0.0", + "description": "Detect native Symbol.asyncIterator support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-async-iterator-symbol-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "symbol", + "async", + "iterator", + "symbol.asynciterator", + "es2018", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-async-iterator-symbol-support/test/test.cli.js b/has-async-iterator-symbol-support/test/test.cli.js new file mode 100644 index 00000000..e67aca27 --- /dev/null +++ b/has-async-iterator-symbol-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Symbol.asyncIterator` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-async-iterator-symbol-support/test/test.js b/has-async-iterator-symbol-support/test/test.js new file mode 100644 index 00000000..7f879b5b --- /dev/null +++ b/has-async-iterator-symbol-support/test/test.js @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable stdlib/require-globals */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasSymbols = hasSymbolSupport(); +var hasAsyncIteratorSymbol = ( typeof Symbol === 'function' && typeof Symbol.asyncIterator === 'symbol' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Symbol.asyncIterator` is supported, detection result is `true`', function test( t ) { + if ( hasAsyncIteratorSymbol ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + t.end(); +}); + +tape( 'the function guards against a `Symbol` global variable which does not produce `symbols`', function test( t ) { + var tmp; + if ( hasSymbols ) { + tmp = Symbol; + Symbol = {}; // eslint-disable-line no-global-assign + } else { + global.Symbol = {}; + } + t.strictEqual( detect(), false, 'detection result is `false`' ); + if ( hasSymbols ) { + Symbol = tmp; // eslint-disable-line no-global-assign + } + t.end(); +}); diff --git a/has-bigint-support/README.md b/has-bigint-support/README.md new file mode 100644 index 00000000..dd503b56 --- /dev/null +++ b/has-bigint-support/README.md @@ -0,0 +1,112 @@ + + +# BigInt Support + +> Detect native [`BigInt`][mdn-bigint] support. + +
+ +## Usage + +```javascript +var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' ); +``` + +#### hasBigIntSupport() + +Detects if a runtime environment supports ES2020 [`BigInt`][mdn-bigint]. + +```javascript +var bool = hasBigIntSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' ); + +if ( hasBigIntSupport() ) { + console.log( 'Environment has BigInt support.' ); +} else { + console.log( 'Environment lacks BigInt support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-bigint-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-bigint-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-bigint-support/benchmark/benchmark.js b/has-bigint-support/benchmark/benchmark.js new file mode 100644 index 00000000..0d00bc4c --- /dev/null +++ b/has-bigint-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasBigIntSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasBigIntSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-bigint-support/bin/cli b/has-bigint-support/bin/cli new file mode 100644 index 00000000..e98053ee --- /dev/null +++ b/has-bigint-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-bigint-support/docs/repl.txt b/has-bigint-support/docs/repl.txt new file mode 100644 index 00000000..914e72d4 --- /dev/null +++ b/has-bigint-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `BigInt` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has native `BigInt` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-bigint-support/docs/types/index.d.ts b/has-bigint-support/docs/types/index.d.ts new file mode 100644 index 00000000..78a21f9e --- /dev/null +++ b/has-bigint-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `BigInt` support. +* +* @returns boolean indicating if an environment has `BigInt` support +* +* @example +* var bool = hasBigIntSupport(); +* // returns +*/ +declare function hasBigIntSupport(): boolean; + + +// EXPORTS // + +export = hasBigIntSupport; diff --git a/has-bigint-support/docs/types/test.ts b/has-bigint-support/docs/types/test.ts new file mode 100644 index 00000000..dac178b6 --- /dev/null +++ b/has-bigint-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasBigIntSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasBigIntSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasBigIntSupport( true ); // $ExpectError + hasBigIntSupport( [], 123 ); // $ExpectError +} diff --git a/has-bigint-support/docs/usage.txt b/has-bigint-support/docs/usage.txt new file mode 100644 index 00000000..9b9fa98b --- /dev/null +++ b/has-bigint-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-bigint-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-bigint-support/etc/cli_opts.json b/has-bigint-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-bigint-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-bigint-support/examples/index.js b/has-bigint-support/examples/index.js new file mode 100644 index 00000000..5854aec9 --- /dev/null +++ b/has-bigint-support/examples/index.js @@ -0,0 +1,27 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasBigIntSupport = require( './../lib' ); + +if ( hasBigIntSupport() ) { + console.log( 'Environment has BigInt support.' ); +} else { + console.log( 'Environment lacks BigInt support.' ); +} diff --git a/has-bigint-support/lib/index.js b/has-bigint-support/lib/index.js new file mode 100644 index 00000000..48fab76a --- /dev/null +++ b/has-bigint-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `BigInt` support. +* +* @module @stdlib/assert/has-bigint-support +* +* @example +* var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' ); +* +* var bool = hasBigIntSupport(); +* // returns +*/ + +// MODULES // + +var hasBigIntSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasBigIntSupport; diff --git a/has-bigint-support/lib/main.js b/has-bigint-support/lib/main.js new file mode 100644 index 00000000..309b73a0 --- /dev/null +++ b/has-bigint-support/lib/main.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var getGlobal = require( '@stdlib/utils/global' ); + + +// VARIABLES // + +var Global = getGlobal(); + + +// MAIN // + +/** +* Tests for native `BigInt` support. +* +* @returns {boolean} boolean indicating if an environment has `BigInt` support +* +* @example +* var bool = hasBigIntSupport(); +* // returns +*/ +function hasBigIntSupport() { + return ( + typeof Global.BigInt === 'function' && + typeof BigInt === 'function' && // eslint-disable-line stdlib/require-globals + typeof Global.BigInt( '1' ) === 'bigint' && + typeof BigInt( '1' ) === 'bigint' // eslint-disable-line stdlib/require-globals, no-undef + ); +} + + +// EXPORTS // + +module.exports = hasBigIntSupport; diff --git a/has-bigint-support/package.json b/has-bigint-support/package.json new file mode 100644 index 00000000..30d396d4 --- /dev/null +++ b/has-bigint-support/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/has-bigint-support", + "version": "0.0.0", + "description": "Detect native BigInt support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-bigint-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "bigint", + "es2020", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-bigint-support/test/test.cli.js b/has-bigint-support/test/test.cli.js new file mode 100644 index 00000000..14336e1b --- /dev/null +++ b/has-bigint-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `BigInt` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-bigint-support/test/test.js b/has-bigint-support/test/test.js new file mode 100644 index 00000000..174c2ab5 --- /dev/null +++ b/has-bigint-support/test/test.js @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* global BigInt */ + +/* eslint-disable stdlib/require-globals */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasBigInts = ( typeof BigInt === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `BigInts` are supported, detection result is `true`', function test( t ) { + if ( hasBigInts ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + t.end(); +}); + +tape( 'the function guards against a `BigInt` global variable which does not produce `BigInt`s', function test( t ) { + var tmp; + if ( hasBigInts ) { + tmp = BigInt; + BigInt = {}; // eslint-disable-line no-global-assign + } else { + global.BigInt = {}; + } + t.strictEqual( detect(), false, 'detection result is `false`' ); + if ( hasBigInts ) { + BigInt = tmp; // eslint-disable-line no-global-assign + } + t.end(); +}); diff --git a/has-class-support/README.md b/has-class-support/README.md new file mode 100644 index 00000000..c970cd5f --- /dev/null +++ b/has-class-support/README.md @@ -0,0 +1,125 @@ + + +# Class Support + +> Detect native [`class`][class] support. + +
+ +## Usage + +```javascript +var hasClassSupport = require( '@stdlib/assert/has-class-support' ); +``` + +#### hasClassSupport() + +Detects if a runtime environment supports ES2015 [`class`][class]. + +```javascript +var bool = hasClassSupport(); +// returns +``` + +
+ + + +
+ +## Notes + +- The implementation uses code evaluation, which may be problematic in browser contexts enforcing a strict [content security policy][mdn-csp] (CSP). + +
+ + + +
+ +## Examples + + + +```javascript +var hasClassSupport = require( '@stdlib/assert/has-class-support' ); + +var bool = hasClassSupport(); +if ( bool ) { + console.log( 'Environment has native class support.' ); +} else { + console.log( 'Environment lacks native class support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-class-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-class-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-class-support/benchmark/benchmark.js b/has-class-support/benchmark/benchmark.js new file mode 100644 index 00000000..48abe648 --- /dev/null +++ b/has-class-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasClassSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasClassSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-class-support/bin/cli b/has-class-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-class-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-class-support/docs/repl.txt b/has-class-support/docs/repl.txt new file mode 100644 index 00000000..ce5f3beb --- /dev/null +++ b/has-class-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `class` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has native `class` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-class-support/docs/types/index.d.ts b/has-class-support/docs/types/index.d.ts new file mode 100644 index 00000000..494ae262 --- /dev/null +++ b/has-class-support/docs/types/index.d.ts @@ -0,0 +1,39 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `class` support. +* +* @returns boolean indicating if an environment has native `class` support +* +* @example +* var bool = hasClassSupport(); +* if ( bool ) { +* console.log( 'Environment has native class support.' ); +* } else { +* console.log( 'Environment lacks native class support.' ); +* } +*/ +declare function hasClassSupport(): boolean; + + +// EXPORTS // + +export = hasClassSupport; diff --git a/has-class-support/docs/types/test.ts b/has-class-support/docs/types/test.ts new file mode 100644 index 00000000..b4c1b962 --- /dev/null +++ b/has-class-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasClassSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasClassSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasClassSupport( true ); // $ExpectError + hasClassSupport( [], 123 ); // $ExpectError +} diff --git a/has-class-support/docs/usage.txt b/has-class-support/docs/usage.txt new file mode 100644 index 00000000..75b7bf44 --- /dev/null +++ b/has-class-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-class-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-class-support/etc/cli_opts.json b/has-class-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-class-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-class-support/examples/index.js b/has-class-support/examples/index.js new file mode 100644 index 00000000..c2aeb59f --- /dev/null +++ b/has-class-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasClassSupport = require( './../lib' ); + +var bool = hasClassSupport(); +if ( bool ) { + console.log( 'Environment has native class support.' ); +} else { + console.log( 'Environment lacks native class support.' ); +} diff --git a/has-class-support/lib/index.js b/has-class-support/lib/index.js new file mode 100644 index 00000000..9f8b5e41 --- /dev/null +++ b/has-class-support/lib/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `class` support. +* +* @module @stdlib/assert/has-class-support +* +* @example +* var hasClassSupport = require( '@stdlib/assert/has-class-support' ); +* +* var bool = hasClassSupport(); +* if ( bool ) { +* console.log( 'Environment has native class support.' ); +* } else { +* console.log( 'Environment lacks native class support.' ); +* } +*/ + +// MODULES // + +var hasClassSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasClassSupport; diff --git a/has-class-support/lib/main.js b/has-class-support/lib/main.js new file mode 100644 index 00000000..cf28cdb9 --- /dev/null +++ b/has-class-support/lib/main.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var evil = require( '@stdlib/utils/eval' ); + + +// MAIN // + +/** +* Tests for native `class` support. +* +* @returns {boolean} boolean indicating if an environment has native `class` support +* +* @example +* var bool = hasClassSupport(); +* if ( bool ) { +* console.log( 'Environment has native class support.' ); +* } else { +* console.log( 'Environment lacks native class support.' ); +* } +*/ +function hasClassSupport() { + var bool; + try { + evil( '"use strict"; class A{};' ); + bool = true; + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasClassSupport; diff --git a/has-class-support/package.json b/has-class-support/package.json new file mode 100644 index 00000000..1bcba479 --- /dev/null +++ b/has-class-support/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/has-class-support", + "version": "0.0.0", + "description": "Detect native class support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-class-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "class", + "es2015", + "es6", + "support", + "has", + "hasclass", + "native", + "issupported", + "subclass", + "extends", + "cli" + ] +} diff --git a/has-class-support/test/test.cli.js b/has-class-support/test/test.cli.js new file mode 100644 index 00000000..9383bcf1 --- /dev/null +++ b/has-class-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `class` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-class-support/test/test.js b/has-class-support/test/test.js new file mode 100644 index 00000000..492b12dd --- /dev/null +++ b/has-class-support/test/test.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var detect = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `class` is supported, detection result is `true`', function test( t ) { + var detect = proxyquire( './../lib/main.js', { + '@stdlib/utils/eval': stub + }); + + t.strictEqual( detect(), true, 'detection result is `true`' ); + t.end(); + + function stub() { + return 'beep'; + } +}); + +tape( 'if `class` is not supported, detection result is `false`', function test( t ) { + var detect = proxyquire( './../lib/main.js', { + '@stdlib/utils/eval': stub + }); + + t.strictEqual( detect(), false, 'detection result is `false`' ); + t.end(); + + function stub() { + throw new Error( 'boop' ); + } +}); diff --git a/has-dataview-support/README.md b/has-dataview-support/README.md new file mode 100644 index 00000000..82296cbe --- /dev/null +++ b/has-dataview-support/README.md @@ -0,0 +1,113 @@ + + +# DataView Support + +> Detect native [`DataView`][mdn-dataview] support. + +
+ +## Usage + +```javascript +var hasDataViewSupport = require( '@stdlib/assert/has-dataview-support' ); +``` + +#### hasDataViewSupport() + +Detects if a runtime environment supports [`DataView`][mdn-dataview]. + +```javascript +var bool = hasDataViewSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasDataViewSupport = require( '@stdlib/assert/has-dataview-support' ); + +var bool = hasDataViewSupport(); +if ( bool ) { + console.log( 'Environment has DataView support.' ); +} else { + console.log( 'Environment lacks DataView support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-dataview-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-dataview-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-dataview-support/benchmark/benchmark.js b/has-dataview-support/benchmark/benchmark.js new file mode 100644 index 00000000..e2ef0aa2 --- /dev/null +++ b/has-dataview-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasDataViewSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasDataViewSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-dataview-support/bin/cli b/has-dataview-support/bin/cli new file mode 100644 index 00000000..e98053ee --- /dev/null +++ b/has-dataview-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-dataview-support/docs/repl.txt b/has-dataview-support/docs/repl.txt new file mode 100644 index 00000000..e19021d7 --- /dev/null +++ b/has-dataview-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `DataView` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `DataView` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-dataview-support/docs/types/index.d.ts b/has-dataview-support/docs/types/index.d.ts new file mode 100644 index 00000000..31e87a99 --- /dev/null +++ b/has-dataview-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `DataView` support. +* +* @returns boolean indicating if an environment has `DataView` support +* +* @example +* var bool = hasDataViewSupport(); +* // returns +*/ +declare function hasDataViewSupport(): boolean; + + +// EXPORTS // + +export = hasDataViewSupport; diff --git a/has-dataview-support/docs/types/test.ts b/has-dataview-support/docs/types/test.ts new file mode 100644 index 00000000..1f2ea5bf --- /dev/null +++ b/has-dataview-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasDataViewSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasDataViewSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasDataViewSupport( true ); // $ExpectError + hasDataViewSupport( [], 123 ); // $ExpectError +} diff --git a/has-dataview-support/docs/usage.txt b/has-dataview-support/docs/usage.txt new file mode 100644 index 00000000..f0988167 --- /dev/null +++ b/has-dataview-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-dataview-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-dataview-support/etc/cli_opts.json b/has-dataview-support/etc/cli_opts.json new file mode 100644 index 00000000..92119c45 --- /dev/null +++ b/has-dataview-support/etc/cli_opts.json @@ -0,0 +1,15 @@ +{ + "string": [], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-dataview-support/examples/index.js b/has-dataview-support/examples/index.js new file mode 100644 index 00000000..5315471b --- /dev/null +++ b/has-dataview-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasDataViewSupport = require( './../lib' ); + +var bool = hasDataViewSupport(); +if ( bool ) { + console.log( 'Environment has DataView support.' ); +} else { + console.log( 'Environment lacks DataView support.' ); +} diff --git a/has-dataview-support/lib/dataview.js b/has-dataview-support/lib/dataview.js new file mode 100644 index 00000000..1699b936 --- /dev/null +++ b/has-dataview-support/lib/dataview.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof DataView === 'function' ) ? DataView : null; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = main; diff --git a/has-dataview-support/lib/index.js b/has-dataview-support/lib/index.js new file mode 100644 index 00000000..39cc26f7 --- /dev/null +++ b/has-dataview-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `DataView` support. +* +* @module @stdlib/assert/has-dataview-support +* +* @example +* var hasDataViewSupport = require( '@stdlib/assert/has-dataview-support' ); +* +* var bool = hasDataViewSupport(); +* // returns +*/ + +// MODULES // + +var hasDataViewSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasDataViewSupport; diff --git a/has-dataview-support/lib/main.js b/has-dataview-support/lib/main.js new file mode 100644 index 00000000..fcdd79b9 --- /dev/null +++ b/has-dataview-support/lib/main.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isDataView = require( '@stdlib/assert/is-dataview' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var GlobalDataView = require( './dataview.js' ); + + +// MAIN // + +/** +* Tests for native `DataView` support. +* +* @returns {boolean} boolean indicating if an environment has `DataView` support +* +* @example +* var bool = hasDataViewSupport(); +* // returns +*/ +function hasDataViewSupport() { + var bool; + var view; + var buf; + + if ( typeof GlobalDataView !== 'function' ) { + return false; + } + // Test basic support... + try { + buf = new ArrayBuffer( 24 ); + view = new GlobalDataView( buf, 8 ); + bool = ( isDataView( view ) && typeof view.getFloat64 === 'function' && typeof view.setFloat64 === 'function' ); + if ( bool ) { + view.setFloat64( 0, -3.14 ); + view.setFloat64( 8, NaN ); + bool = ( + bool && + view.buffer === buf && + view.byteLength === 16 && + view.byteOffset === 8 && + view.getFloat64( 0 ) === -3.14 && + view.getFloat64( 8 ) !== view.getFloat64( 8 ) + ); + } + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasDataViewSupport; diff --git a/has-dataview-support/package.json b/has-dataview-support/package.json new file mode 100644 index 00000000..6e389cb1 --- /dev/null +++ b/has-dataview-support/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/assert/has-arraybuffer-support", + "version": "0.0.0", + "description": "Detect native ArrayBuffer support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-arraybuffer-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "arraybuffer", + "es2015", + "es6", + "array", + "buffer", + "buf", + "binary", + "typed array", + "typed-array", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-dataview-support/test/test.cli.js b/has-dataview-support/test/test.cli.js new file mode 100644 index 00000000..6afafd14 --- /dev/null +++ b/has-dataview-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var EXEC_PATH = require( '@stdlib/process/exec-path' ); + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `DataView` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-dataview-support/test/test.js b/has-dataview-support/test/test.js new file mode 100644 index 00000000..bd567cf5 --- /dev/null +++ b/has-dataview-support/test/test.js @@ -0,0 +1,216 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasDataView = ( typeof DataView === 'function' ); // eslint-disable-line stdlib/require-globals + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `DataView` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasDataView ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + + mocked = proxyquire( './../lib/main.js', { + './dataview.js': Mock, + '@stdlib/assert/is-dataview': isDataView + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function Mock( buf, byteOffset, byteLength ) { + var out; + var i; + + if ( byteOffset === void 0 ) { + byteOffset = 0; + } + if ( byteLength === void 0 ) { + byteLength = buf.byteLength - byteOffset; + } + out = {}; + out.buffer = buf; + out._buffer = new Array( byteLength ); // eslint-disable-line no-underscore-dangle + for ( i = 0; i < byteLength; i++ ) { + out._buffer[ i ] = 0; // eslint-disable-line no-underscore-dangle + } + out.byteLength = byteLength; + out.byteOffset = byteOffset; + out.getFloat64 = getFloat64; + out.setFloat64 = setFloat64; + return out; + } + + function isDataView() { + return true; + } + + function getFloat64( byteOffset ) { + return this._buffer[ byteOffset ]; // eslint-disable-line no-invalid-this + } + + function setFloat64( byteOffset, value ) { + this._buffer[ byteOffset ] = value; // eslint-disable-line no-invalid-this + } +}); + +tape( 'if `DataView` is not supported, detection result is `false` (no DataView global function)', function test( t ) { + var mocked; + if ( hasDataView ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './dataview.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); +}); + +tape( 'if `DataView` is not supported, detected result is `false` (constructor throws)', function test( t ) { + var mocked; + if ( hasDataView ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './dataview.js': mock + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); + + function mock() { + throw new Error( 'beep' ); + } +}); + +tape( 'if `DataView` is not supported, detected result is `false` (instance has no `getFloat64` method)', function test( t ) { + var mocked; + if ( hasDataView ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './dataview.js': mock + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); + + function mock( buf ) { + var view = new DataView( buf ); // eslint-disable-line stdlib/require-globals + view.getFloat64 = null; + return view; + } +}); + +tape( 'if `DataView` is not supported, detected result is `false` (instance has no `setFloat64` method)', function test( t ) { + var mocked; + if ( hasDataView ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './dataview.js': mock + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); + + function mock( buf ) { + var view = new DataView( buf ); // eslint-disable-line stdlib/require-globals + view.setFloat64 = null; + return view; + } +}); + +tape( 'if `DataView` is not supported, detected result is `false` (broken setFloat64)', function test( t ) { + var mocked; + if ( hasDataView ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './dataview.js': mock + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); + + function mock( buf, offset, len ) { + var view = new DataView( buf, offset, len ); // eslint-disable-line stdlib/require-globals + view.setFloat64 = setFloat64; + return view; + } + + function setFloat64() { + // No-op... + } +}); + +tape( 'if `DataView` is not supported, detected result is `false` (broken getFloat64)', function test( t ) { + var mocked; + if ( hasDataView ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './dataview.js': mock + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); + + function mock( buf, offset, len ) { + var view = new DataView( buf, offset, len ); // eslint-disable-line stdlib/require-globals + view.getFloat64 = getFloat64; + return view; + } + + function getFloat64() { + // No-op... + } +}); diff --git a/has-define-properties-support/README.md b/has-define-properties-support/README.md new file mode 100644 index 00000000..bd17609a --- /dev/null +++ b/has-define-properties-support/README.md @@ -0,0 +1,117 @@ + + +# `Object.defineProperties` Support + +> Detect [`Object.defineProperties`][mdn-define-properties] support. + +
+ +## Usage + + + +```javascript +var hasDefinePropertiesSupport = require( '@stdlib/assert/has-define-properties-support' ); +``` + +#### hasDefinePropertiesSupport() + +Detects if a runtime environment supports [`Object.defineProperties`][mdn-define-properties]. + + + +```javascript +var bool = hasDefinePropertiesSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasDefinePropertiesSupport = require( '@stdlib/assert/has-define-properties-support' ); + +var bool = hasDefinePropertiesSupport(); +if ( bool ) { + console.log( 'Environment has `Object.defineProperties` support.' ); +} else { + console.log( 'Environment lacks `Object.defineProperties` support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-define-properties-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-define-properties-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-define-properties-support/benchmark/benchmark.js b/has-define-properties-support/benchmark/benchmark.js new file mode 100644 index 00000000..b8c7c45d --- /dev/null +++ b/has-define-properties-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasDefinePropertiesSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasDefinePropertiesSupport(); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-define-properties-support/bin/cli b/has-define-properties-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-define-properties-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-define-properties-support/docs/repl.txt b/has-define-properties-support/docs/repl.txt new file mode 100644 index 00000000..4a08bf29 --- /dev/null +++ b/has-define-properties-support/docs/repl.txt @@ -0,0 +1,18 @@ + +{{alias}}() + Tests for `Object.defineProperties` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `Object.defineProperties` + support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-define-properties-support/docs/types/index.d.ts b/has-define-properties-support/docs/types/index.d.ts new file mode 100644 index 00000000..1be785df --- /dev/null +++ b/has-define-properties-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for `Object.defineProperties` support. +* +* @returns boolean indicating if an environment has `Object.defineProperties` support +* +* @example +* var bool = hasDefinePropertiesSupport(); +* // returns +*/ +declare function hasDefinePropertiesSupport(): boolean; + + +// EXPORTS // + +export = hasDefinePropertiesSupport; diff --git a/has-define-properties-support/docs/types/test.ts b/has-define-properties-support/docs/types/test.ts new file mode 100644 index 00000000..94de8e3d --- /dev/null +++ b/has-define-properties-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasDefinePropertiesSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasDefinePropertiesSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasDefinePropertiesSupport( true ); // $ExpectError + hasDefinePropertiesSupport( [], 123 ); // $ExpectError +} diff --git a/has-define-properties-support/docs/usage.txt b/has-define-properties-support/docs/usage.txt new file mode 100644 index 00000000..5777d76b --- /dev/null +++ b/has-define-properties-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-define-properties-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-define-properties-support/etc/cli_opts.json b/has-define-properties-support/etc/cli_opts.json new file mode 100644 index 00000000..92119c45 --- /dev/null +++ b/has-define-properties-support/etc/cli_opts.json @@ -0,0 +1,15 @@ +{ + "string": [], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-define-properties-support/examples/index.js b/has-define-properties-support/examples/index.js new file mode 100644 index 00000000..cc95340e --- /dev/null +++ b/has-define-properties-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasDefinePropertiesSupport = require( './../lib' ); + +var bool = hasDefinePropertiesSupport(); +if ( bool ) { + console.log( 'Environment has `Object.defineProperties` support.' ); +} else { + console.log( 'Environment lacks `Object.defineProperties` support.' ); +} diff --git a/has-define-properties-support/lib/define_properties.js b/has-define-properties-support/lib/define_properties.js new file mode 100644 index 00000000..3628f2c2 --- /dev/null +++ b/has-define-properties-support/lib/define_properties.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Object.defineProperties === 'function' ) ? Object.defineProperties : null; + + +// EXPORTS // + +module.exports = main; diff --git a/has-define-properties-support/lib/index.js b/has-define-properties-support/lib/index.js new file mode 100644 index 00000000..be0632a9 --- /dev/null +++ b/has-define-properties-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for `Object.defineProperties` support. +* +* @module @stdlib/assert/has-define-properties-support +* +* @example +* var hasDefinePropertiesSupport = require( '@stdlib/assert/has-define-properties-support' ); +* +* var bool = hasDefinePropertiesSupport(); +* // returns +*/ + +// MODULES // + +var hasDefinePropertiesSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasDefinePropertiesSupport; diff --git a/has-define-properties-support/lib/main.js b/has-define-properties-support/lib/main.js new file mode 100644 index 00000000..18d5176d --- /dev/null +++ b/has-define-properties-support/lib/main.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var defineProperties = require( './define_properties.js' ); + + +// MAIN // + +/** +* Tests for `Object.defineProperties` support. +* +* @returns {boolean} boolean indicating if an environment has `Object.defineProperties` support +* +* @example +* var bool = hasDefinePropertiesSupport(); +* // returns +*/ +function hasDefinePropertiesSupport() { + var bool; + + if ( typeof defineProperties !== 'function' ) { + return false; + } + // Test basic support... + try { + defineProperties( {}, { + 'x': {} + }); + bool = true; + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasDefinePropertiesSupport; diff --git a/has-define-properties-support/package.json b/has-define-properties-support/package.json new file mode 100644 index 00000000..8c84c1c8 --- /dev/null +++ b/has-define-properties-support/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/has-define-properties-support", + "version": "0.0.0", + "description": "Detect `Object.defineProperties` support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-define-property-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "es6", + "object", + "property", + "properties", + "issupported", + "cli" + ] +} diff --git a/has-define-properties-support/test/test.cli.js b/has-define-properties-support/test/test.cli.js new file mode 100644 index 00000000..eddaec69 --- /dev/null +++ b/has-define-properties-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Object.defineProperties` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-define-properties-support/test/test.js b/has-define-properties-support/test/test.js new file mode 100644 index 00000000..21a7b0b5 --- /dev/null +++ b/has-define-properties-support/test/test.js @@ -0,0 +1,81 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasDefineProperties = ( typeof Object.defineProperties === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Object.defineProperties` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasDefineProperties ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + + mocked = proxyquire( './../lib/main.js', { + './define_properties.js': Mock + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + t.end(); + + function Mock( obj ) { + return obj; + } +}); + +tape( 'if `Object.defineProperties` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasDefineProperties ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './define_properties.js': Mock + }); + t.strictEqual( mocked(), false, 'detection result is `false` (mocked)' ); + t.end(); + + function Mock() { + throw new Error( 'not supported' ); + } +}); diff --git a/has-define-property-support/README.md b/has-define-property-support/README.md new file mode 100644 index 00000000..ce59ff6d --- /dev/null +++ b/has-define-property-support/README.md @@ -0,0 +1,113 @@ + + +# `Object.defineProperty` Support + +> Detect [`Object.defineProperty`][mdn-define-property] support. + +
+ +## Usage + +```javascript +var hasDefinePropertySupport = require( '@stdlib/assert/has-define-property-support' ); +``` + +#### hasDefinePropertySupport() + +Detects if a runtime environment supports [`Object.defineProperty`][mdn-define-property]. + +```javascript +var bool = hasDefinePropertySupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasDefinePropertySupport = require( '@stdlib/assert/has-define-property-support' ); + +var bool = hasDefinePropertySupport(); +if ( bool ) { + console.log( 'Environment has `Object.defineProperty` support.' ); +} else { + console.log( 'Environment lacks `Object.defineProperty` support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-define-property-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-define-property-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-define-property-support/benchmark/benchmark.js b/has-define-property-support/benchmark/benchmark.js new file mode 100644 index 00000000..3794aaef --- /dev/null +++ b/has-define-property-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasDefinePropertySupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasDefinePropertySupport(); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-define-property-support/bin/cli b/has-define-property-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-define-property-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-define-property-support/docs/repl.txt b/has-define-property-support/docs/repl.txt new file mode 100644 index 00000000..e45b959d --- /dev/null +++ b/has-define-property-support/docs/repl.txt @@ -0,0 +1,18 @@ + +{{alias}}() + Tests for `Object.defineProperty` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `Object.defineProperty` + support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-define-property-support/docs/types/index.d.ts b/has-define-property-support/docs/types/index.d.ts new file mode 100644 index 00000000..4ec35ea3 --- /dev/null +++ b/has-define-property-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for `Object.defineProperty` support. +* +* @returns boolean indicating if an environment has `Object.defineProperty` support +* +* @example +* var bool = hasDefinePropertySupport(); +* // returns +*/ +declare function hasDefinePropertySupport(): boolean; + + +// EXPORTS // + +export = hasDefinePropertySupport; diff --git a/has-define-property-support/docs/types/test.ts b/has-define-property-support/docs/types/test.ts new file mode 100644 index 00000000..c4b9feca --- /dev/null +++ b/has-define-property-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasDefinePropertySupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasDefinePropertySupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasDefinePropertySupport( true ); // $ExpectError + hasDefinePropertySupport( [], 123 ); // $ExpectError +} diff --git a/has-define-property-support/docs/usage.txt b/has-define-property-support/docs/usage.txt new file mode 100644 index 00000000..d4df2709 --- /dev/null +++ b/has-define-property-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-define-property-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-define-property-support/etc/cli_opts.json b/has-define-property-support/etc/cli_opts.json new file mode 100644 index 00000000..92119c45 --- /dev/null +++ b/has-define-property-support/etc/cli_opts.json @@ -0,0 +1,15 @@ +{ + "string": [], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-define-property-support/examples/index.js b/has-define-property-support/examples/index.js new file mode 100644 index 00000000..baef9a39 --- /dev/null +++ b/has-define-property-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasDefinePropertySupport = require( './../lib' ); + +var bool = hasDefinePropertySupport(); +if ( bool ) { + console.log( 'Environment has `Object.defineProperty` support.' ); +} else { + console.log( 'Environment lacks `Object.defineProperty` support.' ); +} diff --git a/has-define-property-support/lib/define_property.js b/has-define-property-support/lib/define_property.js new file mode 100644 index 00000000..1cc1d78c --- /dev/null +++ b/has-define-property-support/lib/define_property.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Object.defineProperty === 'function' ) ? Object.defineProperty : null; + + +// EXPORTS // + +module.exports = main; diff --git a/has-define-property-support/lib/index.js b/has-define-property-support/lib/index.js new file mode 100644 index 00000000..4d4f26d6 --- /dev/null +++ b/has-define-property-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for `Object.defineProperty` support. +* +* @module @stdlib/assert/has-define-property-support +* +* @example +* var hasDefinePropertySupport = require( '@stdlib/assert/has-define-property-support' ); +* +* var bool = hasDefinePropertySupport(); +* // returns +*/ + +// MODULES // + +var hasDefinePropertySupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasDefinePropertySupport; diff --git a/has-define-property-support/lib/main.js b/has-define-property-support/lib/main.js new file mode 100644 index 00000000..365c9be9 --- /dev/null +++ b/has-define-property-support/lib/main.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var defineProperty = require( './define_property.js' ); + + +// MAIN // + +/** +* Tests for `Object.defineProperty` support. +* +* @returns {boolean} boolean indicating if an environment has `Object.defineProperty` support +* +* @example +* var bool = hasDefinePropertySupport(); +* // returns +*/ +function hasDefinePropertySupport() { + var bool; + + if ( typeof defineProperty !== 'function' ) { + return false; + } + // Test basic support... + try { + defineProperty( {}, 'x', {} ); + bool = true; + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasDefinePropertySupport; diff --git a/has-define-property-support/package.json b/has-define-property-support/package.json new file mode 100644 index 00000000..066e8e02 --- /dev/null +++ b/has-define-property-support/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/has-define-property-support", + "version": "0.0.0", + "description": "Detect `Object.defineProperty` support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-define-property-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "es6", + "object", + "property", + "properties", + "issupported", + "cli" + ] +} diff --git a/has-define-property-support/test/test.cli.js b/has-define-property-support/test/test.cli.js new file mode 100644 index 00000000..24eddd4e --- /dev/null +++ b/has-define-property-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Object.defineProperty` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-define-property-support/test/test.js b/has-define-property-support/test/test.js new file mode 100644 index 00000000..ba974bef --- /dev/null +++ b/has-define-property-support/test/test.js @@ -0,0 +1,81 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasDefineProperty = ( typeof Object.defineProperty === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Object.defineProperty` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasDefineProperty ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + + mocked = proxyquire( './../lib/main.js', { + './define_property.js': Mock + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + t.end(); + + function Mock( obj ) { + return obj; + } +}); + +tape( 'if `Object.defineProperty` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasDefineProperty ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './define_property.js': Mock + }); + t.strictEqual( mocked(), false, 'detection result is `false` (mocked)' ); + t.end(); + + function Mock() { + throw new Error( 'not supported' ); + } +}); diff --git a/has-float32array-support/README.md b/has-float32array-support/README.md new file mode 100644 index 00000000..199c7be4 --- /dev/null +++ b/has-float32array-support/README.md @@ -0,0 +1,113 @@ + + +# Float32Array Support + +> Detect native [`Float32Array`][mdn-float32array] support. + +
+ +## Usage + +```javascript +var hasFloat32ArraySupport = require( '@stdlib/assert/has-float32array-support' ); +``` + +#### hasFloat32ArraySupport() + +Detects if a runtime environment supports [`Float32Array`][mdn-float32array]. + +```javascript +var bool = hasFloat32ArraySupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasFloat32ArraySupport = require( '@stdlib/assert/has-float32array-support' ); + +var bool = hasFloat32ArraySupport(); +if ( bool ) { + console.log( 'Environment has Float32Array support.' ); +} else { + console.log( 'Environment lacks Float32Array support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-float32array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-float32array-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-float32array-support/benchmark/benchmark.js b/has-float32array-support/benchmark/benchmark.js new file mode 100644 index 00000000..dc48ae40 --- /dev/null +++ b/has-float32array-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasFloat32ArraySupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasFloat32ArraySupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-float32array-support/bin/cli b/has-float32array-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-float32array-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-float32array-support/docs/repl.txt b/has-float32array-support/docs/repl.txt new file mode 100644 index 00000000..422302ed --- /dev/null +++ b/has-float32array-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `Float32Array` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `Float32Array` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-float32array-support/docs/types/index.d.ts b/has-float32array-support/docs/types/index.d.ts new file mode 100644 index 00000000..1dc6843a --- /dev/null +++ b/has-float32array-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Float32Array` support. +* +* @returns boolean indicating if an environment has `Float32Array` support +* +* @example +* var bool = hasFloat32ArraySupport(); +* // returns +*/ +declare function hasFloat32ArraySupport(): boolean; + + +// EXPORTS // + +export = hasFloat32ArraySupport; diff --git a/has-float32array-support/docs/types/test.ts b/has-float32array-support/docs/types/test.ts new file mode 100644 index 00000000..94121f96 --- /dev/null +++ b/has-float32array-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasFloat32ArraySupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasFloat32ArraySupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasFloat32ArraySupport( true ); // $ExpectError + hasFloat32ArraySupport( [], 123 ); // $ExpectError +} diff --git a/has-float32array-support/docs/usage.txt b/has-float32array-support/docs/usage.txt new file mode 100644 index 00000000..c2de8315 --- /dev/null +++ b/has-float32array-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-float32array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-float32array-support/etc/cli_opts.json b/has-float32array-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-float32array-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-float32array-support/examples/index.js b/has-float32array-support/examples/index.js new file mode 100644 index 00000000..9ac57601 --- /dev/null +++ b/has-float32array-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasFloat32ArraySupport = require( './../lib' ); + +var bool = hasFloat32ArraySupport(); +if ( bool ) { + console.log( 'Environment has Float32Array support.' ); +} else { + console.log( 'Environment lacks Float32Array support.' ); +} diff --git a/has-float32array-support/lib/float32array.js b/has-float32array-support/lib/float32array.js new file mode 100644 index 00000000..d858a798 --- /dev/null +++ b/has-float32array-support/lib/float32array.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Float32Array === 'function' ) ? Float32Array : null; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = main; diff --git a/has-float32array-support/lib/index.js b/has-float32array-support/lib/index.js new file mode 100644 index 00000000..e8fa23c9 --- /dev/null +++ b/has-float32array-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Float32Array` support. +* +* @module @stdlib/assert/has-float32array-support +* +* @example +* var hasFloat32ArraySupport = require( '@stdlib/assert/has-float32array-support' ); +* +* var bool = hasFloat32ArraySupport(); +* // returns +*/ + +// MODULES // + +var hasFloat32ArraySupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasFloat32ArraySupport; diff --git a/has-float32array-support/lib/main.js b/has-float32array-support/lib/main.js new file mode 100644 index 00000000..f3cf6a8d --- /dev/null +++ b/has-float32array-support/lib/main.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isFloat32Array = require( '@stdlib/assert/is-float32array' ); +var PINF = require( '@stdlib/constants/math/float64-pinf' ); +var GlobalFloat32Array = require( './float32array.js' ); + + +// MAIN // + +/** +* Tests for native `Float32Array` support. +* +* @returns {boolean} boolean indicating if an environment has `Float32Array` support +* +* @example +* var bool = hasFloat32ArraySupport(); +* // returns +*/ +function hasFloat32ArraySupport() { + var bool; + var arr; + + if ( typeof GlobalFloat32Array !== 'function' ) { + return false; + } + // Test basic support... + try { + arr = new GlobalFloat32Array( [ 1.0, 3.14, -3.14, 5.0e40 ] ); + bool = ( + isFloat32Array( arr ) && + arr[ 0 ] === 1.0 && + arr[ 1 ] === 3.140000104904175 && + arr[ 2 ] === -3.140000104904175 && + arr[ 3 ] === PINF + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasFloat32ArraySupport; diff --git a/has-float32array-support/package.json b/has-float32array-support/package.json new file mode 100644 index 00000000..3b9aae50 --- /dev/null +++ b/has-float32array-support/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/has-float32array-support", + "version": "0.0.0", + "description": "Detect native Float32Array support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-float32array-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "float32array", + "es2015", + "es6", + "typed array", + "typed-array", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-float32array-support/test/test.cli.js b/has-float32array-support/test/test.cli.js new file mode 100644 index 00000000..48c96998 --- /dev/null +++ b/has-float32array-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Float32Array` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-float32array-support/test/test.js b/has-float32array-support/test/test.js new file mode 100644 index 00000000..cb7dd339 --- /dev/null +++ b/has-float32array-support/test/test.js @@ -0,0 +1,135 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Number = require( '@stdlib/number/ctor' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasFloat32Array = ( typeof Float32Array === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Float32Array` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasFloat32Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './float32array.js': Mock, + '@stdlib/assert/is-float32array': isArray + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock() { + return [ + 1.0, + 3.140000104904175, + -3.140000104904175, + Number.POSITIVE_INFINITY + ]; + } +}); + +tape( 'if `Float32Array` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasFloat32Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './float32array.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './float32array.js': Mock1 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './float32array.js': Mock2, + '@stdlib/assert/is-float32array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './float32array.js': Mock3, + '@stdlib/assert/is-float32array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './float32array.js': Mock4 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock1() { + // Not a typed array: + return []; + } + + function Mock2() { + // Does not lose precision... + return [ 1.0, 3.14, -3.14, Number.POSITIVE_INFINITY ]; + } + + function Mock3() { + // Does not overflow... + return [ 1.0, 3.140000104904175, -3.140000104904175, 5.0e40 ]; + } + + function Mock4() { + throw new Error( 'beep' ); + } +}); diff --git a/has-float64array-support/README.md b/has-float64array-support/README.md new file mode 100644 index 00000000..d677eeb6 --- /dev/null +++ b/has-float64array-support/README.md @@ -0,0 +1,113 @@ + + +# Float64Array Support + +> Detect native [`Float64Array`][mdn-float64array] support. + +
+ +## Usage + +```javascript +var hasFloat64ArraySupport = require( '@stdlib/assert/has-float64array-support' ); +``` + +#### hasFloat64ArraySupport() + +Detects if a runtime environment supports [`Float64Array`][mdn-float64array]. + +```javascript +var bool = hasFloat64ArraySupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasFloat64ArraySupport = require( '@stdlib/assert/has-float64array-support' ); + +var bool = hasFloat64ArraySupport(); +if ( bool ) { + console.log( 'Environment has Float64Array support.' ); +} else { + console.log( 'Environment lacks Float64Array support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-float64array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-float64array-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-float64array-support/benchmark/benchmark.js b/has-float64array-support/benchmark/benchmark.js new file mode 100644 index 00000000..76809e31 --- /dev/null +++ b/has-float64array-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasFloat64ArraySupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasFloat64ArraySupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-float64array-support/bin/cli b/has-float64array-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-float64array-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-float64array-support/docs/repl.txt b/has-float64array-support/docs/repl.txt new file mode 100644 index 00000000..1abed503 --- /dev/null +++ b/has-float64array-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `Float64Array` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `Float64Array` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-float64array-support/docs/types/index.d.ts b/has-float64array-support/docs/types/index.d.ts new file mode 100644 index 00000000..868f1f72 --- /dev/null +++ b/has-float64array-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Float64Array` support. +* +* @returns boolean indicating if an environment has `Float64Array` support +* +* @example +* var bool = hasFloat64ArraySupport(); +* // returns +*/ +declare function hasFloat64ArraySupport(): boolean; + + +// EXPORTS // + +export = hasFloat64ArraySupport; diff --git a/has-float64array-support/docs/types/test.ts b/has-float64array-support/docs/types/test.ts new file mode 100644 index 00000000..682a4982 --- /dev/null +++ b/has-float64array-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasFloat64ArraySupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasFloat64ArraySupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasFloat64ArraySupport( true ); // $ExpectError + hasFloat64ArraySupport( [], 123 ); // $ExpectError +} diff --git a/has-float64array-support/docs/usage.txt b/has-float64array-support/docs/usage.txt new file mode 100644 index 00000000..acb4b705 --- /dev/null +++ b/has-float64array-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-float64array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-float64array-support/etc/cli_opts.json b/has-float64array-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-float64array-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-float64array-support/examples/index.js b/has-float64array-support/examples/index.js new file mode 100644 index 00000000..8590569c --- /dev/null +++ b/has-float64array-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasFloat64ArraySupport = require( './../lib' ); + +var bool = hasFloat64ArraySupport(); +if ( bool ) { + console.log( 'Environment has Float64Array support.' ); +} else { + console.log( 'Environment lacks Float64Array support.' ); +} diff --git a/has-float64array-support/lib/float64array.js b/has-float64array-support/lib/float64array.js new file mode 100644 index 00000000..1b1a6409 --- /dev/null +++ b/has-float64array-support/lib/float64array.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Float64Array === 'function' ) ? Float64Array : null; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = main; diff --git a/has-float64array-support/lib/index.js b/has-float64array-support/lib/index.js new file mode 100644 index 00000000..87f77c53 --- /dev/null +++ b/has-float64array-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Float64Array` support. +* +* @module @stdlib/assert/has-float64array-support +* +* @example +* var hasFloat64ArraySupport = require( '@stdlib/assert/has-float64array-support' ); +* +* var bool = hasFloat64ArraySupport(); +* // returns +*/ + +// MODULES // + +var hasFloat64ArraySupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasFloat64ArraySupport; diff --git a/has-float64array-support/lib/main.js b/has-float64array-support/lib/main.js new file mode 100644 index 00000000..1d8b1d0e --- /dev/null +++ b/has-float64array-support/lib/main.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isFloat64Array = require( '@stdlib/assert/is-float64array' ); +var GlobalFloat64Array = require( './float64array.js' ); + + +// MAIN // + +/** +* Tests for native `Float64Array` support. +* +* @returns {boolean} boolean indicating if an environment has `Float64Array` support +* +* @example +* var bool = hasFloat64ArraySupport(); +* // returns +*/ +function hasFloat64ArraySupport() { + var bool; + var arr; + + if ( typeof GlobalFloat64Array !== 'function' ) { + return false; + } + // Test basic support... + try { + arr = new GlobalFloat64Array( [ 1.0, 3.14, -3.14, NaN ] ); + bool = ( + isFloat64Array( arr ) && + arr[ 0 ] === 1.0 && + arr[ 1 ] === 3.14 && + arr[ 2 ] === -3.14 && + arr[ 3 ] !== arr[ 3 ] + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasFloat64ArraySupport; diff --git a/has-float64array-support/package.json b/has-float64array-support/package.json new file mode 100644 index 00000000..8e3af986 --- /dev/null +++ b/has-float64array-support/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/has-float64array-support", + "version": "0.0.0", + "description": "Detect native Float64Array support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-float64array-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "float64array", + "es2015", + "es6", + "typed array", + "typed-array", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-float64array-support/test/test.cli.js b/has-float64array-support/test/test.cli.js new file mode 100644 index 00000000..bb7525da --- /dev/null +++ b/has-float64array-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Float64Array` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-float64array-support/test/test.js b/has-float64array-support/test/test.js new file mode 100644 index 00000000..ad663b30 --- /dev/null +++ b/has-float64array-support/test/test.js @@ -0,0 +1,132 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Float64Array = require( '@stdlib/array/float64' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasFloat64Array = ( typeof Float64Array === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Float64Array` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasFloat64Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './float64array.js': Mock, + '@stdlib/assert/is-float64array': isArray + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock() { + return [ + 1.0, + 3.14, + -3.14, + NaN + ]; + } +}); + +tape( 'if `Float64Array` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasFloat64Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './float64array.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './float64array.js': Mock1 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './float64array.js': Mock2, + '@stdlib/assert/is-float64array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './float64array.js': Mock3, + '@stdlib/assert/is-float64array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './float64array.js': Mock4 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock1() { + // Not a typed array: + return []; + } + + function Mock2() { + return [ 0.0, 0.0, 0.0, NaN ]; + } + + function Mock3() { + return [ 1.0, 3.14, -3.14, 0.0 ]; + } + + function Mock4() { + throw new Error( 'beep' ); + } +}); diff --git a/has-function-name-support/README.md b/has-function-name-support/README.md new file mode 100644 index 00000000..e81a2b5a --- /dev/null +++ b/has-function-name-support/README.md @@ -0,0 +1,113 @@ + + +# Function Name Support + +> Detect native function [`name`][function-name] support. + +
+ +## Usage + +```javascript +var hasFunctionNameSupport = require( '@stdlib/assert/has-function-name-support' ); +``` + +#### hasFunctionNameSupport() + +Detects if a runtime environment supports the ES2015 function [`name`][function-name] property. + +```javascript +var bool = hasFunctionNameSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasFunctionNameSupport = require( '@stdlib/assert/has-function-name-support' ); + +var bool = hasFunctionNameSupport(); +if ( bool ) { + console.log( 'Environment has function name support.' ); +} else { + console.log( 'Environment lacks function name support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-function-name-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-function-name-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-function-name-support/benchmark/benchmark.js b/has-function-name-support/benchmark/benchmark.js new file mode 100644 index 00000000..5d6b16f9 --- /dev/null +++ b/has-function-name-support/benchmark/benchmark.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasFunctionNameSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasFunctionNameSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/has-function-name-support/bin/cli b/has-function-name-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-function-name-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-function-name-support/docs/repl.txt b/has-function-name-support/docs/repl.txt new file mode 100644 index 00000000..824bbf5a --- /dev/null +++ b/has-function-name-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native function `name` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has function `name` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-function-name-support/docs/types/index.d.ts b/has-function-name-support/docs/types/index.d.ts new file mode 100644 index 00000000..0bf6e22a --- /dev/null +++ b/has-function-name-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native function `name` support. +* +* @returns boolean indicating if an environment has function `name` support +* +* @example +* var bool = hasFunctionNameSupport(); +* // returns +*/ +declare function hasFunctionNameSupport(): boolean; + + +// EXPORTS // + +export = hasFunctionNameSupport; diff --git a/has-function-name-support/docs/types/test.ts b/has-function-name-support/docs/types/test.ts new file mode 100644 index 00000000..b6c01060 --- /dev/null +++ b/has-function-name-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasFunctionNameSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasFunctionNameSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasFunctionNameSupport( true ); // $ExpectError + hasFunctionNameSupport( [], 123 ); // $ExpectError +} diff --git a/has-function-name-support/docs/usage.txt b/has-function-name-support/docs/usage.txt new file mode 100644 index 00000000..3c2963a9 --- /dev/null +++ b/has-function-name-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-function-name-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-function-name-support/etc/cli_opts.json b/has-function-name-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-function-name-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-function-name-support/examples/index.js b/has-function-name-support/examples/index.js new file mode 100644 index 00000000..0eaaf940 --- /dev/null +++ b/has-function-name-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasFunctionNameSupport = require( './../lib' ); + +var bool = hasFunctionNameSupport(); +if ( bool ) { + console.log( 'Environment has function name support.' ); +} else { + console.log( 'Environment lacks function name support.' ); +} diff --git a/has-function-name-support/lib/foo.js b/has-function-name-support/lib/foo.js new file mode 100644 index 00000000..5125fd49 --- /dev/null +++ b/has-function-name-support/lib/foo.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Dummy function. +* +* @private +*/ +function foo() { + // No-op... +} + + +// EXPORTS // + +module.exports = foo; diff --git a/has-function-name-support/lib/index.js b/has-function-name-support/lib/index.js new file mode 100644 index 00000000..67bbcd61 --- /dev/null +++ b/has-function-name-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native function `name` support. +* +* @module @stdlib/assert/has-function-name-support +* +* @example +* var hasFunctionNameSupport = require( '@stdlib/assert/has-function-name-support' ); +* +* var bool = hasFunctionNameSupport(); +* // returns +*/ + +// MODULES // + +var hasFunctionNameSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasFunctionNameSupport; diff --git a/has-function-name-support/lib/main.js b/has-function-name-support/lib/main.js new file mode 100644 index 00000000..51bb45a9 --- /dev/null +++ b/has-function-name-support/lib/main.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var foo = require( './foo.js' ); + + +// MAIN // + +/** +* Tests for native function `name` support. +* +* @returns {boolean} boolean indicating if an environment has function `name` support +* +* @example +* var bool = hasFunctionNameSupport(); +* // returns +*/ +function hasFunctionNameSupport() { + return ( foo.name === 'foo' ); +} + + +// EXPORTS // + +module.exports = hasFunctionNameSupport; diff --git a/has-function-name-support/package.json b/has-function-name-support/package.json new file mode 100644 index 00000000..a1d6d589 --- /dev/null +++ b/has-function-name-support/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/has-function-name-support", + "version": "0.0.0", + "description": "Detect native function name support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-function-name-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "function", + "name", + "es2015", + "es6", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-function-name-support/test/test.cli.js b/has-function-name-support/test/test.cli.js new file mode 100644 index 00000000..e9ec2a3b --- /dev/null +++ b/has-function-name-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether functions have a `name` property', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-function-name-support/test/test.foo.js b/has-function-name-support/test/test.foo.js new file mode 100644 index 00000000..c45c17a5 --- /dev/null +++ b/has-function-name-support/test/test.foo.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var foo = require( './../lib/foo.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof foo, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function does not take any arguments', function test( t ) { + t.strictEqual( foo.length, 0, 'number of parameters is equal to zero' ); + t.end(); +}); + +tape( 'the function returns `undefined`', function test( t ) { + t.strictEqual( foo(), void 0, 'returns undefined' ); + t.end(); +}); + +tape( 'the function should be empty', function test( t ) { + // NOTE: this will fail if code is instrumented for test coverage. + // t.strictEqual( foo.toString(), 'function foo(){}', 'is empty' ); + t.end(); +}); diff --git a/has-function-name-support/test/test.js b/has-function-name-support/test/test.js new file mode 100644 index 00000000..b5f5686d --- /dev/null +++ b/has-function-name-support/test/test.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var detect = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if functions have a `name` property, detection result is `true`', function test( t ) { + var detect; + var stub; + + stub = {}; + stub.name = 'foo'; + + detect = proxyquire( './../lib/main.js', { + './foo.js': stub + }); + + t.strictEqual( detect(), true, 'detection result is `true`' ); + t.end(); +}); + +tape( 'if functions do not have a `name` property, detection result is `false`', function test( t ) { + var detect; + var stub; + + stub = {}; + stub.name = null; + + detect = proxyquire( './../lib/main.js', { + './foo.js': stub + }); + + t.strictEqual( detect(), false, 'detection result is `false`' ); + t.end(); +}); diff --git a/has-generator-support/README.md b/has-generator-support/README.md new file mode 100644 index 00000000..ef2b24b2 --- /dev/null +++ b/has-generator-support/README.md @@ -0,0 +1,125 @@ + + +# Generator Support + +> Detect native [`generator function`][generator-function] support. + +
+ +## Usage + +```javascript +var hasGeneratorSupport = require( '@stdlib/assert/has-generator-support' ); +``` + +#### hasGeneratorSupport() + +Detects if a runtime environment supports ES2015 [`generator functions`][generator-function], i.e. `function*()`. + +```javascript +var bool = hasGeneratorSupport(); +// returns +``` + +
+ + + +
+ +## Notes + +- The implementation uses code evaluation, which may be problematic in browser contexts enforcing a strict [content security policy][mdn-csp] (CSP). + +
+ + + +
+ +## Examples + + + +```javascript +var hasGeneratorSupport = require( '@stdlib/assert/has-generator-support' ); + +var bool = hasGeneratorSupport(); +if ( bool ) { + console.log( 'Environment has native generator function support.' ); +} else { + console.log( 'Environment lacks native generator function support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-generator-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-generator-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-generator-support/benchmark/benchmark.js b/has-generator-support/benchmark/benchmark.js new file mode 100644 index 00000000..0e892710 --- /dev/null +++ b/has-generator-support/benchmark/benchmark.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasGeneratorSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasGeneratorSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/has-generator-support/bin/cli b/has-generator-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-generator-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-generator-support/docs/repl.txt b/has-generator-support/docs/repl.txt new file mode 100644 index 00000000..96bce34b --- /dev/null +++ b/has-generator-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests whether an environment supports native generator functions. + + Returns + ------- + bool: boolean + Boolean indicating if an environment support generator functions. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-generator-support/docs/types/index.d.ts b/has-generator-support/docs/types/index.d.ts new file mode 100644 index 00000000..0c832834 --- /dev/null +++ b/has-generator-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `function*()` support. +* +* @returns boolean indicating if an environment has native `function*()` support +* +* @example +* var bool = hasGeneratorSupport(); +* // returns +*/ +declare function hasGeneratorSupport(): boolean; + + +// EXPORTS // + +export = hasGeneratorSupport; diff --git a/has-generator-support/docs/types/test.ts b/has-generator-support/docs/types/test.ts new file mode 100644 index 00000000..07f60e1b --- /dev/null +++ b/has-generator-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasGeneratorSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasGeneratorSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasGeneratorSupport( true ); // $ExpectError + hasGeneratorSupport( [], 123 ); // $ExpectError +} diff --git a/has-generator-support/docs/usage.txt b/has-generator-support/docs/usage.txt new file mode 100644 index 00000000..ff22ff3d --- /dev/null +++ b/has-generator-support/docs/usage.txt @@ -0,0 +1,7 @@ + +Usage: has-generator-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. diff --git a/has-generator-support/etc/cli_opts.json b/has-generator-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-generator-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-generator-support/examples/index.js b/has-generator-support/examples/index.js new file mode 100644 index 00000000..25445c2f --- /dev/null +++ b/has-generator-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasGeneratorSupport = require( './../lib' ); + +var bool = hasGeneratorSupport(); +if ( bool ) { + console.log( 'Environment has native generator support.' ); +} else { + console.log( 'Environment lacks native generator support.' ); +} diff --git a/has-generator-support/lib/index.js b/has-generator-support/lib/index.js new file mode 100644 index 00000000..0981d875 --- /dev/null +++ b/has-generator-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `function*()` support. +* +* @module @stdlib/assert/has-generator-support +* +* @example +* var hasGeneratorSupport = require( '@stdlib/assert/has-generator-support' ); +* +* var bool = hasGeneratorSupport(); +* // returns +*/ + +// MODULES // + +var hasGeneratorSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasGeneratorSupport; diff --git a/has-generator-support/lib/main.js b/has-generator-support/lib/main.js new file mode 100644 index 00000000..65a10216 --- /dev/null +++ b/has-generator-support/lib/main.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var evil = require( '@stdlib/utils/eval' ); + + +// MAIN // + +/** +* Tests for native `function*()` support. +* +* @returns {boolean} boolean indicating if an environment has native `function*()` support +* +* @example +* var bool = hasGeneratorSupport(); +* // returns +*/ +function hasGeneratorSupport() { + var bool; + try { + evil( '"use strict"; (function* () {})' ); + bool = true; + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasGeneratorSupport; diff --git a/has-generator-support/package.json b/has-generator-support/package.json new file mode 100644 index 00000000..321db8a2 --- /dev/null +++ b/has-generator-support/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/has-generator-support", + "version": "0.0.0", + "description": "Detect native generator function support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-generator-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "generator", + "generator function", + "yield", + "es2015", + "es6", + "support", + "has", + "hasgenerator", + "hasgenerators", + "native", + "issupported", + "cli" + ] +} diff --git a/has-generator-support/test/test.cli.js b/has-generator-support/test/test.cli.js new file mode 100644 index 00000000..469ab7f9 --- /dev/null +++ b/has-generator-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `function*()` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-generator-support/test/test.js b/has-generator-support/test/test.js new file mode 100644 index 00000000..a673aa0d --- /dev/null +++ b/has-generator-support/test/test.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var detect = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `function*()` is supported, detection result is `true`', function test( t ) { + var detect = proxyquire( './../lib/main.js', { + '@stdlib/utils/eval': stub + }); + + t.ok( detect(), 'detection result is `true`' ); + t.end(); + + function stub() { + return 'beep'; + } +}); + +tape( 'if `function*()` is not supported, detection result is `false`', function test( t ) { + var detect = proxyquire( './../lib/main.js', { + '@stdlib/utils/eval': stub + }); + + t.notOk( detect(), 'detection result is `false`' ); + t.end(); + + function stub() { + throw new Error( 'boop' ); + } +}); diff --git a/has-globalthis-support/README.md b/has-globalthis-support/README.md new file mode 100644 index 00000000..36348f5e --- /dev/null +++ b/has-globalthis-support/README.md @@ -0,0 +1,113 @@ + + +# globalThis Support + +> Detect [`globalThis`][mdn-global-this] support. + +
+ +## Usage + +```javascript +var hasGlobalThisSupport = require( '@stdlib/assert/has-globalthis-support' ); +``` + +#### hasGlobalThisSupport() + +Detects if a runtime environment supports [`globalThis`][mdn-global-this]. + +```javascript +var bool = hasGlobalThisSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasGlobalThisSupport = require( '@stdlib/assert/has-globalthis-support' ); + +var bool = hasGlobalThisSupport(); +if ( bool ) { + console.log( 'Environment has `globalThis` support.' ); +} else { + console.log( 'Environment lacks `globalThis` support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-globalthis-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-globalthis-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-globalthis-support/benchmark/benchmark.js b/has-globalthis-support/benchmark/benchmark.js new file mode 100644 index 00000000..14d38f88 --- /dev/null +++ b/has-globalthis-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasGlobalThisSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasGlobalThisSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-globalthis-support/bin/cli b/has-globalthis-support/bin/cli new file mode 100644 index 00000000..59aead4e --- /dev/null +++ b/has-globalthis-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-globalthis-support/docs/repl.txt b/has-globalthis-support/docs/repl.txt new file mode 100644 index 00000000..c3522b72 --- /dev/null +++ b/has-globalthis-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for `globalThis` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `globalThis` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-globalthis-support/docs/types/index.d.ts b/has-globalthis-support/docs/types/index.d.ts new file mode 100644 index 00000000..4701db10 --- /dev/null +++ b/has-globalthis-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for `globalThis` support. +* +* @returns boolean indicating if an environment has `globalThis` support +* +* @example +* var bool = hasGlobalThisSupport(); +* // returns +*/ +declare function hasGlobalThisSupport(): boolean; + + +// EXPORTS // + +export = hasGlobalThisSupport; diff --git a/has-globalthis-support/docs/types/test.ts b/has-globalthis-support/docs/types/test.ts new file mode 100644 index 00000000..1dd24ac5 --- /dev/null +++ b/has-globalthis-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasGlobalThisSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasGlobalThisSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasGlobalThisSupport( true ); // $ExpectError + hasGlobalThisSupport( [], 123 ); // $ExpectError +} diff --git a/has-globalthis-support/docs/usage.txt b/has-globalthis-support/docs/usage.txt new file mode 100644 index 00000000..7269d4d7 --- /dev/null +++ b/has-globalthis-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-globalthis-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-globalthis-support/etc/cli_opts.json b/has-globalthis-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-globalthis-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-globalthis-support/examples/index.js b/has-globalthis-support/examples/index.js new file mode 100644 index 00000000..609ef8d1 --- /dev/null +++ b/has-globalthis-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasGlobalThisSupport = require( './../lib' ); + +var bool = hasGlobalThisSupport(); +if ( bool ) { + console.log( 'Environment has `globalThis` support.' ); +} else { + console.log( 'Environment lacks `globalThis` support.' ); +} diff --git a/has-globalthis-support/lib/index.js b/has-globalthis-support/lib/index.js new file mode 100644 index 00000000..3b69d190 --- /dev/null +++ b/has-globalthis-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for `globalThis` support. +* +* @module @stdlib/assert/has-globalthis-support +* +* @example +* var hasGlobalThisSupport = require( '@stdlib/assert/has-globalthis-support' ); +* +* var bool = hasGlobalThisSupport(); +* // returns +*/ + +// MODULES // + +var hasGlobalThisSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasGlobalThisSupport; diff --git a/has-globalthis-support/lib/main.js b/has-globalthis-support/lib/main.js new file mode 100644 index 00000000..73f75fc3 --- /dev/null +++ b/has-globalthis-support/lib/main.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests for `globalThis` support. +* +* @returns {boolean} boolean indicating if an environment has `globalThis` support +* +* @example +* var bool = hasGlobalThisSupport(); +* // returns +*/ +function hasGlobalThisSupport() { + return ( + typeof globalThis === 'object' && + typeof globalThis.setTimeout === 'function' // eslint-disable-line no-undef + ); +} + + +// EXPORTS // + +module.exports = hasGlobalThisSupport; diff --git a/has-globalthis-support/package.json b/has-globalthis-support/package.json new file mode 100644 index 00000000..593726ff --- /dev/null +++ b/has-globalthis-support/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/has-globalthis-support", + "version": "0.0.0", + "description": "Detect globalThis support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-globalthis-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "global", + "this", + "window", + "self", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-globalthis-support/test/test.cli.js b/has-globalthis-support/test/test.cli.js new file mode 100644 index 00000000..e01b390a --- /dev/null +++ b/has-globalthis-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides `globalThis` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-globalthis-support/test/test.js b/has-globalthis-support/test/test.js new file mode 100644 index 00000000..bc6214cb --- /dev/null +++ b/has-globalthis-support/test/test.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasGlobalThis = ( typeof globalThis === 'object' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `globalThis` is supported, detection result is `true`', function test( t ) { + if ( hasGlobalThis ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + t.end(); +}); diff --git a/has-int16array-support/README.md b/has-int16array-support/README.md new file mode 100644 index 00000000..aaca40a9 --- /dev/null +++ b/has-int16array-support/README.md @@ -0,0 +1,113 @@ + + +# Int16Array Support + +> Detect native [`Int16Array`][mdn-int16array] support. + +
+ +## Usage + +```javascript +var hasInt16ArraySupport = require( '@stdlib/assert/has-int16array-support' ); +``` + +#### hasInt16ArraySupport() + +Detects if a runtime environment supports [`Int16Array`][mdn-int16array]. + +```javascript +var bool = hasInt16ArraySupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasInt16ArraySupport = require( '@stdlib/assert/has-int16array-support' ); + +var bool = hasInt16ArraySupport(); +if ( bool ) { + console.log( 'Environment has Int16Array support.' ); +} else { + console.log( 'Environment lacks Int16Array support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-int16array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-int16array-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-int16array-support/benchmark/benchmark.js b/has-int16array-support/benchmark/benchmark.js new file mode 100644 index 00000000..ccc90044 --- /dev/null +++ b/has-int16array-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasInt16ArraySupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasInt16ArraySupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-int16array-support/bin/cli b/has-int16array-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-int16array-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-int16array-support/docs/repl.txt b/has-int16array-support/docs/repl.txt new file mode 100644 index 00000000..e0282f92 --- /dev/null +++ b/has-int16array-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `Int16Array` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `Int16Array` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-int16array-support/docs/types/index.d.ts b/has-int16array-support/docs/types/index.d.ts new file mode 100644 index 00000000..7a873aa9 --- /dev/null +++ b/has-int16array-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Int16Array` support. +* +* @returns boolean indicating if an environment has `Int16Array` support +* +* @example +* var bool = hasInt16ArraySupport(); +* // returns +*/ +declare function hasInt16ArraySupport(): boolean; + + +// EXPORTS // + +export = hasInt16ArraySupport; diff --git a/has-int16array-support/docs/types/test.ts b/has-int16array-support/docs/types/test.ts new file mode 100644 index 00000000..b8f58014 --- /dev/null +++ b/has-int16array-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasInt16ArraySupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasInt16ArraySupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasInt16ArraySupport( true ); // $ExpectError + hasInt16ArraySupport( [], 123 ); // $ExpectError +} diff --git a/has-int16array-support/docs/usage.txt b/has-int16array-support/docs/usage.txt new file mode 100644 index 00000000..53274e9b --- /dev/null +++ b/has-int16array-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-int16array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-int16array-support/etc/cli_opts.json b/has-int16array-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-int16array-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-int16array-support/examples/index.js b/has-int16array-support/examples/index.js new file mode 100644 index 00000000..f930d31d --- /dev/null +++ b/has-int16array-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasInt16ArraySupport = require( './../lib' ); + +var bool = hasInt16ArraySupport(); +if ( bool ) { + console.log( 'Environment has Int16Array support.' ); +} else { + console.log( 'Environment lacks Int16Array support.' ); +} diff --git a/has-int16array-support/lib/index.js b/has-int16array-support/lib/index.js new file mode 100644 index 00000000..3445d904 --- /dev/null +++ b/has-int16array-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Int16Array` support. +* +* @module @stdlib/assert/has-int16array-support +* +* @example +* var hasInt16ArraySupport = require( '@stdlib/assert/has-int16array-support' ); +* +* var bool = hasInt16ArraySupport(); +* // returns +*/ + +// MODULES // + +var hasInt16ArraySupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasInt16ArraySupport; diff --git a/has-int16array-support/lib/int16array.js b/has-int16array-support/lib/int16array.js new file mode 100644 index 00000000..e04cf3a5 --- /dev/null +++ b/has-int16array-support/lib/int16array.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Int16Array === 'function' ) ? Int16Array : null; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = main; diff --git a/has-int16array-support/lib/main.js b/has-int16array-support/lib/main.js new file mode 100644 index 00000000..873be48b --- /dev/null +++ b/has-int16array-support/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInt16Array = require( '@stdlib/assert/is-int16array' ); +var INT16_MAX = require( '@stdlib/constants/math/int16-max' ); +var INT16_MIN = require( '@stdlib/constants/math/int16-min' ); +var GlobalInt16Array = require( './int16array.js' ); + + +// MAIN // + +/** +* Tests for native `Int16Array` support. +* +* @returns {boolean} boolean indicating if an environment has `Int16Array` support +* +* @example +* var bool = hasInt16ArraySupport(); +* // returns +*/ +function hasInt16ArraySupport() { + var bool; + var arr; + + if ( typeof GlobalInt16Array !== 'function' ) { + return false; + } + // Test basic support... + try { + arr = new GlobalInt16Array( [ 1, 3.14, -3.14, INT16_MAX+1 ] ); + bool = ( + isInt16Array( arr ) && + arr[ 0 ] === 1 && + arr[ 1 ] === 3 && // truncation + arr[ 2 ] === -3 && // truncation + arr[ 3 ] === INT16_MIN // wrap around + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasInt16ArraySupport; diff --git a/has-int16array-support/package.json b/has-int16array-support/package.json new file mode 100644 index 00000000..959a1bc4 --- /dev/null +++ b/has-int16array-support/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/has-int16array-support", + "version": "0.0.0", + "description": "Detect native Int16Array support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-int16array-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "int16array", + "es2015", + "es6", + "typed array", + "typed-array", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-int16array-support/test/test.cli.js b/has-int16array-support/test/test.cli.js new file mode 100644 index 00000000..50cb2e64 --- /dev/null +++ b/has-int16array-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Int16Array` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-int16array-support/test/test.js b/has-int16array-support/test/test.js new file mode 100644 index 00000000..ce078eee --- /dev/null +++ b/has-int16array-support/test/test.js @@ -0,0 +1,134 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Int16Array = require( '@stdlib/array/int16' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasInt16Array = ( typeof Int16Array === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Int16Array` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasInt16Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './int16array.js': Mock, + '@stdlib/assert/is-int16array': isArray + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock() { + return [ + 1, + 3, + -3, + -32768 + ]; + } +}); + +tape( 'if `Int16Array` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasInt16Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './int16array.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './int16array.js': Mock1 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './int16array.js': Mock2, + '@stdlib/assert/is-int16array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './int16array.js': Mock3, + '@stdlib/assert/is-int16array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './int16array.js': Mock4 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock1() { + // Not a typed array: + return []; + } + + function Mock2() { + // Does not truncate... + return [ 1, 3.14, -3.14, -32768 ]; + } + + function Mock3() { + // Does not wrap around... + return [ 1, 3, -3, 32768 ]; + } + + function Mock4() { + throw new Error( 'beep' ); + } +}); diff --git a/has-int32array-support/README.md b/has-int32array-support/README.md new file mode 100644 index 00000000..57a9691e --- /dev/null +++ b/has-int32array-support/README.md @@ -0,0 +1,113 @@ + + +# Int32Array Support + +> Detect native [`Int32Array`][mdn-int32array] support. + +
+ +## Usage + +```javascript +var hasInt32ArraySupport = require( '@stdlib/assert/has-int32array-support' ); +``` + +#### hasInt32ArraySupport() + +Detects if a runtime environment supports [`Int32Array`][mdn-int32array]. + +```javascript +var bool = hasInt32ArraySupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasInt32ArraySupport = require( '@stdlib/assert/has-int32array-support' ); + +var bool = hasInt32ArraySupport(); +if ( bool ) { + console.log( 'Environment has Int32Array support.' ); +} else { + console.log( 'Environment lacks Int32Array support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-int32array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-int32array-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-int32array-support/benchmark/benchmark.js b/has-int32array-support/benchmark/benchmark.js new file mode 100644 index 00000000..b5129c38 --- /dev/null +++ b/has-int32array-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasInt32ArraySupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasInt32ArraySupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-int32array-support/bin/cli b/has-int32array-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-int32array-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-int32array-support/docs/repl.txt b/has-int32array-support/docs/repl.txt new file mode 100644 index 00000000..a824e3d6 --- /dev/null +++ b/has-int32array-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `Int32Array` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `Int32Array` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-int32array-support/docs/types/index.d.ts b/has-int32array-support/docs/types/index.d.ts new file mode 100644 index 00000000..b1340062 --- /dev/null +++ b/has-int32array-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Int32Array` support. +* +* @returns boolean indicating if an environment has `Int32Array` support +* +* @example +* var bool = hasInt32ArraySupport(); +* // returns +*/ +declare function hasInt32ArraySupport(): boolean; + + +// EXPORTS // + +export = hasInt32ArraySupport; diff --git a/has-int32array-support/docs/types/test.ts b/has-int32array-support/docs/types/test.ts new file mode 100644 index 00000000..6e6c04df --- /dev/null +++ b/has-int32array-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasInt32ArraySupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasInt32ArraySupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasInt32ArraySupport( true ); // $ExpectError + hasInt32ArraySupport( [], 123 ); // $ExpectError +} diff --git a/has-int32array-support/docs/usage.txt b/has-int32array-support/docs/usage.txt new file mode 100644 index 00000000..21abea04 --- /dev/null +++ b/has-int32array-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-int32array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-int32array-support/etc/cli_opts.json b/has-int32array-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-int32array-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-int32array-support/examples/index.js b/has-int32array-support/examples/index.js new file mode 100644 index 00000000..1cca0786 --- /dev/null +++ b/has-int32array-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasInt32ArraySupport = require( './../lib' ); + +var bool = hasInt32ArraySupport(); +if ( bool ) { + console.log( 'Environment has Int32Array support.' ); +} else { + console.log( 'Environment lacks Int32Array support.' ); +} diff --git a/has-int32array-support/lib/index.js b/has-int32array-support/lib/index.js new file mode 100644 index 00000000..c23f0fc3 --- /dev/null +++ b/has-int32array-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Int32Array` support. +* +* @module @stdlib/assert/has-int32array-support +* +* @example +* var hasInt32ArraySupport = require( '@stdlib/assert/has-int32array-support' ); +* +* var bool = hasInt32ArraySupport(); +* // returns +*/ + +// MODULES // + +var hasInt32ArraySupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasInt32ArraySupport; diff --git a/has-int32array-support/lib/int32array.js b/has-int32array-support/lib/int32array.js new file mode 100644 index 00000000..c1b80b0a --- /dev/null +++ b/has-int32array-support/lib/int32array.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Int32Array === 'function' ) ? Int32Array : null; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = main; diff --git a/has-int32array-support/lib/main.js b/has-int32array-support/lib/main.js new file mode 100644 index 00000000..0859f30f --- /dev/null +++ b/has-int32array-support/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInt32Array = require( '@stdlib/assert/is-int32array' ); +var INT32_MAX = require( '@stdlib/constants/math/int32-max' ); +var INT32_MIN = require( '@stdlib/constants/math/int32-min' ); +var GlobalInt32Array = require( './int32array.js' ); + + +// MAIN // + +/** +* Tests for native `Int32Array` support. +* +* @returns {boolean} boolean indicating if an environment has `Int32Array` support +* +* @example +* var bool = hasInt32ArraySupport(); +* // returns +*/ +function hasInt32ArraySupport() { + var bool; + var arr; + + if ( typeof GlobalInt32Array !== 'function' ) { + return false; + } + // Test basic support... + try { + arr = new GlobalInt32Array( [ 1, 3.14, -3.14, INT32_MAX+1 ] ); + bool = ( + isInt32Array( arr ) && + arr[ 0 ] === 1 && + arr[ 1 ] === 3 && // truncation + arr[ 2 ] === -3 && // truncation + arr[ 3 ] === INT32_MIN // wrap around + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasInt32ArraySupport; diff --git a/has-int32array-support/package.json b/has-int32array-support/package.json new file mode 100644 index 00000000..82c32924 --- /dev/null +++ b/has-int32array-support/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/has-int32array-support", + "version": "0.0.0", + "description": "Detect native Int32Array support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-int32array-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "int32array", + "es2015", + "es6", + "typed array", + "typed-array", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-int32array-support/test/test.cli.js b/has-int32array-support/test/test.cli.js new file mode 100644 index 00000000..97dae270 --- /dev/null +++ b/has-int32array-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Int32Array` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-int32array-support/test/test.js b/has-int32array-support/test/test.js new file mode 100644 index 00000000..1bb813c1 --- /dev/null +++ b/has-int32array-support/test/test.js @@ -0,0 +1,134 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Int32Array = require( '@stdlib/array/int32' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasInt32Array = ( typeof Int32Array === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Int32Array` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasInt32Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './int32array.js': Mock, + '@stdlib/assert/is-int32array': isArray + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock() { + return [ + 1, + 3, + -3, + -2147483648 + ]; + } +}); + +tape( 'if `Int32Array` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasInt32Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './int32array.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './int32array.js': Mock1 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './int32array.js': Mock2, + '@stdlib/assert/is-int32array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './int32array.js': Mock3, + '@stdlib/assert/is-int32array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './int32array.js': Mock4 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock1() { + // Not a typed array: + return []; + } + + function Mock2() { + // Does not truncate... + return [ 1, 3.14, -3.14, -2147483648 ]; + } + + function Mock3() { + // Does not wrap around... + return [ 1, 3, -3, 2147483648 ]; + } + + function Mock4() { + throw new Error( 'beep' ); + } +}); diff --git a/has-int8array-support/README.md b/has-int8array-support/README.md new file mode 100644 index 00000000..69a91a00 --- /dev/null +++ b/has-int8array-support/README.md @@ -0,0 +1,113 @@ + + +# Int8Array Support + +> Detect native [`Int8Array`][mdn-int8array] support. + +
+ +## Usage + +```javascript +var hasInt8ArraySupport = require( '@stdlib/assert/has-int8array-support' ); +``` + +#### hasInt8ArraySupport() + +Detects if a runtime environment supports [`Int8Array`][mdn-int8array]. + +```javascript +var bool = hasInt8ArraySupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasInt8ArraySupport = require( '@stdlib/assert/has-int8array-support' ); + +var bool = hasInt8ArraySupport(); +if ( bool ) { + console.log( 'Environment has Int8Array support.' ); +} else { + console.log( 'Environment lacks Int8Array support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-int8array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-int8array-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-int8array-support/benchmark/benchmark.js b/has-int8array-support/benchmark/benchmark.js new file mode 100644 index 00000000..0d576b14 --- /dev/null +++ b/has-int8array-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasInt8ArraySupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasInt8ArraySupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-int8array-support/bin/cli b/has-int8array-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-int8array-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-int8array-support/docs/repl.txt b/has-int8array-support/docs/repl.txt new file mode 100644 index 00000000..5f8af2e2 --- /dev/null +++ b/has-int8array-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `Int8Array` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `Int8Array` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-int8array-support/docs/types/index.d.ts b/has-int8array-support/docs/types/index.d.ts new file mode 100644 index 00000000..699d605e --- /dev/null +++ b/has-int8array-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Int8Array` support. +* +* @returns boolean indicating if an environment has `Int8Array` support +* +* @example +* var bool = hasInt8ArraySupport(); +* // returns +*/ +declare function hasInt8ArraySupport(): boolean; + + +// EXPORTS // + +export = hasInt8ArraySupport; diff --git a/has-int8array-support/docs/types/test.ts b/has-int8array-support/docs/types/test.ts new file mode 100644 index 00000000..5ebfd445 --- /dev/null +++ b/has-int8array-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasInt8ArraySupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasInt8ArraySupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasInt8ArraySupport( true ); // $ExpectError + hasInt8ArraySupport( [], 123 ); // $ExpectError +} diff --git a/has-int8array-support/docs/usage.txt b/has-int8array-support/docs/usage.txt new file mode 100644 index 00000000..08bd9f3b --- /dev/null +++ b/has-int8array-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-int8array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-int8array-support/etc/cli_opts.json b/has-int8array-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-int8array-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-int8array-support/examples/index.js b/has-int8array-support/examples/index.js new file mode 100644 index 00000000..a5fc88fb --- /dev/null +++ b/has-int8array-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasInt8ArraySupport = require( './../lib' ); + +var bool = hasInt8ArraySupport(); +if ( bool ) { + console.log( 'Environment has Int8Array support.' ); +} else { + console.log( 'Environment lacks Int8Array support.' ); +} diff --git a/has-int8array-support/lib/index.js b/has-int8array-support/lib/index.js new file mode 100644 index 00000000..b43bcfcf --- /dev/null +++ b/has-int8array-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Int8Array` support. +* +* @module @stdlib/assert/has-int8array-support +* +* @example +* var hasInt8ArraySupport = require( '@stdlib/assert/has-int8array-support' ); +* +* var bool = hasInt8ArraySupport(); +* // returns +*/ + +// MODULES // + +var hasInt8ArraySupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasInt8ArraySupport; diff --git a/has-int8array-support/lib/int8array.js b/has-int8array-support/lib/int8array.js new file mode 100644 index 00000000..f8341a05 --- /dev/null +++ b/has-int8array-support/lib/int8array.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Int8Array === 'function' ) ? Int8Array : null; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = main; diff --git a/has-int8array-support/lib/main.js b/has-int8array-support/lib/main.js new file mode 100644 index 00000000..d2a866b0 --- /dev/null +++ b/has-int8array-support/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInt8Array = require( '@stdlib/assert/is-int8array' ); +var INT8_MAX = require( '@stdlib/constants/math/int8-max' ); +var INT8_MIN = require( '@stdlib/constants/math/int8-min' ); +var GlobalInt8Array = require( './int8array.js' ); + + +// MAIN // + +/** +* Tests for native `Int8Array` support. +* +* @returns {boolean} boolean indicating if an environment has `Int8Array` support +* +* @example +* var bool = hasInt8ArraySupport(); +* // returns +*/ +function hasInt8ArraySupport() { + var bool; + var arr; + + if ( typeof GlobalInt8Array !== 'function' ) { + return false; + } + // Test basic support... + try { + arr = new GlobalInt8Array( [ 1, 3.14, -3.14, INT8_MAX+1 ] ); + bool = ( + isInt8Array( arr ) && + arr[ 0 ] === 1 && + arr[ 1 ] === 3 && // truncation + arr[ 2 ] === -3 && // truncation + arr[ 3 ] === INT8_MIN // wrap around + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasInt8ArraySupport; diff --git a/has-int8array-support/package.json b/has-int8array-support/package.json new file mode 100644 index 00000000..f595422f --- /dev/null +++ b/has-int8array-support/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/has-int8array-support", + "version": "0.0.0", + "description": "Detect native Int8Array support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-int8array-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "int8array", + "es2015", + "es6", + "typed array", + "typed-array", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-int8array-support/test/test.cli.js b/has-int8array-support/test/test.cli.js new file mode 100644 index 00000000..5d7b3ff5 --- /dev/null +++ b/has-int8array-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Int8Array` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-int8array-support/test/test.js b/has-int8array-support/test/test.js new file mode 100644 index 00000000..9824b74f --- /dev/null +++ b/has-int8array-support/test/test.js @@ -0,0 +1,134 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Int8Array = require( '@stdlib/array/int8' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasInt8Array = ( typeof Int8Array === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Int8Array` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasInt8Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './int8array.js': Mock, + '@stdlib/assert/is-int8array': isArray + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock() { + return [ + 1, + 3, + -3, + -128 + ]; + } +}); + +tape( 'if `Int8Array` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasInt8Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './int8array.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './int8array.js': Mock1 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './int8array.js': Mock2, + '@stdlib/assert/is-int8array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './int8array.js': Mock3, + '@stdlib/assert/is-int8array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './int8array.js': Mock4 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock1() { + // Not a typed array: + return []; + } + + function Mock2() { + // Does not truncate... + return [ 1, 3.14, -3.14, -128 ]; + } + + function Mock3() { + // Does not wrap around... + return [ 1, 3, -3, 128 ]; + } + + function Mock4() { + throw new Error( 'beep' ); + } +}); diff --git a/has-iterator-symbol-support/README.md b/has-iterator-symbol-support/README.md new file mode 100644 index 00000000..185efcf1 --- /dev/null +++ b/has-iterator-symbol-support/README.md @@ -0,0 +1,113 @@ + + +# Iterator Symbol Support + +> Detect native [`Symbol.iterator`][mdn-iterator-symbol] support. + +
+ +## Usage + +```javascript +var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' ); +``` + +#### hasIteratorSymbolSupport() + +Detects if a runtime environment supports ES2015 [`Symbol.iterator`][mdn-iterator-symbol]. + +```javascript +var bool = hasIteratorSymbolSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' ); + +var bool = hasIteratorSymbolSupport(); +if ( bool ) { + console.log( 'Environment has Symbol.iterator support.' ); +} else { + console.log( 'Environment lacks Symbol.iterator support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-iterator-symbol-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-iterator-symbol-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-iterator-symbol-support/benchmark/benchmark.js b/has-iterator-symbol-support/benchmark/benchmark.js new file mode 100644 index 00000000..544d53f4 --- /dev/null +++ b/has-iterator-symbol-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasIteratorSymbolSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasIteratorSymbolSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-iterator-symbol-support/bin/cli b/has-iterator-symbol-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-iterator-symbol-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-iterator-symbol-support/docs/repl.txt b/has-iterator-symbol-support/docs/repl.txt new file mode 100644 index 00000000..5f19a860 --- /dev/null +++ b/has-iterator-symbol-support/docs/repl.txt @@ -0,0 +1,18 @@ + +{{alias}}() + Tests for native `Symbol.iterator` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has native `Symbol.iterator` + support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-iterator-symbol-support/docs/types/index.d.ts b/has-iterator-symbol-support/docs/types/index.d.ts new file mode 100644 index 00000000..b69ca7c6 --- /dev/null +++ b/has-iterator-symbol-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Symbol.iterator` support. +* +* @returns boolean indicating if an environment has `Symbol.iterator` support +* +* @example +* var bool = hasIteratorSymbolSupport(); +* // returns +*/ +declare function hasIteratorSymbolSupport(): boolean; + + +// EXPORTS // + +export = hasIteratorSymbolSupport; diff --git a/has-iterator-symbol-support/docs/types/test.ts b/has-iterator-symbol-support/docs/types/test.ts new file mode 100644 index 00000000..ecb8342c --- /dev/null +++ b/has-iterator-symbol-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasIteratorSymbolSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasIteratorSymbolSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasIteratorSymbolSupport( true ); // $ExpectError + hasIteratorSymbolSupport( [], 123 ); // $ExpectError +} diff --git a/has-iterator-symbol-support/docs/usage.txt b/has-iterator-symbol-support/docs/usage.txt new file mode 100644 index 00000000..71208ec2 --- /dev/null +++ b/has-iterator-symbol-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-iterator-symbol-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-iterator-symbol-support/etc/cli_opts.json b/has-iterator-symbol-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-iterator-symbol-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-iterator-symbol-support/examples/index.js b/has-iterator-symbol-support/examples/index.js new file mode 100644 index 00000000..9eb1c134 --- /dev/null +++ b/has-iterator-symbol-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasIteratorSymbolSupport = require( './../lib' ); + +var bool = hasIteratorSymbolSupport(); +if ( bool ) { + console.log( 'Environment has Symbol.iterator support.' ); +} else { + console.log( 'Environment lacks Symbol.iterator support.' ); +} diff --git a/has-iterator-symbol-support/lib/index.js b/has-iterator-symbol-support/lib/index.js new file mode 100644 index 00000000..a203352c --- /dev/null +++ b/has-iterator-symbol-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Symbol.iterator` support. +* +* @module @stdlib/assert/has-iterator-symbol-support +* +* @example +* var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' ); +* +* var bool = hasIteratorSymbolSupport(); +* // returns +*/ + +// MODULES // + +var hasIteratorSymbolSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasIteratorSymbolSupport; diff --git a/has-iterator-symbol-support/lib/main.js b/has-iterator-symbol-support/lib/main.js new file mode 100644 index 00000000..6ea95bd3 --- /dev/null +++ b/has-iterator-symbol-support/lib/main.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); + + +// MAIN // + +/** +* Tests for native `Symbol.iterator` support. +* +* @returns {boolean} boolean indicating if an environment has `Symbol.iterator` support +* +* @example +* var bool = hasIteratorSymbolSupport(); +* // returns +*/ +function hasIteratorSymbolSupport() { + return ( + typeof Symbol === 'function' && + typeof Symbol( 'foo' ) === 'symbol' && + hasOwnProp( Symbol, 'iterator' ) && + typeof Symbol.iterator === 'symbol' + ); +} + + +// EXPORTS // + +module.exports = hasIteratorSymbolSupport; diff --git a/has-iterator-symbol-support/package.json b/has-iterator-symbol-support/package.json new file mode 100644 index 00000000..c758dae5 --- /dev/null +++ b/has-iterator-symbol-support/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/has-iterator-symbol-support", + "version": "0.0.0", + "description": "Detect native Symbol.iterator support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-iterator-symbol-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "symbol", + "iterator", + "symbol.iterator", + "es2015", + "es6", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-iterator-symbol-support/test/test.cli.js b/has-iterator-symbol-support/test/test.cli.js new file mode 100644 index 00000000..ff60fb18 --- /dev/null +++ b/has-iterator-symbol-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Symbol.iterator` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-iterator-symbol-support/test/test.js b/has-iterator-symbol-support/test/test.js new file mode 100644 index 00000000..817ffab1 --- /dev/null +++ b/has-iterator-symbol-support/test/test.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasSymbols = hasSymbolSupport(); +var hasIteratorSymbol = ( typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Symbol.iterator` is supported, detection result is `true`', function test( t ) { + if ( hasIteratorSymbol ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + t.end(); +}); + +tape( 'the function guards against a `Symbol` global variable which does not produce `symbols`', function test( t ) { + var tmp; + if ( hasSymbols ) { + tmp = Symbol; + Symbol = {}; // eslint-disable-line no-global-assign + } else { + global.Symbol = {}; + } + t.strictEqual( detect(), false, 'detection result is `false`' ); + if ( hasSymbols ) { + Symbol = tmp; // eslint-disable-line no-global-assign + } + t.end(); +}); diff --git a/has-map-support/README.md b/has-map-support/README.md new file mode 100644 index 00000000..e73b2b81 --- /dev/null +++ b/has-map-support/README.md @@ -0,0 +1,113 @@ + + +# Map Support + +> Detect native [`Map`][mdn-map] support. + +
+ +## Usage + +```javascript +var hasMapSupport = require( '@stdlib/assert/has-map-support' ); +``` + +#### hasMapSupport() + +Detects if a runtime environment supports ES2015 [`Map`][mdn-map]. + +```javascript +var bool = hasMapSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasMapSupport = require( '@stdlib/assert/has-map-support' ); + +var bool = hasMapSupport(); +if ( bool ) { + console.log( 'Environment has Map support.' ); +} else { + console.log( 'Environment lacks Map support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-map-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-map-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-map-support/benchmark/benchmark.js b/has-map-support/benchmark/benchmark.js new file mode 100644 index 00000000..b3578a2a --- /dev/null +++ b/has-map-support/benchmark/benchmark.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasMapSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasMapSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/has-map-support/bin/cli b/has-map-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-map-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-map-support/docs/repl.txt b/has-map-support/docs/repl.txt new file mode 100644 index 00000000..ef53ab1c --- /dev/null +++ b/has-map-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `Map` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `Map` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-map-support/docs/types/index.d.ts b/has-map-support/docs/types/index.d.ts new file mode 100644 index 00000000..6d8eface --- /dev/null +++ b/has-map-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Map` support. +* +* @returns boolean indicating if an environment has `Map` support +* +* @example +* var bool = hasMapSupport(); +* // returns +*/ +declare function hasMapSupport(): boolean; + + +// EXPORTS // + +export = hasMapSupport; diff --git a/has-map-support/docs/types/test.ts b/has-map-support/docs/types/test.ts new file mode 100644 index 00000000..2a756e62 --- /dev/null +++ b/has-map-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasMapSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasMapSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasMapSupport( true ); // $ExpectError + hasMapSupport( [], 123 ); // $ExpectError +} diff --git a/has-map-support/docs/usage.txt b/has-map-support/docs/usage.txt new file mode 100644 index 00000000..caa2fa8e --- /dev/null +++ b/has-map-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-map-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-map-support/etc/cli_opts.json b/has-map-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-map-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-map-support/examples/index.js b/has-map-support/examples/index.js new file mode 100644 index 00000000..8222b2e7 --- /dev/null +++ b/has-map-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasMapSupport = require( './../lib' ); + +var bool = hasMapSupport(); +if ( bool ) { + console.log( 'Environment has Map support.' ); +} else { + console.log( 'Environment lacks Map support.' ); +} diff --git a/has-map-support/lib/index.js b/has-map-support/lib/index.js new file mode 100644 index 00000000..fcfe1099 --- /dev/null +++ b/has-map-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Map` support. +* +* @module @stdlib/assert/has-map-support +* +* @example +* var hasMapSupport = require( '@stdlib/assert/has-map-support' ); +* +* var bool = hasMapSupport(); +* // returns +*/ + +// MODULES // + +var hasMapSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasMapSupport; diff --git a/has-map-support/lib/main.js b/has-map-support/lib/main.js new file mode 100644 index 00000000..fab0eeab --- /dev/null +++ b/has-map-support/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var GlobalMap = require( './map.js' ); + + +// MAIN // + +/** +* Tests for native `Map` support. +* +* @returns {boolean} boolean indicating if an environment has `Map` support +* +* @example +* var bool = hasMapSupport(); +* // returns +*/ +function hasMapSupport() { + var bool; + var key; + var map; + + if ( typeof GlobalMap !== 'function' ) { + return false; + } + // Test basic support... + try { + key = {}; + map = new GlobalMap(); + map.set( key, 'beep' ); + bool = ( + map.has( key ) && + map.get( key ) === 'beep' + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasMapSupport; diff --git a/has-map-support/lib/map.js b/has-map-support/lib/map.js new file mode 100644 index 00000000..3d6aff7f --- /dev/null +++ b/has-map-support/lib/map.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Map === 'function' ) ? Map : null; + + +// EXPORTS // + +module.exports = main; diff --git a/has-map-support/package.json b/has-map-support/package.json new file mode 100644 index 00000000..ad989cd5 --- /dev/null +++ b/has-map-support/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/has-map-support", + "version": "0.0.0", + "description": "Detect native Map support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-map-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "map", + "es2015", + "es6", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-map-support/test/test.cli.js b/has-map-support/test/test.cli.js new file mode 100644 index 00000000..55a5d859 --- /dev/null +++ b/has-map-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Map` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-map-support/test/test.js b/has-map-support/test/test.js new file mode 100644 index 00000000..6fd23f8b --- /dev/null +++ b/has-map-support/test/test.js @@ -0,0 +1,150 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasMap = ( typeof Map === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Map` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasMap ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './map.js': Mock + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function Mock() { + var o = {}; + function has( key ) { + return hasOwnProp( o, key ); + } + function set( key, value ) { + o[ key ] = value; + } + function get( key ) { + return o[ key ]; + } + return { + 'has': has, + 'set': set, + 'get': get + }; + } +}); + +tape( 'if `Map` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasMap ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './map.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false` (mocked)' ); + + mocked = proxyquire( './../lib/main.js', { + './map.js': Mock1 + }); + t.strictEqual( mocked(), false, 'detection result is `false` (no `has` method)' ); + + mocked = proxyquire( './../lib/main.js', { + './map.js': Mock2 + }); + t.strictEqual( mocked(), false, 'detection result is `false` (no `set` method)' ); + + mocked = proxyquire( './../lib/main.js', { + './map.js': Mock3 + }); + t.strictEqual( mocked(), false, 'detection result is `false` (no `get` method)' ); + + t.end(); + + function Mock1() { + var o = {}; + function set( key, value ) { + o[ key ] = value; + } + function get( key ) { + return o[ key ]; + } + return { + 'set': set, + 'get': get + }; + } + + function Mock2() { + var o = {}; + function has( key ) { + return hasOwnProp( o, key ); + } + function get( key ) { + return o[ key ]; + } + return { + 'has': has, + 'get': get + }; + } + + function Mock3() { + var o = {}; + function has( key ) { + return hasOwnProp( o, key ); + } + function set( key, value ) { + o[ key ] = value; + } + return { + 'has': has, + 'set': set + }; + } +}); diff --git a/has-node-buffer-support/README.md b/has-node-buffer-support/README.md new file mode 100644 index 00000000..91a469cd --- /dev/null +++ b/has-node-buffer-support/README.md @@ -0,0 +1,113 @@ + + +# Node.js Buffer Support + +> Detect native [`Buffer`][node-buffer] support. + +
+ +## Usage + +```javascript +var hasNodeBufferSupport = require( '@stdlib/assert/has-node-buffer-support' ); +``` + +#### hasNodeBufferSupport() + +Detects if a runtime environment supports [`Buffer`][node-buffer]. + +```javascript +var bool = hasNodeBufferSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasNodeBufferSupport = require( '@stdlib/assert/has-node-buffer-support' ); + +var bool = hasNodeBufferSupport(); +if ( bool ) { + console.log( 'Environment has Buffer support.' ); +} else { + console.log( 'Environment lacks Buffer support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-node-buffer-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-node-buffer-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-node-buffer-support/benchmark/benchmark.js b/has-node-buffer-support/benchmark/benchmark.js new file mode 100644 index 00000000..1019c8b3 --- /dev/null +++ b/has-node-buffer-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasNodeBufferSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasNodeBufferSupport(); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-node-buffer-support/bin/cli b/has-node-buffer-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-node-buffer-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-node-buffer-support/docs/repl.txt b/has-node-buffer-support/docs/repl.txt new file mode 100644 index 00000000..45a9bab0 --- /dev/null +++ b/has-node-buffer-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `Buffer` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `Buffer` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-node-buffer-support/docs/types/index.d.ts b/has-node-buffer-support/docs/types/index.d.ts new file mode 100644 index 00000000..711ea06b --- /dev/null +++ b/has-node-buffer-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Buffer` support. +* +* @returns boolean indicating if an environment has `Buffer` support +* +* @example +* var bool = hasNodeBufferSupport(); +* // returns +*/ +declare function hasNodeBufferSupport(): boolean; + + +// EXPORTS // + +export = hasNodeBufferSupport; diff --git a/has-node-buffer-support/docs/types/test.ts b/has-node-buffer-support/docs/types/test.ts new file mode 100644 index 00000000..11620de3 --- /dev/null +++ b/has-node-buffer-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasNodeBufferSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasNodeBufferSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasNodeBufferSupport( true ); // $ExpectError + hasNodeBufferSupport( [], 123 ); // $ExpectError +} diff --git a/has-node-buffer-support/docs/usage.txt b/has-node-buffer-support/docs/usage.txt new file mode 100644 index 00000000..6e64dd3d --- /dev/null +++ b/has-node-buffer-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-node-buffer-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-node-buffer-support/etc/cli_opts.json b/has-node-buffer-support/etc/cli_opts.json new file mode 100644 index 00000000..92119c45 --- /dev/null +++ b/has-node-buffer-support/etc/cli_opts.json @@ -0,0 +1,15 @@ +{ + "string": [], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-node-buffer-support/examples/index.js b/has-node-buffer-support/examples/index.js new file mode 100644 index 00000000..9cb1430f --- /dev/null +++ b/has-node-buffer-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasNodeBufferSupport = require( './../lib' ); + +var bool = hasNodeBufferSupport(); +if ( bool ) { + console.log( 'Environment has Buffer support.' ); +} else { + console.log( 'Environment lacks Buffer support.' ); +} diff --git a/has-node-buffer-support/lib/buffer.js b/has-node-buffer-support/lib/buffer.js new file mode 100644 index 00000000..c7814a1d --- /dev/null +++ b/has-node-buffer-support/lib/buffer.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Buffer === 'function' ) ? Buffer : null; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = main; diff --git a/has-node-buffer-support/lib/index.js b/has-node-buffer-support/lib/index.js new file mode 100644 index 00000000..da010dfd --- /dev/null +++ b/has-node-buffer-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Buffer` support. +* +* @module @stdlib/assert/has-node-buffer-support +* +* @example +* var hasNodeBufferSupport = require( '@stdlib/assert/has-node-buffer-support' ); +* +* var bool = hasNodeBufferSupport(); +* // returns +*/ + +// MODULES // + +var hasNodeBufferSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasNodeBufferSupport; diff --git a/has-node-buffer-support/lib/main.js b/has-node-buffer-support/lib/main.js new file mode 100644 index 00000000..f8b76147 --- /dev/null +++ b/has-node-buffer-support/lib/main.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isBuffer = require( '@stdlib/assert/is-buffer' ); +var GlobalBuffer = require( './buffer.js' ); + + +// MAIN // + +/** +* Tests for native `Buffer` support. +* +* @returns {boolean} boolean indicating if an environment has `Buffer` support +* +* @example +* var bool = hasNodeBufferSupport(); +* // returns +*/ +function hasNodeBufferSupport() { + var bool; + var b; + + if ( typeof GlobalBuffer !== 'function' ) { + return false; + } + // Test basic support... + try { + if ( typeof GlobalBuffer.from === 'function' ) { + b = GlobalBuffer.from( [ 1, 2, 3, 4 ] ); + } else { + b = new GlobalBuffer( [ 1, 2, 3, 4 ] ); // Note: this is deprecated behavior starting in Node v6 (see https://nodejs.org/api/buffer.html#buffer_new_buffer_array) + } + bool = ( + isBuffer( b ) && + b[ 0 ] === 1 && + b[ 1 ] === 2 && + b[ 2 ] === 3 && + b[ 3 ] === 4 + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasNodeBufferSupport; diff --git a/has-node-buffer-support/package.json b/has-node-buffer-support/package.json new file mode 100644 index 00000000..d04b2bb4 --- /dev/null +++ b/has-node-buffer-support/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/has-node-buffer-support", + "version": "0.0.0", + "description": "Detect native Buffer support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-node-buffer-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "buffer", + "node.js", + "nodejs", + "node", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-node-buffer-support/test/test.cli.js b/has-node-buffer-support/test/test.cli.js new file mode 100644 index 00000000..f991cbfb --- /dev/null +++ b/has-node-buffer-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Buffer` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-node-buffer-support/test/test.js b/has-node-buffer-support/test/test.js new file mode 100644 index 00000000..f548d65e --- /dev/null +++ b/has-node-buffer-support/test/test.js @@ -0,0 +1,168 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Buffer = require( '@stdlib/buffer/ctor' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasNodeBuffer = ( typeof Buffer === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Buffer` is supported, detection result is `true` (no `from` method; Node + +# hasOwnProperty + +> Test if an object has a specified property. + +
+ +## Usage + +```javascript +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +``` + +#### hasOwnProp( value, property ) + +Returns a `boolean` indicating if a `value` has a specified `property`. + +```javascript +var value = { + 'beep': 'boop' +}; + +var bool = hasOwnProp( value, 'beep' ); +// returns true + +bool = hasOwnProp( value, 'bap' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- In contrast to the native [Object.prototype.hasOwnProperty][mdn-object-has-own-property], this function does **not** throw when provided `null` or `undefined`. Instead, the function returns `false`. + + ```javascript + var bool = hasOwnProp( null, 'a' ); + // returns false + + bool = hasOwnProp( void 0, 'a' ); + // returns false + ``` + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = hasOwnProp( 'beep', 'length' ); + // returns true + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var value = { + 'null': false + }; + var bool = hasOwnProp( value, null ); + // returns true + + value = { + '[object Object]': false + }; + bool = hasOwnProp( value, {} ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); + +var bool = hasOwnProp( { 'a': 'b' }, 'a' ); +// returns true + +bool = hasOwnProp( { 'a': 'b' }, 'c' ); +// returns false + +bool = hasOwnProp( { 'a': 'b' }, null ); +// returns false + +bool = hasOwnProp( {}, 'hasOwnProperty' ); +// returns false + +bool = hasOwnProp( null, 'a' ); +// returns false + +bool = hasOwnProp( void 0, 'a' ); +// returns false + +bool = hasOwnProp( { 'null': false }, null ); +// returns true + +bool = hasOwnProp( { '[object Object]': false }, {} ); +// returns true +``` + +
+ + + + + + diff --git a/has-own-property/benchmark/benchmark.js b/has-own-property/benchmark/benchmark.js new file mode 100644 index 00000000..bf399949 --- /dev/null +++ b/has-own-property/benchmark/benchmark.js @@ -0,0 +1,80 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-prototype-builtins */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var hasOwnProp = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var obj; + var key; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + key = fromCodePoint( 97+(i%26) ); + obj = {}; + obj[ key ] = i; + bool = hasOwnProp( obj, key ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::built-in', function benchmark( b ) { + var bool; + var obj; + var key; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + key = fromCodePoint( 97+(i%26) ); + obj = {}; + obj[ key ] = i; + bool = obj.hasOwnProperty( key ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-own-property/docs/repl.txt b/has-own-property/docs/repl.txt new file mode 100644 index 00000000..eb7d0d95 --- /dev/null +++ b/has-own-property/docs/repl.txt @@ -0,0 +1,36 @@ + +{{alias}}( value, property ) + Tests if an object has a specified property. + + In contrast to the native `Object.prototype.hasOwnProperty`, this function + does not throw when provided `null` or `undefined`. Instead, the function + returns `false`. + + Value arguments other than `null` or `undefined` are coerced to objects. + + Property arguments are coerced to strings. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object has a specified property. + + Examples + -------- + > var beep = { 'boop': true }; + > var bool = {{alias}}( beep, 'boop' ) + true + > bool = {{alias}}( beep, 'bop' ) + false + + See Also + -------- + diff --git a/has-own-property/docs/types/index.d.ts b/has-own-property/docs/types/index.d.ts new file mode 100644 index 00000000..bf253832 --- /dev/null +++ b/has-own-property/docs/types/index.d.ts @@ -0,0 +1,49 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object has a specified property. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object has a specified property +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = hasOwnProp( beep, 'boop' ); +* // returns true +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = hasOwnProp( beep, 'bap' ); +* // returns false +*/ +declare function hasOwnProp( value: any, property: any ): boolean; + + +// EXPORTS // + +export = hasOwnProp; diff --git a/has-own-property/docs/types/test.ts b/has-own-property/docs/types/test.ts new file mode 100644 index 00000000..a12604b6 --- /dev/null +++ b/has-own-property/docs/types/test.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasOwnProp = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + hasOwnProp( obj, 'boop' ); // $ExpectType boolean + hasOwnProp( obj, 'beep' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + hasOwnProp(); // $ExpectError + hasOwnProp( {} ); // $ExpectError + hasOwnProp( {}, 'beep', 123 ); // $ExpectError +} diff --git a/has-own-property/examples/index.js b/has-own-property/examples/index.js new file mode 100644 index 00000000..708d9d29 --- /dev/null +++ b/has-own-property/examples/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var hasOwnProp = require( './../lib' ); + +var bool = hasOwnProp( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = hasOwnProp( { 'a': 'b' }, 'c' ); +console.log( bool ); +// => false + +bool = hasOwnProp( { 'a': 'b' }, null ); +console.log( bool ); +// => false + +bool = hasOwnProp( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = hasOwnProp( null, 'a' ); +console.log( bool ); +// => false + +bool = hasOwnProp( void 0, 'a' ); +console.log( bool ); +// => false + +bool = hasOwnProp( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = hasOwnProp( { '[object Object]': false }, {} ); +console.log( bool ); +// => true diff --git a/has-own-property/lib/index.js b/has-own-property/lib/index.js new file mode 100644 index 00000000..65fe6a0b --- /dev/null +++ b/has-own-property/lib/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object has a specified property. +* +* @module @stdlib/assert/has-own-property +* +* @example +* var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +* +* var beep = { +* 'boop': true +* }; +* +* var bool = hasOwnProp( beep, 'boop' ); +* // returns true +* +* bool = hasOwnProp( beep, 'bop' ); +* // returns false +*/ + +// MODULES // + +var hasOwnProp = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasOwnProp; diff --git a/has-own-property/lib/main.js b/has-own-property/lib/main.js new file mode 100644 index 00000000..f35baff9 --- /dev/null +++ b/has-own-property/lib/main.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// FUNCTIONS // + +var has = Object.prototype.hasOwnProperty; + + +// MAIN // + +/** +* Tests if an object has a specified property. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object has a specified property +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = hasOwnProp( beep, 'boop' ); +* // returns true +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = hasOwnProp( beep, 'bap' ); +* // returns false +*/ +function hasOwnProp( value, property ) { + if ( + value === void 0 || + value === null + ) { + return false; + } + return has.call( value, property ); +} + + +// EXPORTS // + +module.exports = hasOwnProp; diff --git a/has-own-property/package.json b/has-own-property/package.json new file mode 100644 index 00000000..8a2fe19b --- /dev/null +++ b/has-own-property/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/has-own-property", + "version": "0.0.0", + "description": "Test if an object has a specified property.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "has", + "hasown", + "hasownproperty", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/has-own-property/test/test.js b/has-own-property/test/test.js new file mode 100644 index 00000000..776fa3d1 --- /dev/null +++ b/has-own-property/test/test.js @@ -0,0 +1,104 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasOwnProp = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof hasOwnProp, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument (native throws)', function test( t ) { + var bool; + + bool = hasOwnProp( null, 'beep' ); + t.strictEqual( bool, false, 'returns false when provided null' ); + + bool = hasOwnProp( void 0, 'beep' ); + t.strictEqual( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `true` if an object has a specified property', function test( t ) { + var bool; + + bool = hasOwnProp( { 'a': 'b' }, 'a' ); + t.strictEqual( bool, true, 'returns true' ); + + bool = hasOwnProp( [ 1, 2, 3 ], '1' ); + t.strictEqual( bool, true, 'returns true' ); + + bool = hasOwnProp( [ 1, 2, 3 ], 1 ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object does not have a specified property', function test( t ) { + var bool; + + bool = hasOwnProp( { 'a': 'b' }, 'c' ); + t.strictEqual( bool, false, 'returns false' ); + + bool = hasOwnProp( { 'a': 'b' }, null ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a prototype property', function test( t ) { + var bool; + + bool = hasOwnProp( {}, 'hasOwnProperty' ); + t.strictEqual( bool, false, 'returns false when provided a prototype property' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = hasOwnProp( 'beep', 'length' ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'property values are coerced to strings', function test( t ) { + var bool; + + bool = hasOwnProp( { 'null': false }, null ); + t.strictEqual( bool, true, 'returns true' ); + + bool = hasOwnProp( { '[object Object]': false }, {} ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/has-property/README.md b/has-property/README.md new file mode 100644 index 00000000..1914bf73 --- /dev/null +++ b/has-property/README.md @@ -0,0 +1,141 @@ + + +# hasProperty + +> Test if an object has a specified property, either own or inherited. + +
+ +## Usage + +```javascript +var hasProp = require( '@stdlib/assert/has-property' ); +``` + +#### hasProp( value, property ) + +Returns a `boolean` indicating if a `value` has a specified `property`, either own or inherited. + +```javascript +var value = { + 'beep': 'boop' +}; + +var bool = hasProp( value, 'beep' ); +// returns true + +bool = hasProp( value, 'hasOwnProperty' ); +// returns true + +bool = hasProp( value, 'bap' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- The function does **not** throw when provided `null` or `undefined`. Instead, the function returns `false`. + + ```javascript + var bool = hasProp( null, 'a' ); + // returns false + + bool = hasProp( void 0, 'a' ); + // returns false + ``` + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = hasProp( 'beep', 'length' ); + // returns true + ``` + +- Non-symbol property arguments are coerced to `strings`. + + ```javascript + var value = { + 'null': false + }; + var bool = hasProp( value, null ); + // returns true + + value = { + '[object Object]': false + }; + bool = hasProp( value, {} ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var hasProp = require( '@stdlib/assert/has-property' ); + +var bool = hasProp( { 'a': 'b' }, 'a' ); +// returns true + +bool = hasProp( {}, 'hasOwnProperty' ); +// returns true + +bool = hasProp( { 'a': 'b' }, 'c' ); +// returns false + +bool = hasProp( { 'a': 'b' }, null ); +// returns false + +bool = hasProp( null, 'a' ); +// returns false + +bool = hasProp( void 0, 'a' ); +// returns false + +bool = hasProp( { 'null': false }, null ); +// returns true + +bool = hasProp( { '[object Object]': false }, {} ); +// returns true +``` + +
+ + + + + + diff --git a/has-property/benchmark/benchmark.js b/has-property/benchmark/benchmark.js new file mode 100644 index 00000000..9b7fd48c --- /dev/null +++ b/has-property/benchmark/benchmark.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var hasProp = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var obj; + var key; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + key = fromCodePoint( 97+(i%26) ); + obj = {}; + obj[ key ] = i; + bool = hasProp( obj, key ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::built-in', function benchmark( b ) { + var bool; + var obj; + var key; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + key = fromCodePoint( 97+(i%26) ); + obj = {}; + obj[ key ] = i; + bool = ( key in obj ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-property/docs/repl.txt b/has-property/docs/repl.txt new file mode 100644 index 00000000..acc86814 --- /dev/null +++ b/has-property/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, property ) + Tests if an object has a specified property, either own or inherited. + + Value arguments other than `null` or `undefined` are coerced to objects. + + Non-symbol property arguments are coerced to strings. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object has a specified property. + + Examples + -------- + > var beep = { 'boop': true }; + > var bool = {{alias}}( beep, 'boop' ) + true + > bool = {{alias}}( beep, 'toString' ) + true + > bool = {{alias}}( beep, 'bop' ) + false + + See Also + -------- + diff --git a/has-property/docs/types/index.d.ts b/has-property/docs/types/index.d.ts new file mode 100644 index 00000000..d97357b2 --- /dev/null +++ b/has-property/docs/types/index.d.ts @@ -0,0 +1,49 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object has a specified property, either own or inherited. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object has a specified property +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = hasProp( beep, 'boop' ); +* // returns true +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = hasProp( beep, 'bap' ); +* // returns false +*/ +declare function hasProp( value: any, property: any ): boolean; + + +// EXPORTS // + +export = hasProp; diff --git a/has-property/docs/types/test.ts b/has-property/docs/types/test.ts new file mode 100644 index 00000000..11cd6c65 --- /dev/null +++ b/has-property/docs/types/test.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasProp = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + hasProp( obj, 'boop' ); // $ExpectType boolean + hasProp( obj, 'beep' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + hasProp(); // $ExpectError + hasProp( {} ); // $ExpectError + hasProp( {}, 'beep', 123 ); // $ExpectError +} diff --git a/has-property/examples/index.js b/has-property/examples/index.js new file mode 100644 index 00000000..131d9edb --- /dev/null +++ b/has-property/examples/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var hasProp = require( './../lib' ); + +var bool = hasProp( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = hasProp( {}, 'hasOwnProperty' ); +console.log( bool ); +// => true + +bool = hasProp( { 'a': 'b' }, 'c' ); +console.log( bool ); +// => false + +bool = hasProp( { 'a': 'b' }, null ); +console.log( bool ); +// => false + +bool = hasProp( null, 'a' ); +console.log( bool ); +// => false + +bool = hasProp( void 0, 'a' ); +console.log( bool ); +// => false + +bool = hasProp( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = hasProp( { '[object Object]': false }, {} ); +console.log( bool ); +// => true diff --git a/has-property/lib/index.js b/has-property/lib/index.js new file mode 100644 index 00000000..0edfbe87 --- /dev/null +++ b/has-property/lib/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object has a specified property, either own or inherited. +* +* @module @stdlib/assert/has-property +* +* @example +* var hasProp = require( '@stdlib/assert/has-property' ); +* +* var beep = { +* 'boop': true +* }; +* +* var bool = hasProp( beep, 'boop' ); +* // returns true +* +* bool = hasProp( beep, 'bop' ); +* // returns false +*/ + +// MODULES // + +var hasProp = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasProp; diff --git a/has-property/lib/main.js b/has-property/lib/main.js new file mode 100644 index 00000000..16b99046 --- /dev/null +++ b/has-property/lib/main.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests if an object has a specified property, either own or inherited. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object has a specified property +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = hasProp( beep, 'boop' ); +* // returns true +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = hasProp( beep, 'bap' ); +* // returns false +*/ +function hasProp( value, property ) { + if ( value === void 0 || value === null ) { + return false; + } + if ( typeof property === 'symbol' ) { + return property in Object( value ); + } + return ( String( property ) in Object( value ) ); +} + + +// EXPORTS // + +module.exports = hasProp; diff --git a/has-property/package.json b/has-property/package.json new file mode 100644 index 00000000..1c95b77c --- /dev/null +++ b/has-property/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/has-property", + "version": "0.0.0", + "description": "Test if an object has a specified property, either own or inherited.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "has", + "hasown", + "hasownproperty", + "hasprop", + "hasproperty", + "inherited", + "prototype", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/has-property/test/test.js b/has-property/test/test.js new file mode 100644 index 00000000..c4621948 --- /dev/null +++ b/has-property/test/test.js @@ -0,0 +1,117 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var hasProp = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof hasProp, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = hasProp( null, 'beep' ); + t.strictEqual( bool, false, 'returns false when provided null' ); + + bool = hasProp( void 0, 'beep' ); + t.strictEqual( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `true` if an object has a specified property (either own or inherited)', function test( t ) { + var bool; + + bool = hasProp( { 'a': 'b' }, 'a' ); + t.strictEqual( bool, true, 'returns true' ); + + bool = hasProp( [ 1, 2, 3 ], '1' ); + t.strictEqual( bool, true, 'returns true' ); + + bool = hasProp( [ 1, 2, 3 ], 1 ); + t.strictEqual( bool, true, 'returns true' ); + + bool = hasProp( {}, 'hasOwnProperty' ); + t.strictEqual( bool, true, 'returns true when provided a prototype property' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object does not have a specified property (either own or inherited)', function test( t ) { + var bool; + + bool = hasProp( { 'a': 'b' }, 'c' ); + t.strictEqual( bool, false, 'returns false' ); + + bool = hasProp( { 'a': 'b' }, null ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = hasProp( 'beep', 'length' ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'non-symbol property values are coerced to strings', function test( t ) { + var bool; + + bool = hasProp( { 'null': false }, null ); + t.strictEqual( bool, true, 'returns true' ); + + bool = hasProp( { '[object Object]': false }, {} ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function supports symbols', function test( t ) { + var bool; + var obj; + var s; + + if ( hasSymbolSupport() ) { + obj = {}; + s = Symbol( 'foo' ); + obj[ s ] = true; + bool = hasProp( obj, s ); + t.strictEqual( bool, true, 'returns true' ); + } else { + t.pass( 'environment does not support symbols' ); + } + t.end(); +}); diff --git a/has-proxy-support/README.md b/has-proxy-support/README.md new file mode 100644 index 00000000..4763deff --- /dev/null +++ b/has-proxy-support/README.md @@ -0,0 +1,125 @@ + + +# Proxy Support + +> Detect native [`Proxy`][mdn-proxy] support. + +
+ +## Usage + +```javascript +var hasProxySupport = require( '@stdlib/assert/has-proxy-support' ); +``` + +#### hasProxySupport() + +Detects if a runtime environment supports ES2015 [`Proxy`][mdn-proxy]. + +```javascript +var bool = hasProxySupport(); +// returns +``` + +
+ + + +
+ +## Notes + +- The implementation uses code evaluation, which may be problematic in browser contexts enforcing a strict [content security policy][mdn-csp] (CSP). + +
+ + + +
+ +## Examples + + + +```javascript +var hasProxySupport = require( '@stdlib/assert/has-proxy-support' ); + +var bool = hasProxySupport(); +if ( bool ) { + console.log( 'Environment has native Proxy support.' ); +} else { + console.log( 'Environment lacks native Proxy support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-proxy-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-proxy-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-proxy-support/benchmark/benchmark.js b/has-proxy-support/benchmark/benchmark.js new file mode 100644 index 00000000..5b982bea --- /dev/null +++ b/has-proxy-support/benchmark/benchmark.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasProxySupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasProxySupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/has-proxy-support/bin/cli b/has-proxy-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-proxy-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-proxy-support/docs/repl.txt b/has-proxy-support/docs/repl.txt new file mode 100644 index 00000000..cf678885 --- /dev/null +++ b/has-proxy-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests whether an environment has native `Proxy` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has native `Proxy` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-proxy-support/docs/types/index.d.ts b/has-proxy-support/docs/types/index.d.ts new file mode 100644 index 00000000..0af28bd6 --- /dev/null +++ b/has-proxy-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Proxy` support. +* +* @returns boolean indicating if an environment has native `Proxy` support +* +* @example +* var bool = hasProxySupport(); +* // returns +*/ +declare function hasProxySupport(): boolean; + + +// EXPORTS // + +export = hasProxySupport; diff --git a/has-proxy-support/docs/types/test.ts b/has-proxy-support/docs/types/test.ts new file mode 100644 index 00000000..3b43f0a1 --- /dev/null +++ b/has-proxy-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasProxySupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasProxySupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasProxySupport( true ); // $ExpectError + hasProxySupport( [], 123 ); // $ExpectError +} diff --git a/has-proxy-support/docs/usage.txt b/has-proxy-support/docs/usage.txt new file mode 100644 index 00000000..24c7dc78 --- /dev/null +++ b/has-proxy-support/docs/usage.txt @@ -0,0 +1,7 @@ + +Usage: has-proxy-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. diff --git a/has-proxy-support/etc/cli_opts.json b/has-proxy-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-proxy-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-proxy-support/examples/index.js b/has-proxy-support/examples/index.js new file mode 100644 index 00000000..83d4494a --- /dev/null +++ b/has-proxy-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasProxySupport = require( './../lib' ); + +var bool = hasProxySupport(); +if ( bool ) { + console.log( 'Environment has native Proxy support.' ); +} else { + console.log( 'Environment lacks native Proxy support.' ); +} diff --git a/has-proxy-support/lib/index.js b/has-proxy-support/lib/index.js new file mode 100644 index 00000000..18abf893 --- /dev/null +++ b/has-proxy-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Proxy` support. +* +* @module @stdlib/assert/has-proxy-support +* +* @example +* var hasProxySupport = require( '@stdlib/assert/has-proxy-support' ); +* +* var bool = hasProxySupport(); +* // returns +*/ + +// MODULES // + +var hasProxySupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasProxySupport; diff --git a/has-proxy-support/lib/main.js b/has-proxy-support/lib/main.js new file mode 100644 index 00000000..d6088827 --- /dev/null +++ b/has-proxy-support/lib/main.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var evil = require( '@stdlib/utils/eval' ); + + +// MAIN // + +/** +* Tests for native `Proxy` support. +* +* @returns {boolean} boolean indicating if an environment has native `Proxy` support +* +* @example +* var bool = hasProxySupport(); +* // returns +*/ +function hasProxySupport() { + var bool; + try { + evil( '"use strict"; var handler = { "get": function get( t, n ) { return t[ n ] * 100; } }; var target = { "beep": 3.14 }; var p = new Proxy( target, handler ); var x = p.beep; if ( x !== 314 ) { throw new Error( "native Proxy is not supported." ); }' ); + bool = true; + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasProxySupport; diff --git a/has-proxy-support/package.json b/has-proxy-support/package.json new file mode 100644 index 00000000..58384806 --- /dev/null +++ b/has-proxy-support/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/has-proxy-support", + "version": "0.0.0", + "description": "Detect native Proxy function support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-proxy-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "proxy", + "object", + "es2015", + "es6", + "support", + "has", + "hasproxy", + "hasproxies", + "native", + "issupported", + "cli" + ] +} diff --git a/has-proxy-support/test/test.cli.js b/has-proxy-support/test/test.cli.js new file mode 100644 index 00000000..66eb6b8a --- /dev/null +++ b/has-proxy-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Proxy` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-proxy-support/test/test.js b/has-proxy-support/test/test.js new file mode 100644 index 00000000..c7e61ad0 --- /dev/null +++ b/has-proxy-support/test/test.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var detect = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Proxy` is supported, detection result is `true`', function test( t ) { + var detect = proxyquire( './../lib/main.js', { + '@stdlib/utils/eval': stub + }); + + t.ok( detect(), 'detection result is `true`' ); + t.end(); + + function stub() { + return 'beep'; + } +}); + +tape( 'if `Proxy` is not supported, detection result is `false`', function test( t ) { + var detect = proxyquire( './../lib/main.js', { + '@stdlib/utils/eval': stub + }); + + t.notOk( detect(), 'detection result is `false`' ); + t.end(); + + function stub() { + throw new Error( 'boop' ); + } +}); diff --git a/has-set-support/README.md b/has-set-support/README.md new file mode 100644 index 00000000..9b96f8c6 --- /dev/null +++ b/has-set-support/README.md @@ -0,0 +1,113 @@ + + +# Set Support + +> Detect native [`Set`][mdn-set] support. + +
+ +## Usage + +```javascript +var hasSetSupport = require( '@stdlib/assert/has-set-support' ); +``` + +#### hasSetSupport() + +Detects if a runtime environment supports ES2015 [`Set`][mdn-set]. + +```javascript +var bool = hasSetSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasSetSupport = require( '@stdlib/assert/has-set-support' ); + +var bool = hasSetSupport(); +if ( bool ) { + console.log( 'Environment has Set support.' ); +} else { + console.log( 'Environment lacks Set support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-set-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-set-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-set-support/benchmark/benchmark.js b/has-set-support/benchmark/benchmark.js new file mode 100644 index 00000000..2a4f7a1c --- /dev/null +++ b/has-set-support/benchmark/benchmark.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasSetSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasSetSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/has-set-support/bin/cli b/has-set-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-set-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-set-support/docs/repl.txt b/has-set-support/docs/repl.txt new file mode 100644 index 00000000..bbc5f53e --- /dev/null +++ b/has-set-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `Set` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has native `Set` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-set-support/docs/types/index.d.ts b/has-set-support/docs/types/index.d.ts new file mode 100644 index 00000000..f318c783 --- /dev/null +++ b/has-set-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Set` support. +* +* @returns boolean indicating if an environment has `Set` support +* +* @example +* var bool = hasSetSupport(); +* // returns +*/ +declare function hasSetSupport(): boolean; + + +// EXPORTS // + +export = hasSetSupport; diff --git a/has-set-support/docs/types/test.ts b/has-set-support/docs/types/test.ts new file mode 100644 index 00000000..5d4330e0 --- /dev/null +++ b/has-set-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasSetSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasSetSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasSetSupport( true ); // $ExpectError + hasSetSupport( [], 123 ); // $ExpectError +} diff --git a/has-set-support/docs/usage.txt b/has-set-support/docs/usage.txt new file mode 100644 index 00000000..3e7a9155 --- /dev/null +++ b/has-set-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-set-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-set-support/etc/cli_opts.json b/has-set-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-set-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-set-support/examples/index.js b/has-set-support/examples/index.js new file mode 100644 index 00000000..2ffb93ce --- /dev/null +++ b/has-set-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasSetSupport = require( './../lib' ); + +var bool = hasSetSupport(); +if ( bool ) { + console.log( 'Environment has Set support.' ); +} else { + console.log( 'Environment lacks Set support.' ); +} diff --git a/has-set-support/lib/index.js b/has-set-support/lib/index.js new file mode 100644 index 00000000..c5abfbc9 --- /dev/null +++ b/has-set-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Set` support. +* +* @module @stdlib/assert/has-set-support +* +* @example +* var hasSetSupport = require( '@stdlib/assert/has-set-support' ); +* +* var bool = hasSetSupport(); +* // returns +*/ + +// MODULES // + +var hasSetSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasSetSupport; diff --git a/has-set-support/lib/main.js b/has-set-support/lib/main.js new file mode 100644 index 00000000..b8aefa84 --- /dev/null +++ b/has-set-support/lib/main.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var GlobalSet = require( './set.js' ); + + +// MAIN // + +/** +* Tests for native `Set` support. +* +* @returns {boolean} boolean indicating if an environment has `Set` support +* +* @example +* var bool = hasSetSupport(); +* // returns +*/ +function hasSetSupport() { + var bool; + var val; + var set; + + if ( typeof GlobalSet !== 'function' ) { + return false; + } + // Test basic support... + try { + val = {}; + set = new GlobalSet(); + set.add( val ); + set.add( val ); + bool = set.has( val ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasSetSupport; diff --git a/has-set-support/lib/set.js b/has-set-support/lib/set.js new file mode 100644 index 00000000..31e19f98 --- /dev/null +++ b/has-set-support/lib/set.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Set === 'function' ) ? Set : null; + + +// EXPORTS // + +module.exports = main; diff --git a/has-set-support/package.json b/has-set-support/package.json new file mode 100644 index 00000000..4f35fef0 --- /dev/null +++ b/has-set-support/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/has-set-support", + "version": "0.0.0", + "description": "Detect native Set support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-set-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "set", + "es2015", + "es6", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-set-support/test/test.cli.js b/has-set-support/test/test.cli.js new file mode 100644 index 00000000..4ab3c665 --- /dev/null +++ b/has-set-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Set` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-set-support/test/test.js b/has-set-support/test/test.js new file mode 100644 index 00000000..acdc8558 --- /dev/null +++ b/has-set-support/test/test.js @@ -0,0 +1,127 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasSet = ( typeof Set === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Set` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasSet ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './set.js': Mock + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function Mock() { + var arr = []; + + function has( value ) { + return arr.indexOf( value ) !== -1; + } + function add( value ) { + if ( has( value ) ) { + return; + } + arr.push( value ); + } + + return { + 'has': has, + 'add': add + }; + } +}); + +tape( 'if `Set` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasSet ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './set.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false` (mocked)' ); + + mocked = proxyquire( './../lib/main.js', { + './set.js': Mock1 + }); + t.strictEqual( mocked(), false, 'detection result is `false` (no `has` method)' ); + + mocked = proxyquire( './../lib/main.js', { + './set.js': Mock2 + }); + t.strictEqual( mocked(), false, 'detection result is `false` (no `add` method)' ); + + t.end(); + + function Mock1() { + var arr = []; + + function add( value ) { + arr.push( value ); + } + + return { + 'add': add + }; + } + + function Mock2() { + var arr = []; + + function has( value ) { + return arr.indexOf( value ) !== -1; + } + + return { + 'has': has + }; + } +}); diff --git a/has-sharedarraybuffer-support/README.md b/has-sharedarraybuffer-support/README.md new file mode 100644 index 00000000..21d4bca6 --- /dev/null +++ b/has-sharedarraybuffer-support/README.md @@ -0,0 +1,117 @@ + + +# SharedArrayBuffer Support + +> Detect native [`SharedArrayBuffer`][mdn-sharedarraybuffer] support. + +
+ +## Usage + + + +```javascript +var hasSharedArrayBufferSupport = require( '@stdlib/assert/has-sharedarraybuffer-support' ); +``` + +#### hasSharedArrayBufferSupport() + +Detects if a runtime environment supports [`SharedArrayBuffer`][mdn-sharedarraybuffer]. + +```javascript +var bool = hasSharedArrayBufferSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var hasSharedArrayBufferSupport = require( '@stdlib/assert/has-sharedarraybuffer-support' ); + +var bool = hasSharedArrayBufferSupport(); +if ( bool ) { + console.log( 'Environment has SharedArrayBuffer support.' ); +} else { + console.log( 'Environment lacks SharedArrayBuffer support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-sharedarraybuffer-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-sharedarraybuffer-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-sharedarraybuffer-support/benchmark/benchmark.js b/has-sharedarraybuffer-support/benchmark/benchmark.js new file mode 100644 index 00000000..5f16bfd0 --- /dev/null +++ b/has-sharedarraybuffer-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasSharedArrayBufferSupport = require( './../lib' ); // eslint-disable-line id-length + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasSharedArrayBufferSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-sharedarraybuffer-support/bin/cli b/has-sharedarraybuffer-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-sharedarraybuffer-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-sharedarraybuffer-support/docs/repl.txt b/has-sharedarraybuffer-support/docs/repl.txt new file mode 100644 index 00000000..cd3fb718 --- /dev/null +++ b/has-sharedarraybuffer-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `SharedArrayBuffer` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `SharedArrayBuffer` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-sharedarraybuffer-support/docs/types/index.d.ts b/has-sharedarraybuffer-support/docs/types/index.d.ts new file mode 100644 index 00000000..c2dbf6a3 --- /dev/null +++ b/has-sharedarraybuffer-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `SharedArrayBuffer` support. +* +* @returns boolean indicating if an environment has `SharedArrayBuffer` support +* +* @example +* var bool = hasSharedArrayBufferSupport(); +* // returns +*/ +declare function hasSharedArrayBufferSupport(): boolean; + + +// EXPORTS // + +export = hasSharedArrayBufferSupport; diff --git a/has-sharedarraybuffer-support/docs/types/test.ts b/has-sharedarraybuffer-support/docs/types/test.ts new file mode 100644 index 00000000..1874b2fd --- /dev/null +++ b/has-sharedarraybuffer-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasSharedArrayBufferSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasSharedArrayBufferSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasSharedArrayBufferSupport( true ); // $ExpectError + hasSharedArrayBufferSupport( [], 123 ); // $ExpectError +} diff --git a/has-sharedarraybuffer-support/docs/usage.txt b/has-sharedarraybuffer-support/docs/usage.txt new file mode 100644 index 00000000..36248b70 --- /dev/null +++ b/has-sharedarraybuffer-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-sharedarraybuffer-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-sharedarraybuffer-support/etc/cli_opts.json b/has-sharedarraybuffer-support/etc/cli_opts.json new file mode 100644 index 00000000..92119c45 --- /dev/null +++ b/has-sharedarraybuffer-support/etc/cli_opts.json @@ -0,0 +1,15 @@ +{ + "string": [], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-sharedarraybuffer-support/examples/index.js b/has-sharedarraybuffer-support/examples/index.js new file mode 100644 index 00000000..e54c49f9 --- /dev/null +++ b/has-sharedarraybuffer-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasSharedArrayBufferSupport = require( './../lib' ); // eslint-disable-line id-length + +var bool = hasSharedArrayBufferSupport(); +if ( bool ) { + console.log( 'Environment has SharedArrayBuffer support.' ); +} else { + console.log( 'Environment lacks SharedArrayBuffer support.' ); +} diff --git a/has-sharedarraybuffer-support/lib/index.js b/has-sharedarraybuffer-support/lib/index.js new file mode 100644 index 00000000..6f73e149 --- /dev/null +++ b/has-sharedarraybuffer-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `SharedArrayBuffer` support. +* +* @module @stdlib/assert/has-sharedarraybuffer-support +* +* @example +* var hasSharedArrayBufferSupport = require( '@stdlib/assert/has-sharedarraybuffer-support' ); +* +* var bool = hasSharedArrayBufferSupport(); +* // returns +*/ + +// MODULES // + +var hasSharedArrayBufferSupport = require( './main.js' ); // eslint-disable-line id-length + + +// EXPORTS // + +module.exports = hasSharedArrayBufferSupport; diff --git a/has-sharedarraybuffer-support/lib/main.js b/has-sharedarraybuffer-support/lib/main.js new file mode 100644 index 00000000..807dd873 --- /dev/null +++ b/has-sharedarraybuffer-support/lib/main.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isSharedArrayBuffer = require( '@stdlib/assert/is-sharedarraybuffer' ); +var GlobalSharedArrayBuffer = require( './sharedarraybuffer.js' ); + + +// MAIN // + +/** +* Tests for native `SharedArrayBuffer` support. +* +* @returns {boolean} boolean indicating if an environment has `SharedArrayBuffer` support +* +* @example +* var bool = hasSharedArrayBufferSupport(); +* // returns +*/ +function hasSharedArrayBufferSupport() { // eslint-disable-line id-length + var bool; + var b1; + var b2; + + if ( typeof GlobalSharedArrayBuffer !== 'function' ) { + return false; + } + // Test basic support... + try { + b1 = new GlobalSharedArrayBuffer( 16 ); + b2 = b1.slice( 4, 8 ); + bool = ( + isSharedArrayBuffer( b1 ) && + isSharedArrayBuffer( b2 ) && + b1.byteLength === 16 && + b2.byteLength === 4 + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasSharedArrayBufferSupport; diff --git a/has-sharedarraybuffer-support/lib/sharedarraybuffer.js b/has-sharedarraybuffer-support/lib/sharedarraybuffer.js new file mode 100644 index 00000000..a3c7d45c --- /dev/null +++ b/has-sharedarraybuffer-support/lib/sharedarraybuffer.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof SharedArrayBuffer === 'function' ) ? SharedArrayBuffer : null; // eslint-disable-line no-undef, stdlib/require-globals + + +// EXPORTS // + +module.exports = main; diff --git a/has-sharedarraybuffer-support/package.json b/has-sharedarraybuffer-support/package.json new file mode 100644 index 00000000..9dab39a1 --- /dev/null +++ b/has-sharedarraybuffer-support/package.json @@ -0,0 +1,82 @@ +{ + "name": "@stdlib/assert/has-sharedarraybuffer-support", + "version": "0.0.0", + "description": "Detect native SharedArrayBuffer support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-sharedarraybuffer-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "arraybuffer", + "shared", + "sharedarraybuffer", + "es2017", + "es7", + "array", + "buffer", + "buf", + "binary", + "typed array", + "typed-array", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-sharedarraybuffer-support/test/test.cli.js b/has-sharedarraybuffer-support/test/test.cli.js new file mode 100644 index 00000000..da934f48 --- /dev/null +++ b/has-sharedarraybuffer-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `SharedArrayBuffer` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-sharedarraybuffer-support/test/test.js b/has-sharedarraybuffer-support/test/test.js new file mode 100644 index 00000000..a728145d --- /dev/null +++ b/has-sharedarraybuffer-support/test/test.js @@ -0,0 +1,194 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasSharedArrayBuffer = ( typeof SharedArrayBuffer === 'function' ); // eslint-disable-line stdlib/require-globals + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `SharedArrayBuffer` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasSharedArrayBuffer ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + function Mock( len ) { + var out; + var i; + + out = new Array( len/8 ); // we assume evenly divisible + for ( i = 0; i < out.length; i++ ) { + out[ i ] = 0; + } + out.byteLength = len; + out.slice = slice; + return out; + + function slice( start, end ) { + var arr; + var i; + + arr = []; + for ( i = start; i < end; i++ ) { + arr.push( out[ i ] ); + } + arr.byteLength = arr.length; + arr.slice = slice; + return arr; + } + } + + mocked = proxyquire( './../lib/main.js', { + './sharedarraybuffer.js': Mock, + '@stdlib/assert/is-sharedarraybuffer': isBuffer + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function isBuffer() { + return true; + } +}); + +tape( 'if `SharedArrayBuffer` is not supported, detection result is `false` (no SharedArrayBuffer global function)', function test( t ) { + var mocked; + if ( hasSharedArrayBuffer ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './sharedarraybuffer.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); +}); + +tape( 'if `SharedArrayBuffer` is not supported, detected result is `false` (constructor throws)', function test( t ) { + var mocked; + if ( hasSharedArrayBuffer ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './sharedarraybuffer.js': mock + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); + + function mock() { + throw new Error( 'beep' ); + } +}); + +tape( 'if `SharedArrayBuffer` is not supported, detected result is `false` (no slice method)', function test( t ) { + var mocked; + if ( hasSharedArrayBuffer ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + function Mock( len ) { + var out; + var i; + + out = new Array( len/8 ); // we assume evenly divisible + for ( i = 0; i < out.length; i++ ) { + out[ i ] = 0; + } + out.byteLength = len; + return out; + } + mocked = proxyquire( './../lib/main.js', { + './sharedarraybuffer.js': Mock, + '@stdlib/assert/is-sharedarraybuffer': isBuffer + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); + + function isBuffer() { + return true; + } +}); + +tape( 'if `SharedArrayBuffer` is not supported, detected result is `false` (no byteLength property)', function test( t ) { + var mocked; + if ( hasSharedArrayBuffer ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + function Mock( len ) { + var out; + var i; + + out = new Array( len/8 ); // we assume evenly divisible + for ( i = 0; i < out.length; i++ ) { + out[ i ] = 0; + } + out.slice = slice; + return out; + + function slice( start, end ) { + var arr; + var i; + + arr = []; + for ( i = start; i < end; i++ ) { + arr.push( out[ i ] ); + } + arr.slice = slice; + return arr; + } + } + mocked = proxyquire( './../lib/main.js', { + './sharedarraybuffer.js': Mock, + '@stdlib/assert/is-sharedarraybuffer': isBuffer + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + t.end(); + + function isBuffer() { + return true; + } +}); diff --git a/has-symbol-support/README.md b/has-symbol-support/README.md new file mode 100644 index 00000000..ea8d928e --- /dev/null +++ b/has-symbol-support/README.md @@ -0,0 +1,113 @@ + + +# Symbol Support + +> Detect native [`Symbol`][mdn-symbol] support. + +
+ +## Usage + +```javascript +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +``` + +#### hasSymbolSupport() + +Detects if a runtime environment supports ES2015 [`Symbol`][mdn-symbol]. + +```javascript +var bool = hasSymbolSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); + +var bool = hasSymbolSupport(); +if ( bool ) { + console.log( 'Environment has Symbol support.' ); +} else { + console.log( 'Environment lacks Symbol support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-symbol-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-symbol-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-symbol-support/benchmark/benchmark.js b/has-symbol-support/benchmark/benchmark.js new file mode 100644 index 00000000..292b8f54 --- /dev/null +++ b/has-symbol-support/benchmark/benchmark.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasSymbolSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasSymbolSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/has-symbol-support/bin/cli b/has-symbol-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-symbol-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-symbol-support/docs/repl.txt b/has-symbol-support/docs/repl.txt new file mode 100644 index 00000000..7f79b22a --- /dev/null +++ b/has-symbol-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `Symbol` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has native `Symbol` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-symbol-support/docs/types/index.d.ts b/has-symbol-support/docs/types/index.d.ts new file mode 100644 index 00000000..2382ab8c --- /dev/null +++ b/has-symbol-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Symbol` support. +* +* @returns boolean indicating if an environment has `Symbol` support +* +* @example +* var bool = hasSymbolSupport(); +* // returns +*/ +declare function hasSymbolSupport(): boolean; + + +// EXPORTS // + +export = hasSymbolSupport; diff --git a/has-symbol-support/docs/types/test.ts b/has-symbol-support/docs/types/test.ts new file mode 100644 index 00000000..2be9e898 --- /dev/null +++ b/has-symbol-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasSymbolSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasSymbolSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasSymbolSupport( true ); // $ExpectError + hasSymbolSupport( [], 123 ); // $ExpectError +} diff --git a/has-symbol-support/docs/usage.txt b/has-symbol-support/docs/usage.txt new file mode 100644 index 00000000..2fa15aca --- /dev/null +++ b/has-symbol-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-symbol-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-symbol-support/etc/cli_opts.json b/has-symbol-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-symbol-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-symbol-support/examples/index.js b/has-symbol-support/examples/index.js new file mode 100644 index 00000000..f18159fb --- /dev/null +++ b/has-symbol-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasSymbolSupport = require( './../lib' ); + +var bool = hasSymbolSupport(); +if ( bool ) { + console.log( 'Environment has Symbol support.' ); +} else { + console.log( 'Environment lacks Symbol support.' ); +} diff --git a/has-symbol-support/lib/index.js b/has-symbol-support/lib/index.js new file mode 100644 index 00000000..f218afe8 --- /dev/null +++ b/has-symbol-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Symbol` support. +* +* @module @stdlib/assert/has-symbol-support +* +* @example +* var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +* +* var bool = hasSymbolSupport(); +* // returns +*/ + +// MODULES // + +var hasSymbolSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasSymbolSupport; diff --git a/has-symbol-support/lib/main.js b/has-symbol-support/lib/main.js new file mode 100644 index 00000000..79bd6d16 --- /dev/null +++ b/has-symbol-support/lib/main.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests for native `Symbol` support. +* +* @returns {boolean} boolean indicating if an environment has `Symbol` support +* +* @example +* var bool = hasSymbolSupport(); +* // returns +*/ +function hasSymbolSupport() { + return ( + typeof Symbol === 'function' && + typeof Symbol( 'foo' ) === 'symbol' + ); +} + + +// EXPORTS // + +module.exports = hasSymbolSupport; diff --git a/has-symbol-support/package.json b/has-symbol-support/package.json new file mode 100644 index 00000000..08c4794f --- /dev/null +++ b/has-symbol-support/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/has-symbol-support", + "version": "0.0.0", + "description": "Detect native Symbol support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-symbol-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "symbol", + "es2015", + "es6", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-symbol-support/test/test.cli.js b/has-symbol-support/test/test.cli.js new file mode 100644 index 00000000..1da7763d --- /dev/null +++ b/has-symbol-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Symbol` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-symbol-support/test/test.js b/has-symbol-support/test/test.js new file mode 100644 index 00000000..fbc0d5f8 --- /dev/null +++ b/has-symbol-support/test/test.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable stdlib/require-globals */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasSymbols = ( typeof Symbol === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Symbols` are supported, detection result is `true`', function test( t ) { + if ( hasSymbols ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + t.end(); +}); + +tape( 'the function guards against a `Symbol` global variable which does not produce `symbols`', function test( t ) { + var tmp; + if ( hasSymbols ) { + tmp = Symbol; + Symbol = {}; // eslint-disable-line no-global-assign + } else { + global.Symbol = {}; + } + t.strictEqual( detect(), false, 'detection result is `false`' ); + if ( hasSymbols ) { + Symbol = tmp; // eslint-disable-line no-global-assign + } + t.end(); +}); diff --git a/has-tostringtag-support/README.md b/has-tostringtag-support/README.md new file mode 100644 index 00000000..8d46f280 --- /dev/null +++ b/has-tostringtag-support/README.md @@ -0,0 +1,113 @@ + + +# Symbol.toStringTag Support + +> Detect native [`Symbol.toStringTag`][mdn-symbol] support. + +
+ +## Usage + +```javascript +var hasToStringTagSupport = require( '@stdlib/assert/has-tostringtag-support' ); +``` + +#### hasToStringTagSupport() + +Detects if a runtime environment supports ES2015 [`Symbol.toStringTag`][mdn-symbol]. + +```javascript +var bool = hasToStringTagSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasToStringTagSupport = require( '@stdlib/assert/has-tostringtag-support' ); + +var bool = hasToStringTagSupport(); +if ( bool ) { + console.log( 'Environment has `toStringTag` support.' ); +} else { + console.log( 'Environment lacks `toStringTag` support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-tostringtag-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-tostringtag-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-tostringtag-support/benchmark/benchmark.js b/has-tostringtag-support/benchmark/benchmark.js new file mode 100644 index 00000000..4c5b9982 --- /dev/null +++ b/has-tostringtag-support/benchmark/benchmark.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasToStringTagSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasToStringTagSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/has-tostringtag-support/bin/cli b/has-tostringtag-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-tostringtag-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-tostringtag-support/docs/repl.txt b/has-tostringtag-support/docs/repl.txt new file mode 100644 index 00000000..4673c033 --- /dev/null +++ b/has-tostringtag-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `toStringTag` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `toStringTag` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-tostringtag-support/docs/types/index.d.ts b/has-tostringtag-support/docs/types/index.d.ts new file mode 100644 index 00000000..f5e9dbdc --- /dev/null +++ b/has-tostringtag-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `toStringTag` support. +* +* @returns boolean indicating if an environment has `toStringTag` support +* +* @example +* var bool = hasToStringTagSupport(); +* // returns +*/ +declare function hasToStringTagSupport(): boolean; + + +// EXPORTS // + +export = hasToStringTagSupport; diff --git a/has-tostringtag-support/docs/types/test.ts b/has-tostringtag-support/docs/types/test.ts new file mode 100644 index 00000000..8f8bfbe4 --- /dev/null +++ b/has-tostringtag-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasToStringTagSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasToStringTagSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasToStringTagSupport( true ); // $ExpectError + hasToStringTagSupport( [], 123 ); // $ExpectError +} diff --git a/has-tostringtag-support/docs/usage.txt b/has-tostringtag-support/docs/usage.txt new file mode 100644 index 00000000..0a722c42 --- /dev/null +++ b/has-tostringtag-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-tostringtag-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-tostringtag-support/etc/cli_opts.json b/has-tostringtag-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-tostringtag-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-tostringtag-support/examples/index.js b/has-tostringtag-support/examples/index.js new file mode 100644 index 00000000..dec614c5 --- /dev/null +++ b/has-tostringtag-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasToStringTagSupport = require( './../lib' ); + +var bool = hasToStringTagSupport(); +if ( bool ) { + console.log( 'Environment has `toStringTag` support.' ); +} else { + console.log( 'Environment lacks `toStringTag` support.' ); +} diff --git a/has-tostringtag-support/lib/index.js b/has-tostringtag-support/lib/index.js new file mode 100644 index 00000000..80672c0d --- /dev/null +++ b/has-tostringtag-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `toStringTag` support. +* +* @module @stdlib/assert/has-tostringtag-support +* +* @example +* var hasToStringTagSupport = require( '@stdlib/assert/has-tostringtag-support' ); +* +* var bool = hasToStringTagSupport(); +* // returns +*/ + +// MODULES // + +var hasToStringTagSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasToStringTagSupport; diff --git a/has-tostringtag-support/lib/main.js b/has-tostringtag-support/lib/main.js new file mode 100644 index 00000000..b336c895 --- /dev/null +++ b/has-tostringtag-support/lib/main.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); + + +// VARIABLES // + +var FLG = hasSymbols(); + + +// MAIN // + +/** +* Tests for native `toStringTag` support. +* +* @returns {boolean} boolean indicating if an environment has `toStringTag` support +* +* @example +* var bool = hasToStringTagSupport(); +* // returns +*/ +function hasToStringTagSupport() { + return ( FLG && typeof Symbol.toStringTag === 'symbol' ); +} + + +// EXPORTS // + +module.exports = hasToStringTagSupport; diff --git a/has-tostringtag-support/package.json b/has-tostringtag-support/package.json new file mode 100644 index 00000000..17c35844 --- /dev/null +++ b/has-tostringtag-support/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/has-tostringtag-support", + "version": "0.0.0", + "description": "Detect native Symbol.toStringTag support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-tostringtag-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "symbol", + "tostringtag", + "class", + "type", + "typeof", + "es2015", + "es6", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-tostringtag-support/test/test.cli.js b/has-tostringtag-support/test/test.cli.js new file mode 100644 index 00000000..b749e54a --- /dev/null +++ b/has-tostringtag-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `toStringTag` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-tostringtag-support/test/test.js b/has-tostringtag-support/test/test.js new file mode 100644 index 00000000..2049779f --- /dev/null +++ b/has-tostringtag-support/test/test.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Symbols` are not supported, detection result is `false`', function test( t ) { + var detect = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-symbol-support': hasSupport + }); + + t.strictEqual( detect(), false, 'detection result is `true`' ); + t.end(); + + function hasSupport() { + return false; + } +}); + +tape( 'if `toStringTag` is supported, detection result is `true`', opts, function test( t ) { + if ( typeof Symbol.toStringTag === 'symbol' ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + t.end(); +}); + +tape( 'if `toStringTag` is not supported, the function guards against non-symbol `toStringTag` properties and the detection result is `false`', opts, function test( t ) { + if ( typeof Symbol.toStringTag === 'symbol' ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + Symbol.toStringTag = 'beep'; + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + t.end(); +}); diff --git a/has-uint16array-support/README.md b/has-uint16array-support/README.md new file mode 100644 index 00000000..a11c2db0 --- /dev/null +++ b/has-uint16array-support/README.md @@ -0,0 +1,113 @@ + + +# Uint16Array Support + +> Detect native [`Uint16Array`][mdn-uint16array] support. + +
+ +## Usage + +```javascript +var hasUint16ArraySupport = require( '@stdlib/assert/has-uint16array-support' ); +``` + +#### hasUint16ArraySupport() + +Detects if a runtime environment supports [`Uint16Array`][mdn-uint16array]. + +```javascript +var bool = hasUint16ArraySupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasUint16ArraySupport = require( '@stdlib/assert/has-uint16array-support' ); + +var bool = hasUint16ArraySupport(); +if ( bool ) { + console.log( 'Environment has Uint16Array support.' ); +} else { + console.log( 'Environment lacks Uint16Array support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-uint16array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-uint16array-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-uint16array-support/benchmark/benchmark.js b/has-uint16array-support/benchmark/benchmark.js new file mode 100644 index 00000000..0b0422eb --- /dev/null +++ b/has-uint16array-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasUint16ArraySupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasUint16ArraySupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-uint16array-support/bin/cli b/has-uint16array-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-uint16array-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-uint16array-support/docs/repl.txt b/has-uint16array-support/docs/repl.txt new file mode 100644 index 00000000..76e23301 --- /dev/null +++ b/has-uint16array-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `Uint16Array` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `Uint16Array` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-uint16array-support/docs/types/index.d.ts b/has-uint16array-support/docs/types/index.d.ts new file mode 100644 index 00000000..35e35fb0 --- /dev/null +++ b/has-uint16array-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Uint16Array` support. +* +* @returns boolean indicating if an environment has `Uint16Array` support +* +* @example +* var bool = hasUint16ArraySupport(); +* // returns +*/ +declare function hasUint16ArraySupport(): boolean; + + +// EXPORTS // + +export = hasUint16ArraySupport; diff --git a/has-uint16array-support/docs/types/test.ts b/has-uint16array-support/docs/types/test.ts new file mode 100644 index 00000000..ccf073b8 --- /dev/null +++ b/has-uint16array-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasUint16ArraySupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasUint16ArraySupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasUint16ArraySupport( true ); // $ExpectError + hasUint16ArraySupport( [], 123 ); // $ExpectError +} diff --git a/has-uint16array-support/docs/usage.txt b/has-uint16array-support/docs/usage.txt new file mode 100644 index 00000000..b319e877 --- /dev/null +++ b/has-uint16array-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-uint16array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-uint16array-support/etc/cli_opts.json b/has-uint16array-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-uint16array-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-uint16array-support/examples/index.js b/has-uint16array-support/examples/index.js new file mode 100644 index 00000000..07017acf --- /dev/null +++ b/has-uint16array-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasUint16ArraySupport = require( './../lib' ); + +var bool = hasUint16ArraySupport(); +if ( bool ) { + console.log( 'Environment has Uint16Array support.' ); +} else { + console.log( 'Environment lacks Uint16Array support.' ); +} diff --git a/has-uint16array-support/lib/index.js b/has-uint16array-support/lib/index.js new file mode 100644 index 00000000..aae6d982 --- /dev/null +++ b/has-uint16array-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Uint16Array` support. +* +* @module @stdlib/assert/has-uint16array-support +* +* @example +* var hasUint16ArraySupport = require( '@stdlib/assert/has-uint16array-support' ); +* +* var bool = hasUint16ArraySupport(); +* // returns +*/ + +// MODULES // + +var hasUint16ArraySupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasUint16ArraySupport; diff --git a/has-uint16array-support/lib/main.js b/has-uint16array-support/lib/main.js new file mode 100644 index 00000000..899cd8e7 --- /dev/null +++ b/has-uint16array-support/lib/main.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isUint16Array = require( '@stdlib/assert/is-uint16array' ); +var UINT16_MAX = require( '@stdlib/constants/math/uint16-max' ); +var GlobalUint16Array = require( './uint16array.js' ); + + +// MAIN // + +/** +* Tests for native `Uint16Array` support. +* +* @returns {boolean} boolean indicating if an environment has `Uint16Array` support +* +* @example +* var bool = hasUint16ArraySupport(); +* // returns +*/ +function hasUint16ArraySupport() { + var bool; + var arr; + + if ( typeof GlobalUint16Array !== 'function' ) { + return false; + } + // Test basic support... + try { + arr = [ 1, 3.14, -3.14, UINT16_MAX+1, UINT16_MAX+2 ]; + arr = new GlobalUint16Array( arr ); + bool = ( + isUint16Array( arr ) && + arr[ 0 ] === 1 && + arr[ 1 ] === 3 && // truncation + arr[ 2 ] === UINT16_MAX-2 && // truncation and wrap around + arr[ 3 ] === 0 && // wrap around + arr[ 4 ] === 1 // wrap around + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasUint16ArraySupport; diff --git a/has-uint16array-support/lib/uint16array.js b/has-uint16array-support/lib/uint16array.js new file mode 100644 index 00000000..f2e7bb7f --- /dev/null +++ b/has-uint16array-support/lib/uint16array.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Uint16Array === 'function' ) ? Uint16Array : null; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = main; diff --git a/has-uint16array-support/package.json b/has-uint16array-support/package.json new file mode 100644 index 00000000..aeed3559 --- /dev/null +++ b/has-uint16array-support/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/has-uint16array-support", + "version": "0.0.0", + "description": "Detect native Uint16Array support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-uint16array-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "uint16array", + "es2015", + "es6", + "typed array", + "typed-array", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-uint16array-support/test/test.cli.js b/has-uint16array-support/test/test.cli.js new file mode 100644 index 00000000..452fd284 --- /dev/null +++ b/has-uint16array-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Uint16Array` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-uint16array-support/test/test.js b/has-uint16array-support/test/test.js new file mode 100644 index 00000000..ccecbd63 --- /dev/null +++ b/has-uint16array-support/test/test.js @@ -0,0 +1,135 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasUint16Array = ( typeof Uint16Array === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Uint16Array` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasUint16Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './uint16array.js': Mock, + '@stdlib/assert/is-uint16array': isArray + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock() { + return [ + 1, + 3, + 65533, + 0, + 1 + ]; + } +}); + +tape( 'if `Uint16Array` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasUint16Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './uint16array.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint16array.js': Mock1 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint16array.js': Mock2, + '@stdlib/assert/is-uint16array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint16array.js': Mock3, + '@stdlib/assert/is-uint16array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint16array.js': Mock4 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock1() { + // Not a typed array: + return []; + } + + function Mock2() { + // Does not truncate... + return [ 1, 3.14, -3.14, 0, 1 ]; + } + + function Mock3() { + // Does not wrap around... + return [ 1, 3, 65533, 65536, 65537 ]; + } + + function Mock4() { + throw new Error( 'beep' ); + } +}); diff --git a/has-uint32array-support/README.md b/has-uint32array-support/README.md new file mode 100644 index 00000000..7ae9b233 --- /dev/null +++ b/has-uint32array-support/README.md @@ -0,0 +1,113 @@ + + +# Uint32Array Support + +> Detect native [`Uint32Array`][mdn-uint32array] support. + +
+ +## Usage + +```javascript +var hasUint32ArraySupport = require( '@stdlib/assert/has-uint32array-support' ); +``` + +#### hasUint32ArraySupport() + +Detects if a runtime environment supports [`Uint32Array`][mdn-uint32array]. + +```javascript +var bool = hasUint32ArraySupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasUint32ArraySupport = require( '@stdlib/assert/has-uint32array-support' ); + +var bool = hasUint32ArraySupport(); +if ( bool ) { + console.log( 'Environment has Uint32Array support.' ); +} else { + console.log( 'Environment lacks Uint32Array support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-uint32array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-uint32array-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-uint32array-support/benchmark/benchmark.js b/has-uint32array-support/benchmark/benchmark.js new file mode 100644 index 00000000..fa484c6a --- /dev/null +++ b/has-uint32array-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasUint32ArraySupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasUint32ArraySupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-uint32array-support/bin/cli b/has-uint32array-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-uint32array-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-uint32array-support/docs/repl.txt b/has-uint32array-support/docs/repl.txt new file mode 100644 index 00000000..936027ff --- /dev/null +++ b/has-uint32array-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `Uint32Array` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `Uint32Array` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-uint32array-support/docs/types/index.d.ts b/has-uint32array-support/docs/types/index.d.ts new file mode 100644 index 00000000..63d8e9ed --- /dev/null +++ b/has-uint32array-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Uint32Array` support. +* +* @returns boolean indicating if an environment has `Uint32Array` support +* +* @example +* var bool = hasUint32ArraySupport(); +* // returns +*/ +declare function hasUint32ArraySupport(): boolean; + + +// EXPORTS // + +export = hasUint32ArraySupport; diff --git a/has-uint32array-support/docs/types/test.ts b/has-uint32array-support/docs/types/test.ts new file mode 100644 index 00000000..afc4e9c2 --- /dev/null +++ b/has-uint32array-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasUint32ArraySupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasUint32ArraySupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasUint32ArraySupport( true ); // $ExpectError + hasUint32ArraySupport( [], 123 ); // $ExpectError +} diff --git a/has-uint32array-support/docs/usage.txt b/has-uint32array-support/docs/usage.txt new file mode 100644 index 00000000..29a1a809 --- /dev/null +++ b/has-uint32array-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-uint32array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-uint32array-support/etc/cli_opts.json b/has-uint32array-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-uint32array-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-uint32array-support/examples/index.js b/has-uint32array-support/examples/index.js new file mode 100644 index 00000000..43b3ba81 --- /dev/null +++ b/has-uint32array-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasUint32ArraySupport = require( './../lib' ); + +var bool = hasUint32ArraySupport(); +if ( bool ) { + console.log( 'Environment has Uint32Array support.' ); +} else { + console.log( 'Environment lacks Uint32Array support.' ); +} diff --git a/has-uint32array-support/lib/index.js b/has-uint32array-support/lib/index.js new file mode 100644 index 00000000..6a83df26 --- /dev/null +++ b/has-uint32array-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Uint32Array` support. +* +* @module @stdlib/assert/has-uint32array-support +* +* @example +* var hasUint32ArraySupport = require( '@stdlib/assert/has-uint32array-support' ); +* +* var bool = hasUint32ArraySupport(); +* // returns +*/ + +// MODULES // + +var hasUint32ArraySupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasUint32ArraySupport; diff --git a/has-uint32array-support/lib/main.js b/has-uint32array-support/lib/main.js new file mode 100644 index 00000000..3c6ae8ca --- /dev/null +++ b/has-uint32array-support/lib/main.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isUint32Array = require( '@stdlib/assert/is-uint32array' ); +var UINT32_MAX = require( '@stdlib/constants/math/uint32-max' ); +var GlobalUint32Array = require( './uint32array.js' ); + + +// MAIN // + +/** +* Tests for native `Uint32Array` support. +* +* @returns {boolean} boolean indicating if an environment has `Uint32Array` support +* +* @example +* var bool = hasUint32ArraySupport(); +* // returns +*/ +function hasUint32ArraySupport() { + var bool; + var arr; + + if ( typeof GlobalUint32Array !== 'function' ) { + return false; + } + // Test basic support... + try { + arr = [ 1, 3.14, -3.14, UINT32_MAX+1, UINT32_MAX+2 ]; + arr = new GlobalUint32Array( arr ); + bool = ( + isUint32Array( arr ) && + arr[ 0 ] === 1 && + arr[ 1 ] === 3 && // truncation + arr[ 2 ] === UINT32_MAX-2 && // truncation and wrap around + arr[ 3 ] === 0 && // wrap around + arr[ 4 ] === 1 // wrap around + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasUint32ArraySupport; diff --git a/has-uint32array-support/lib/uint32array.js b/has-uint32array-support/lib/uint32array.js new file mode 100644 index 00000000..d85130e8 --- /dev/null +++ b/has-uint32array-support/lib/uint32array.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Uint32Array === 'function' ) ? Uint32Array : null; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = main; diff --git a/has-uint32array-support/package.json b/has-uint32array-support/package.json new file mode 100644 index 00000000..0cd6e997 --- /dev/null +++ b/has-uint32array-support/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/has-uint32array-support", + "version": "0.0.0", + "description": "Detect native Uint32Array support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-uint32array-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "uint32array", + "es2015", + "es6", + "typed array", + "typed-array", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-uint32array-support/test/test.cli.js b/has-uint32array-support/test/test.cli.js new file mode 100644 index 00000000..62226a97 --- /dev/null +++ b/has-uint32array-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Uint32Array` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-uint32array-support/test/test.js b/has-uint32array-support/test/test.js new file mode 100644 index 00000000..8b5f2a11 --- /dev/null +++ b/has-uint32array-support/test/test.js @@ -0,0 +1,135 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasUint32Array = ( typeof Uint32Array === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Uint32Array` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasUint32Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './uint32array.js': Mock, + '@stdlib/assert/is-uint32array': isArray + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock() { + return [ + 1, + 3, + 4294967293, + 0, + 1 + ]; + } +}); + +tape( 'if `Uint32Array` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasUint32Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './uint32array.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint32array.js': Mock1 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint32array.js': Mock2, + '@stdlib/assert/is-uint32array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint32array.js': Mock3, + '@stdlib/assert/is-uint32array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint32array.js': Mock4 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock1() { + // Not a typed array: + return []; + } + + function Mock2() { + // Does not truncate... + return [ 1, 3.14, -3.14, 0, 1 ]; + } + + function Mock3() { + // Does not wrap around... + return [ 1, 3, 4294967293, 4294967296, 4294967297 ]; + } + + function Mock4() { + throw new Error( 'beep' ); + } +}); diff --git a/has-uint8array-support/README.md b/has-uint8array-support/README.md new file mode 100644 index 00000000..8348695e --- /dev/null +++ b/has-uint8array-support/README.md @@ -0,0 +1,113 @@ + + +# Uint8Array Support + +> Detect native [`Uint8Array`][mdn-uint8array] support. + +
+ +## Usage + +```javascript +var hasUint8ArraySupport = require( '@stdlib/assert/has-uint8array-support' ); +``` + +#### hasUint8ArraySupport() + +Detects if a runtime environment supports [`Uint8Array`][mdn-uint8array]. + +```javascript +var bool = hasUint8ArraySupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasUint8ArraySupport = require( '@stdlib/assert/has-uint8array-support' ); + +var bool = hasUint8ArraySupport(); +if ( bool ) { + console.log( 'Environment has Uint8Array support.' ); +} else { + console.log( 'Environment lacks Uint8Array support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-uint8array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-uint8array-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-uint8array-support/benchmark/benchmark.js b/has-uint8array-support/benchmark/benchmark.js new file mode 100644 index 00000000..cf86e630 --- /dev/null +++ b/has-uint8array-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasUint8ArraySupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasUint8ArraySupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-uint8array-support/bin/cli b/has-uint8array-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-uint8array-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-uint8array-support/docs/repl.txt b/has-uint8array-support/docs/repl.txt new file mode 100644 index 00000000..4f17b4fb --- /dev/null +++ b/has-uint8array-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `Uint8Array` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `Uint8Array` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-uint8array-support/docs/types/index.d.ts b/has-uint8array-support/docs/types/index.d.ts new file mode 100644 index 00000000..9900754c --- /dev/null +++ b/has-uint8array-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Uint8Array` support. +* +* @returns boolean indicating if an environment has `Uint8Array` support +* +* @example +* var bool = hasUint8ArraySupport(); +* // returns +*/ +declare function hasUint8ArraySupport(): boolean; + + +// EXPORTS // + +export = hasUint8ArraySupport; diff --git a/has-uint8array-support/docs/types/test.ts b/has-uint8array-support/docs/types/test.ts new file mode 100644 index 00000000..5a638152 --- /dev/null +++ b/has-uint8array-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasUint8ArraySupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasUint8ArraySupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasUint8ArraySupport( true ); // $ExpectError + hasUint8ArraySupport( [], 123 ); // $ExpectError +} diff --git a/has-uint8array-support/docs/usage.txt b/has-uint8array-support/docs/usage.txt new file mode 100644 index 00000000..de373991 --- /dev/null +++ b/has-uint8array-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-uint8array-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-uint8array-support/etc/cli_opts.json b/has-uint8array-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-uint8array-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-uint8array-support/examples/index.js b/has-uint8array-support/examples/index.js new file mode 100644 index 00000000..70184df4 --- /dev/null +++ b/has-uint8array-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasUint8ArraySupport = require( './../lib' ); + +var bool = hasUint8ArraySupport(); +if ( bool ) { + console.log( 'Environment has Uint8Array support.' ); +} else { + console.log( 'Environment lacks Uint8Array support.' ); +} diff --git a/has-uint8array-support/lib/index.js b/has-uint8array-support/lib/index.js new file mode 100644 index 00000000..ed2beaf3 --- /dev/null +++ b/has-uint8array-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Uint8Array` support. +* +* @module @stdlib/assert/has-uint8array-support +* +* @example +* var hasUint8ArraySupport = require( '@stdlib/assert/has-uint8array-support' ); +* +* var bool = hasUint8ArraySupport(); +* // returns +*/ + +// MODULES // + +var hasUint8ArraySupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasUint8ArraySupport; diff --git a/has-uint8array-support/lib/main.js b/has-uint8array-support/lib/main.js new file mode 100644 index 00000000..7daf3bbe --- /dev/null +++ b/has-uint8array-support/lib/main.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isUint8Array = require( '@stdlib/assert/is-uint8array' ); +var UINT8_MAX = require( '@stdlib/constants/math/uint8-max' ); +var GlobalUint8Array = require( './uint8array.js' ); + + +// MAIN // + +/** +* Tests for native `Uint8Array` support. +* +* @returns {boolean} boolean indicating if an environment has `Uint8Array` support +* +* @example +* var bool = hasUint8ArraySupport(); +* // returns +*/ +function hasUint8ArraySupport() { + var bool; + var arr; + + if ( typeof GlobalUint8Array !== 'function' ) { + return false; + } + // Test basic support... + try { + arr = [ 1, 3.14, -3.14, UINT8_MAX+1, UINT8_MAX+2 ]; + arr = new GlobalUint8Array( arr ); + bool = ( + isUint8Array( arr ) && + arr[ 0 ] === 1 && + arr[ 1 ] === 3 && // truncation + arr[ 2 ] === UINT8_MAX-2 && // truncation and wrap around + arr[ 3 ] === 0 && // wrap around + arr[ 4 ] === 1 // wrap around + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasUint8ArraySupport; diff --git a/has-uint8array-support/lib/uint8array.js b/has-uint8array-support/lib/uint8array.js new file mode 100644 index 00000000..25462f0a --- /dev/null +++ b/has-uint8array-support/lib/uint8array.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Uint8Array === 'function' ) ? Uint8Array : null; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = main; diff --git a/has-uint8array-support/package.json b/has-uint8array-support/package.json new file mode 100644 index 00000000..31996c05 --- /dev/null +++ b/has-uint8array-support/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/has-uint8array-support", + "version": "0.0.0", + "description": "Detect native Uint8Array support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-uint8array-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "uint8array", + "es2015", + "es6", + "typed array", + "typed-array", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-uint8array-support/test/test.cli.js b/has-uint8array-support/test/test.cli.js new file mode 100644 index 00000000..64d62cb0 --- /dev/null +++ b/has-uint8array-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Uint8Array` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-uint8array-support/test/test.js b/has-uint8array-support/test/test.js new file mode 100644 index 00000000..8a16db67 --- /dev/null +++ b/has-uint8array-support/test/test.js @@ -0,0 +1,135 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasUint8Array = ( typeof Uint8Array === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Uint8Array` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasUint8Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './uint8array.js': Mock, + '@stdlib/assert/is-uint8array': isArray + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock() { + return [ + 1, + 3, + 253, + 0, + 1 + ]; + } +}); + +tape( 'if `Uint8Array` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasUint8Array ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './uint8array.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint8array.js': Mock1 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint8array.js': Mock2, + '@stdlib/assert/is-uint8array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint8array.js': Mock3, + '@stdlib/assert/is-uint8array': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint8array.js': Mock4 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock1() { + // Not a typed array: + return []; + } + + function Mock2() { + // Does not truncate... + return [ 1, 3.14, -3.14, 0, 1 ]; + } + + function Mock3() { + // Does not wrap around... + return [ 1, 3, 253, 256, 257 ]; + } + + function Mock4() { + throw new Error( 'beep' ); + } +}); diff --git a/has-uint8clampedarray-support/README.md b/has-uint8clampedarray-support/README.md new file mode 100644 index 00000000..a284a020 --- /dev/null +++ b/has-uint8clampedarray-support/README.md @@ -0,0 +1,117 @@ + + +# Uint8ClampedArray Support + +> Detect native [`Uint8ClampedArray`][mdn-uint8clampedarray] support. + +
+ +## Usage + + + +```javascript +var hasUint8ClampedArraySupport = require( '@stdlib/assert/has-uint8clampedarray-support' ); +``` + +#### hasUint8ClampedArraySupport() + +Detects if a runtime environment supports [`Uint8ClampedArray`][mdn-uint8clampedarray]. + +```javascript +var bool = hasUint8ClampedArraySupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var hasUint8ClampedArraySupport = require( '@stdlib/assert/has-uint8clampedarray-support' ); + +var bool = hasUint8ClampedArraySupport(); +if ( bool ) { + console.log( 'Environment has Uint8ClampedArray support.' ); +} else { + console.log( 'Environment lacks Uint8ClampedArray support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-uint8clampedarray-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-uint8clampedarray-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-uint8clampedarray-support/benchmark/benchmark.js b/has-uint8clampedarray-support/benchmark/benchmark.js new file mode 100644 index 00000000..9fe8945d --- /dev/null +++ b/has-uint8clampedarray-support/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasUint8ClampedArraySupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasUint8ClampedArraySupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/has-uint8clampedarray-support/bin/cli b/has-uint8clampedarray-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-uint8clampedarray-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-uint8clampedarray-support/docs/repl.txt b/has-uint8clampedarray-support/docs/repl.txt new file mode 100644 index 00000000..7906fa50 --- /dev/null +++ b/has-uint8clampedarray-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `Uint8ClampedArray` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `Uint8ClampedArray` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-uint8clampedarray-support/docs/types/index.d.ts b/has-uint8clampedarray-support/docs/types/index.d.ts new file mode 100644 index 00000000..6d48aa04 --- /dev/null +++ b/has-uint8clampedarray-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `Uint8ClampedArray` support. +* +* @returns boolean indicating if an environment has `Uint8ClampedArray` support +* +* @example +* var bool = hasUint8ClampedArraySupport(); +* // returns +*/ +declare function hasUint8ClampedArraySupport(): boolean; + + +// EXPORTS // + +export = hasUint8ClampedArraySupport; diff --git a/has-uint8clampedarray-support/docs/types/test.ts b/has-uint8clampedarray-support/docs/types/test.ts new file mode 100644 index 00000000..ec1e3bab --- /dev/null +++ b/has-uint8clampedarray-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasUint8ClampedArraySupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasUint8ClampedArraySupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasUint8ClampedArraySupport( true ); // $ExpectError + hasUint8ClampedArraySupport( [], 123 ); // $ExpectError +} diff --git a/has-uint8clampedarray-support/docs/usage.txt b/has-uint8clampedarray-support/docs/usage.txt new file mode 100644 index 00000000..8f3816a7 --- /dev/null +++ b/has-uint8clampedarray-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-uint8clampedarray-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-uint8clampedarray-support/etc/cli_opts.json b/has-uint8clampedarray-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-uint8clampedarray-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-uint8clampedarray-support/examples/index.js b/has-uint8clampedarray-support/examples/index.js new file mode 100644 index 00000000..1fa9e916 --- /dev/null +++ b/has-uint8clampedarray-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasUint8ClampedArraySupport = require( './../lib' ); + +var bool = hasUint8ClampedArraySupport(); +if ( bool ) { + console.log( 'Environment has Uint8ClampedArray support.' ); +} else { + console.log( 'Environment lacks Uint8ClampedArray support.' ); +} diff --git a/has-uint8clampedarray-support/lib/index.js b/has-uint8clampedarray-support/lib/index.js new file mode 100644 index 00000000..8410d00d --- /dev/null +++ b/has-uint8clampedarray-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `Uint8ClampedArray` support. +* +* @module @stdlib/assert/has-uint8clampedarray-support +* +* @example +* var hasUint8ClampedArraySupport = require( '@stdlib/assert/has-uint8clampedarray-support' ); +* +* var bool = hasUint8ClampedArraySupport(); +* // returns +*/ + +// MODULES // + +var hasUint8ClampedArraySupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasUint8ClampedArraySupport; diff --git a/has-uint8clampedarray-support/lib/main.js b/has-uint8clampedarray-support/lib/main.js new file mode 100644 index 00000000..2c4425d9 --- /dev/null +++ b/has-uint8clampedarray-support/lib/main.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isUint8ClampedArray = require( '@stdlib/assert/is-uint8clampedarray' ); +var GlobalUint8ClampedArray = require( './uint8clampedarray.js' ); + + +// MAIN // + +/** +* Tests for native `Uint8ClampedArray` support. +* +* @returns {boolean} boolean indicating if an environment has `Uint8ClampedArray` support +* +* @example +* var bool = hasUint8ClampedArraySupport(); +* // returns +*/ +function hasUint8ClampedArraySupport() { // eslint-disable-line id-length + var bool; + var arr; + + if ( typeof GlobalUint8ClampedArray !== 'function' ) { + return false; + } + // Test basic support... + try { + arr = new GlobalUint8ClampedArray( [ -1, 0, 1, 3.14, 4.99, 255, 256 ] ); + bool = ( + isUint8ClampedArray( arr ) && + arr[ 0 ] === 0 && // clamped + arr[ 1 ] === 0 && + arr[ 2 ] === 1 && + arr[ 3 ] === 3 && // round to nearest + arr[ 4 ] === 5 && // round to nearest + arr[ 5 ] === 255 && + arr[ 6 ] === 255 // clamped + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasUint8ClampedArraySupport; diff --git a/has-uint8clampedarray-support/lib/uint8clampedarray.js b/has-uint8clampedarray-support/lib/uint8clampedarray.js new file mode 100644 index 00000000..cb895ff5 --- /dev/null +++ b/has-uint8clampedarray-support/lib/uint8clampedarray.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof Uint8ClampedArray === 'function' ) ? Uint8ClampedArray : null; // eslint-disable-line stdlib/require-globals + + +// EXPORTS // + +module.exports = main; diff --git a/has-uint8clampedarray-support/package.json b/has-uint8clampedarray-support/package.json new file mode 100644 index 00000000..c3f575bb --- /dev/null +++ b/has-uint8clampedarray-support/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/has-uint8clampedarray-support", + "version": "0.0.0", + "description": "Detect native Uint8ClampedArray support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-uint8clampedarray-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "uint8clampedarray", + "es2015", + "es6", + "typed array", + "typed-array", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-uint8clampedarray-support/test/test.cli.js b/has-uint8clampedarray-support/test/test.cli.js new file mode 100644 index 00000000..a998c44c --- /dev/null +++ b/has-uint8clampedarray-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `Uint8ClampedArray` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-uint8clampedarray-support/test/test.js b/has-uint8clampedarray-support/test/test.js new file mode 100644 index 00000000..388acbb0 --- /dev/null +++ b/has-uint8clampedarray-support/test/test.js @@ -0,0 +1,185 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var round = require( '@stdlib/math/base/special/round' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasUint8ClampedArray = ( typeof Uint8ClampedArray === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `Uint8ClampedArray` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasUint8ClampedArray ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './uint8clampedarray.js': Mock, + '@stdlib/assert/is-uint8clampedarray': isArray + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock( arr ) { + var out = []; + var v; + var i; + + for ( i = 0; i < arr.length; i++ ) { + v = arr[ i ]; + if ( v < 0 ) { + v = 0; + } else if ( v > 255 ) { + v = 255; + } + out.push( round( v ) ); + } + return out; + } +}); + +tape( 'if `Uint8ClampedArray` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasUint8ClampedArray ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './uint8clampedarray.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint8clampedarray.js': Mock1 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint8clampedarray.js': Mock2, + '@stdlib/assert/is-uint8clampedarray': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint8clampedarray.js': Mock3, + '@stdlib/assert/is-uint8clampedarray': isArray + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint8clampedarray.js': Mock4 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + mocked = proxyquire( './../lib/main.js', { + './uint8clampedarray.js': Mock5 + }); + t.strictEqual( mocked(), false, 'detection result is `false`' ); + + t.end(); + + function isArray() { + return true; + } + + function Mock1() { + // Not a typed array: + return []; + } + + function Mock2( arr ) { + var out = []; + var v; + var i; + + // Does not clamp... + for ( i = 0; i < arr.length; i++ ) { + v = arr[ i ]; + out.push( round( v ) ); + } + return out; + } + + function Mock3( arr ) { + var out = []; + var v; + var i; + + // Does not clamp... + for ( i = 0; i < arr.length; i++ ) { + v = arr[ i ]; + if ( v < 0 ) { + v = 0; + } + out.push( round( v ) ); + } + return out; + } + + function Mock4() { + throw new Error( 'beep' ); + } + + function Mock5( arr ) { + var out = []; + var v; + var i; + + // Does not round to nearest... + for ( i = 0; i < arr.length; i++ ) { + v = arr[ i ]; + if ( v < 0 ) { + v = 0; + } else if ( v > 255 ) { + v = 255; + } + out.push( v ); + } + return out; + } +}); diff --git a/has-utf16-surrogate-pair-at/README.md b/has-utf16-surrogate-pair-at/README.md new file mode 100644 index 00000000..c12ad8dc --- /dev/null +++ b/has-utf16-surrogate-pair-at/README.md @@ -0,0 +1,136 @@ + + +# hasUTF16SurrogatePairAt + +> Test if a position in a string marks the start of a [UTF-16][utf-16] surrogate pair. + +
+ +## Usage + +```javascript +var hasUTF16SurrogatePairAt = require( '@stdlib/assert/has-utf16-surrogate-pair-at' ); +``` + +#### hasUTF16SurrogatePairAt( string, position ) + +Tests if a `position` (in [UTF-16][utf-16] code units) in a `string` marks the start of a [UTF-16][utf-16] surrogate pair. + +```javascript +var bool = hasUTF16SurrogatePairAt( '🌷', 0 ); +// returns true + +bool = hasUTF16SurrogatePairAt( '🌷', 1 ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Note that `position` does **not** refer to a visual character position, but to an index in the ordered sequence of [UTF-16][utf-16] code units. + +
+ + + +
+ +## Examples + + + +```javascript +var hasUTF16SurrogatePairAt = require( '@stdlib/assert/has-utf16-surrogate-pair-at' ); + +var bool = hasUTF16SurrogatePairAt( '🌷', 0 ); +// returns true + +bool = hasUTF16SurrogatePairAt( '🌷', 1 ); +// returns false + +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-utf16-surrogate-pair-at [options] [] --pos= + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --pos index Position in string. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-utf16-surrogate-pair-at --pos=0 🌷 +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n '🌷' | has-utf16-surrogate-pair-at --pos=1 +false +``` + +
+ + + +
+ + + + + + diff --git a/has-utf16-surrogate-pair-at/benchmark/benchmark.js b/has-utf16-surrogate-pair-at/benchmark/benchmark.js new file mode 100644 index 00000000..78f61fa5 --- /dev/null +++ b/has-utf16-surrogate-pair-at/benchmark/benchmark.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var UNICODE_MAX = require( '@stdlib/constants/string/unicode-max' ); +var pkg = require( './../package.json' ).name; +var hasUTF16SurrogatePairAt = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var x; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = floor( randu() * UNICODE_MAX ); + bool = hasUTF16SurrogatePairAt( fromCodePoint( x ), 0 ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + b.pass( 'benchmark finished' ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/has-utf16-surrogate-pair-at/bin/cli b/has-utf16-surrogate-pair-at/bin/cli new file mode 100644 index 00000000..ee759019 --- /dev/null +++ b/has-utf16-surrogate-pair-at/bin/cli @@ -0,0 +1,88 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var hasUTF16SurrogatePairAt = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + var pos; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + pos = parseInt( flags.pos, 10 ); + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( hasUTF16SurrogatePairAt( args[ 0 ], pos ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + if ( error ) { + return cli.error( error ); + } + console.log( hasUTF16SurrogatePairAt( data.toString(), pos ) ); // eslint-disable-line no-console + } +} + +main(); diff --git a/has-utf16-surrogate-pair-at/docs/repl.txt b/has-utf16-surrogate-pair-at/docs/repl.txt new file mode 100644 index 00000000..06a3067a --- /dev/null +++ b/has-utf16-surrogate-pair-at/docs/repl.txt @@ -0,0 +1,27 @@ + +{{alias}}( str, pos ) + Tests if a position in a string marks the start of a UTF-16 surrogate pair. + + Parameters + ---------- + str: string + Input string. + + pos: integer + Position. + + Returns + ------- + bool: boolean + Boolean indicating whether surrogate pair exists at specified position. + + Examples + -------- + > var out = {{alias}}( '🌷', 0 ) + true + > out = {{alias}}( '🌷', 1 ) + false + + See Also + -------- + diff --git a/has-utf16-surrogate-pair-at/docs/types/index.d.ts b/has-utf16-surrogate-pair-at/docs/types/index.d.ts new file mode 100644 index 00000000..69e38da7 --- /dev/null +++ b/has-utf16-surrogate-pair-at/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a position in a string marks the start of a UTF-16 surrogate pair. +* +* @param str - input string +* @param pos - position +* @throws first argument must be a string +* @throws second argument must be a nonnegative integer +* @throws position must be a valid index in string +* @returns boolean indicating if a position in a string marks the start of a UTF-16 surrogate pair +* +* @example +* var bool = hasUTF16SurrogatePairAt( '🌷', 0 ); +* // returns true +* +* @example +* var bool = hasUTF16SurrogatePairAt( '🌷', 1 ); +* // returns false +*/ +declare function hasUTF16SurrogatePairAt( str: string, pos: number ): boolean; + + +// EXPORTS // + +export = hasUTF16SurrogatePairAt; diff --git a/has-utf16-surrogate-pair-at/docs/types/test.ts b/has-utf16-surrogate-pair-at/docs/types/test.ts new file mode 100644 index 00000000..6a93a295 --- /dev/null +++ b/has-utf16-surrogate-pair-at/docs/types/test.ts @@ -0,0 +1,45 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasUTF16SurrogatePairAt = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasUTF16SurrogatePairAt( '🌷', 0 ); // $ExpectType boolean + hasUTF16SurrogatePairAt( '🌷', 1 ); // $ExpectType boolean +} + +// The function does not compile if provided incorrect arguments... +{ + hasUTF16SurrogatePairAt( false, 3 ); // $ExpectError + hasUTF16SurrogatePairAt( {}, 3 ); // $ExpectError + hasUTF16SurrogatePairAt( ( x: number ): number => x, 3 ); // $ExpectError + + hasUTF16SurrogatePairAt( 'string', true ); // $ExpectError + hasUTF16SurrogatePairAt( 'string', false ); // $ExpectError + hasUTF16SurrogatePairAt( 'string', {} ); // $ExpectError + hasUTF16SurrogatePairAt( 'string', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + hasUTF16SurrogatePairAt(); // $ExpectError +} diff --git a/has-utf16-surrogate-pair-at/docs/usage.txt b/has-utf16-surrogate-pair-at/docs/usage.txt new file mode 100644 index 00000000..ae440549 --- /dev/null +++ b/has-utf16-surrogate-pair-at/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-utf16-surrogate-pair-at [options] [] --pos= + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --pos index Position in string. diff --git a/has-utf16-surrogate-pair-at/etc/cli_opts.json b/has-utf16-surrogate-pair-at/etc/cli_opts.json new file mode 100644 index 00000000..6afcde3e --- /dev/null +++ b/has-utf16-surrogate-pair-at/etc/cli_opts.json @@ -0,0 +1,18 @@ + +{ + "string": [ + "pos" + ], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-utf16-surrogate-pair-at/examples/index.js b/has-utf16-surrogate-pair-at/examples/index.js new file mode 100644 index 00000000..0507cd23 --- /dev/null +++ b/has-utf16-surrogate-pair-at/examples/index.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasUTF16SurrogatePairAt = require( './../lib' ); + +var bool = hasUTF16SurrogatePairAt( '🌷', 0 ); +console.log( bool ); +// => true + +bool = hasUTF16SurrogatePairAt( '🌷🠀', 2 ); +console.log( bool ); +// => true + +bool = hasUTF16SurrogatePairAt( '🌷', 1 ); +console.log( bool ); +// => false + +bool = hasUTF16SurrogatePairAt( '🌷🠀', 3 ); +console.log( bool ); +// => false diff --git a/has-utf16-surrogate-pair-at/lib/index.js b/has-utf16-surrogate-pair-at/lib/index.js new file mode 100644 index 00000000..bf578d92 --- /dev/null +++ b/has-utf16-surrogate-pair-at/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a position in a string marks the start of a UTF-16 surrogate pair. +* +* @module @stdlib/assert/has-utf16-surrogate-pair-at +* +* @example +* var hasUTF16SurrogatePairAt = require( '@stdlib/assert/has-utf16-surrogate-pair-at' ); +* +* var bool = hasUTF16SurrogatePairAt( '🌷', 0 ); +* // returns true +* +* bool = hasUTF16SurrogatePairAt( '🌷', 1 ); +* // returns false +*/ + +// MODULES // + +var hasUTF16SurrogatePairAt = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasUTF16SurrogatePairAt; diff --git a/has-utf16-surrogate-pair-at/lib/main.js b/has-utf16-surrogate-pair-at/lib/main.js new file mode 100644 index 00000000..2a9bac9a --- /dev/null +++ b/has-utf16-surrogate-pair-at/lib/main.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; + + +// VARIABLES // + +// Range for a high surrogate +var OxD800 = 0xD800|0; // 55296 +var OxDBFF = 0xDBFF|0; // 56319 + +// Range for a low surrogate +var OxDC00 = 0xDC00|0; // 56320 +var OxDFFF = 0xDFFF|0; // 57343 + + +// MAIN // + +/** +* Tests if a position in a string marks the start of a UTF-16 surrogate pair. +* +* @private +* @param {string} str - input string +* @param {NonNegativeInteger} pos - position in string +* @throws {TypeError} first argument must be a string primitive +* @throws {TypeError} second argument must be a nonnegative integer +* @throws {RangeError} position must be a valid index in string +* @returns {boolean} boolean indicating whether the string has a surrogate pair at a position +* +* @example +* var out = hasUTF16SurrogatePairAt( '🌷', 0 ); +* // returns true +* +* @example +* var out = hasUTF16SurrogatePairAt( '🌷', 1 ); +* // returns false +*/ +function hasUTF16SurrogatePairAt( str, pos ) { + var ch1; + var ch2; + if ( !isString( str ) ) { + throw new TypeError( 'invalid argument. Must provide a string. Value: `' + str + '`.' ); + } + if ( !isNonNegativeInteger( pos ) ) { + throw new TypeError( 'invalid argument. Must provide a valid position (nonnegative integer). Value: `' + pos + '`.' ); + } + if ( pos >= str.length ) { + throw new RangeError( 'invalid argument. Must provide a valid position (within string bounds). Value: `' + pos + '`.' ); + } + ch1 = str.charCodeAt( pos ); + ch2 = str.charCodeAt( pos + 1 ); + return ch1 >= OxD800 && ch1 <= OxDBFF && ch2 >= OxDC00 && ch2 <= OxDFFF; +} + + +// EXPORTS // + +module.exports = hasUTF16SurrogatePairAt; diff --git a/has-utf16-surrogate-pair-at/package.json b/has-utf16-surrogate-pair-at/package.json new file mode 100644 index 00000000..97915397 --- /dev/null +++ b/has-utf16-surrogate-pair-at/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/has-utf16-surrogate-pair-at", + "version": "0.0.0", + "description": "Test if a position in a string marks the start of a UTF-16 surrogate pair.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-utf16-surrogate-pair-at": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdstring", + "string", + "str", + "utilities", + "utility", + "utils", + "util", + "code", + "point", + "pt", + "unicode", + "utf-16", + "utf16", + "surrogate", + "astral" + ] +} diff --git a/has-utf16-surrogate-pair-at/test/fixtures/stdin_error.js.txt b/has-utf16-surrogate-pair-at/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..103efa25 --- /dev/null +++ b/has-utf16-surrogate-pair-at/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/has-utf16-surrogate-pair-at/test/test.cli.js b/has-utf16-surrogate-pair-at/test/test.cli.js new file mode 100644 index 00000000..e86a9852 --- /dev/null +++ b/has-utf16-surrogate-pair-at/test/test.cli.js @@ -0,0 +1,220 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a position in a string marks the start of a UTF-16 surrogate pair', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'--pos=0\'; process.argv[ 3 ] = \'🌷\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf \'🌷\'', + '|', + EXEC_PATH, + fpath, + '--pos=1' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'false\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/has-utf16-surrogate-pair-at/test/test.js b/has-utf16-surrogate-pair-at/test/test.js new file mode 100644 index 00000000..aef92800 --- /dev/null +++ b/has-utf16-surrogate-pair-at/test/test.js @@ -0,0 +1,143 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasUTF16SurrogatePairAt = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof hasUTF16SurrogatePairAt, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the arity of the function is 2', function test( t ) { + t.strictEqual( hasUTF16SurrogatePairAt.length, 2, 'has length 2' ); + t.end(); +}); + +tape( 'the function throws an error if the first argument is not a string primitive', function test( t ) { + var values; + var i; + + values = [ + -5, + 3.14, + -1.0, + NaN, + true, + false, + null, + void 0, + [ 'beep', 'boop' ], + [ 1, 2, 3 ], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + hasUTF16SurrogatePairAt( value, 0 ); + }; + } +}); + +tape( 'the function throws an error if the second argument is not a nonnegative integer', function test( t ) { + var values; + var i; + + values = [ + 'bar', + -5, + 3.14, + -1.0, + NaN, + null, + void 0, + [ 'beep', 'boop' ], + [ 1, 2, 3 ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + hasUTF16SurrogatePairAt( 'foo', value ); + }; + } +}); + +tape( 'the function throws an error if a provided position is not a valid index in the provided string', function test( t ) { + var values; + var i; + + values = [ + [ 'bar', 3 ], + [ 'string', 7 ], + [ '', 0 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + hasUTF16SurrogatePairAt( value[ 0 ], value[ 1 ] ); + }; + } +}); + +tape( 'the function returns true if the specified position marks the start of a surrogate pair', function test( t ) { + var out; + + out = hasUTF16SurrogatePairAt( '🌷🠀', 2 ); + t.strictEqual( out, true, 'returns expected value' ); + + out = hasUTF16SurrogatePairAt( '🌷', 0 ); + t.strictEqual( out, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns false if the specified position does not mark the start of a surrogate pair', function test( t ) { + var out; + + out = hasUTF16SurrogatePairAt( '🌷🠀', 3 ); + t.strictEqual( out, false, 'returns expected value' ); + + out = hasUTF16SurrogatePairAt( '🌷', 1 ); + t.strictEqual( out, false, 'returns expected value' ); + + t.end(); +}); diff --git a/has-wasm-support/README.md b/has-wasm-support/README.md new file mode 100644 index 00000000..734e3446 --- /dev/null +++ b/has-wasm-support/README.md @@ -0,0 +1,113 @@ + + +# WebAssembly Support + +> Detect native [WebAssembly][webassembly] support. + +
+ +## Usage + +```javascript +var hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' ); +``` + +#### hasWebAssemblySupport() + +Detects if a runtime environment supports [WebAssembly][webassembly]. + +```javascript +var bool = hasWebAssemblySupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' ); + +var bool = hasWebAssemblySupport(); +if ( bool ) { + console.log( 'Environment has WebAssembly support.' ); +} else { + console.log( 'Environment lacks WebAssembly support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-wasm-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-wasm-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-wasm-support/benchmark/benchmark.js b/has-wasm-support/benchmark/benchmark.js new file mode 100644 index 00000000..8ae77d20 --- /dev/null +++ b/has-wasm-support/benchmark/benchmark.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasWebAssemblySupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasWebAssemblySupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/has-wasm-support/bin/cli b/has-wasm-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-wasm-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-wasm-support/docs/repl.txt b/has-wasm-support/docs/repl.txt new file mode 100644 index 00000000..ac1e9017 --- /dev/null +++ b/has-wasm-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native WebAssembly support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has native WebAssembly support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-wasm-support/docs/types/index.d.ts b/has-wasm-support/docs/types/index.d.ts new file mode 100644 index 00000000..2352fdfc --- /dev/null +++ b/has-wasm-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native WebAssembly support. +* +* @returns boolean indicating if an environment has native WebAssembly support +* +* @example +* var bool = hasWebAssemblySupport(); +* // returns +*/ +declare function hasWebAssemblySupport(): boolean; + + +// EXPORTS // + +export = hasWebAssemblySupport; diff --git a/has-wasm-support/docs/types/test.ts b/has-wasm-support/docs/types/test.ts new file mode 100644 index 00000000..be2fa500 --- /dev/null +++ b/has-wasm-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasWebAssemblySupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasWebAssemblySupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasWebAssemblySupport( true ); // $ExpectError + hasWebAssemblySupport( [], 123 ); // $ExpectError +} diff --git a/has-wasm-support/docs/usage.txt b/has-wasm-support/docs/usage.txt new file mode 100644 index 00000000..44cd9295 --- /dev/null +++ b/has-wasm-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-wasm-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-wasm-support/etc/cli_opts.json b/has-wasm-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-wasm-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-wasm-support/examples/index.js b/has-wasm-support/examples/index.js new file mode 100644 index 00000000..c42c05ce --- /dev/null +++ b/has-wasm-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasWebAssemblySupport = require( './../lib' ); + +var bool = hasWebAssemblySupport(); +if ( bool ) { + console.log( 'Environment has WebAssembly support.' ); +} else { + console.log( 'Environment lacks WebAssembly support.' ); +} diff --git a/has-wasm-support/lib/index.js b/has-wasm-support/lib/index.js new file mode 100644 index 00000000..ab2ff56d --- /dev/null +++ b/has-wasm-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native WebAssembly support. +* +* @module @stdlib/assert/has-wasm-support +* +* @example +* var hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' ); +* +* var bool = hasWebAssemblySupport(); +* // returns +*/ + +// MODULES // + +var hasWebAssemblySupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasWebAssemblySupport; diff --git a/has-wasm-support/lib/main.js b/has-wasm-support/lib/main.js new file mode 100644 index 00000000..2947aba5 --- /dev/null +++ b/has-wasm-support/lib/main.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests for native WebAssembly support. +* +* @returns {boolean} boolean indicating if an environment has native WebAssembly support +* +* @example +* var bool = hasWebAssemblySupport(); +* // returns +*/ +function hasWebAssemblySupport() { + return ( typeof WebAssembly === 'object' ); +} + + +// EXPORTS // + +module.exports = hasWebAssemblySupport; diff --git a/has-wasm-support/package.json b/has-wasm-support/package.json new file mode 100644 index 00000000..6cba1679 --- /dev/null +++ b/has-wasm-support/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/has-wasm-support", + "version": "0.0.0", + "description": "Detect native WebAssembly support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-wasm-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "wasm", + "asm", + "asm.js", + "webassembly", + "web", + "assembly", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-wasm-support/test/test.cli.js b/has-wasm-support/test/test.cli.js new file mode 100644 index 00000000..09706fce --- /dev/null +++ b/has-wasm-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides WebAssembly support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-wasm-support/test/test.js b/has-wasm-support/test/test.js new file mode 100644 index 00000000..5f714882 --- /dev/null +++ b/has-wasm-support/test/test.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasWebAssembly = ( typeof WebAssembly === 'object' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if WebAssembly is supported, detection result is `true`', function test( t ) { + if ( hasWebAssembly ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + t.end(); +}); diff --git a/has-weakmap-support/README.md b/has-weakmap-support/README.md new file mode 100644 index 00000000..5212ae85 --- /dev/null +++ b/has-weakmap-support/README.md @@ -0,0 +1,113 @@ + + +# WeakMap Support + +> Detect native [`WeakMap`][mdn-weakmap] support. + +
+ +## Usage + +```javascript +var hasWeakMapSupport = require( '@stdlib/assert/has-weakmap-support' ); +``` + +#### hasWeakMapSupport() + +Detects if a runtime environment supports ES2015 [`WeakMap`][mdn-weakmap]. + +```javascript +var bool = hasWeakMapSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasWeakMapSupport = require( '@stdlib/assert/has-weakmap-support' ); + +var bool = hasWeakMapSupport(); +if ( bool ) { + console.log( 'Environment has WeakMap support.' ); +} else { + console.log( 'Environment lacks WeakMap support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-weakmap-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-weakmap-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-weakmap-support/benchmark/benchmark.js b/has-weakmap-support/benchmark/benchmark.js new file mode 100644 index 00000000..5d6c736e --- /dev/null +++ b/has-weakmap-support/benchmark/benchmark.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasWeakMapSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasWeakMapSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/has-weakmap-support/bin/cli b/has-weakmap-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-weakmap-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-weakmap-support/docs/repl.txt b/has-weakmap-support/docs/repl.txt new file mode 100644 index 00000000..85d4de8f --- /dev/null +++ b/has-weakmap-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `WeakMap` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `WeakMap` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-weakmap-support/docs/types/index.d.ts b/has-weakmap-support/docs/types/index.d.ts new file mode 100644 index 00000000..64351cea --- /dev/null +++ b/has-weakmap-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `WeakMap` support. +* +* @returns boolean indicating if an environment has `WeakMap` support +* +* @example +* var bool = hasWeakMapSupport(); +* // returns +*/ +declare function hasWeakMapSupport(): boolean; + + +// EXPORTS // + +export = hasWeakMapSupport; diff --git a/has-weakmap-support/docs/types/test.ts b/has-weakmap-support/docs/types/test.ts new file mode 100644 index 00000000..1561c537 --- /dev/null +++ b/has-weakmap-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasWeakMapSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasWeakMapSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasWeakMapSupport( true ); // $ExpectError + hasWeakMapSupport( [], 123 ); // $ExpectError +} diff --git a/has-weakmap-support/docs/usage.txt b/has-weakmap-support/docs/usage.txt new file mode 100644 index 00000000..2cdcd824 --- /dev/null +++ b/has-weakmap-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-weakmap-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-weakmap-support/etc/cli_opts.json b/has-weakmap-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-weakmap-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-weakmap-support/examples/index.js b/has-weakmap-support/examples/index.js new file mode 100644 index 00000000..e7ecd923 --- /dev/null +++ b/has-weakmap-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasWeakMapSupport = require( './../lib' ); + +var bool = hasWeakMapSupport(); +if ( bool ) { + console.log( 'Environment has WeakMap support.' ); +} else { + console.log( 'Environment lacks WeakMap support.' ); +} diff --git a/has-weakmap-support/lib/index.js b/has-weakmap-support/lib/index.js new file mode 100644 index 00000000..a1acf6a7 --- /dev/null +++ b/has-weakmap-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `WeakMap` support. +* +* @module @stdlib/assert/has-weakmap-support +* +* @example +* var hasWeakMapSupport = require( '@stdlib/assert/has-weakmap-support' ); +* +* var bool = hasWeakMapSupport(); +* // returns +*/ + +// MODULES // + +var hasWeakMapSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasWeakMapSupport; diff --git a/has-weakmap-support/lib/main.js b/has-weakmap-support/lib/main.js new file mode 100644 index 00000000..395c13ac --- /dev/null +++ b/has-weakmap-support/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var GlobalWeakMap = require( './weakmap.js' ); + + +// MAIN // + +/** +* Tests for native `WeakMap` support. +* +* @returns {boolean} boolean indicating if an environment has `WeakMap` support +* +* @example +* var bool = hasWeakMapSupport(); +* // returns +*/ +function hasWeakMapSupport() { + var bool; + var key; + var wm; + + if ( typeof GlobalWeakMap !== 'function' ) { + return false; + } + // Test basic support... + try { + key = {}; + wm = new GlobalWeakMap(); + wm.set( key, 'beep' ); + bool = ( + wm.has( key ) && + wm.get( key ) === 'beep' + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasWeakMapSupport; diff --git a/has-weakmap-support/lib/weakmap.js b/has-weakmap-support/lib/weakmap.js new file mode 100644 index 00000000..6fe3a25e --- /dev/null +++ b/has-weakmap-support/lib/weakmap.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof WeakMap === 'function' ) ? WeakMap : null; + + +// EXPORTS // + +module.exports = main; diff --git a/has-weakmap-support/package.json b/has-weakmap-support/package.json new file mode 100644 index 00000000..1e493c62 --- /dev/null +++ b/has-weakmap-support/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/has-weakmap-support", + "version": "0.0.0", + "description": "Detect native WeakMap support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-weakmap-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "weak", + "map", + "weakmap", + "es2015", + "es6", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-weakmap-support/test/test.cli.js b/has-weakmap-support/test/test.cli.js new file mode 100644 index 00000000..403ea9bc --- /dev/null +++ b/has-weakmap-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `WeakMap` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-weakmap-support/test/test.js b/has-weakmap-support/test/test.js new file mode 100644 index 00000000..5bf418be --- /dev/null +++ b/has-weakmap-support/test/test.js @@ -0,0 +1,150 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasWeakMap = ( typeof WeakMap === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `WeakMap` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasWeakMap ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './weakmap.js': Mock + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function Mock() { + var o = {}; + function has( key ) { + return hasOwnProp( o, key ); + } + function set( key, value ) { + o[ key ] = value; + } + function get( key ) { + return o[ key ]; + } + return { + 'has': has, + 'set': set, + 'get': get + }; + } +}); + +tape( 'if `WeakMap` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasWeakMap ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './weakmap.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false` (mocked)' ); + + mocked = proxyquire( './../lib/main.js', { + './weakmap.js': Mock1 + }); + t.strictEqual( mocked(), false, 'detection result is `false` (no `has` method)' ); + + mocked = proxyquire( './../lib/main.js', { + './weakmap.js': Mock2 + }); + t.strictEqual( mocked(), false, 'detection result is `false` (no `set` method)' ); + + mocked = proxyquire( './../lib/main.js', { + './weakmap.js': Mock3 + }); + t.strictEqual( mocked(), false, 'detection result is `false` (no `get` method)' ); + + t.end(); + + function Mock1() { + var o = {}; + function set( key, value ) { + o[ key ] = value; + } + function get( key ) { + return o[ key ]; + } + return { + 'set': set, + 'get': get + }; + } + + function Mock2() { + var o = {}; + function has( key ) { + return hasOwnProp( o, key ); + } + function get( key ) { + return o[ key ]; + } + return { + 'has': has, + 'get': get + }; + } + + function Mock3() { + var o = {}; + function has( key ) { + return hasOwnProp( o, key ); + } + function set( key, value ) { + o[ key ] = value; + } + return { + 'has': has, + 'set': set + }; + } +}); diff --git a/has-weakset-support/README.md b/has-weakset-support/README.md new file mode 100644 index 00000000..396ac4ea --- /dev/null +++ b/has-weakset-support/README.md @@ -0,0 +1,113 @@ + + +# WeakSet Support + +> Detect native [`WeakSet`][mdn-weakset] support. + +
+ +## Usage + +```javascript +var hasWeakSetSupport = require( '@stdlib/assert/has-weakset-support' ); +``` + +#### hasWeakSetSupport() + +Detects if a runtime environment supports ES2015 [`WeakSet`][mdn-weakset]. + +```javascript +var bool = hasWeakSetSupport(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasWeakSetSupport = require( '@stdlib/assert/has-weakset-support' ); + +var bool = hasWeakSetSupport(); +if ( bool ) { + console.log( 'Environment has WeakSet support.' ); +} else { + console.log( 'Environment lacks WeakSet support.' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: has-weakset-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ has-weakset-support + +``` + +
+ + + +
+ + + + + + diff --git a/has-weakset-support/benchmark/benchmark.js b/has-weakset-support/benchmark/benchmark.js new file mode 100644 index 00000000..32a910a3 --- /dev/null +++ b/has-weakset-support/benchmark/benchmark.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var hasWeakSetSupport = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: the following *could* be optimized away via loop-invariant code motion. If so, the entire loop will disappear. + bool = hasWeakSetSupport(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/has-weakset-support/bin/cli b/has-weakset-support/bin/cli new file mode 100644 index 00000000..45ea86d1 --- /dev/null +++ b/has-weakset-support/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var detect = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( detect() ); // eslint-disable-line no-console +} + +main(); diff --git a/has-weakset-support/docs/repl.txt b/has-weakset-support/docs/repl.txt new file mode 100644 index 00000000..4de0a280 --- /dev/null +++ b/has-weakset-support/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Tests for native `WeakSet` support. + + Returns + ------- + bool: boolean + Boolean indicating if an environment has `WeakSet` support. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/has-weakset-support/docs/types/index.d.ts b/has-weakset-support/docs/types/index.d.ts new file mode 100644 index 00000000..e1852bc6 --- /dev/null +++ b/has-weakset-support/docs/types/index.d.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests for native `WeakSet` support. +* +* @returns boolean indicating if an environment has `WeakSet` support +* +* @example +* var bool = hasWeakSetSupport(); +* // returns +*/ +declare function hasWeakSetSupport(): boolean; + + +// EXPORTS // + +export = hasWeakSetSupport; diff --git a/has-weakset-support/docs/types/test.ts b/has-weakset-support/docs/types/test.ts new file mode 100644 index 00000000..45f60870 --- /dev/null +++ b/has-weakset-support/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hasWeakSetSupport = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + hasWeakSetSupport(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + hasWeakSetSupport( true ); // $ExpectError + hasWeakSetSupport( [], 123 ); // $ExpectError +} diff --git a/has-weakset-support/docs/usage.txt b/has-weakset-support/docs/usage.txt new file mode 100644 index 00000000..9d143dd1 --- /dev/null +++ b/has-weakset-support/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: has-weakset-support [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/has-weakset-support/etc/cli_opts.json b/has-weakset-support/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/has-weakset-support/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/has-weakset-support/examples/index.js b/has-weakset-support/examples/index.js new file mode 100644 index 00000000..5690163c --- /dev/null +++ b/has-weakset-support/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasWeakSetSupport = require( './../lib' ); + +var bool = hasWeakSetSupport(); +if ( bool ) { + console.log( 'Environment has WeakSet support.' ); +} else { + console.log( 'Environment lacks WeakSet support.' ); +} diff --git a/has-weakset-support/lib/index.js b/has-weakset-support/lib/index.js new file mode 100644 index 00000000..d2382543 --- /dev/null +++ b/has-weakset-support/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test for native `WeakSet` support. +* +* @module @stdlib/assert/has-weakset-support +* +* @example +* var hasWeakSetSupport = require( '@stdlib/assert/has-weakset-support' ); +* +* var bool = hasWeakSetSupport(); +* // returns +*/ + +// MODULES // + +var hasWeakSetSupport = require( './main.js' ); + + +// EXPORTS // + +module.exports = hasWeakSetSupport; diff --git a/has-weakset-support/lib/main.js b/has-weakset-support/lib/main.js new file mode 100644 index 00000000..1c1933a3 --- /dev/null +++ b/has-weakset-support/lib/main.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var GlobalWeakSet = require( './weakset.js' ); + + +// MAIN // + +/** +* Tests for native `WeakSet` support. +* +* @returns {boolean} boolean indicating if an environment has `WeakSet` support +* +* @example +* var bool = hasWeakSetSupport(); +* // returns +*/ +function hasWeakSetSupport() { + var bool; + var val; + var ws; + + if ( typeof GlobalWeakSet !== 'function' ) { + return false; + } + // Test basic support... + try { + val = {}; + ws = new GlobalWeakSet(); + ws.add( val ); + ws.add( val ); + bool = ws.has( val ); + } catch ( err ) { // eslint-disable-line no-unused-vars + bool = false; + } + return bool; +} + + +// EXPORTS // + +module.exports = hasWeakSetSupport; diff --git a/has-weakset-support/lib/weakset.js b/has-weakset-support/lib/weakset.js new file mode 100644 index 00000000..d16e3e7a --- /dev/null +++ b/has-weakset-support/lib/weakset.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +var main = ( typeof WeakSet === 'function' ) ? WeakSet : null; + + +// EXPORTS // + +module.exports = main; diff --git a/has-weakset-support/package.json b/has-weakset-support/package.json new file mode 100644 index 00000000..f224b98e --- /dev/null +++ b/has-weakset-support/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/has-weakset-support", + "version": "0.0.0", + "description": "Detect native WeakSet support.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "has-weakset-support": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "detect", + "feature", + "weak", + "set", + "weakset", + "es2015", + "es6", + "support", + "has", + "native", + "issupported", + "cli" + ] +} diff --git a/has-weakset-support/test/test.cli.js b/has-weakset-support/test/test.cli.js new file mode 100644 index 00000000..01a27bbb --- /dev/null +++ b/has-weakset-support/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment provides native `WeakSet` support', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/has-weakset-support/test/test.js b/has-weakset-support/test/test.js new file mode 100644 index 00000000..28398639 --- /dev/null +++ b/has-weakset-support/test/test.js @@ -0,0 +1,127 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var detect = require( './../lib' ); + + +// VARIABLES // + +var hasWeakSet = ( typeof WeakSet === 'function' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof detect, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'feature detection result is a boolean', function test( t ) { + t.strictEqual( typeof detect(), 'boolean', 'detection result is a boolean' ); + t.end(); +}); + +tape( 'if `WeakSet` is supported, detection result is `true`', function test( t ) { + var mocked; + if ( hasWeakSet ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './weakset.js': Mock + }); + t.strictEqual( mocked(), true, 'detection result is `true` (mocked)' ); + + t.end(); + + function Mock() { + var arr = []; + + function has( value ) { + return arr.indexOf( value ) !== -1; + } + function add( value ) { + if ( has( value ) ) { + return; + } + arr.push( value ); + } + + return { + 'has': has, + 'add': add + }; + } +}); + +tape( 'if `WeakSet` is not supported, detection result is `false`', function test( t ) { + var mocked; + if ( hasWeakSet ) { + t.strictEqual( detect(), true, 'detection result is `true`' ); + } else { + t.strictEqual( detect(), false, 'detection result is `false`' ); + } + mocked = proxyquire( './../lib/main.js', { + './weakset.js': {} + }); + t.strictEqual( mocked(), false, 'detection result is `false` (mocked)' ); + + mocked = proxyquire( './../lib/main.js', { + './weakset.js': Mock1 + }); + t.strictEqual( mocked(), false, 'detection result is `false` (no `has` method)' ); + + mocked = proxyquire( './../lib/main.js', { + './weakset.js': Mock2 + }); + t.strictEqual( mocked(), false, 'detection result is `false` (no `add` method)' ); + + t.end(); + + function Mock1() { + var arr = []; + + function add( value ) { + arr.push( value ); + } + + return { + 'add': add + }; + } + + function Mock2() { + var arr = []; + + function has( value ) { + return arr.indexOf( value ) !== -1; + } + + return { + 'has': has + }; + } +}); diff --git a/instance-of/README.md b/instance-of/README.md new file mode 100644 index 00000000..d9d6566a --- /dev/null +++ b/instance-of/README.md @@ -0,0 +1,145 @@ + + +# instanceOf + +> Test whether a value has in its prototype chain a specified constructor as a prototype property. + +
+ +
+ + + +
+ +## Usage + +```javascript +var instanceOf = require( '@stdlib/assert/instance-of' ); +``` + +#### instanceOf( value, constructor ) + +Tests whether a `value` has in its prototype chain a specified `constructor` as a `prototype` property. + +```javascript +var inherit = require( '@stdlib/utils/inherit' ); + +function Foo() { + return this; +} + +function Bar() { + return this; +} +inherit( Bar, Foo ); + +var bar = new Bar(); + +var bool = instanceOf( bar, Foo ); +// returns true +``` + +
+ + + +
+ +## Notes + +- The function throws a `TypeError` if provided a `constructor` argument which is not callable. + + ```javascript + var bool = instanceOf( {}, null ); + // throws + ``` + +- While the prototype of an `object` created using object literal notion is `undefined`, the function returns `true` when provided an `object` literal and the `Object` constructor. This maintains consistent behavior with the `instanceof` operator. + + ```javascript + var bool = ( {} instanceof Object ); + // returns true + + bool = instanceOf( {}, Object ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); + +var bool = instanceOf( [], Array ); +// returns true + +bool = instanceOf( [], Object ); +// returns true + +bool = instanceOf( {}, Object ); +// returns true + +bool = instanceOf( new Date(), Date ); +// returns true + +bool = instanceOf( /.*/, RegExp ); +// returns true + +bool = instanceOf( instanceOf, Function ); +// returns true + +bool = instanceOf( null, Object ); +// returns false + +bool = instanceOf( 5, Number ); +// returns false + +bool = instanceOf( '5', String ); +// returns false + +bool = instanceOf( void 0, Object ); +// returns false + +bool = instanceOf( {}, Array ); +// returns false + +bool = instanceOf( {}, Function ); +// returns false +``` + +
+ + + + + + diff --git a/instance-of/benchmark/benchmark.js b/instance-of/benchmark/benchmark.js new file mode 100644 index 00000000..41eb9d6e --- /dev/null +++ b/instance-of/benchmark/benchmark.js @@ -0,0 +1,89 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var instanceOf = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var vals; + var i; + + vals = [ + new Date(), + new RegExp( '.*' ), + new Array( 10 ), + {}, + '5', + 5, + null, + void 0 + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = instanceOf( vals[ i%vals.length ], Object ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::built-in', function benchmark( b ) { + var bool; + var vals; + var i; + + vals = [ + new Date(), + new RegExp( '.*' ), + new Array( 10 ), + {}, + '5', + 5, + null, + void 0 + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = ( vals[ i%vals.length ] instanceof Object ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/instance-of/docs/repl.txt b/instance-of/docs/repl.txt new file mode 100644 index 00000000..08a084cb --- /dev/null +++ b/instance-of/docs/repl.txt @@ -0,0 +1,35 @@ + +{{alias}}( value, constructor ) + Tests whether a value has in its prototype chain a specified constructor as + a prototype property. + + While the prototype of an `object` created using object literal notion is + `undefined`, the function returns `true` when provided an `object` literal + and the `Object` constructor. This maintains consistent behavior with the + `instanceof` operator. + + Parameters + ---------- + value: any + Input value. + + constructor: Function + Constructor. + + Returns + ------- + bool: boolean + Boolean indicating if a value is an instance of a provided constructor. + + Examples + -------- + > var bool = {{alias}}( [], Array ) + true + > bool = {{alias}}( {}, Object ) + true + > bool = {{alias}}( null, Object ) + false + + See Also + -------- + diff --git a/instance-of/docs/types/index.d.ts b/instance-of/docs/types/index.d.ts new file mode 100644 index 00000000..a1daf9bb --- /dev/null +++ b/instance-of/docs/types/index.d.ts @@ -0,0 +1,54 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests whether a value has in its prototype chain a specified constructor as a prototype property. +* +* @param value - value to test +* @param constructor - constructor to test against +* @throws constructor must be callable +* @returns boolean indicating whether a value is an instance of a provided constructor +* +* @example +* var bool = instanceOf( [], Array ); +* // returns true +* +* @example +* var bool = instanceOf( {}, Object ); // exception +* // returns true +* +* @example +* var bool = instanceOf( 'beep', String ); +* // returns false +* +* @example +* var bool = instanceOf( null, Object ); +* // returns false +* +* @example +* var bool = instanceOf( 5, Object ); +* // returns false +*/ +declare function instanceOf( value: any, constructor: Function ): boolean; + + +// EXPORTS // + +export = instanceOf; diff --git a/instance-of/docs/types/test.ts b/instance-of/docs/types/test.ts new file mode 100644 index 00000000..49e54b62 --- /dev/null +++ b/instance-of/docs/types/test.ts @@ -0,0 +1,45 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import instanceOf = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + instanceOf( [], Array ); // $ExpectType boolean + instanceOf( {}, Object ); // $ExpectType boolean +} + +// The function does not compile if provided a second argument that is not a function... +{ + instanceOf( [], [] ); // $ExpectError + instanceOf( [], {} ); // $ExpectError + instanceOf( [], 123 ); // $ExpectError + instanceOf( [], 'abc' ); // $ExpectError + instanceOf( [], null ); // $ExpectError + instanceOf( [], true ); // $ExpectError + instanceOf( [], false ); // $ExpectError +} + +// The function does not compile if provided insufficient arguments... +{ + instanceOf(); // $ExpectError + instanceOf( [] ); // $ExpectError +} diff --git a/instance-of/examples/index.js b/instance-of/examples/index.js new file mode 100644 index 00000000..e7751bf3 --- /dev/null +++ b/instance-of/examples/index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var instanceOf = require( './../lib' ); + +var bool = instanceOf( [], Array ); +console.log( bool ); +// => true + +bool = instanceOf( [], Object ); +console.log( bool ); +// => true + +bool = instanceOf( {}, Object ); +console.log( bool ); +// => true + +bool = instanceOf( new Date(), Date ); +console.log( bool ); +// => true + +bool = instanceOf( /.*/, RegExp ); +console.log( bool ); +// => true + +bool = instanceOf( instanceOf, Function ); +console.log( bool ); +// => true + +bool = instanceOf( null, Object ); +console.log( bool ); +// => false + +bool = instanceOf( 5, Number ); +console.log( bool ); +// => false + +bool = instanceOf( '5', String ); +console.log( bool ); +// => false + +bool = instanceOf( void 0, Object ); +console.log( bool ); +// => false + +bool = instanceOf( {}, Array ); +console.log( bool ); +// => false + +bool = instanceOf( {}, Function ); +console.log( bool ); +// => false diff --git a/instance-of/lib/index.js b/instance-of/lib/index.js new file mode 100644 index 00000000..34418df6 --- /dev/null +++ b/instance-of/lib/index.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether a value has in its prototype chain a specified constructor as a prototype property. +* +* @module @stdlib/assert/instance-of +* +* @example +* var instanceOf = require( '@stdlib/assert/instance-of' ); +* +* var bool = instanceOf( [], Array ); +* // returns true +* +* bool = instanceOf( {}, Object ); // exception +* // returns true +* +* bool = instanceOf( 'beep', String ); +* // returns false +* +* bool = instanceOf( null, Object ); +* // returns false +* +* bool = instanceOf( 5, Object ); +* // returns false +*/ + +// MODULES // + +var instanceOf = require( './main.js' ); + + +// EXPORTS // + +module.exports = instanceOf; diff --git a/instance-of/lib/main.js b/instance-of/lib/main.js new file mode 100644 index 00000000..55ab5217 --- /dev/null +++ b/instance-of/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests whether a value has in its prototype chain a specified constructor as a prototype property. +* +* @param {*} value - value to test +* @param {Function} constructor - constructor to test against +* @throws {TypeError} constructor must be callable +* @returns {boolean} boolean indicating whether a value is an instance of a provided constructor +* +* @example +* var bool = instanceOf( [], Array ); +* // returns true +* +* @example +* var bool = instanceOf( {}, Object ); // exception +* // returns true +* +* @example +* var bool = instanceOf( 'beep', String ); +* // returns false +* +* @example +* var bool = instanceOf( null, Object ); +* // returns false +* +* @example +* var bool = instanceOf( 5, Object ); +* // returns false +*/ +function instanceOf( value, constructor ) { + // TODO: replace with `isCallable` check + if ( typeof constructor !== 'function' ) { + throw new TypeError( 'invalid argument. `constructor` argument must be callable. Value: `'+constructor+'`.' ); + } + return ( value instanceof constructor ); +} + + +// EXPORTS // + +module.exports = instanceOf; diff --git a/instance-of/package.json b/instance-of/package.json new file mode 100644 index 00000000..d2ab154f --- /dev/null +++ b/instance-of/package.json @@ -0,0 +1,79 @@ +{ + "name": "@stdlib/assert/instance-of", + "version": "0.0.0", + "description": "Test whether a value has in its prototype chain a specified constructor as a prototype property.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "prototype", + "proto", + "inherit", + "inherits", + "class", + "oop", + "object-oriented", + "inheritance", + "instanceof", + "instance", + "super", + "parent", + "test", + "check", + "is", + "validate", + "valid", + "verify" + ] +} diff --git a/instance-of/test/test.js b/instance-of/test/test.js new file mode 100644 index 00000000..b722fd9a --- /dev/null +++ b/instance-of/test/test.js @@ -0,0 +1,174 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var inherit = require( '@stdlib/utils/inherit' ); +var Number = require( '@stdlib/number/ctor' ); +var instanceOf = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof instanceOf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a constructor argument which is not callable', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined, + [], + {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + instanceOf( {}, value ); + }; + } +}); + +tape( 'the function returns `true` if provided a value which is an instance of a provided constructor', function test( t ) { + var bool; + + bool = instanceOf( [], Array ); + t.strictEqual( bool, true, 'returns expected value' ); + + bool = instanceOf( [], Object ); + t.strictEqual( bool, true, 'returns expected value' ); + + bool = instanceOf( new Object(), Object ); // eslint-disable-line no-new-object + t.strictEqual( bool, true, 'returns expected value' ); + + bool = instanceOf( instanceOf, Function ); + t.strictEqual( bool, true, 'returns expected value' ); + + bool = instanceOf( new Date(), Date ); + t.strictEqual( bool, true, 'returns expected value' ); + + bool = instanceOf( /.*/, RegExp ); + t.strictEqual( bool, true, 'returns expected value' ); + + bool = instanceOf( new RegExp( '.*' ), RegExp ); + t.strictEqual( bool, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `true` when provided an object literal and the `Object` constructor (exception)', function test( t ) { + var bool = instanceOf( {}, Object ); + t.strictEqual( bool, true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `true` if a provided a value which is an instance of a provided constructor (inheritance)', function test( t ) { + var bool; + var bar; + + function Foo() { + return this; + } + + function Bar() { + return this; + } + inherit( Bar, Foo ); + + bar = new Bar(); + bool = instanceOf( bar, Foo ); + + t.strictEqual( bool, true, 'returns true' ); + t.strictEqual( bar instanceof Bar, true, 'is instance of Bar' ); + t.strictEqual( bar instanceof Foo, true, 'is instance of Foo' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a test value which is not an instance of a provided constructor (primitives)', function test( t ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined + ]; + for ( i = 0; i < values.length; i++ ) { + bool = instanceOf( values[i], Object ); + t.strictEqual( bool, false, 'returns false when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if a provided value which is not an instance of a provided constructor (inheritance)', function test( t ) { + var bool; + var bar; + + function Foo() { + return this; + } + + function Bar() { + return this; + } + + bar = new Bar(); + bool = instanceOf( bar, Foo ); + + t.strictEqual( bool, false, 'returns false' ); + t.strictEqual( bar instanceof Bar, true, 'is instance of Bar' ); + t.strictEqual( bar instanceof Foo, false, 'is not instance of Foo' ); + t.end(); +}); + +tape( 'the function returns `false` if provided primitives and their corresponding object constructors', function test( t ) { + var bool; + + bool = instanceOf( true, Boolean ); + t.strictEqual( bool, false, 'returns expected value' ); + + bool = instanceOf( 'beep', String ); + t.strictEqual( bool, false, 'returns expected value' ); + + bool = instanceOf( 5, Number ); + t.strictEqual( bool, false, 'returns expected value' ); + + t.end(); +}); diff --git a/is-absolute-path/README.md b/is-absolute-path/README.md new file mode 100644 index 00000000..fc0e56ae --- /dev/null +++ b/is-absolute-path/README.md @@ -0,0 +1,210 @@ + + +# isAbsolutePath + +> Test if a value is an absolute path. + +
+ +
+ + + +
+ +## Usage + +```javascript +var isAbsolutePath = require( '@stdlib/assert/is-absolute-path' ); +``` + +#### isAbsolutePath( value ) + +Tests if a `value` is an absolute path. + +```javascript +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); + +var bool; +if ( IS_WINDOWS ) { + bool = isAbsolutePath( 'C:\\foo\\bar\\baz' ); + // returns true +} else { + bool = isAbsolutePath( '/foo/bar/baz' ); + // returns true +} +``` + +#### isAbsolutePath.posix( value ) + +Tests if a `value` is a POSIX absolute path. + +```javascript +var bool = isAbsolutePath.posix( '/foo/bar/baz' ); +// returns true + +bool = isAbsolutePath.posix( 'foo/bar/baz' ); +// returns false +``` + +#### isAbsolutePath.win32( value ) + +Tests if a `value` is a Windows absolute path. + +```javascript +var bool = isAbsolutePath.win32( 'C:\\foo\\bar\\baz' ); +// returns true + +bool = isAbsolutePath.win32( 'foo\\bar\\baz' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- `isAbsolutePath()` is platform-specific. On Windows platforms, the function is equal to `isAbsolutePath.win32()`. On POSIX platforms, the function is equal to `isAbsolutePath.posix()`. + +
+ + + +
+ +## Examples + + + +```javascript +var isAbsolutePath = require( '@stdlib/assert/is-absolute-path' ); + +var bool = isAbsolutePath.posix( '/foo/bar/baz' ); +// returns true + +bool = isAbsolutePath.posix( '/foo/..' ); +// returns true + +bool = isAbsolutePath.posix( 'foo/' ); +// returns false + +bool = isAbsolutePath.posix( 'foo' ); +// returns false + +bool = isAbsolutePath.posix( '.' ); +// returns false + +bool = isAbsolutePath.posix( '' ); +// returns false + +bool = isAbsolutePath.win32( 'C:\\foo\\bar\\baz' ); +// returns true + +bool = isAbsolutePath.win32( '//server' ); +// returns true + +bool = isAbsolutePath.win32( '\\\\server' ); +// returns true + +bool = isAbsolutePath.win32( 'C:/foo/bar/baz' ); +// returns true + +bool = isAbsolutePath.win32( '/foo/..' ); +// returns true + +bool = isAbsolutePath.win32( 'foo\\bar\\baz' ); +// returns false + +bool = isAbsolutePath.win32( 'foo/bar/baz' ); +// returns false + +bool = isAbsolutePath.win32( 'foo/..' ); +// returns false + +bool = isAbsolutePath.win32( '.' ); +// returns false + +bool = isAbsolutePath.win32( '' ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-absolute-path [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --platform name Platform: 'win32' or 'posix'. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-absolute-path /foo/bar/baz --platform=posix +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n './docs/repl.txt' | is-absolute-path +false +``` + +
+ + + +
+ + + + + + diff --git a/is-absolute-path/benchmark/benchmark.js b/is-absolute-path/benchmark/benchmark.js new file mode 100644 index 00000000..6e017d10 --- /dev/null +++ b/is-absolute-path/benchmark/benchmark.js @@ -0,0 +1,96 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isAbsolutePath = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = '/foo/bar/beep/boop/'+fromCodePoint( 97 + (i%26) ); + bool = isAbsolutePath( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+':posix', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = '/foo/bar/beep/boop/'+fromCodePoint( 97 + (i%26) ); + bool = isAbsolutePath.posix( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+':win32', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = 'C:\\foo\\bar\\beep\\boop\\'+fromCodePoint( 97 + (i%26) ); + bool = isAbsolutePath.win32( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-absolute-path/bin/cli b/is-absolute-path/bin/cli new file mode 100644 index 00000000..52917d54 --- /dev/null +++ b/is-absolute-path/bin/cli @@ -0,0 +1,100 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isAbsolutePath = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + var fun; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + if ( flags.platform === 'win32' ) { + fun = isAbsolutePath.win32; + } else if ( flags.platform === 'posix' ) { + fun = isAbsolutePath.posix; + } else { + fun = isAbsolutePath; + } + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( fun( args[ 0 ] ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( fun( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-absolute-path/docs/repl.txt b/is-absolute-path/docs/repl.txt new file mode 100644 index 00000000..2d89a27e --- /dev/null +++ b/is-absolute-path/docs/repl.txt @@ -0,0 +1,73 @@ + +{{alias}}( value ) + Tests if a value is an absolute path. + + Function behavior is platform-specific. On Windows platforms, the function + is equal to `.win32()`. On POSIX platforms, the function is equal to + `.posix()`. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating if a value is an absolute path. + + Examples + -------- + // Windows environment: + > var bool = {{alias}}( 'C:\\foo\\bar\\baz' ) + true + + // POSIX environment: + > bool = {{alias}}( '/foo/bar/baz' ) + true + + +{{alias}}.posix( value ) + Tests if a value is a POSIX absolute path. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating if a value is a POSIX absolute path. + + Examples + -------- + > var bool = {{alias}}.posix( '/foo/bar/baz' ) + true + > bool = {{alias}}.posix( 'foo/bar/baz' ) + false + + +{{alias}}.win32( value ) + Tests if a value is a Windows absolute path. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating if a value is a Windows absolute path. + + Examples + -------- + > var bool = {{alias}}.win32( 'C:\\foo\\bar\\baz' ) + true + > bool = {{alias}}.win32( 'foo\\bar\\baz' ) + false + + See Also + -------- + diff --git a/is-absolute-path/docs/types/index.d.ts b/is-absolute-path/docs/types/index.d.ts new file mode 100644 index 00000000..02780a25 --- /dev/null +++ b/is-absolute-path/docs/types/index.d.ts @@ -0,0 +1,119 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isAbsolutePath` with methods for testing POSIX and Windows paths, respectively. +*/ +interface IsAbsolutePath { + /** + * Tests if a value is an absolute path. + * + * @param value - value to test + * @returns boolean indicating whether value is an absolute path + * + * @example + * var IS_WINDOWS = require( `@stdlib/assert/is-windows` ); + * var bool; + * if ( IS_WINDOWS ) { + * bool = isAbsolutePath( 'C:\\foo\\bar\\baz' ); + * // returns true + * } else { + * bool = isAbsolutePath( '/foo/bar/baz' ); + * // returns true + * } + */ + ( value: any ): boolean; + + /** + * Tests if a value is a POSIX absolute path. + * + * @param value - value to test + * @returns boolean indicating whether value is a POSIX absolute path + * + * @example + * var bool = isAbsolutePath.posix( '/foo/bar/baz' ); + * // returns true + * + * @example + * var bool = isAbsolutePath.posix( 'foo/bar/baz' ); + * // returns false + */ + posix( value: any ): boolean; + + /** + * Tests if a value is a Windows absolute path. + * + * @param value - value to test + * @returns boolean indicating whether value is a Windows absolute path + * + * @example + * var bool = isAbsolutePath.win32( 'C:\\foo\\bar\\baz' ); + * // returns true + * + * @example + * var bool = isAbsolutePath.win32( 'foo\\bar\\baz' ); + * // returns false + */ + win32( value: any ): boolean; +} + +/** +* Tests if a value is an absolute path. +* +* ## Notes +* +* - Function behavior is platform-specific. On Windows platforms, the function is equal to `.win32()`. On POSIX platforms, the function is equal to `.posix()`. +* +* @param value - value to test +* @returns boolean indicating whether value is an absolute path +* +* @example +* var IS_WINDOWS = require( `@stdlib/assert/is-windows` ); +* var bool; +* if ( IS_WINDOWS ) { +* bool = isAbsolutePath( 'C:\\foo\\bar\\baz' ); +* // returns true +* } else { +* bool = isAbsolutePath( '/foo/bar/baz' ); +* // returns true +* } +* +* @example +* var bool = isAbsolutePath.posix( '/foo/bar/baz' ); +* // returns true +* +* @example +* var bool = isAbsolutePath.posix( 'foo/bar/baz' ); +* // returns false +* +* @example +* var bool = isAbsolutePath.win32( 'C:\\foo\\bar\\baz' ); +* // returns true +* +* @example +* var bool = isAbsolutePath.win32( 'foo\\bar\\baz' ); +* // returns false +*/ +declare var isAbsolutePath: IsAbsolutePath; + + +// EXPORTS // + +export = isAbsolutePath; diff --git a/is-absolute-path/docs/types/test.ts b/is-absolute-path/docs/types/test.ts new file mode 100644 index 00000000..74270c3e --- /dev/null +++ b/is-absolute-path/docs/types/test.ts @@ -0,0 +1,58 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isAbsolutePath = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isAbsolutePath( 'C:\\foo\\bar\\baz' ); // $ExpectType boolean + isAbsolutePath( '/foo/bar/baz' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isAbsolutePath(); // $ExpectError + isAbsolutePath( 'C:\\foo\\bar\\baz', 123 ); // $ExpectError +} + +// Attached to main export is a `posix` method which returns a boolean... +{ + isAbsolutePath.posix( '/foo/bar/baz' ); // $ExpectType boolean + isAbsolutePath.posix( '/foo/../bar/baz' ); // $ExpectType boolean +} + +// The compiler throws an error if the `posix` method is provided an unsupported number of arguments... +{ + isAbsolutePath.posix(); // $ExpectError + isAbsolutePath.posix( '/foo/../bar/baz', 123 ); // $ExpectError +} + +// Attached to main export is a `win32` method which returns a boolean... +{ + isAbsolutePath.win32( 'foo\\bar\\baz' ); // $ExpectType boolean + isAbsolutePath.win32( 'C:\\foo\\..\\bar\\baz' ); // $ExpectType boolean +} + +// The compiler throws an error if the `win32` method is provided an unsupported number of arguments... +{ + isAbsolutePath.win32(); // $ExpectError + isAbsolutePath.win32( 'C:\\foo\\..\\bar\\baz', 123 ); // $ExpectError +} diff --git a/is-absolute-path/docs/usage.txt b/is-absolute-path/docs/usage.txt new file mode 100644 index 00000000..c7893095 --- /dev/null +++ b/is-absolute-path/docs/usage.txt @@ -0,0 +1,9 @@ + +Usage: is-absolute-path [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --platform name Platform: 'win32' or 'posix'. + diff --git a/is-absolute-path/etc/cli_opts.json b/is-absolute-path/etc/cli_opts.json new file mode 100644 index 00000000..cd0c304c --- /dev/null +++ b/is-absolute-path/etc/cli_opts.json @@ -0,0 +1,17 @@ +{ + "string": [ + "platform" + ], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-absolute-path/examples/index.js b/is-absolute-path/examples/index.js new file mode 100644 index 00000000..18df8e75 --- /dev/null +++ b/is-absolute-path/examples/index.js @@ -0,0 +1,85 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isAbsolutePath = require( './../lib' ); + +var bool = isAbsolutePath.posix( '/foo/bar/baz' ); +console.log( bool ); +// => true + +bool = isAbsolutePath.posix( '/foo/..' ); +console.log( bool ); +// => true + +bool = isAbsolutePath.posix( 'foo/' ); +console.log( bool ); +// => false + +bool = isAbsolutePath.posix( 'foo' ); +console.log( bool ); +// => false + +bool = isAbsolutePath.posix( '.' ); +console.log( bool ); +// => false + +bool = isAbsolutePath.posix( '' ); +console.log( bool ); +// => false + +bool = isAbsolutePath.win32( 'C:\\foo\\bar\\baz' ); +console.log( bool ); +// => true + +bool = isAbsolutePath.win32( '//server' ); +console.log( bool ); +// => true + +bool = isAbsolutePath.win32( '\\\\server' ); +console.log( bool ); +// => true + +bool = isAbsolutePath.win32( 'C:/foo/bar/baz' ); +console.log( bool ); +// => true + +bool = isAbsolutePath.win32( '/foo/..' ); +console.log( bool ); +// => true + +bool = isAbsolutePath.win32( 'foo\\bar\\baz' ); +console.log( bool ); +// => false + +bool = isAbsolutePath.win32( 'foo/bar/baz' ); +console.log( bool ); +// => false + +bool = isAbsolutePath.win32( 'foo/..' ); +console.log( bool ); +// => false + +bool = isAbsolutePath.win32( '.' ); +console.log( bool ); +// => false + +bool = isAbsolutePath.win32( '' ); +console.log( bool ); +// => false diff --git a/is-absolute-path/lib/index.js b/is-absolute-path/lib/index.js new file mode 100644 index 00000000..ebc34a82 --- /dev/null +++ b/is-absolute-path/lib/index.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an absolute path. +* +* @module @stdlib/assert/is-absolute-path +* +* @example +* var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +* var isAbsolutePath = require( '@stdlib/assert/is-absolute-path' ); +* +* var bool; +* if ( IS_WINDOWS ) { +* bool = isAbsolutePath( 'C:\\foo\\bar\\baz' ); +* // returns true +* } else { +* bool = isAbsolutePath( '/foo/bar/baz' ); +* // returns true +* } +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var isAbsolutePathPosix = require( './posix.js' ); +var isAbsolutePathWin32 = require( './win32.js' ); + + +// MAIN // + +var isAbsolutePath; +if ( IS_WINDOWS ) { + isAbsolutePath = isAbsolutePathWin32; +} else { + isAbsolutePath = isAbsolutePathPosix; +} +setReadOnly( isAbsolutePath, 'posix', isAbsolutePathPosix ); +setReadOnly( isAbsolutePath, 'win32', isAbsolutePathWin32 ); + + +// EXPORTS // + +module.exports = isAbsolutePath; diff --git a/is-absolute-path/lib/posix.js b/is-absolute-path/lib/posix.js new file mode 100644 index 00000000..0b009da9 --- /dev/null +++ b/is-absolute-path/lib/posix.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a POSIX absolute path. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a POSIX absolute path +* +* @example +* var bool = isAbsolutePath( '/foo/bar/baz' ); +* // returns true +* +* @example +* var bool = isAbsolutePath( 'foo/bar/baz' ); +* // returns false +*/ +function isAbsolutePath( value ) { + // Must be string longer than `0` characters and start with a forward slash '/': + return ( + isString( value ) && + value.length > 0 && + value.charCodeAt( 0 ) === 47 + ); +} + + +// EXPORTS // + +module.exports = isAbsolutePath; diff --git a/is-absolute-path/lib/win32.js b/is-absolute-path/lib/win32.js new file mode 100644 index 00000000..dd309991 --- /dev/null +++ b/is-absolute-path/lib/win32.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a Windows absolute path. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a Windows absolute path +* +* @example +* var bool = isAbsolutePath( 'C:\\foo\\bar\\baz' ); +* // returns true +* +* @example +* var bool = isAbsolutePath( 'foo\\bar\\baz' ); +* // returns false +*/ +function isAbsolutePath( value ) { + var code; + var len; + if ( !isString( value ) ) { + return false; + } + len = value.length; + if ( len === 0 ) { + return false; + } + code = value.charCodeAt( 0 ); + + // Check if the string begins with either a forward '/' or backward slash '\\': + if ( code === 47 || code === 92 ) { + return true; + } + // Check for a device root (e.g., C:\\)... + if ( + (code >= 65 && code <= 90) || // A-Z + (code >= 97 && code <= 122) // a-z + ) { + // Check if the string has a colon ':' character: + if ( len > 2 && value.charCodeAt( 1 ) === 58 ) { + code = value.charCodeAt( 2 ); + + // Check for either a forward or backward slash: + if ( code === 47 || code === 92 ) { + return true; + } + } + } + return false; +} + + +// EXPORTS // + +module.exports = isAbsolutePath; diff --git a/is-absolute-path/package.json b/is-absolute-path/package.json new file mode 100644 index 00000000..4b7fabbd --- /dev/null +++ b/is-absolute-path/package.json @@ -0,0 +1,83 @@ +{ + "name": "@stdlib/assert/is-absolute-path", + "version": "0.0.0", + "description": "Test if a value is an absolute path.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-absolute-path": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "is-absolute", + "is-path", + "path", + "absolute", + "filesystem", + "file", + "filepath", + "filename", + "dir", + "directory", + "dirpath", + "check", + "validate", + "valid", + "isvalid", + "is-valid", + "test" + ] +} diff --git a/is-absolute-path/test/fixtures/stdin_error.js.txt b/is-absolute-path/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-absolute-path/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-absolute-path/test/test.cli.js b/is-absolute-path/test/test.cli.js new file mode 100644 index 00000000..3985ab4d --- /dev/null +++ b/is-absolute-path/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument is an absolute path', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'/var/www/\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "./index.js\n/var/www/"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'false\ntrue\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-absolute-path/test/test.js b/is-absolute-path/test/test.js new file mode 100644 index 00000000..4cb9ea5f --- /dev/null +++ b/is-absolute-path/test/test.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var isAbsolutePath = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isAbsolutePath, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if platform is POSIX, main export is method to test if a string is a POSIX absolute path', function test( t ) { + var isAbsolutePath = proxyquire( './../lib', { + '@stdlib/assert/is-windows': false + }); + t.equal( isAbsolutePath, isAbsolutePath.posix, 'equals posix method' ); + t.end(); +}); + +tape( 'if platform is Windows, main export is method to test if a string is a Windows absolute path', function test( t ) { + var isAbsolutePath = proxyquire( './../lib', { + '@stdlib/assert/is-windows': true + }); + t.equal( isAbsolutePath, isAbsolutePath.win32, 'equals win32 method' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test if a string is a POSIX absolute path', function test( t ) { + t.equal( typeof isAbsolutePath.posix, 'function', 'has a posix method' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test if a string is a Windows absolute path', function test( t ) { + t.equal( typeof isAbsolutePath.win32, 'function', 'has a win32 method' ); + t.end(); +}); diff --git a/is-absolute-path/test/test.posix.js b/is-absolute-path/test/test.posix.js new file mode 100644 index 00000000..5de90e7f --- /dev/null +++ b/is-absolute-path/test/test.posix.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isAbsolutePath = require( './../lib/posix.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isAbsolutePath, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a string', function test( t ) { + var values; + var bool; + var i; + + values = [ + 5, + NaN, + null, + undefined, + true, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isAbsolutePath( values[ i ] ); + t.equal( bool, false, 'returns `false` when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided an empty string', function test( t ) { + var bool = isAbsolutePath( '' ); + t.strictEqual( bool, false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a string which starts with a forward slash \'/\'', function test( t ) { + var values; + var bool; + var i; + + values = [ + '/foo/bar/baz', + '/foo/..', + '/beep boop', + '/' + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isAbsolutePath( values[ i ] ); + t.strictEqual( bool, true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided a relative path', function test( t ) { + var values; + var bool; + var i; + + values = [ + '.', + 'foo/bar', + './foo/bar', + '..', + '../foo/bar', + 'foo/../bar' + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isAbsolutePath( values[ i ] ); + t.strictEqual( bool, false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-absolute-path/test/test.win32.js b/is-absolute-path/test/test.win32.js new file mode 100644 index 00000000..5cd0acde --- /dev/null +++ b/is-absolute-path/test/test.win32.js @@ -0,0 +1,135 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isAbsolutePath = require( './../lib/win32.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isAbsolutePath, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a string', function test( t ) { + var values; + var bool; + var i; + + values = [ + 5, + NaN, + null, + undefined, + true, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isAbsolutePath( values[ i ] ); + t.equal( bool, false, 'returns `false` when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided an empty string', function test( t ) { + var bool = isAbsolutePath( '' ); + t.strictEqual( bool, false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a string which starts with a forward slash \'/\' or a backward slash \'\\\'', function test( t ) { + var values; + var bool; + var i; + + values = [ + '/foo/bar/baz', + '/foo/..', + '/beep boop', + '/', + '\\', + '\\foo\\bar\\baz', + '\\foo\\..', + '\\beep boop' + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isAbsolutePath( values[ i ] ); + t.strictEqual( bool, true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided a string which starts with a device root', function test( t ) { + var values; + var bool; + var i; + + values = [ + 'C:\\foo\\bar\\baz', + 'C:/foo/bar/baz', + 'c:\\foo\\bar\\baz', + 'c:/foo/bar/baz', + '//server', + '\\\\server' + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isAbsolutePath( values[ i ] ); + t.strictEqual( bool, true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided a relative path', function test( t ) { + var values; + var bool; + var i; + + values = [ + '.', + 'foo/bar', + './foo/bar', + '..', + '../foo/bar', + 'foo/../bar', + 'foo\\bar', + '.\\foo\\bar', + '..\\foo\\bar', + 'foo\\..\\bar', + 'c', + 'c:', + 'C', + 'C:', + 'C;\\', + 'C;/', + 'c;\\', + 'c;/' + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isAbsolutePath( values[ i ] ); + t.strictEqual( bool, false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-accessor-property-in/README.md b/is-accessor-property-in/README.md new file mode 100644 index 00000000..36cd0bf6 --- /dev/null +++ b/is-accessor-property-in/README.md @@ -0,0 +1,167 @@ + + +# isAccessorPropertyIn + +> Test if an object's own or inherited property has an accessor descriptor. + +
+ +## Usage + +```javascript +var isAccessorPropertyIn = require( '@stdlib/assert/is-accessor-property-in' ); +``` + +#### isAccessorPropertyIn( value, property ) + +Returns a `boolean` indicating if an object's own or inherited `property` has an accessor descriptor. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var bool; +var obj; + +function Foo() { + this.foo = 'bar'; + return this; +} + +defineProperty( Foo.prototype, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'boop' +}); + +defineProperty( Foo.prototype, 'accessor', { + 'configurable': false, + 'enumerable': false, + 'get': function getter() { + return obj.foo; + }, + 'set': function setter( v ) { + obj.foo = v; + } +}); + +obj = new Foo(); + +bool = isAccessorPropertyIn( obj, 'foo' ); +// returns false + +bool = isAccessorPropertyIn( obj, 'beep' ); +// returns false + +bool = isAccessorPropertyIn( obj, 'accessor' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isAccessorPropertyIn( 'beep', 'length' ); + // returns false + ``` + +- Non-symbol property arguments are coerced to `strings`. + + ```javascript + var defineProperty = require( '@stdlib/utils/define-property' ); + + var obj = {}; + + function getter() { + return true; + } + + defineProperty( obj, 'null', { + 'configurable': true, + 'enumerable': true, + 'get': getter + }); + + var bool = isAccessorPropertyIn( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isAccessorPropertyIn = require( '@stdlib/assert/is-accessor-property-in' ); + +var bool = isAccessorPropertyIn( [ 'a' ], 'length' ); +// returns false + +bool = isAccessorPropertyIn( { 'a': 'b' }, 'a' ); +// returns false + +bool = isAccessorPropertyIn( [ 'a' ], 0 ); +// returns false + +bool = isAccessorPropertyIn( { 'null': false }, null ); +// returns false + +bool = isAccessorPropertyIn( { '[object Object]': false }, {} ); +// returns false + +bool = isAccessorPropertyIn( {}, 'toString' ); +// returns false + +bool = isAccessorPropertyIn( {}, 'hasOwnProperty' ); +// returns false + +bool = isAccessorPropertyIn( null, 'a' ); +// returns false + +bool = isAccessorPropertyIn( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-accessor-property-in/benchmark/benchmark.js b/is-accessor-property-in/benchmark/benchmark.js new file mode 100644 index 00000000..5e17bd8e --- /dev/null +++ b/is-accessor-property-in/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isAccessorPropertyIn = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isAccessorPropertyIn( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-accessor-property-in/docs/repl.txt b/is-accessor-property-in/docs/repl.txt new file mode 100644 index 00000000..50081d09 --- /dev/null +++ b/is-accessor-property-in/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, property ) + Tests if an object's own or inherited property has an accessor descriptor. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own or inherited property has an + accessor descriptor. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = false; + > desc.get = function getter() { return 'beep'; }; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + false + > bool = {{alias}}( obj, 'beep' ) + true + + See Also + -------- + diff --git a/is-accessor-property-in/docs/types/index.d.ts b/is-accessor-property-in/docs/types/index.d.ts new file mode 100644 index 00000000..f99e435a --- /dev/null +++ b/is-accessor-property-in/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own or inherited property has an accessor descriptor. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property has an accessor descriptor +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* function getter() { +* return 'beep'; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'get': getter +* }); +* +* var bool = isAccessorPropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isAccessorPropertyIn( obj, 'beep' ); +* // returns true +*/ +declare function isAccessorPropertyIn( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isAccessorPropertyIn; diff --git a/is-accessor-property-in/docs/types/test.ts b/is-accessor-property-in/docs/types/test.ts new file mode 100644 index 00000000..a6d5ab90 --- /dev/null +++ b/is-accessor-property-in/docs/types/test.ts @@ -0,0 +1,49 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isAccessorPropertyIn = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + + const getter = () => { + return 'beep'; + }; + + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'get': getter + } ); + + isAccessorPropertyIn( obj, 'boop' ); // $ExpectType boolean + isAccessorPropertyIn( obj, 'beep' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isAccessorPropertyIn(); // $ExpectError + isAccessorPropertyIn( {} ); // $ExpectError + isAccessorPropertyIn( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-accessor-property-in/examples/index.js b/is-accessor-property-in/examples/index.js new file mode 100644 index 00000000..1bfa4501 --- /dev/null +++ b/is-accessor-property-in/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isAccessorPropertyIn = require( './../lib' ); + +var bool = isAccessorPropertyIn( [ 'a' ], 'length' ); +console.log( bool ); +// => false + +bool = isAccessorPropertyIn( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => false + +bool = isAccessorPropertyIn( [ 'a' ], 0 ); +console.log( bool ); +// => false + +bool = isAccessorPropertyIn( { 'null': false }, null ); +console.log( bool ); +// => false + +bool = isAccessorPropertyIn( { '[object Object]': false }, {} ); +console.log( bool ); +// => false + +bool = isAccessorPropertyIn( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isAccessorPropertyIn( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isAccessorPropertyIn( null, 'a' ); +console.log( bool ); +// => false + +bool = isAccessorPropertyIn( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-accessor-property-in/lib/index.js b/is-accessor-property-in/lib/index.js new file mode 100644 index 00000000..5f8f49df --- /dev/null +++ b/is-accessor-property-in/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own or inherited property has an accessor descriptor. +* +* @module @stdlib/assert/is-accessor-property-in +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isAccessorPropertyIn = require( '@stdlib/assert/is-accessor-property-in' ); +* +* var obj = { +* 'boop': true +* }; +* +* function getter() { +* return 'beep'; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'get': getter +* }); +* +* var bool = isAccessorPropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isAccessorPropertyIn( obj, 'beep' ); +* // returns true +*/ + +// MODULES // + +var isAccessorPropertyIn = require( './main.js' ); + + +// EXPORTS // + +module.exports = isAccessorPropertyIn; diff --git a/is-accessor-property-in/lib/main.js b/is-accessor-property-in/lib/main.js new file mode 100644 index 00000000..d0d33ffc --- /dev/null +++ b/is-accessor-property-in/lib/main.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptorIn = require( '@stdlib/utils/property-descriptor-in' ); + + +// MAIN // + +/** +* Tests if an object's own or inherited property has an accessor descriptor. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property has an accessor descriptor +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function getter() { +* return 'beep'; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'get': getter +* }); +* +* var bool = isAccessorPropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isAccessorPropertyIn( obj, 'beep' ); +* // returns true +*/ +function isAccessorPropertyIn( value, property ) { + var desc = propertyDescriptorIn( value, property ); + return ( + desc !== null && + ( + typeof desc.get === 'function' || + typeof desc.set === 'function' + ) + ); +} + + +// EXPORTS // + +module.exports = isAccessorPropertyIn; diff --git a/is-accessor-property-in/package.json b/is-accessor-property-in/package.json new file mode 100644 index 00000000..961c1b4e --- /dev/null +++ b/is-accessor-property-in/package.json @@ -0,0 +1,79 @@ +{ + "name": "@stdlib/assert/is-accessor-property-in", + "version": "0.0.0", + "description": "Test if an object's own or inherited property has an accessor descriptor.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "inherited", + "in", + "prototype", + "proto", + "accessor", + "get", + "set", + "descriptor", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-accessor-property-in/test/test.js b/is-accessor-property-in/test/test.js new file mode 100644 index 00000000..830fd2de --- /dev/null +++ b/is-accessor-property-in/test/test.js @@ -0,0 +1,239 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isAccessorPropertyIn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isAccessorPropertyIn, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property has an accessor descriptor', function test( t ) { + var bool; + var obj; + + obj = {}; + + defineProperty( obj, 'a', { + 'configurable': true, + 'enumerable': true, + 'get': getter, + 'set': setter + }); + + defineProperty( obj, 'b', { + 'configurable': true, + 'enumerable': true, + 'get': getter + }); + + defineProperty( obj, 'c', { + 'configurable': true, + 'enumerable': true, + 'set': setter + }); + + bool = isAccessorPropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isAccessorPropertyIn( obj, 'b' ); + t.equal( bool, true, 'returns true' ); + + bool = isAccessorPropertyIn( obj, 'c' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function getter() { + // No-op... + } + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `true` if provided an inherited property having an accessor descriptor', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + defineProperty( Foo.prototype, 'a', { + 'configurable': true, + 'enumerable': true, + 'get': getter, + 'set': setter + }); + + defineProperty( Foo.prototype, 'b', { + 'configurable': true, + 'enumerable': true, + 'get': getter + }); + + defineProperty( Foo.prototype, 'c', { + 'configurable': true, + 'enumerable': true, + 'set': setter + }); + + obj = new Foo(); + + bool = isAccessorPropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isAccessorPropertyIn( obj, 'b' ); + t.equal( bool, true, 'returns true' ); + + bool = isAccessorPropertyIn( obj, 'c' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function getter() { + // No-op... + } + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `false` if an object property has a data descriptor', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isAccessorPropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorPropertyIn( [ 1, 2, 3 ], '1' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorPropertyIn( [ 1, 2, 3 ], 1 ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorPropertyIn( new Foo(), 'bar' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorPropertyIn( [ 'a' ], 'length' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'b' + }); + + bool = isAccessorPropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isAccessorPropertyIn( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isAccessorPropertyIn( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + bool = isAccessorPropertyIn( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorPropertyIn( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an inherited property having a data descriptor', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + defineProperty( Foo.prototype, 'a', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': true + }); + + obj = new Foo(); + + bool = isAccessorPropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorPropertyIn( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorPropertyIn( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorPropertyIn( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isAccessorPropertyIn( 'beep', 'length' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); diff --git a/is-accessor-property/README.md b/is-accessor-property/README.md new file mode 100644 index 00000000..6e57a055 --- /dev/null +++ b/is-accessor-property/README.md @@ -0,0 +1,161 @@ + + +# isAccessorProperty + +> Test if an object's own property has an accessor descriptor. + +
+ +## Usage + +```javascript +var isAccessorProperty = require( '@stdlib/assert/is-accessor-property' ); +``` + +#### isAccessorProperty( value, property ) + +Returns a `boolean` indicating if an object's own `property` has an accessor descriptor. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var obj = { + 'foo': 'bar' +}; + +defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'boop' +}); + +defineProperty( obj, 'accessor', { + 'configurable': false, + 'enumerable': false, + 'get': function getter() { + return obj.foo; + }, + 'set': function setter( v ) { + obj.foo = v; + } +}); + +var bool = isAccessorProperty( obj, 'foo' ); +// returns false + +bool = isAccessorProperty( obj, 'beep' ); +// returns false + +bool = isAccessorProperty( obj, 'accessor' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isAccessorProperty( 'beep', 'length' ); + // returns false + ``` + +- Non-symbol property arguments are coerced to `strings`. + + ```javascript + var defineProperty = require( '@stdlib/utils/define-property' ); + + var obj = {}; + + function getter() { + return true; + } + + defineProperty( obj, 'null', { + 'configurable': true, + 'enumerable': true, + 'get': getter + }); + + var bool = isAccessorProperty( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isAccessorProperty = require( '@stdlib/assert/is-accessor-property' ); + +var bool = isAccessorProperty( [ 'a' ], 'length' ); +// returns false + +bool = isAccessorProperty( { 'a': 'b' }, 'a' ); +// returns false + +bool = isAccessorProperty( [ 'a' ], 0 ); +// returns false + +bool = isAccessorProperty( { 'null': false }, null ); +// returns false + +bool = isAccessorProperty( { '[object Object]': false }, {} ); +// returns false + +bool = isAccessorProperty( {}, 'toString' ); +// returns false + +bool = isAccessorProperty( {}, 'hasOwnProperty' ); +// returns false + +bool = isAccessorProperty( null, 'a' ); +// returns false + +bool = isAccessorProperty( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-accessor-property/benchmark/benchmark.js b/is-accessor-property/benchmark/benchmark.js new file mode 100644 index 00000000..94b59b7a --- /dev/null +++ b/is-accessor-property/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isAccessorProperty = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isAccessorProperty( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-accessor-property/docs/repl.txt b/is-accessor-property/docs/repl.txt new file mode 100644 index 00000000..7a813761 --- /dev/null +++ b/is-accessor-property/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, property ) + Tests if an object's own property has an accessor descriptor. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own property has an accessor + descriptor. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = false; + > desc.get = function getter() { return 'beep'; }; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + false + > bool = {{alias}}( obj, 'beep' ) + true + + See Also + -------- + diff --git a/is-accessor-property/docs/types/index.d.ts b/is-accessor-property/docs/types/index.d.ts new file mode 100644 index 00000000..58175a62 --- /dev/null +++ b/is-accessor-property/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own property has an accessor descriptor. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property has an accessor descriptor +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* function getter() { +* return 'beep'; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'get': getter +* }); +* +* var bool = isAccessorProperty( obj, 'boop' ); +* // returns false +* +* bool = isAccessorProperty( obj, 'beep' ); +* // returns true +*/ +declare function isAccessorProperty( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isAccessorProperty; diff --git a/is-accessor-property/docs/types/test.ts b/is-accessor-property/docs/types/test.ts new file mode 100644 index 00000000..3723ed35 --- /dev/null +++ b/is-accessor-property/docs/types/test.ts @@ -0,0 +1,49 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isAccessorProperty = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + + const getter = () => { + return 'beep'; + }; + + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'get': getter + } ); + + isAccessorProperty( obj, 'boop' ); // $ExpectType boolean + isAccessorProperty( obj, 'beep' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isAccessorProperty(); // $ExpectError + isAccessorProperty( {} ); // $ExpectError + isAccessorProperty( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-accessor-property/examples/index.js b/is-accessor-property/examples/index.js new file mode 100644 index 00000000..0c31749f --- /dev/null +++ b/is-accessor-property/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isAccessorProperty = require( './../lib' ); + +var bool = isAccessorProperty( [ 'a' ], 'length' ); +console.log( bool ); +// => false + +bool = isAccessorProperty( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => false + +bool = isAccessorProperty( [ 'a' ], 0 ); +console.log( bool ); +// => false + +bool = isAccessorProperty( { 'null': false }, null ); +console.log( bool ); +// => false + +bool = isAccessorProperty( { '[object Object]': false }, {} ); +console.log( bool ); +// => false + +bool = isAccessorProperty( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isAccessorProperty( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isAccessorProperty( null, 'a' ); +console.log( bool ); +// => false + +bool = isAccessorProperty( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-accessor-property/lib/index.js b/is-accessor-property/lib/index.js new file mode 100644 index 00000000..71945ae4 --- /dev/null +++ b/is-accessor-property/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own property has an accessor descriptor. +* +* @module @stdlib/assert/is-accessor-property +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isAccessorProperty = require( '@stdlib/assert/is-accessor-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function getter() { +* return 'beep'; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'get': getter +* }); +* +* var bool = isAccessorProperty( obj, 'boop' ); +* // returns false +* +* bool = isAccessorProperty( obj, 'beep' ); +* // returns true +*/ + +// MODULES // + +var isAccessorProperty = require( './main.js' ); + + +// EXPORTS // + +module.exports = isAccessorProperty; diff --git a/is-accessor-property/lib/main.js b/is-accessor-property/lib/main.js new file mode 100644 index 00000000..dfeec60f --- /dev/null +++ b/is-accessor-property/lib/main.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptor = require( '@stdlib/utils/property-descriptor' ); + + +// MAIN // + +/** +* Tests if an object's own property has an accessor descriptor. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property has an accessor descriptor +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function getter() { +* return 'beep'; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'get': getter +* }); +* +* var bool = isAccessorProperty( obj, 'boop' ); +* // returns false +* +* bool = isAccessorProperty( obj, 'beep' ); +* // returns true +*/ +function isAccessorProperty( value, property ) { + var desc = propertyDescriptor( value, property ); + return ( + desc !== null && + ( + typeof desc.get === 'function' || + typeof desc.set === 'function' + ) + ); +} + + +// EXPORTS // + +module.exports = isAccessorProperty; diff --git a/is-accessor-property/package.json b/is-accessor-property/package.json new file mode 100644 index 00000000..557b63d9 --- /dev/null +++ b/is-accessor-property/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-accessor-property", + "version": "0.0.0", + "description": "Test if an object's own property has an accessor descriptor.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "accessor", + "get", + "set", + "descriptor", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-accessor-property/test/test.js b/is-accessor-property/test/test.js new file mode 100644 index 00000000..d05b8b72 --- /dev/null +++ b/is-accessor-property/test/test.js @@ -0,0 +1,193 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isAccessorProperty = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isAccessorProperty, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property has an accessor descriptor', function test( t ) { + var bool; + var obj; + + obj = {}; + + defineProperty( obj, 'a', { + 'configurable': true, + 'enumerable': true, + 'get': getter, + 'set': setter + }); + + defineProperty( obj, 'b', { + 'configurable': true, + 'enumerable': true, + 'get': getter + }); + + defineProperty( obj, 'c', { + 'configurable': true, + 'enumerable': true, + 'set': setter + }); + + bool = isAccessorProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isAccessorProperty( obj, 'b' ); + t.equal( bool, true, 'returns true' ); + + bool = isAccessorProperty( obj, 'c' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function getter() { + // No-op... + } + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `false` if an object property has a data descriptor', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isAccessorProperty( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorProperty( [ 1, 2, 3 ], '1' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorProperty( [ 1, 2, 3 ], 1 ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorProperty( new Foo(), 'bar' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorProperty( [ 'a' ], 'length' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'b' + }); + + bool = isAccessorProperty( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isAccessorProperty( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isAccessorProperty( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + bool = isAccessorProperty( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorProperty( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an inherited property', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + defineProperty( Foo.prototype, 'a', { + 'configurable': true, + 'enumerable': true, + 'get': getter + }); + + obj = new Foo(); + + bool = isAccessorProperty( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorProperty( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorProperty( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isAccessorProperty( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function getter() { + // No-op... + } +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isAccessorProperty( 'beep', 'length' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); diff --git a/is-alphagram/README.md b/is-alphagram/README.md new file mode 100644 index 00000000..4dd82bd7 --- /dev/null +++ b/is-alphagram/README.md @@ -0,0 +1,144 @@ + + +# isAlphagram + +> Test if a value is an [alphagram][alphagram]. + +
+ +## Usage + +```javascript +var isAlphagram = require( '@stdlib/assert/is-alphagram' ); +``` + +#### isAlphagram( value ) + +Tests if a `value` is an [alphagram][alphagram] (i.e., a sequence of characters arranged in alphabetical order). + +```javascript +var value = 'beep'; + +var bool = isAlphagram( value ); +// returns true +``` + +
+ + + +
+ +## Notes + +- The function first checks that an input `value` is a `string` before validating that the `value` is an [alphagram][alphagram]. For non-string values, the function returns `false`. + +
+ + + +
+ +## Examples + + + + + +```javascript +var isAlphagram = require( '@stdlib/assert/is-alphagram' ); + +var out = isAlphagram( 'beep' ); +// returns true + +out = isAlphagram( new String( 'beep' ) ); +// returns true + +out = isAlphagram( '' ); +// returns false + +out = isAlphagram( 'zba' ); +// returns false + +out = isAlphagram( 123 ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-alphagram [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-alphagram beep +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n 'hello' | is-alphagram +false +``` + +
+ + + +
+ + + + + + diff --git a/is-alphagram/benchmark/benchmark.js b/is-alphagram/benchmark/benchmark.js new file mode 100644 index 00000000..6e041a91 --- /dev/null +++ b/is-alphagram/benchmark/benchmark.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isAlphagram = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + 'abc', + 'TAB', + 't a b', + 'abbt', + 'bbt', + 'zba', + 5, + null, + true, + NaN, + [], + {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isAlphagram( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-alphagram/bin/cli b/is-alphagram/bin/cli new file mode 100644 index 00000000..894275bc --- /dev/null +++ b/is-alphagram/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isAlphagram = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isAlphagram( args[ 0 ] ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isAlphagram( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-alphagram/docs/repl.txt b/is-alphagram/docs/repl.txt new file mode 100644 index 00000000..2e568ae3 --- /dev/null +++ b/is-alphagram/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( value ) + Tests if a value is an alphagram (i.e., a sequence of characters arranged in + alphabetical order). + + The function first checks that an input value is a string before validating + that the value is an alphagram. For non-string values, the function returns + `false`. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an alphagram. + + Examples + -------- + > var out = {{alias}}( 'beep' ) + true + > out = {{alias}}( 'zba' ) + false + > out = {{alias}}( '' ) + false + + See Also + -------- + diff --git a/is-alphagram/docs/types/index.d.ts b/is-alphagram/docs/types/index.d.ts new file mode 100644 index 00000000..95d179c2 --- /dev/null +++ b/is-alphagram/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an alphagram (i.e., a sequence of characters arranged in alphabetical order). +* +* ## Notes +* +* - The function first checks that an input value is a string before validating that the value is an alphagram. For non-string values, the function returns `false`. +* +* @param value - value to test +* @returns boolean indicating if a value is an alphagram +* +* @example +* var out = isAlphagram( 'beep' ); +* // returns true +* +* @example +* var out = isAlphagram( new String( 'beep' ) ); +* // returns true +* +* @example +* var out = isAlphagram( 'zba' ); +* // returns false +* +* @example +* var out = isAlphagram( '' ); +* // returns false +* +* @example +* var out = isAlphagram( 123 ); +* // returns false +*/ +declare function isAlphagram( value: any ): boolean; + + +// EXPORTS // + +export = isAlphagram; diff --git a/is-alphagram/docs/types/test.ts b/is-alphagram/docs/types/test.ts new file mode 100644 index 00000000..f8c61b14 --- /dev/null +++ b/is-alphagram/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isAlphagram = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isAlphagram( 'beep' ); // $ExpectType boolean + isAlphagram( 'zba' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isAlphagram(); // $ExpectError + isAlphagram( undefined, 123 ); // $ExpectError +} diff --git a/is-alphagram/docs/usage.txt b/is-alphagram/docs/usage.txt new file mode 100644 index 00000000..26e7539b --- /dev/null +++ b/is-alphagram/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-alphagram [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-alphagram/etc/cli_opts.json b/is-alphagram/etc/cli_opts.json new file mode 100644 index 00000000..92119c45 --- /dev/null +++ b/is-alphagram/etc/cli_opts.json @@ -0,0 +1,15 @@ +{ + "string": [], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-alphagram/examples/index.js b/is-alphagram/examples/index.js new file mode 100644 index 00000000..ceeeae83 --- /dev/null +++ b/is-alphagram/examples/index.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isAlphagram = require( './../lib' ); + +console.log( isAlphagram( 'beep' ) ); +// => true + +console.log( isAlphagram( new String( 'beep' ) ) ); +// => true + +console.log( isAlphagram( '' ) ); +// => false + +console.log( isAlphagram( 'zba' ) ); +// => false + +console.log( isAlphagram( 123 ) ); +// => false diff --git a/is-alphagram/lib/index.js b/is-alphagram/lib/index.js new file mode 100644 index 00000000..d95eefbc --- /dev/null +++ b/is-alphagram/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an alphagram. +* +* @module @stdlib/assert/is-alphagram +* +* @example +* var isAlphagram = require( '@stdlib/assert/is-alphagram' ); +* +* var out = isAlphagram( 'beep' ); +* // returns true +* +* out = isAlphagram( 'zba' ); +* // returns false +* +* out = isAlphagram( '' ); +* // returns false +*/ + +// MODULES // + +var isAlphagram = require( './main.js' ); + + +// EXPORTS // + +module.exports = isAlphagram; diff --git a/is-alphagram/lib/main.js b/is-alphagram/lib/main.js new file mode 100644 index 00000000..57b15583 --- /dev/null +++ b/is-alphagram/lib/main.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ); + + +// MAIN // + +/** +* Tests if a value is an alphagram. +* +* @param {*} x - value to test +* @returns {boolean} boolean indicating if a value is an alphagram +* +* @example +* var out = isAlphagram( 'beep' ); +* // returns true +* +* @example +* var out = isAlphagram( new String( 'beep' ) ); +* // returns true +* +* @example +* var out = isAlphagram( 'zba' ); +* // returns false +* +* @example +* var out = isAlphagram( '' ); +* // returns false +* +* @example +* var out = isAlphagram( 123 ); +* // returns false +*/ +function isAlphagram( x ) { + var len; + var i; + + if ( !isString( x ) ) { + return false; + } + len = x.length; + if ( !len ) { + return false; + } + for ( i = 1; i < len; i++ ) { + if ( x[ i-1 ] > x[ i ] ) { + return false; + } + } + return true; +} + + +// EXPORTS // + +module.exports = isAlphagram; diff --git a/is-alphagram/package.json b/is-alphagram/package.json new file mode 100644 index 00000000..8e198e02 --- /dev/null +++ b/is-alphagram/package.json @@ -0,0 +1,79 @@ +{ + "name": "@stdlib/assert/is-alphagram", + "version": "0.0.0", + "description": "Test if a value is an alphagram.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-alphagram": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "string", + "alphabetical", + "order", + "alphagram", + "anagram", + "sorted", + "is", + "isstring", + "isalphagram", + "issorted", + "validate", + "valid", + "test", + "check" + ] +} diff --git a/is-alphagram/test/fixtures/stdin_error.js.txt b/is-alphagram/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-alphagram/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-alphagram/test/test.cli.js b/is-alphagram/test/test.cli.js new file mode 100644 index 00000000..fe7e59e9 --- /dev/null +++ b/is-alphagram/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument is an alphagram', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'abcdef\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "beep\nzba"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\nfalse\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-alphagram/test/test.js b/is-alphagram/test/test.js new file mode 100644 index 00000000..950e3763 --- /dev/null +++ b/is-alphagram/test/test.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isAlphagram = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isAlphagram, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an alphagram', function test( t ) { + t.strictEqual( isAlphagram( 'abcdef' ), true, 'returns true' ); + t.strictEqual( isAlphagram( new String( 'beep' ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an alphagram', function test( t ) { + var values; + var i; + + values = [ + 'zba', + 'akjdfa', + ' abc ', + '', + 5, + null, + NaN, + undefined, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isAlphagram( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-alphanumeric/README.md b/is-alphanumeric/README.md new file mode 100644 index 00000000..0b368a0d --- /dev/null +++ b/is-alphanumeric/README.md @@ -0,0 +1,136 @@ + + +# isAlphaNumeric + +> Test whether a string contains only alphanumeric characters. + +
+ +## Usage + +```javascript +var isAlphaNumeric = require( '@stdlib/assert/is-alphanumeric' ); +``` + +#### isAlphaNumeric( value ) + +Tests whether a string contains only alphanumeric characters. + +```javascript +var bool = isAlphaNumeric( 'abc0123456789' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- For non-string values, the function returns `false`. +- _Alphanumeric_ is defined as the characters `a-zA-Z` and the numeric characters `0-9`. + +
+ + + +
+ +## Examples + + + +```javascript +var isAlphaNumeric = require( '@stdlib/assert/is-alphanumeric' ); + +var out = isAlphaNumeric( 'abs0123456789' ); +// returns true + +out = isAlphaNumeric( '0xffffff' ); +// returns true + +out = isAlphaNumeric( '' ); +// returns false + +out = isAlphaNumeric( 123 ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-alphanumeric [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-alphanumeric 01abc23456789 +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n '0123456789' | is-alphanumeric +true +``` + +
+ + + +
+ + + + + + diff --git a/is-alphanumeric/benchmark/benchmark.js b/is-alphanumeric/benchmark/benchmark.js new file mode 100644 index 00000000..2173b85c --- /dev/null +++ b/is-alphanumeric/benchmark/benchmark.js @@ -0,0 +1,118 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isAlphaNumeric = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::single-character', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = fromCodePoint( (i%74)+48 ); + bool = isAlphaNumeric( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::string', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = '0123456789' + fromCodePoint( (i%74)+48 ); + bool = isAlphaNumeric( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::regexp-single-character', function benchmark( b ) { + var bool; + var str; + var RE; + var i; + + RE = /[a-zA-Z0-9]+/; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = fromCodePoint( (i%74)+48 ); + bool = RE.test( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::regexp-string', function benchmark( b ) { + var bool; + var str; + var RE; + var i; + + RE = /[a-zA-Z0-9]+/; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = '0123456789' + fromCodePoint( (i%74)+48 ); + bool = RE.test( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-alphanumeric/bin/cli b/is-alphanumeric/bin/cli new file mode 100644 index 00000000..4b804820 --- /dev/null +++ b/is-alphanumeric/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isAlphaNumeric = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isAlphaNumeric( String( args[ 0 ] ) ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isAlphaNumeric( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-alphanumeric/docs/repl.txt b/is-alphanumeric/docs/repl.txt new file mode 100644 index 00000000..12facfbb --- /dev/null +++ b/is-alphanumeric/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( str ) + Tests whether a string contains only alphanumeric characters. + + Parameters + ---------- + str: string + Input string. + + Returns + ------- + bool: boolean + Boolean indicating whether a string contains only alphanumeric + characters. + + Examples + -------- + > var bool = {{alias}}( 'abc0123456789' ) + true + > bool = {{alias}}( 'abcdef' ) + true + > bool = {{alias}}( '0xff' ) + true + > bool = {{alias}}( '' ) + false + + See Also + -------- + diff --git a/is-alphanumeric/docs/types/index.d.ts b/is-alphanumeric/docs/types/index.d.ts new file mode 100644 index 00000000..c2ca1776 --- /dev/null +++ b/is-alphanumeric/docs/types/index.d.ts @@ -0,0 +1,52 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests whether a string contains only alphanumeric characters. +* +* @param value - value to test +* @returns boolean indicating if a string contains only alphanumeric characters +* +* @example +* var out = isAlphaNumeric( 'abc123def456' ); +* // returns true +* +* @example +* var out = isAlphaNumeric( '0xffffff' ); +* // returns true +* +* @example +* var out = isAlphaNumeric( '123' ); +* // returns true +* +* @example +* var out = isAlphaNumeric( '' ); +* // returns false +* +* @example +* var out = isAlphaNumeric( 123 ); +* // returns false +*/ +declare function isAlphaNumeric( value: any ): boolean; + + +// EXPORTS // + +export = isAlphaNumeric; diff --git a/is-alphanumeric/docs/types/test.ts b/is-alphanumeric/docs/types/test.ts new file mode 100644 index 00000000..d7630e2f --- /dev/null +++ b/is-alphanumeric/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isAlphaNumeric = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isAlphaNumeric( '0xffffff' ); // $ExpectType boolean + isAlphaNumeric( '' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isAlphaNumeric(); // $ExpectError + isAlphaNumeric( undefined, 123 ); // $ExpectError +} diff --git a/is-alphanumeric/docs/usage.txt b/is-alphanumeric/docs/usage.txt new file mode 100644 index 00000000..52af1456 --- /dev/null +++ b/is-alphanumeric/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-alphanumeric [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-alphanumeric/etc/cli_opts.json b/is-alphanumeric/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-alphanumeric/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-alphanumeric/examples/index.js b/is-alphanumeric/examples/index.js new file mode 100644 index 00000000..8da6caa2 --- /dev/null +++ b/is-alphanumeric/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isAlphaNumeric = require( './../lib' ); + +console.log( isAlphaNumeric( 'abs0123456789' ) ); +// => true + +console.log( isAlphaNumeric( '0xffffff' ) ); +// => true + +console.log( isAlphaNumeric( '' ) ); +// => false + +console.log( isAlphaNumeric( 123 ) ); +// => false diff --git a/is-alphanumeric/lib/index.js b/is-alphanumeric/lib/index.js new file mode 100644 index 00000000..d1dd6110 --- /dev/null +++ b/is-alphanumeric/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether a string contains only alphanumeric characters. +* +* @module @stdlib/assert/is-alphanumeric +* +* @example +* var isAlphaNumeric = require( '@stdlib/assert/is-alphanumeric' ); +* +* var out = isAlphaNumeric( 'abc123a' ); +* // returns true +* +* out = isAlphaNumeric( '0xffffff' ); +* // returns true +* +* out = isAlphaNumeric( '' ); +* // returns false +*/ + +// MODULES // + +var isAlphaNumeric = require( './main.js' ); + + +// EXPORTS // + +module.exports = isAlphaNumeric; diff --git a/is-alphanumeric/lib/main.js b/is-alphanumeric/lib/main.js new file mode 100644 index 00000000..bd65cef3 --- /dev/null +++ b/is-alphanumeric/lib/main.js @@ -0,0 +1,92 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; + + +// VARIABLES // + +// Character codes: +var ZERO = 48; +var NINE = 57; +var A = 65; +var Z = 90; +var a = 97; +var z = 122; + + +// MAIN // + +/** +* Tests whether a string contains only alphanumeric characters. +* +* @param {*} x - value to test +* @returns {boolean} boolean indicating if a string contains only alphanumeric characters +* +* @example +* var out = isAlphaNumeric( 'abc123def456' ); +* // returns true +* +* @example +* var out = isAlphaNumeric( '0xffffff' ); +* // returns true +* +* @example +* var out = isAlphaNumeric( '123' ); +* // returns true +* +* @example +* var out = isAlphaNumeric( '' ); +* // returns false +* +* @example +* var out = isAlphaNumeric( 123 ); +* // returns false +*/ +function isAlphaNumeric( x ) { + var len; + var ch; + var i; + if ( !isString( x ) ) { + return false; + } + len = x.length; + if ( len === 0 ) { + return false; + } + for ( i = 0; i < len; i++ ) { + ch = x.charCodeAt( i ); + if ( + (ch < ZERO || ch > NINE) && + (ch < a || ch > z) && + (ch < A || ch > Z) + ) { + return false; + } + } + return true; +} + + +// EXPORTS // + +module.exports = isAlphaNumeric; diff --git a/is-alphanumeric/package.json b/is-alphanumeric/package.json new file mode 100644 index 00000000..14d7df27 --- /dev/null +++ b/is-alphanumeric/package.json @@ -0,0 +1,82 @@ +{ + "name": "@stdlib/assert/is-alphanumeric", + "version": "0.0.0", + "description": "Test whether a string contains only alphanumeric characters.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-alphanumeric": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "string", + "str", + "digit", + "numeric", + "number", + "num", + "isstring", + "isalphanumeric", + "alphanumeric", + "isnumber", + "isnum", + "isletter", + "validate", + "valid", + "isvalid", + "test", + "check" + ] +} diff --git a/is-alphanumeric/test/fixtures/stdin_error.js.txt b/is-alphanumeric/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-alphanumeric/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-alphanumeric/test/test.cli.js b/is-alphanumeric/test/test.cli.js new file mode 100644 index 00000000..554a3f62 --- /dev/null +++ b/is-alphanumeric/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument consists only of alphanumeric characters', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'0abc123456789\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "0123456789\nbeep\n/frank.\n."', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\ntrue\nfalse\nfalse\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-alphanumeric/test/test.js b/is-alphanumeric/test/test.js new file mode 100644 index 00000000..317062cd --- /dev/null +++ b/is-alphanumeric/test/test.js @@ -0,0 +1,121 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isAlphaNumeric = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isAlphaNumeric, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a string containing all alphanumeric characters', function test( t ) { + var values; + var i; + + values = [ + 'a123', + 'b', + 'c', + 'd', + 'e', + 'f456', + '123g', + 'hacd', + 'idc', + '54', + 'j21', + 'kasd', + 'l', + 'm', + 'n', + 'o', + 'pop', + 'q234', + 'r98765', + 's', + 't', + 'u', + 'v', + 'w1234123', + 'sdcasdx', + '354367854y', + 'z', + '6583924A', + 'BFUNFPIO443', + 'C', + 'DRCJENRJNRFWERF', + 'E', + 'F', + 'G', + 'H', + 'I', + 'J', + 'K', + 'L', + 'M', + 'N', + 'O', + 'P', + 'Q', + 'R', + 'S', + 'T', + 'U', + 'V', + 'W', + 'X', + 'Y', + 'Z', + '0123456789' + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isAlphaNumeric( values[i] ), true, 'returns true when provided ' + values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a string containing exclusively alphanumeric characters', function test( t ) { + var values; + var i; + + values = [ + 5, + '6y;', + '.', + null, + NaN, + function noop() { }, + ' ', + new String( '0123456789' ) // eslint-disable-line no-new-wrappers + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isAlphaNumeric( values[i] ), false, 'returns true when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-anagram/README.md b/is-anagram/README.md new file mode 100644 index 00000000..73a9c98e --- /dev/null +++ b/is-anagram/README.md @@ -0,0 +1,160 @@ + + +# isAnagram + +> Test if a value is an [anagram][anagram]. + +
+ +## Usage + +```javascript +var isAnagram = require( '@stdlib/assert/is-anagram' ); +``` + +#### isAnagram( str, value ) + +Tests if a `value` is an [anagram][anagram]. + +```javascript +var str = 'I am a weakish speller'; +var value = 'William Shakespeare'; + +var bool = isAnagram( str, value ); +// returns true +``` + +
+ + + +
+ +## Notes + +- The function does **not** address the presence of [diacritics][diacritics]. +- Only **alphanumeric** characters are considered. +- Capitalization is **ignored**. +- If provided a non-string for the first `argument`, the function throws an `Error`. +- If provided a non-string for the second `argument`, the function returns `false`. + +
+ + + +
+ +## Examples + + + +```javascript +var isAnagram = require( '@stdlib/assert/is-anagram' ); + +var bool = isAnagram( 'I am a weakish speller', 'William Shakespeare' ); +// returns true + +bool = isAnagram( 'bat', 'tab' ); +// returns true + +bool = isAnagram( 'bat', 'TAB' ); +// returns true + +bool = isAnagram( 'bat', 't a b' ); +// returns true + +bool = isAnagram( 'bat 321', 'tab 123' ); +// returns true + +bool = isAnagram( 'bat', 'tabba' ); +// returns false + +bool = isAnagram( 'bat', 5 ); +// returns false + +bool = isAnagram( '123', 321 ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-anagram [options] [] --str= + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --str string Comparison string. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-anagram baz --str=zab +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n 'tab\nbaz' | is-anagram --str=bat +true +false +``` + +
+ + + +
+ + + + + + diff --git a/is-anagram/benchmark/benchmark.js b/is-anagram/benchmark/benchmark.js new file mode 100644 index 00000000..5a201f7f --- /dev/null +++ b/is-anagram/benchmark/benchmark.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isAnagram = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + 'bat', + 'TAB', + 't a b', + 'abbt', + 'bbt', + 5, + null, + void 0, + true, + NaN, + [], + {}, + b.tic, + b.toc + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isAnagram( 'bat', values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-anagram/bin/cli b/is-anagram/bin/cli new file mode 100644 index 00000000..8685abbd --- /dev/null +++ b/is-anagram/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var join = require( 'path' ).join; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isAnagram = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( join( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isAnagram( flags.str, args[ 0 ] ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isAnagram( flags.str, lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-anagram/docs/repl.txt b/is-anagram/docs/repl.txt new file mode 100644 index 00000000..69010fb3 --- /dev/null +++ b/is-anagram/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( str, value ) + Tests if a value is an anagram. + + Parameters + ---------- + str: string + Comparison string. + + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an anagram. + + Examples + -------- + > var str1 = 'I am a weakish speller'; + > var str2 = 'William Shakespeare'; + > var bool = {{alias}}( str1, str2 ) + true + > bool = {{alias}}( 'bat', 'tabba' ) + false + + See Also + -------- + diff --git a/is-anagram/docs/types/index.d.ts b/is-anagram/docs/types/index.d.ts new file mode 100644 index 00000000..d3f22fd0 --- /dev/null +++ b/is-anagram/docs/types/index.d.ts @@ -0,0 +1,41 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an anagram. +* +* @param str - comparison string +* @param x - value to test +* @returns boolean indicating if a value is an anagram +* +* @example +* var bool = isAnagram( 'I am a weakish speller', 'William Shakespeare' ); +* // returns true +* +* @example +* var bool = isAnagram( 'bat', 'tabba' ); +* // returns false +*/ +declare function isAnagram( str: string, x: any ): boolean; + + +// EXPORTS // + +export = isAnagram; diff --git a/is-anagram/docs/types/test.ts b/is-anagram/docs/types/test.ts new file mode 100644 index 00000000..80a88c4f --- /dev/null +++ b/is-anagram/docs/types/test.ts @@ -0,0 +1,45 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isAnagram = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isAnagram( 'I am a weakish speller', 'William Shakespeare' ); // $ExpectType boolean + isAnagram( 'bat', 'tabba' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an invalid first argument... +{ + isAnagram( 5, 'a' ); // $ExpectError + isAnagram( true, 'a' ); // $ExpectError + isAnagram( false, 'a' ); // $ExpectError + isAnagram( null, 'a' ); // $ExpectError + isAnagram( {}, 'a' ); // $ExpectError + isAnagram( [], 'a' ); // $ExpectError + isAnagram( ( x: number ): number => x, 'a' ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isAnagram(); // $ExpectError + isAnagram( 'bat' ); // $ExpectError +} diff --git a/is-anagram/docs/usage.txt b/is-anagram/docs/usage.txt new file mode 100644 index 00000000..0cc6d3c2 --- /dev/null +++ b/is-anagram/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-anagram [options] [] --str= + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --str string Comparison string. diff --git a/is-anagram/etc/cli_opts.json b/is-anagram/etc/cli_opts.json new file mode 100644 index 00000000..4a8e7b5e --- /dev/null +++ b/is-anagram/etc/cli_opts.json @@ -0,0 +1,17 @@ +{ + "string": [ + "str" + ], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-anagram/examples/index.js b/is-anagram/examples/index.js new file mode 100644 index 00000000..9e5eb997 --- /dev/null +++ b/is-anagram/examples/index.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isAnagram = require( './../lib' ); + +var bool = isAnagram( 'I am a weakish speller', 'William Shakespeare' ); +console.log( bool ); +// => true + +bool = isAnagram( 'bat', 'tab' ); +console.log( bool ); +// => true + +bool = isAnagram( 'bat', 'TAB' ); +console.log( bool ); +// => true + +bool = isAnagram( 'bat', 't a b' ); +console.log( bool ); +// => true + +bool = isAnagram( 'bat 321', 'tab 123' ); +console.log( bool ); +// => true + +bool = isAnagram( 'bat', 'tabba' ); +console.log( bool ); +// => false + +bool = isAnagram( 'bat', 5 ); +console.log( bool ); +// => false + +bool = isAnagram( '123', 321 ); +console.log( bool ); +// => false diff --git a/is-anagram/lib/index.js b/is-anagram/lib/index.js new file mode 100644 index 00000000..df147445 --- /dev/null +++ b/is-anagram/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an anagram. +* +* @module @stdlib/assert/is-anagram +* +* @example +* var isAnagram = require( '@stdlib/assert/is-anagram' ); +* +* var bool = isAnagram( 'I am a weakish speller', 'William Shakespeare' ); +* // returns true +* +* bool = isAnagram( 'bat', 'tabba' ); +* // returns false +*/ + +// MODULES // + +var isAnagram = require( './main.js' ); + + +// EXPORTS // + +module.exports = isAnagram; diff --git a/is-anagram/lib/main.js b/is-anagram/lib/main.js new file mode 100644 index 00000000..9f987927 --- /dev/null +++ b/is-anagram/lib/main.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var lowercase = require( '@stdlib/string/lowercase' ); +var replace = require( '@stdlib/string/replace' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; + + +// VARIABLES // + +var RE_NON_ALPHANUMERIC = /[^a-z0-9]/g; + + +// FUNCTIONS // + +/** +* Comparator function for sorting characters in ascending order. +* +* @private +* @param {string} a - character +* @param {string} b - character +* @returns {number} comparison value +*/ +function ascending( a, b ) { + if ( a < b ) { + return -1; + } + if ( a === b ) { + return 0; + } + return 1; +} + + +// MAIN // + +/** +* Tests if a value is an anagram. +* +* @param {string} str - comparison string +* @param {*} x - value to test +* @throws {TypeError} first argument must be a string primitive +* @returns {boolean} boolean indicating if a value is an anagram +* +* @example +* var bool = isAnagram( 'I am a weakish speller', 'William Shakespeare' ); +* // returns true +* +* @example +* var bool = isAnagram( 'bat', 'tabba' ); +* // returns false +*/ +function isAnagram( str, x ) { + if ( !isString( str ) ) { + throw new TypeError( 'invalid argument. First argument must be a string primitive. Value: `' + str + '`.' ); + } + if ( !isString( x ) ) { + return false; + } + str = lowercase( str ); + str = replace( str, RE_NON_ALPHANUMERIC, '' ); + x = lowercase( x ); + x = replace( x, RE_NON_ALPHANUMERIC, '' ); + if ( str.length !== x.length ) { + return false; + } + str = str.split( '' ) + .sort( ascending ) + .join( '' ); + x = x.split( '' ) + .sort( ascending ) + .join( '' ); + return ( str === x ); +} + + +// EXPORTS // + +module.exports = isAnagram; diff --git a/is-anagram/package.json b/is-anagram/package.json new file mode 100644 index 00000000..c196fce6 --- /dev/null +++ b/is-anagram/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-anagram", + "version": "0.0.0", + "description": "Test if a value is an anagram.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-anagram": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "anagram", + "string", + "isstring", + "isanagram", + "type", + "check", + "validate", + "valid", + "test", + "comparison", + "compare" + ] +} diff --git a/is-anagram/test/fixtures/stdin_error.js.txt b/is-anagram/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..72a6a3f3 --- /dev/null +++ b/is-anagram/test/fixtures/stdin_error.js.txt @@ -0,0 +1,34 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; +proc.argv[ 2 ] = '--str=tab'; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-anagram/test/test.cli.js b/is-anagram/test/test.cli.js new file mode 100644 index 00000000..0107bfca --- /dev/null +++ b/is-anagram/test/test.cli.js @@ -0,0 +1,221 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument is an anagram', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'bat\'; process.argv[ 3 ] = \'--str=tab\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "beep\nzba"', + '|', + EXEC_PATH, + fpath, + '--str', + 'baz' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'false\ntrue\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-anagram/test/test.js b/is-anagram/test/test.js new file mode 100644 index 00000000..f55c2f55 --- /dev/null +++ b/is-anagram/test/test.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isAnagram = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isAnagram, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a primitive comparison string', function test( t ) { + var values; + var i; + + values = [ + 5, + null, + undefined, + NaN, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + isAnagram( value ); + }; + } +}); + +tape( 'the function returns `true` if provided an anagram', function test( t ) { + t.strictEqual( isAnagram( 'bat', 'tab' ), true, 'returns true when provided `bat` and `tab`' ); + t.strictEqual( isAnagram( 'bat', 'TaB' ), true, 'returns true when provided `bat` and `TaB`' ); + t.strictEqual( isAnagram( 'bat', 't a b' ), true, 'returns true when provided `bat` and `t a b`' ); + t.strictEqual( isAnagram( 'bat', 'TAB' ), true, 'returns true when provided `bat` and `TAB`' ); + t.strictEqual( isAnagram( 'William Shakespeare', 'I am a weakish speller' ), true, 'returns true when provided `William Shakespeare` and `I am a weakish speller`' ); + t.strictEqual( isAnagram( 'bat 321', 'tab 123' ), true, 'returns true when provided `bat 321` and `tab 123`' ); + + t.end(); +}); + +tape( 'the function returns `false` if not provided an anagram', function test( t ) { + var values; + var i; + + values = [ + 'abbt', + 'bbt', + 5, + null, + undefined, + true, + NaN, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isAnagram( 'bat', values[i] ), false, 'returns false when provided ' + values[i] + ' and `bat`' ); + } + t.end(); +}); diff --git a/is-arguments/README.md b/is-arguments/README.md new file mode 100644 index 00000000..9f40e18e --- /dev/null +++ b/is-arguments/README.md @@ -0,0 +1,96 @@ + + +# isArguments + +> Test if a value is an arguments object. + +
+ +## Usage + +```javascript +var isArguments = require( '@stdlib/assert/is-arguments' ); +``` + +#### isArguments( value ) + +Tests if a `value` is an `arguments` object. + +```javascript +function foo() { + return arguments; +} + +var bool = isArguments( foo() ); +// returns true + +bool = isArguments( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isArguments = require( '@stdlib/assert/is-arguments' ); + +function foo() { + return arguments; +} + +var bool = isArguments( foo() ); +// returns true + +bool = isArguments( [] ); +// returns false + +bool = isArguments( {} ); +// returns false + +bool = isArguments( null ); +// returns false + +bool = isArguments( 'Arguments' ); +// returns false + +function Arguments() { + return this; +} +bool = isArguments( new Arguments() ); +// returns false +``` + +
+ + + + + + diff --git a/is-arguments/benchmark/benchmark.js b/is-arguments/benchmark/benchmark.js new file mode 100644 index 00000000..271450ba --- /dev/null +++ b/is-arguments/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isArguments = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isArguments( [ i, i+1, i+2, i+3, i+4 ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-arguments/docs/repl.txt b/is-arguments/docs/repl.txt new file mode 100644 index 00000000..ceb819a2 --- /dev/null +++ b/is-arguments/docs/repl.txt @@ -0,0 +1,25 @@ + +{{alias}}( value ) + Tests if a value is an arguments object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an arguments object. + + Examples + -------- + > function foo() { return arguments; }; + > var bool = {{alias}}( foo() ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-arguments/docs/types/index.d.ts b/is-arguments/docs/types/index.d.ts new file mode 100644 index 00000000..f08c11d6 --- /dev/null +++ b/is-arguments/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests whether a value is an `arguments` object. +* +* @param value - value to test +* @returns boolean indicating whether a value is an `arguments` object +* +* @example +* function foo() { +* return arguments; +* } +* +* var bool = isArguments( foo() ); +* // returns true +* +* @example +* var bool = isArguments( [] ); +* // returns false +*/ +declare function isArguments( value: any ): boolean; + + +// EXPORTS // + +export = isArguments; diff --git a/is-arguments/docs/types/test.ts b/is-arguments/docs/types/test.ts new file mode 100644 index 00000000..3a00773a --- /dev/null +++ b/is-arguments/docs/types/test.ts @@ -0,0 +1,46 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isArguments = require( './index' ); + + +// FUNCTIONS // + +/** +* Returns the `arguments` object of the function. +* +* @returns arguments +*/ +function foo(): any { + return arguments; +} + + +// TESTS // + +// The function returns a boolean... +{ + isArguments( foo() ); // $ExpectType boolean + isArguments( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isArguments(); // $ExpectError + isArguments( foo(), 123 ); // $ExpectError +} diff --git a/is-arguments/examples/index.js b/is-arguments/examples/index.js new file mode 100644 index 00000000..6a20ff03 --- /dev/null +++ b/is-arguments/examples/index.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isArguments = require( './../lib' ); + +function foo() { + return arguments; +} + +var bool = isArguments( foo() ); +console.log( bool ); +// => true + +bool = isArguments( [] ); +console.log( bool ); +// => false + +bool = isArguments( {} ); +console.log( bool ); +// => false + +bool = isArguments( null ); +console.log( bool ); +// => false + +bool = isArguments( 'Arguments' ); +console.log( bool ); +// => false + +function Arguments() { + return this; +} +bool = isArguments( new Arguments() ); +console.log( bool ); +// => false diff --git a/is-arguments/lib/detect.js b/is-arguments/lib/detect.js new file mode 100644 index 00000000..c7df5bab --- /dev/null +++ b/is-arguments/lib/detect.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isArguments = require( './main.js' ); + + +// VARIABLES // + +var bool; + + +// FUNCTIONS // + +/** +* Detects whether an environment returns the expected internal class of the `arguments` object. +* +* @private +* @returns {boolean} boolean indicating whether an environment behaves as expected +* +* @example +* var bool = detect(); +* // returns +*/ +function detect() { + return isArguments( arguments ); +} + + +// MAIN // + +bool = detect(); + + +// EXPORTS // + +module.exports = bool; diff --git a/is-arguments/lib/index.js b/is-arguments/lib/index.js new file mode 100644 index 00000000..025d346c --- /dev/null +++ b/is-arguments/lib/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an `arguments` object. +* +* @module @stdlib/assert/is-arguments +* +* @example +* var isArguments = require( '@stdlib/assert/is-arguments' ); +* +* function foo() { +* return arguments; +* } +* +* var bool = isArguments( foo() ); +* // returns true +* +* bool = isArguments( [] ); +* // returns false +*/ + +// MODULES // + +var hasArgumentsClass = require( './detect.js' ); +var main = require( './main.js' ); +var polyfill = require( './polyfill.js' ); + + +// MAIN // + +var isArguments; +if ( hasArgumentsClass ) { + isArguments = main; +} else { + isArguments = polyfill; +} + + +// EXPORTS // + +module.exports = isArguments; diff --git a/is-arguments/lib/main.js b/is-arguments/lib/main.js new file mode 100644 index 00000000..8df81652 --- /dev/null +++ b/is-arguments/lib/main.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// MAIN // + +/** +* Tests whether a value is an `arguments` object. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is an `arguments` object +* +* @example +* function foo() { +* return arguments; +* } +* +* var bool = isArguments( foo() ); +* // returns true +* +* @example +* var bool = isArguments( [] ); +* // returns false +*/ +function isArguments( value ) { + return ( nativeClass( value ) === '[object Arguments]' ); +} + + +// EXPORTS // + +module.exports = isArguments; diff --git a/is-arguments/lib/polyfill.js b/is-arguments/lib/polyfill.js new file mode 100644 index 00000000..70cfc69e --- /dev/null +++ b/is-arguments/lib/polyfill.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isEnumerableProperty = require( '@stdlib/assert/is-enumerable-property' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isInteger = require( '@stdlib/math/base/assert/is-integer' ); +var MAX_LENGTH = require( '@stdlib/constants/math/uint32-max' ); + + +// MAIN // + +/** +* Tests whether a value is an `arguments` object. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is an `arguments` object +* +* @example +* function foo() { +* return arguments; +* } +* +* var bool = isArguments( foo() ); +* // returns true +* +* @example +* var bool = isArguments( [] ); +* // returns false +*/ +function isArguments( value ) { + return ( + value !== null && + typeof value === 'object' && + !isArray( value ) && + typeof value.length === 'number' && + isInteger( value.length ) && + value.length >= 0 && + value.length <= MAX_LENGTH && + hasOwnProp( value, 'callee' ) && + !isEnumerableProperty( value, 'callee' ) + ); +} + + +// EXPORTS // + +module.exports = isArguments; diff --git a/is-arguments/package.json b/is-arguments/package.json new file mode 100644 index 00000000..695e0a44 --- /dev/null +++ b/is-arguments/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-arguments", + "version": "0.0.0", + "description": "Test if a value is an arguments object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "arguments", + "args", + "isarguments", + "array-like", + "object", + "obj", + "is", + "is-arguments", + "type", + "check", + "validate", + "valid", + "test" + ] +} diff --git a/is-arguments/test/test.js b/is-arguments/test/test.js new file mode 100644 index 00000000..2e4d5769 --- /dev/null +++ b/is-arguments/test/test.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var isArguments = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isArguments, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an `arguments` object', function test( t ) { + t.strictEqual( isArguments( arguments ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an `arguments` object', function test( t ) { + var values; + var i; + + function Arguments() { + return this; + } + + values = [ + '5', + 5, + NaN, + null, + undefined, + true, + [], + {}, + { 'length': 3.14 }, + { 'length': -1 }, + { 'length': '5' }, + function noop() {}, + new Arguments(), + Array.prototype.slice.call( arguments ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isArguments( values[i] ), false, 'returns false for when provided ' + values[i] ); + } + t.end(); +}); + +tape( 'the function uses a polyfill for environments which return an unexpected internal class', function test( t ) { + var isArguments; + var values; + var i; + + function Arguments() { + return this; + } + + isArguments = proxyquire( './../lib/index.js', { + './detect.js': false + }); + + t.strictEqual( isArguments( arguments ), true, 'returns true when provided an `arguments` object' ); + + values = [ + '5', + 5, + NaN, + null, + undefined, + true, + [], + {}, + { 'length': 3.14 }, + { 'length': -1 }, + { 'length': '5' }, + function noop() {}, + new Arguments(), + Array.prototype.slice.call( arguments ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isArguments( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-array-array/README.md b/is-array-array/README.md new file mode 100644 index 00000000..df1af376 --- /dev/null +++ b/is-array-array/README.md @@ -0,0 +1,94 @@ + + +# isArrayArray + +> Test if a value is an array of arrays. + +
+ +## Usage + +```javascript +var isArrayArray = require( '@stdlib/assert/is-array-array' ); +``` + +#### isArrayArray( value ) + +Tests if a `value` is an `array` of `arrays`. + +```javascript +var bool = isArrayArray( [ [], [] ] ); +// returns true + +bool = isArrayArray( [ {}, {} ] ); +// returns false + +bool = isArrayArray( [] ); +// returns false + +bool = isArrayArray( {} ); +// returns false + +bool = isArrayArray( [ null, {} ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isArrayArray = require( '@stdlib/assert/is-array-array' ); + +var bool = isArrayArray( [ [], [], [] ] ); +// returns true + +bool = isArrayArray( [ [], {} ] ); +// returns false + +bool = isArrayArray( [] ); +// returns false + +bool = isArrayArray( {} ); +// returns false + +bool = isArrayArray( [ 'a', 'b' ] ); +// returns false + +bool = isArrayArray( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-array-array/benchmark/benchmark.js b/is-array-array/benchmark/benchmark.js new file mode 100644 index 00000000..29ba22f3 --- /dev/null +++ b/is-array-array/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isArrayArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( [ i ] ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i; + bool = isArrayArray( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/is-array-array/docs/repl.txt b/is-array-array/docs/repl.txt new file mode 100644 index 00000000..f511bbdb --- /dev/null +++ b/is-array-array/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( value ) + Tests if a value is an array of arrays. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array of arrays. + + Examples + -------- + > var bool = {{alias}}( [ [], [] ] ) + true + > bool = {{alias}}( [ {}, {} ] ) + false + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-array-array/docs/types/index.d.ts b/is-array-array/docs/types/index.d.ts new file mode 100644 index 00000000..a7a91b65 --- /dev/null +++ b/is-array-array/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an array of arrays. +* +* @param value - value to test +* @returns boolean indicating whether a value is an array of arrays +* +* @example +* var bool = isArrayArray( [ [], [] ] ); +* // returns true +* +* bool = isArrayArray( [ {}, {} ] ); +* // returns false +* +* bool = isArrayArray( [] ); +* // returns false +*/ +declare function isArrayArray( value: any ): boolean; + + +// EXPORTS // + +export = isArrayArray; diff --git a/is-array-array/docs/types/test.ts b/is-array-array/docs/types/test.ts new file mode 100644 index 00000000..3043bd1a --- /dev/null +++ b/is-array-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isArrayArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isArrayArray( {} ); // $ExpectType boolean + isArrayArray( [ [], [] ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isArrayArray(); // $ExpectError + isArrayArray( [ [], [] ], 123 ); // $ExpectError +} diff --git a/is-array-array/examples/index.js b/is-array-array/examples/index.js new file mode 100644 index 00000000..b1055268 --- /dev/null +++ b/is-array-array/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isArrayArray = require( './../lib' ); + +var bool = isArrayArray( [ [], [], [] ] ); +console.log( bool ); +// => true + +bool = isArrayArray( [ [], {} ] ); +console.log( bool ); +// => false + +bool = isArrayArray( [] ); +console.log( bool ); +// => false + +bool = isArrayArray( {} ); +console.log( bool ); +// => false + +bool = isArrayArray( [ 'a', 'b' ] ); +console.log( bool ); +// => false + +bool = isArrayArray( null ); +console.log( bool ); +// => false diff --git a/is-array-array/lib/index.js b/is-array-array/lib/index.js new file mode 100644 index 00000000..53856145 --- /dev/null +++ b/is-array-array/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array of arrays. +* +* @module @stdlib/assert/is-array-array +* +* @example +* var isArrayArray = require( '@stdlib/assert/is-array-array' ); +* +* var bool = isArrayArray( [ [], [] ] ); +* // returns true +* +* bool = isArrayArray( [ {}, {} ] ); +* // returns false +* +* bool = isArrayArray( [] ); +* // returns false +*/ + +// MODULES // + +var isArrayArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isArrayArray; diff --git a/is-array-array/lib/main.js b/is-array-array/lib/main.js new file mode 100644 index 00000000..dbb8fa75 --- /dev/null +++ b/is-array-array/lib/main.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var arrayfun = require( '@stdlib/assert/tools/array-function' ); +var isArray = require( '@stdlib/assert/is-array' ); + + +// MAIN // + +/** +* Tests if a value is an array of arrays. +* +* @name isArrayArray +* @type {Function} +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is an array of arrays +* +* @example +* var bool = isArrayArray( [ [], [] ] ); +* // returns true +* +* bool = isArrayArray( [ {}, {} ] ); +* // returns false +* +* bool = isArrayArray( [] ); +* // returns false +*/ +var isArrayArray = arrayfun( isArray ); + + +// EXPORTS // + +module.exports = isArrayArray; diff --git a/is-array-array/package.json b/is-array-array/package.json new file mode 100644 index 00000000..3f79a611 --- /dev/null +++ b/is-array-array/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-array-array", + "version": "0.0.0", + "description": "Test if a value is an array of arrays.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "array", + "is", + "isarray", + "arrays", + "array-of-arrays", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-array-array/test/test.js b/is-array-array/test/test.js new file mode 100644 index 00000000..9212815a --- /dev/null +++ b/is-array-array/test/test.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isArrayArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isArrayArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array of arrays', function test( t ) { + var arr; + + arr = [ [] ]; + t.strictEqual( isArrayArray( arr ), true, 'returns true' ); + + arr = [ [], [], [] ]; + t.strictEqual( isArrayArray( arr ), true, 'returns true' ); + + arr = [ {} ]; + t.strictEqual( isArrayArray( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': [], + '1': [] + }; + t.strictEqual( isArrayArray( arr ), false, 'returns false' ); + + arr = []; + t.strictEqual( isArrayArray( arr ), false, 'returns false when provided an empty array' ); + + arr = {}; + t.strictEqual( isArrayArray( arr ), false, 'returns false' ); + + arr = null; + t.strictEqual( isArrayArray( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-array-length/README.md b/is-array-length/README.md new file mode 100644 index 00000000..4f61d7e2 --- /dev/null +++ b/is-array-length/README.md @@ -0,0 +1,115 @@ + + +# isArrayLength + +> Test if a value is a valid array length. + +
+ +## Usage + +```javascript +var isArrayLength = require( '@stdlib/assert/is-array-length' ); +``` + +#### isArrayLength( value ) + +Tests if a value is a valid `array` length. + +```javascript +var bool = isArrayLength( 5 ); +// returns true + +bool = isArrayLength( -1 ); +// returns false + +bool = isArrayLength( 2.0e200 ); +// returns false + +bool = isArrayLength( 3.14 ); +// returns false + +bool = isArrayLength( null ); +// returns false +``` + +
+ + + +
+ +## Notes + +- A valid `length` property for an [`Array`][mdn-array] is any integer value on the interval `[0, 2^32-1]`. + +
+ + + +
+ +## Examples + + + +```javascript +var isArrayLength = require( '@stdlib/assert/is-array-length' ); + +var bool = isArrayLength( 5 ); +// returns true + +bool = isArrayLength( 0 ); +// returns true + +bool = isArrayLength( 2.0e200 ); +// returns false + +bool = isArrayLength( 5.256 ); +// returns false + +bool = isArrayLength( 1.0/0.0 ); +// returns false + +bool = isArrayLength( -1.0/0.0 ); +// returns false + +bool = isArrayLength( NaN ); +// returns false + +bool = isArrayLength( '5' ); +// returns false + +bool = isArrayLength( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-array-length/benchmark/benchmark.js b/is-array-length/benchmark/benchmark.js new file mode 100644 index 00000000..c61e0a49 --- /dev/null +++ b/is-array-length/benchmark/benchmark.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isArrayLength = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 2e200, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isArrayLength( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-array-length/docs/repl.txt b/is-array-length/docs/repl.txt new file mode 100644 index 00000000..85d2b058 --- /dev/null +++ b/is-array-length/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( value ) + Tests if a value is a valid array length. + + A valid length property for an Array instance is any integer value on the + interval [0, 2^32-1]. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a valid array length. + + Examples + -------- + > var bool = {{alias}}( 5 ) + true + > bool = {{alias}}( 2.0e200 ) + false + > bool = {{alias}}( -3.14 ) + false + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-array-length/docs/types/index.d.ts b/is-array-length/docs/types/index.d.ts new file mode 100644 index 00000000..95a0ff07 --- /dev/null +++ b/is-array-length/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a valid array length. +* +* ## Notes +* +* - A valid length property for an Array instance is any integer value on the interval `[0, 2^32-1]`. +* +* @param value - value to test +* @returns boolean indicating if a value is a valid array length +* +* @example +* var bool = isArrayLength( 3 ); +* // returns true +* +* @example +* var bool = isArrayLength( 3.14 ); +* // returns false +*/ +declare function isArrayLength( value: any ): boolean; + + +// EXPORTS // + +export = isArrayLength; diff --git a/is-array-length/docs/types/test.ts b/is-array-length/docs/types/test.ts new file mode 100644 index 00000000..10633c94 --- /dev/null +++ b/is-array-length/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isArrayLength = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isArrayLength( 3 ); // $ExpectType boolean + isArrayLength( 3.14 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isArrayLength(); // $ExpectError + isArrayLength( 9, 123 ); // $ExpectError +} diff --git a/is-array-length/examples/index.js b/is-array-length/examples/index.js new file mode 100644 index 00000000..66bd4c7c --- /dev/null +++ b/is-array-length/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isArrayLength = require( './../lib' ); + +console.log( isArrayLength( 5 ) ); +// => true + +console.log( isArrayLength( 0 ) ); +// => true + +console.log( isArrayLength( 3.0e200 ) ); +// => false + +console.log( isArrayLength( 5.256 ) ); +// => false + +console.log( isArrayLength( 1.0/0.0 ) ); +// => false + +console.log( isArrayLength( -1.0/0.0 ) ); +// => false + +console.log( isArrayLength( NaN ) ); +// => false + +console.log( isArrayLength( '5' ) ); +// => false + +console.log( isArrayLength( null ) ); +// => false diff --git a/is-array-length/lib/index.js b/is-array-length/lib/index.js new file mode 100644 index 00000000..7b5fd2fb --- /dev/null +++ b/is-array-length/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a valid array length. +* +* @module @stdlib/assert/is-array-length +* +* @example +* var isArrayLength = require( '@stdlib/assert/is-array-length' ); +* +* var bool = isArrayLength( 5 ); +* // returns true +* +* bool = isArrayLength( 2.0e200 ); +* // returns false +* +* bool = isArrayLength( -3.14 ); +* // returns false +* +* bool = isArrayLength( null ); +* // returns false +*/ + +// MODULES // + +var isArrayLength = require( './main.js' ); + + +// EXPORTS // + +module.exports = isArrayLength; diff --git a/is-array-length/lib/main.js b/is-array-length/lib/main.js new file mode 100644 index 00000000..c93b69a6 --- /dev/null +++ b/is-array-length/lib/main.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var MAX_ARRAY_LENGTH = require( '@stdlib/constants/array/max-array-length' ); + + +// MAIN // + +/** +* Tests if a value is a valid array length. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a valid array length +* +* @example +* var bool = isArrayLength( 3 ); +* // returns true +* +* @example +* var bool = isArrayLength( 3.14 ); +* // returns false +*/ +function isArrayLength( value ) { + return ( + isInteger( value ) && + value >= 0 && + value <= MAX_ARRAY_LENGTH + ); +} + + +// EXPORTS // + +module.exports = isArrayLength; diff --git a/is-array-length/package.json b/is-array-length/package.json new file mode 100644 index 00000000..e6dc81ba --- /dev/null +++ b/is-array-length/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-array-length", + "version": "0.0.0", + "description": "Test if a value is a valid array length.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "array", + "arr", + "length", + "is", + "islength", + "type", + "check", + "validate", + "isvalid", + "valid", + "test" + ] +} diff --git a/is-array-length/test/test.js b/is-array-length/test/test.js new file mode 100644 index 00000000..47c4f0c4 --- /dev/null +++ b/is-array-length/test/test.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var MAX_ARRAY_LENGTH = require( '@stdlib/constants/array/max-array-length' ); +var isArrayLength = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isArrayLength, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a valid array length', function test( t ) { + t.strictEqual( isArrayLength( 0 ), true, 'returns true' ); + t.strictEqual( isArrayLength( 10 ), true, 'returns true' ); + t.strictEqual( isArrayLength( MAX_ARRAY_LENGTH ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a valid array length', function test( t ) { + t.strictEqual( isArrayLength( MAX_ARRAY_LENGTH+1 ), false, 'returns false' ); + t.strictEqual( isArrayLength( -1 ), false, 'returns false' ); + t.strictEqual( isArrayLength( 3.14 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + NaN, + null, + true, + false, + undefined, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isArrayLength( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-array-like-object/README.md b/is-array-like-object/README.md new file mode 100644 index 00000000..8cfbbb2a --- /dev/null +++ b/is-array-like-object/README.md @@ -0,0 +1,116 @@ + + +# isArrayLikeObject + +> Test if a value is an array-like object. + +
+ +## Usage + +```javascript +var isArrayLikeObject = require( '@stdlib/assert/is-array-like-object' ); +``` + +#### isArrayLikeObject( value ) + +Tests if a value is an [array-like][array-like] `object`. + + + +```javascript +var bool = isArrayLikeObject( [] ); +// returns true + +bool = isArrayLikeObject( { 'length': 10 } ); +// returns true +``` + +If provided a `string`, the function returns `false`. + +```javascript +var bool = isArrayLikeObject( 'beep' ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var isArrayLikeObject = require( '@stdlib/assert/is-array-like-object' ); + +var bool = isArrayLikeObject( { 'length': 10 } ); +// returns true + +bool = isArrayLikeObject( [] ); +// returns true + +bool = isArrayLikeObject( new Float64Array( 10 ) ); +// returns true + +bool = (function test() { + return isArrayLikeObject( arguments ); +})(); +// returns true + +bool = isArrayLikeObject( 'beep' ); +// returns false + +bool = isArrayLikeObject( null ); +// returns false + +bool = isArrayLikeObject( void 0 ); +// returns false + +bool = isArrayLikeObject( 5 ); +// returns false + +bool = isArrayLikeObject( true ); +// returns false + +bool = isArrayLikeObject( {} ); +// returns false + +bool = isArrayLikeObject( function noop() {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-array-like-object/benchmark/benchmark.js b/is-array-like-object/benchmark/benchmark.js new file mode 100644 index 00000000..2c34cbe2 --- /dev/null +++ b/is-array-like-object/benchmark/benchmark.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isArrayLikeObject = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::array', function benchmark( b ) { + var bool; + var obj; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = [ i, i+1 ]; + bool = isArrayLikeObject( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::typed_array', function benchmark( b ) { + var values; + var bool; + var obj; + var N; + var i; + + values = [ + new Float64Array( [ 1.0, 2.0 ] ), + new Float64Array( [ 3.0, 4.0 ] ) + ]; + N = values.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = values[ i%N ]; + bool = isArrayLikeObject( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array_like_object', function benchmark( b ) { + var bool; + var obj; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = { + 'length': 2, + '0': i, + '1': i + 1 + }; + bool = isArrayLikeObject( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-array-like-object/docs/repl.txt b/is-array-like-object/docs/repl.txt new file mode 100644 index 00000000..9f10c9f1 --- /dev/null +++ b/is-array-like-object/docs/repl.txt @@ -0,0 +1,28 @@ + +{{alias}}( value ) + Tests if a value is an array-like object. + + If provided a string, the function returns `false`. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an array-like object. + + Examples + -------- + > var bool = {{alias}}( [] ) + true + > bool = {{alias}}( { 'length': 10 } ) + true + > bool = {{alias}}( 'beep' ) + false + + See Also + -------- + diff --git a/is-array-like-object/docs/types/index.d.ts b/is-array-like-object/docs/types/index.d.ts new file mode 100644 index 00000000..b8c8d5bd --- /dev/null +++ b/is-array-like-object/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an array-like object. +* +* ## Notes +* +* - If provided a string, the function returns `false`. +* +* @param value - value to test +* @returns boolean indicating if a value is an array-like object +* +* @example +* var bool = isArrayLikeObject( [] ); +* // returns true +* +* @example +* var bool = isArrayLikeObject( { 'length':10 } ); +* // returns true +* +* @example +* var bool = isArrayLikeObject( 'beep' ); +* // returns false +*/ +declare function isArrayLike( value: any ): boolean; + + +// EXPORTS // + +export = isArrayLike; diff --git a/is-array-like-object/docs/types/test.ts b/is-array-like-object/docs/types/test.ts new file mode 100644 index 00000000..ff2f5479 --- /dev/null +++ b/is-array-like-object/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isArrayLikeObject = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isArrayLikeObject( [] ); // $ExpectType boolean + isArrayLikeObject( 'beep' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isArrayLikeObject(); // $ExpectError + isArrayLikeObject( [], 123 ); // $ExpectError +} diff --git a/is-array-like-object/examples/index.js b/is-array-like-object/examples/index.js new file mode 100644 index 00000000..c80e5228 --- /dev/null +++ b/is-array-like-object/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline, no-empty-function, no-restricted-syntax */ + +'use strict'; + +var Float64Array = require( '@stdlib/array/float64' ); +var isArrayLikeObject = require( './../lib' ); + +console.log( isArrayLikeObject( { 'length': 10 } ) ); +// => true + +console.log( isArrayLikeObject( [] ) ); +// => true + +console.log( isArrayLikeObject( new Float64Array( 10 ) ) ); +// => true + +console.log( (function test() { + return isArrayLikeObject( arguments ); +})() ); +// => true + +console.log( isArrayLikeObject( 'beep' ) ); +// => false + +console.log( isArrayLikeObject( null ) ); +// => false + +console.log( isArrayLikeObject( void 0 ) ); +// => false + +console.log( isArrayLikeObject( 5 ) ); +// => false + +console.log( isArrayLikeObject( true ) ); +// => false + +console.log( isArrayLikeObject( {} ) ); +// => false + +console.log( isArrayLikeObject( function noop() {} ) ); +// => false diff --git a/is-array-like-object/lib/index.js b/is-array-like-object/lib/index.js new file mode 100644 index 00000000..8211f884 --- /dev/null +++ b/is-array-like-object/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object. +* +* @module @stdlib/assert/is-array-like-object +* +* @example +* var isArrayLikeObject = require( '@stdlib/assert/is-array-like-object' ); +* +* var bool = isArrayLikeObject( [] ); +* // returns true +* +* bool = isArrayLikeObject( { 'length':10 } ); +* // returns true +* +* bool = isArrayLikeObject( 'beep' ); +* // returns false +*/ + +// MODULES // + +var isArrayLikeObject = require( './main.js' ); + + +// EXPORTS // + +module.exports = isArrayLikeObject; diff --git a/is-array-like-object/lib/main.js b/is-array-like-object/lib/main.js new file mode 100644 index 00000000..9f32eacb --- /dev/null +++ b/is-array-like-object/lib/main.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/math/base/assert/is-integer' ); +var MAX_LENGTH = require( '@stdlib/constants/array/max-array-length' ); + + +// MAIN // + +/** +* Tests if a value is an array-like object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is an array-like object +* +* @example +* var bool = isArrayLikeObject( [] ); +* // returns true +* +* @example +* var bool = isArrayLikeObject( { 'length':10 } ); +* // returns true +* +* @example +* var bool = isArrayLikeObject( 'beep' ); +* // returns false +*/ +function isArrayLikeObject( value ) { + return ( + typeof value === 'object' && + value !== null && + typeof value.length === 'number' && + isInteger( value.length ) && + value.length >= 0 && + value.length <= MAX_LENGTH + ); +} + + +// EXPORTS // + +module.exports = isArrayLikeObject; diff --git a/is-array-like-object/package.json b/is-array-like-object/package.json new file mode 100644 index 00000000..7a295e40 --- /dev/null +++ b/is-array-like-object/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-array-like-object", + "version": "0.0.0", + "description": "Test if a value is an array-like object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "array", + "array-like", + "is", + "isarraylike", + "object", + "obj", + "type", + "check", + "test", + "validate", + "isvalid" + ] +} diff --git a/is-array-like-object/test/test.js b/is-array-like-object/test/test.js new file mode 100644 index 00000000..2f36f0b5 --- /dev/null +++ b/is-array-like-object/test/test.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline, no-unused-vars */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var MAX_ARRAY_LENGTH = require( '@stdlib/constants/array/max-array-length' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isArrayLikeObject = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isArrayLikeObject, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an array-like object', function test( t ) { + var values; + var i; + + values = [ + [], + { 'length': 10 }, + new Float64Array( 10 ), + arguments + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isArrayLikeObject( values[i] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided an array-like object whose length exceeds the maximum array length', function test( t ) { + var o = { 'length': MAX_ARRAY_LENGTH+1 }; + t.strictEqual( isArrayLikeObject( o ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided an array-like object whose length property is not an integer', function test( t ) { + var o = { 'length': 3.14 }; + t.strictEqual( isArrayLikeObject( o ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided an array-like object whose length property is a negative integer', function test( t ) { + var o = { 'length': -1 }; + t.strictEqual( isArrayLikeObject( o ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an array-like object', function test( t ) { + var values; + var i; + + values = [ + 'beep', + 5, + null, + undefined, + NaN, + true, + false, + {}, + function boop( a, b, c ) {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isArrayLikeObject( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-array-like/README.md b/is-array-like/README.md new file mode 100644 index 00000000..00d406d9 --- /dev/null +++ b/is-array-like/README.md @@ -0,0 +1,112 @@ + + +# isArrayLike + +> Test if a value is array-like. + +
+ +## Usage + +```javascript +var isArrayLike = require( '@stdlib/assert/is-array-like' ); +``` + +#### isArrayLike( value ) + +Tests if a value is [array-like][array-like]. + + + +```javascript +var bool = isArrayLike( [] ); +// returns true + +bool = isArrayLike( { 'length': 10 } ); +// returns true +``` + +If provided a `string`, the function returns `true`. + +```javascript +var bool = isArrayLike( 'beep' ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isArrayLike = require( '@stdlib/assert/is-array-like' ); + +var bool = isArrayLike( { 'length': 10 } ); +// returns true + +bool = isArrayLike( [] ); +// returns true + +bool = isArrayLike( 'beep' ); +// returns true + +bool = (function test() { + return isArrayLike( arguments ); +})(); +// returns true + +bool = isArrayLike( null ); +// returns false + +bool = isArrayLike( void 0 ); +// returns false + +bool = isArrayLike( 5 ); +// returns false + +bool = isArrayLike( true ); +// returns false + +bool = isArrayLike( {} ); +// returns false + +bool = isArrayLike( function noop() {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-array-like/benchmark/benchmark.js b/is-array-like/benchmark/benchmark.js new file mode 100644 index 00000000..b79be7dd --- /dev/null +++ b/is-array-like/benchmark/benchmark.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isArrayLike = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::array', function benchmark( b ) { + var bool; + var obj; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = [ i, i+1 ]; + bool = isArrayLike( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::typed_array', function benchmark( b ) { + var values; + var bool; + var obj; + var N; + var i; + + values = [ + new Float64Array( [ 1.0, 2.0 ] ), + new Float64Array( [ 3.0, 4.0 ] ) + ]; + N = values.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = values[ i%N ]; + bool = isArrayLike( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array_like_object', function benchmark( b ) { + var bool; + var obj; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = { + 'length': 2, + '0': i, + '1': i + 1 + }; + bool = isArrayLike( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-array-like/docs/repl.txt b/is-array-like/docs/repl.txt new file mode 100644 index 00000000..74ff0881 --- /dev/null +++ b/is-array-like/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( value ) + Tests if a value is array-like. + + If provided a string, the function returns `true`. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is array-like. + + Examples + -------- + > var bool = {{alias}}( [] ) + true + > bool = {{alias}}( { 'length': 10 } ) + true + > bool = {{alias}}( 'beep' ) + true + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-array-like/docs/types/index.d.ts b/is-array-like/docs/types/index.d.ts new file mode 100644 index 00000000..67d90bf5 --- /dev/null +++ b/is-array-like/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is array-like. +* +* ## Notes +* +* - If provided a string, the function returns `true`. +* +* @param value - value to test +* @returns boolean indicating if a value is array-like +* +* @example +* var bool = isArrayLike( [] ); +* // returns true +* +* @example +* var bool = isArrayLike( {'length':10} ); +* // returns true +*/ +declare function isArrayLike( value: any ): boolean; + + +// EXPORTS // + +export = isArrayLike; diff --git a/is-array-like/docs/types/test.ts b/is-array-like/docs/types/test.ts new file mode 100644 index 00000000..47b6349b --- /dev/null +++ b/is-array-like/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isArrayLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isArrayLike( [] ); // $ExpectType boolean + isArrayLike( {} ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isArrayLike(); // $ExpectError + isArrayLike( [], 123 ); // $ExpectError +} diff --git a/is-array-like/examples/index.js b/is-array-like/examples/index.js new file mode 100644 index 00000000..bd7e8e05 --- /dev/null +++ b/is-array-like/examples/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline, object-curly-spacing, no-empty-function, no-restricted-syntax */ + +'use strict'; + +var isArrayLike = require( './../lib' ); + +console.log( isArrayLike( {'length': 10} ) ); +// => true + +console.log( isArrayLike( [] ) ); +// => true + +console.log( isArrayLike( 'beep' ) ); +// => true + +console.log( (function test() { + return isArrayLike( arguments ); +})() ); +// => true + +console.log( isArrayLike( null ) ); +// => false + +console.log( isArrayLike( void 0 ) ); +// => false + +console.log( isArrayLike( 5 ) ); +// => false + +console.log( isArrayLike( true ) ); +// => false + +console.log( isArrayLike( {} ) ); +// => false + +console.log( isArrayLike( function noop() {} ) ); +// => false diff --git a/is-array-like/lib/index.js b/is-array-like/lib/index.js new file mode 100644 index 00000000..640822af --- /dev/null +++ b/is-array-like/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is array-like. +* +* @module @stdlib/assert/is-array-like +* +* @example +* var isArrayLike = require( '@stdlib/assert/is-array-like' ); +* +* var bool = isArrayLike( [] ); +* // returns true +* +* bool = isArrayLike( { 'length': 10 } ); +* // returns true +* +* bool = isArrayLike( 'beep' ); +* // returns true +*/ + +// MODULES // + +var isArrayLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isArrayLike; diff --git a/is-array-like/lib/main.js b/is-array-like/lib/main.js new file mode 100644 index 00000000..3513cc4c --- /dev/null +++ b/is-array-like/lib/main.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/math/base/assert/is-integer' ); +var MAX_LENGTH = require( '@stdlib/constants/array/max-array-length' ); + + +// MAIN // + +/** +* Tests if a value is array-like. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is array-like +* +* @example +* var bool = isArrayLike( [] ); +* // returns true +* +* @example +* var bool = isArrayLike( {'length':10} ); +* // returns true +*/ +function isArrayLike( value ) { + return ( + value !== void 0 && + value !== null && + typeof value !== 'function' && + typeof value.length === 'number' && + isInteger( value.length ) && + value.length >= 0 && + value.length <= MAX_LENGTH + ); +} + + +// EXPORTS // + +module.exports = isArrayLike; diff --git a/is-array-like/package.json b/is-array-like/package.json new file mode 100644 index 00000000..e069534f --- /dev/null +++ b/is-array-like/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/assert/is-array-like", + "version": "0.0.0", + "description": "Test if a value is array-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "array", + "array-like", + "is", + "isarraylike", + "type", + "check" + ] +} diff --git a/is-array-like/test/test.js b/is-array-like/test/test.js new file mode 100644 index 00000000..03e3f1af --- /dev/null +++ b/is-array-like/test/test.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline, no-unused-vars */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var MAX_ARRAY_LENGTH = require( '@stdlib/constants/array/max-array-length' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isArrayLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isArrayLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an array-like value', function test( t ) { + var values; + var i; + + values = [ + [], + { 'length': 10 }, + new Float64Array( 10 ), + 'beep', + arguments + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isArrayLike( values[i] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided an array-like value whose length exceeds the maximum array length', function test( t ) { + var o = { 'length': MAX_ARRAY_LENGTH+1 }; + t.equal( isArrayLike( o ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided an array-like value whose length property is not an integer', function test( t ) { + var o = { 'length': 3.14 }; + t.equal( isArrayLike( o ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided an array-like value whose length property is a negative integer', function test( t ) { + var o = { 'length': -1 }; + t.equal( isArrayLike( o ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an array-like value', function test( t ) { + var values; + var i; + + values = [ + 5, + null, + undefined, + NaN, + true, + false, + {}, + function boop( a, b, c ) {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isArrayLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-array/README.md b/is-array/README.md new file mode 100644 index 00000000..e297b1a3 --- /dev/null +++ b/is-array/README.md @@ -0,0 +1,70 @@ + + +# isArray + +> Test if a value is an array. + +
+ +## Usage + +```javascript +var isArray = require( '@stdlib/assert/is-array' ); +``` + +#### isArray( value ) + +Tests if a value is an `array`. + +```javascript +var bool = isArray( [] ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isArray = require( '@stdlib/assert/is-array' ); + +var bool = isArray( [ 1, 2, 3, 4 ] ); +// returns true + +bool = isArray( {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-array/benchmark/benchmark.js b/is-array/benchmark/benchmark.js new file mode 100644 index 00000000..5003507c --- /dev/null +++ b/is-array/benchmark/benchmark.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isArray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::array', function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isArray( [ i, i+1 ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-array/docs/repl.txt b/is-array/docs/repl.txt new file mode 100644 index 00000000..fec81a5a --- /dev/null +++ b/is-array/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is an array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array. + + Examples + -------- + > var bool = {{alias}}( [] ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-array/docs/types/index.d.ts b/is-array/docs/types/index.d.ts new file mode 100644 index 00000000..ccd47a56 --- /dev/null +++ b/is-array/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an array. +* +* @param value - value to test +* @returns boolean indicating whether value is an array +* +* @example +* var bool = isArray( [] ); +* // returns true +* +* @example +* var bool = isArray( {} ); +* // returns false +*/ +declare function isArray( value: any ): boolean; + + +// EXPORTS // + +export = isArray; diff --git a/is-array/docs/types/test.ts b/is-array/docs/types/test.ts new file mode 100644 index 00000000..ca9056a0 --- /dev/null +++ b/is-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isArray( {} ); // $ExpectType boolean + isArray( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isArray(); // $ExpectError + isArray( [], 123 ); // $ExpectError +} diff --git a/is-array/examples/index.js b/is-array/examples/index.js new file mode 100644 index 00000000..d37bd708 --- /dev/null +++ b/is-array/examples/index.js @@ -0,0 +1,27 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isArray = require( './../lib' ); + +console.log( isArray( [] ) ); +// => true + +console.log( isArray( {} ) ); +// => false diff --git a/is-array/lib/index.js b/is-array/lib/index.js new file mode 100644 index 00000000..07f8f6f4 --- /dev/null +++ b/is-array/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array. +* +* @module @stdlib/assert/is-array +* +* @example +* var isArray = require( '@stdlib/assert/is-array' ); +* +* var bool = isArray( [] ); +* // returns true +* +* bool = isArray( {} ); +* // returns false +*/ + +// MODULES // + +var isArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isArray; diff --git a/is-array/lib/main.js b/is-array/lib/main.js new file mode 100644 index 00000000..6e2a6554 --- /dev/null +++ b/is-array/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var f; + + +// FUNCTIONS // + +/** +* Tests if a value is an array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an array +* +* @example +* var bool = isArray( [] ); +* // returns true +* +* @example +* var bool = isArray( {} ); +* // returns false +*/ +function isArray( value ) { + return ( nativeClass( value ) === '[object Array]' ); +} + + +// MAIN // + +if ( Array.isArray ) { + f = Array.isArray; +} else { + f = isArray; +} + + +// EXPORTS // + +module.exports = f; diff --git a/is-array/package.json b/is-array/package.json new file mode 100644 index 00000000..04154531 --- /dev/null +++ b/is-array/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/assert/is-array", + "version": "0.0.0", + "description": "Test if a value is an array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "array.isarray", + "array", + "is", + "isarray", + "type", + "check" + ] +} diff --git a/is-array/test/test.js b/is-array/test/test.js new file mode 100644 index 00000000..ef0bef51 --- /dev/null +++ b/is-array/test/test.js @@ -0,0 +1,91 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var isArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an array', function test( t ) { + t.equal( isArray( [] ), true, 'returns true' ); + t.equal( isArray( new Array( 10 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an array', function test( t ) { + var values; + var i; + + values = [ + 5, + {}, + true, + '5', + null, + NaN, + function noop() {}, + undefined + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isArray( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); + +tape( 'the function includes a polyfill for `Array.isArray`', function test( t ) { + var isArray; + var values; + var fcn; + var i; + + fcn = Array.isArray; + Array.isArray = undefined; + isArray = proxyquire( './../lib/main.js', {} ); + + values = [ + 5, + {}, + true, + '5', + null, + NaN, + function noop() {}, + undefined + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isArray( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.equal( isArray( [] ), true, 'returns true when provided an array' ); + + Array.isArray = fcn; + t.end(); +}); diff --git a/is-arraybuffer/README.md b/is-arraybuffer/README.md new file mode 100644 index 00000000..c3d4dbda --- /dev/null +++ b/is-arraybuffer/README.md @@ -0,0 +1,120 @@ + + +# isArrayBuffer + +> Test if a value is an [ArrayBuffer][mdn-arraybuffer]. + +
+ +## Usage + +```javascript +var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); +``` + +#### isArrayBuffer( value ) + +Tests if a value is an [`ArrayBuffer`][mdn-arraybuffer]. + +```javascript +var ArrayBuffer = require( '@stdlib/array/buffer' ); + +var bool = isArrayBuffer( new ArrayBuffer( 10 ) ); +// returns true + +bool = isArrayBuffer( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); + +var bool = isArrayBuffer( new ArrayBuffer( 10 ) ); +// returns true + +bool = isArrayBuffer( new Float32Array( 10 ) ); +// returns false + +bool = isArrayBuffer( new Int8Array( 10 ) ); +// returns false + +bool = isArrayBuffer( new Uint8Array( 10 ) ); +// returns false + +bool = isArrayBuffer( new Uint8ClampedArray( 10 ) ); +// returns false + +bool = isArrayBuffer( new Int16Array( 10 ) ); +// returns false + +bool = isArrayBuffer( new Uint16Array( 10 ) ); +// returns false + +bool = isArrayBuffer( new Int32Array( 10 ) ); +// returns false + +bool = isArrayBuffer( new Uint32Array( 10 ) ); +// returns false + +bool = isArrayBuffer( new Float64Array( 10 ) ); +// returns false + +bool = isArrayBuffer( new Array( 10 ) ); +// returns false + +bool = isArrayBuffer( {} ); +// returns false + +bool = isArrayBuffer( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-arraybuffer/benchmark/benchmark.js b/is-arraybuffer/benchmark/benchmark.js new file mode 100644 index 00000000..d942d7ee --- /dev/null +++ b/is-arraybuffer/benchmark/benchmark.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var pkg = require( './../package.json' ).name; +var isArrayBuffer = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ), + new ArrayBuffer( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isArrayBuffer( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-arraybuffer/docs/repl.txt b/is-arraybuffer/docs/repl.txt new file mode 100644 index 00000000..67b1fe70 --- /dev/null +++ b/is-arraybuffer/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is an ArrayBuffer. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an ArrayBuffer. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/buffer}}( 10 ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-arraybuffer/docs/types/index.d.ts b/is-arraybuffer/docs/types/index.d.ts new file mode 100644 index 00000000..e828b920 --- /dev/null +++ b/is-arraybuffer/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an ArrayBuffer. +* +* @param value - value to test +* @returns boolean indicating whether value is an ArrayBuffer +* +* @example +* var ArrayBuffer = require( `@stdlib/array/buffer` ); +* +* var bool = isArrayBuffer( new ArrayBuffer( 10 ) ); +* // returns true +* +* @example +* var bool = isArrayBuffer( [] ); +* // returns false +*/ +declare function isArrayBuffer( value: any ): boolean; + + +// EXPORTS // + +export = isArrayBuffer; diff --git a/is-arraybuffer/docs/types/test.ts b/is-arraybuffer/docs/types/test.ts new file mode 100644 index 00000000..b72f275b --- /dev/null +++ b/is-arraybuffer/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isArrayBuffer = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isArrayBuffer( new ArrayBuffer( 10 ) ); // $ExpectType boolean + isArrayBuffer( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isArrayBuffer(); // $ExpectError + isArrayBuffer( [], 123 ); // $ExpectError +} diff --git a/is-arraybuffer/examples/index.js b/is-arraybuffer/examples/index.js new file mode 100644 index 00000000..b89a6fd6 --- /dev/null +++ b/is-arraybuffer/examples/index.js @@ -0,0 +1,83 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var isArrayBuffer = require( './../lib' ); + +var bool = isArrayBuffer( new ArrayBuffer( 10 ) ); +console.log( bool ); +// => true + +bool = isArrayBuffer( new Float32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isArrayBuffer( new Int8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isArrayBuffer( new Uint8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isArrayBuffer( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => false + +bool = isArrayBuffer( new Int16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isArrayBuffer( new Uint16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isArrayBuffer( new Int32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isArrayBuffer( new Uint32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isArrayBuffer( new Float64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isArrayBuffer( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isArrayBuffer( {} ); +console.log( bool ); +// => false + +bool = isArrayBuffer( null ); +console.log( bool ); +// => false diff --git a/is-arraybuffer/lib/index.js b/is-arraybuffer/lib/index.js new file mode 100644 index 00000000..ed172ba9 --- /dev/null +++ b/is-arraybuffer/lib/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an ArrayBuffer. +* +* @module @stdlib/assert/is-arraybuffer +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); +* +* var bool = isArrayBuffer( new ArrayBuffer( 10 ) ); +* // returns true +* +* bool = isArrayBuffer( [] ); +* // returns false +*/ + +// MODULES // + +var isArrayBuffer = require( './main.js' ); + + +// EXPORTS // + +module.exports = isArrayBuffer; diff --git a/is-arraybuffer/lib/main.js b/is-arraybuffer/lib/main.js new file mode 100644 index 00000000..f7cfac85 --- /dev/null +++ b/is-arraybuffer/lib/main.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var hasArrayBuffer = ( typeof ArrayBuffer === 'function' ); // eslint-disable-line stdlib/require-globals + + +// MAIN // + +/** +* Tests if a value is an ArrayBuffer. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an ArrayBuffer +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* +* var bool = isArrayBuffer( new ArrayBuffer( 10 ) ); +* // returns true +* +* @example +* var bool = isArrayBuffer( [] ); +* // returns false +*/ +function isArrayBuffer( value ) { + return ( + ( hasArrayBuffer && value instanceof ArrayBuffer ) || // eslint-disable-line stdlib/require-globals + nativeClass( value ) === '[object ArrayBuffer]' + ); +} + + +// EXPORTS // + +module.exports = isArrayBuffer; diff --git a/is-arraybuffer/package.json b/is-arraybuffer/package.json new file mode 100644 index 00000000..de699f27 --- /dev/null +++ b/is-arraybuffer/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-arraybuffer", + "version": "0.0.0", + "description": "Test if a value is an ArrayBuffer.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "arraybuffer", + "array", + "buffer", + "typed", + "typed array", + "typed-array", + "is", + "isbuffer", + "isarraybuffer", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-arraybuffer/test/test.js b/is-arraybuffer/test/test.js new file mode 100644 index 00000000..ee60a839 --- /dev/null +++ b/is-arraybuffer/test/test.js @@ -0,0 +1,80 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var isArrayBuffer = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isArrayBuffer, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an ArrayBuffer', function test( t ) { + t.strictEqual( isArrayBuffer( new ArrayBuffer( 10 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an ArrayBuffer', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + undefined, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Uint32Array( 10 ), + new Int32Array( 10 ), + new Uint16Array( 10 ), + new Int16Array( 10 ), + new Uint8Array( 10 ), + new Int8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isArrayBuffer( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-ascii/README.md b/is-ascii/README.md new file mode 100644 index 00000000..c699a5a5 --- /dev/null +++ b/is-ascii/README.md @@ -0,0 +1,137 @@ + + +# isASCII + +> Test whether a character belongs to the [ASCII][ascii] character set and whether this is true for all characters in a provided string. + +
+ +## Usage + +```javascript +var isASCII = require( '@stdlib/assert/is-ascii' ); +``` + +#### isASCII( value ) + +Tests whether a character belongs to the [ASCII][ascii] character set and whether this is true for all characters in a provided string. + +```javascript +var bool = isASCII( 'beep' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- For non-string values, the function returns `false`. + +
+ + + +
+ +## Examples + + + +```javascript +var isASCII = require( '@stdlib/assert/is-ascii' ); + +var out = isASCII( 'beep' ); +// returns true + +out = isASCII( '' ); +// returns false + +out = isASCII( 'È' ); +// returns false + +out = isASCII( 123 ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-ascii [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-ascii beep +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n 'beep' | is-ascii +true +``` + +
+ + + +
+ + + + + + diff --git a/is-ascii/benchmark/benchmark.js b/is-ascii/benchmark/benchmark.js new file mode 100644 index 00000000..55ac2e9c --- /dev/null +++ b/is-ascii/benchmark/benchmark.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isASCII = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::single-character', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = fromCodePoint( i%200 ); + bool = isASCII( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::string', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = 'beep boop foo bar' + fromCodePoint( i%200 ); + bool = isASCII( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-ascii/bin/cli b/is-ascii/bin/cli new file mode 100644 index 00000000..1666f5b4 --- /dev/null +++ b/is-ascii/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isASCII = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isASCII( args[ 0 ] ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isASCII( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-ascii/docs/repl.txt b/is-ascii/docs/repl.txt new file mode 100644 index 00000000..0f6e9518 --- /dev/null +++ b/is-ascii/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( str ) + Tests whether a character belongs to the ASCII character set and whether + this is true for all characters in a provided string. + + Parameters + ---------- + str: string + Input string. + + Returns + ------- + bool: boolean + Boolean indicating whether a string has all ASCII characters. + + Examples + -------- + > var str = 'beep boop'; + > var bool = {{alias}}( str ) + true + > bool = {{alias}}( {{alias:@stdlib/string/from-code-point}}( 130 ) ) + false + + See Also + -------- + diff --git a/is-ascii/docs/types/index.d.ts b/is-ascii/docs/types/index.d.ts new file mode 100644 index 00000000..805f035f --- /dev/null +++ b/is-ascii/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests whether a character belongs to the ASCII character set and whether this is true for all characters in a provided string. +* +* @param value - value to test +* @returns boolean indicating if a string has all ASCII characters +* +* @example +* var out = isASCII( 'beep' ); +* // returns true +* +* @example +* var out = isASCII( 'È' ); +* // returns false +* +* @example +* var out = isASCII( '' ); +* // returns false +* +* @example +* var out = isASCII( 123 ); +* // returns false +*/ +declare function isASCII( value: any ): boolean; + + +// EXPORTS // + +export = isASCII; diff --git a/is-ascii/docs/types/test.ts b/is-ascii/docs/types/test.ts new file mode 100644 index 00000000..1292d7e7 --- /dev/null +++ b/is-ascii/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isASCII = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isASCII( 'beep' ); // $ExpectType boolean + isASCII( '' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isASCII(); // $ExpectError + isASCII( undefined, 123 ); // $ExpectError +} diff --git a/is-ascii/docs/usage.txt b/is-ascii/docs/usage.txt new file mode 100644 index 00000000..13d156ea --- /dev/null +++ b/is-ascii/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-ascii [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-ascii/etc/cli_opts.json b/is-ascii/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-ascii/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-ascii/examples/index.js b/is-ascii/examples/index.js new file mode 100644 index 00000000..27ebbcd2 --- /dev/null +++ b/is-ascii/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isASCII = require( './../lib' ); + +console.log( isASCII( 'beep' ) ); +// => true + +console.log( isASCII( '' ) ); +// => false + +console.log( isASCII( 'È' ) ); +// => false + +console.log( isASCII( 123 ) ); +// => false diff --git a/is-ascii/lib/index.js b/is-ascii/lib/index.js new file mode 100644 index 00000000..f08bb692 --- /dev/null +++ b/is-ascii/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether a character belongs to the ASCII character set and whether this is true for all characters in a provided string. +* +* @module @stdlib/assert/is-ascii +* +* @example +* var isASCII = require( '@stdlib/assert/is-ascii' ); +* +* var out = isASCII( 'beep' ); +* // returns true +* +* out = isASCII( 'È' ); +* // returns false +* +* out = isASCII( '' ); +* // returns false +*/ + +// MODULES // + +var isASCII = require( './main.js' ); + + +// EXPORTS // + +module.exports = isASCII; diff --git a/is-ascii/lib/main.js b/is-ascii/lib/main.js new file mode 100644 index 00000000..cf22f1dd --- /dev/null +++ b/is-ascii/lib/main.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; + + +// VARIABLES // + +var MAX_ASCII = 127; + + +// MAIN // + +/** +* Tests whether a character belongs to the ASCII character set and whether this is true for all characters in a provided string. +* +* @param {*} x - value to test +* @returns {boolean} boolean indicating if a string has all ASCII characters +* +* @example +* var out = isASCII( 'beep' ); +* // returns true +* +* @example +* var out = isASCII( 'È' ); +* // returns false +* +* @example +* var out = isASCII( '' ); +* // returns false +* +* @example +* var out = isASCII( 123 ); +* // returns false +*/ +function isASCII( x ) { + var len; + var i; + if ( !isString( x ) ) { + return false; + } + len = x.length; + if ( !len ) { + return false; + } + for ( i = 0; i < len; i++ ) { + if ( x.charCodeAt( i ) > MAX_ASCII ) { + return false; + } + } + return true; +} + + +// EXPORTS // + +module.exports = isASCII; diff --git a/is-ascii/package.json b/is-ascii/package.json new file mode 100644 index 00000000..4f100792 --- /dev/null +++ b/is-ascii/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-ascii", + "version": "0.0.0", + "description": "Test whether a character belongs to the ASCII character set and whether this is true for all characters in a provided string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-ascii": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "string", + "str", + "ascii", + "is", + "isstring", + "isascii", + "validate", + "valid", + "isvalid", + "test", + "check" + ] +} diff --git a/is-ascii/test/fixtures/stdin_error.js.txt b/is-ascii/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-ascii/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-ascii/test/test.cli.js b/is-ascii/test/test.cli.js new file mode 100644 index 00000000..29361fc0 --- /dev/null +++ b/is-ascii/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument contains only characters belonging to the ASCII character set', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'abcdef\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "beep\nÈ"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\nfalse\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-ascii/test/test.js b/is-ascii/test/test.js new file mode 100644 index 00000000..4a5a473b --- /dev/null +++ b/is-ascii/test/test.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isASCII = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isASCII, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a string containing all ASCII characters', function test( t ) { + t.strictEqual( isASCII( 'abcdef' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a string containing all ASCII characters', function test( t ) { + var values; + var i; + + values = [ + '', + 'È', + new String( 'abcdef' ), // eslint-disable-line no-new-wrappers + 5, + null, + NaN, + undefined, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isASCII( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-between-array/README.md b/is-between-array/README.md new file mode 100644 index 00000000..7270ebfb --- /dev/null +++ b/is-between-array/README.md @@ -0,0 +1,148 @@ + + +# isBetweenArray + +> Test if a value is an array-like object where every element is between two values. + +
+ +## Usage + +```javascript +var isBetweenArray = require( '@stdlib/assert/is-between-array' ); +``` + +#### isBetweenArray( value, a, b\[, left, right] ) + +Tests if a `value` is an array-like `object` where every element is between two values `a` (left comparison value) and `b` (right comparison value). + +```javascript +var arr = [ 3, 4, 5 ]; + +var bool = isBetweenArray( arr, 3, 5 ); +// returns true + +bool = isBetweenArray( arr, 4, 5 ); +// returns false + +bool = isBetweenArray( arr, 3, 4 ); +// returns false +``` + +By default, the function assumes that `a` and `b` are inclusive. + +```javascript +var arr = [ 3, 4, 5 ]; + +var bool = isBetweenArray( arr, 3, 5 ); +// returns true + +bool = isBetweenArray( arr, 3, 5, 'closed', 'closed' ); +// returns true +``` + +To make `a` and/or `b` exclusive, set the respective arguments to `'open'`. + +```javascript +var arr = [ 3, 4, 5 ]; + +var bool = isBetweenArray( arr, 3, 5, 'open', 'closed' ); +// returns false + +bool = isBetweenArray( arr, 3, 5, 'closed', 'open' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- If `a` and `b` are inclusive, the element-wise comparison is equivalent to + + ```text + a <= v_i <= b + ``` + +- If `a` is exclusive and `b` is inclusive, the element-wise comparison is equivalent to + + ```text + a < v_i <= b + ``` + +- If `a` is inclusive and `b` is exclusive, the element-wise comparison is equivalent to + + ```text + a <= v_i < b + ``` + +- If `a` and `b` are exclusive, the element-wise comparison is equivalent to + + ```text + a < v_i < b + ``` + +- If provided an empty array-like `object`, the function returns `false`. + + ```javascript + var bool = isBetweenArray( [], 0.0, 1.0 ); + // returns false + ``` + +- If provided non-numeric values, element-wise comparisons are performed according to lexicographic order. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isBetweenArray = require( '@stdlib/assert/is-between-array' ); + +var x; +var i; + +x = new Float64Array( 100 ); +for ( i = 0; i < x.length; i++ ) { + x[ i ] = randu(); +} +console.log( isBetweenArray( x, 0.01, 0.99 ) ); +``` + +
+ + + + + + diff --git a/is-between-array/benchmark/benchmark.js b/is-between-array/benchmark/benchmark.js new file mode 100644 index 00000000..f480631a --- /dev/null +++ b/is-between-array/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var isBetweenArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( randu() ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i; + bool = isBetweenArray( x, 0.0, 1.0 ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/is-between-array/docs/repl.txt b/is-between-array/docs/repl.txt new file mode 100644 index 00000000..40ff3f58 --- /dev/null +++ b/is-between-array/docs/repl.txt @@ -0,0 +1,47 @@ + +{{alias}}( value, a, b[, left, right] ) + Tests if a value is an array-like object where every element is between two + values. + + Parameters + ---------- + value: any + Input value. + + a: any + Left comparison value. + + b: any + Right comparison value. + + left: string (optional) + Indicates whether the left comparison value is inclusive. Must be either + 'closed' or 'open'. Default: 'closed' (i.e., inclusive). + + right: string (optional) + Indicates whether the right comparison value is inclusive. Must be + either 'closed' or 'open'. Default: 'closed' (i.e., inclusive). + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an array-like object where every + element is between two values. + + Examples + -------- + > var arr = [ 3.0, 3.14, 4.0 ]; + > var bool = {{alias}}( arr, 3.0, 4.0 ) + true + > bool = {{alias}}( arr, 3.14, 4.0 ) + false + > bool = {{alias}}( arr, 3.0, 3.14 ) + false + > bool = {{alias}}( arr, 3.0, 4.0, 'open', 'closed' ) + false + > bool = {{alias}}( arr, 3.0, 4.0, 'closed', 'open' ) + false + + See Also + -------- + diff --git a/is-between-array/docs/types/index.d.ts b/is-between-array/docs/types/index.d.ts new file mode 100644 index 00000000..7b047a1f --- /dev/null +++ b/is-between-array/docs/types/index.d.ts @@ -0,0 +1,66 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Comparison type. +*/ +type Comparison = 'open' | 'closed'; + +/** +* Tests if a value is an array-like object where every element is between two values. +* +* @param value - value to test +* @param a - left comparison value +* @param b - right comparison value +* @param left - indicates whether the left comparison value is inclusive (default: 'closed') +* @param right - indicates whether the right comparison value is inclusive (default: 'closed') +* @returns boolean indicating whether a value is an array-like object where every element is between two values +* +* @example +* var arr = [ 3.0, 3.14, 4.0 ]; +* var bool = isBetweenArray( arr, 3.0, 4.0 ); +* // returns true +* +* @example +* var arr = [ 3.0, 3.14, 4.0 ]; +* var bool = isBetweenArray( arr, 3.14, 4.0 ); +* // returns false +* +* @example +* var arr = [ 3.0, 3.14, 4.0 ]; +* var bool = isBetweenArray( arr, 3.0, 3.14 ); +* // returns false +* +* @example +* var arr = [ 3.0, 3.14, 4.0 ]; +* var bool = isBetweenArray( arr, 3.0, 4.0, 'open', 'closed' ); +* // returns false +* +* @example +* var arr = [ 3.0, 3.14, 4.0 ]; +* var bool = isBetweenArray( arr, 3.0, 4.0, 'closed', 'open' ); +* // returns false +*/ +declare function isBetweenArray( value: any, a: any, b: any, left?: Comparison, right?: Comparison ): boolean; // tslint-disable-line max-line-length + + +// EXPORTS // + +export = isBetweenArray; diff --git a/is-between-array/docs/types/test.ts b/is-between-array/docs/types/test.ts new file mode 100644 index 00000000..f340e472 --- /dev/null +++ b/is-between-array/docs/types/test.ts @@ -0,0 +1,62 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isBetweenArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const arr = [ 3.0, 3.14, 4.0 ]; + isBetweenArray( arr, 1, 4 ); // $ExpectType boolean + isBetweenArray( arr, 3.14, 4.0, 'open', 'closed' ); // $ExpectType boolean + isBetweenArray( arr, 3.14, 4.0, 'open' ); // $ExpectType boolean +} + +// The function does not compile if provided a fourth argument that is not a recognized string... +{ + const arr = [ 3.0, 3.14, 4.0 ]; + isBetweenArray( arr, 3.14, 4.0, [] ); // $ExpectError + isBetweenArray( arr, 3.14, 4.0, {} ); // $ExpectError + isBetweenArray( arr, 3.14, 4.0, 123 ); // $ExpectError + isBetweenArray( arr, 3.14, 4.0, 'abc' ); // $ExpectError + isBetweenArray( arr, 3.14, 4.0, null ); // $ExpectError + isBetweenArray( arr, 3.14, 4.0, true ); // $ExpectError + isBetweenArray( arr, 3.14, 4.0, false ); // $ExpectError +} + +// The function does not compile if provided a fifth argument that is not a recognized string... +{ + const arr = [ 3.0, 3.14, 4.0 ]; + isBetweenArray( arr, 3.14, 4.0, 'open', [] ); // $ExpectError + isBetweenArray( arr, 3.14, 4.0, 'open', {} ); // $ExpectError + isBetweenArray( arr, 3.14, 4.0, 'open', 123 ); // $ExpectError + isBetweenArray( arr, 3.14, 4.0, 'open', 'abc' ); // $ExpectError + isBetweenArray( arr, 3.14, 4.0, 'open', null ); // $ExpectError + isBetweenArray( arr, 3.14, 4.0, 'open', true ); // $ExpectError + isBetweenArray( arr, 3.14, 4.0, 'open', false ); // $ExpectError +} + +// The function does not compile if provided insufficient arguments... +{ + const arr = [ 3.0, 3.14, 4.0 ]; + isBetweenArray(); // $ExpectError + isBetweenArray( arr ); // $ExpectError + isBetweenArray( arr, 2 ); // $ExpectError +} diff --git a/is-between-array/examples/index.js b/is-between-array/examples/index.js new file mode 100644 index 00000000..d2cf105c --- /dev/null +++ b/is-between-array/examples/index.js @@ -0,0 +1,32 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var randu = require( '@stdlib/random/base/randu' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isBetweenArray = require( './../lib' ); + +var x; +var i; + +x = new Float64Array( 100 ); +for ( i = 0; i < x.length; i++ ) { + x[ i ] = randu(); +} +console.log( isBetweenArray( x, 0.01, 0.99 ) ); diff --git a/is-between-array/lib/index.js b/is-between-array/lib/index.js new file mode 100644 index 00000000..6ca7e4d0 --- /dev/null +++ b/is-between-array/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object where every element is between two values. +* +* @module @stdlib/assert/is-between-array +* +* @example +* var isBetweenArray = require( '@stdlib/assert/is-between-array' ); +* +* var arr = [ 3.0, 3.14, 4.0 ]; +* +* var bool = isBetweenArray( arr, 3.0, 4.0 ); +* // returns true +* +* bool = isBetweenArray( arr, 3.14, 4.0 ); +* // returns false +* +* bool = isBetweenArray( arr, 3.0, 3.14 ); +* // returns false +* +* bool = isBetweenArray( arr, 3.0, 4.0, 'open', 'closed' ); +* // returns false +* +* bool = isBetweenArray( arr, 3.0, 4.0, 'closed', 'open' ); +* // returns false +*/ + +// MODULES // + +var isBetweenArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isBetweenArray; diff --git a/is-between-array/lib/main.js b/is-between-array/lib/main.js new file mode 100644 index 00000000..2722513b --- /dev/null +++ b/is-between-array/lib/main.js @@ -0,0 +1,118 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isCollection = require( '@stdlib/assert/is-collection' ); + + +// MAIN // + +/** +* Tests if a value is an array-like object where every element is between two values. +* +* @param {*} value - value to test +* @param {*} a - left comparison value +* @param {*} b - right comparison value +* @param {string} [left="closed"] - indicates whether the left comparison value is inclusive +* @param {string} [right="closed"] - indicates whether the right comparison value is inclusive +* @throws {TypeError} `left` must be a recognized string +* @throws {TypeError} `right` must be a recognized string +* @returns {boolean} boolean indicating whether a value is an array-like object where every element is between two values +* +* @example +* var arr = [ 3.0, 3.14, 4.0 ]; +* var bool = isBetweenArray( arr, 3.0, 4.0 ); +* // returns true +* +* @example +* var arr = [ 3.0, 3.14, 4.0 ]; +* var bool = isBetweenArray( arr, 3.14, 4.0 ); +* // returns false +* +* @example +* var arr = [ 3.0, 3.14, 4.0 ]; +* var bool = isBetweenArray( arr, 3.0, 3.14 ); +* // returns false +* +* @example +* var arr = [ 3.0, 3.14, 4.0 ]; +* var bool = isBetweenArray( arr, 3.0, 4.0, 'open', 'closed' ); +* // returns false +* +* @example +* var arr = [ 3.0, 3.14, 4.0 ]; +* var bool = isBetweenArray( arr, 3.0, 4.0, 'closed', 'open' ); +* // returns false +*/ +function isBetweenArray( value, a, b, left, right ) { + var len; + var i; + if ( arguments.length > 3 ) { + if ( left !== 'closed' && left !== 'open' ) { + throw new TypeError( 'invalid argument. `left` must be one of the following strings: \'closed\' or \'open\'. Value: `'+left+'`.' ); + } + if ( right !== 'closed' && right !== 'open' ) { + throw new TypeError( 'invalid argument. `right` must be one of the following strings: \'closed\' or \'open\'. Value: `'+right+'`.' ); + } + } + if ( !isCollection( value ) ) { + return false; + } + len = value.length; + if ( len === 0 ) { + return false; + } + if ( left === 'closed' || left === void 0 ) { + if ( right === 'closed' || right === void 0 ) { + for ( i = 0; i < len; i++ ) { + if ( value[ i ] < a || value[ i ] > b ) { + return false; + } + } + return true; + } + for ( i = 0; i < len; i++ ) { + if ( value[ i ] < a || value[ i ] >= b ) { + return false; + } + } + return true; + } + if ( right === 'closed' || right === void 0 ) { + for ( i = 0; i < len; i++ ) { + if ( value[ i ] <= a || value[ i ] > b ) { + return false; + } + } + return true; + } + for ( i = 0; i < len; i++ ) { + if ( value[ i ] <= a || value[ i ] >= b ) { + return false; + } + } + return true; +} + + +// EXPORTS // + +module.exports = isBetweenArray; diff --git a/is-between-array/package.json b/is-between-array/package.json new file mode 100644 index 00000000..86c1479e --- /dev/null +++ b/is-between-array/package.json @@ -0,0 +1,85 @@ +{ + "name": "@stdlib/assert/is-between-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object where every element is between two values.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "comparison", + "compare", + "order", + "interval", + "closed", + "open", + "range", + "between", + "btwn", + "is", + "isbetween", + "isbtwn", + "isarray", + "array", + "array-like", + "typed-array", + "typed array", + "collection", + "every", + "validate", + "valid", + "isvalid", + "test", + "check" + ] +} diff --git a/is-between-array/test/test.js b/is-between-array/test/test.js new file mode 100644 index 00000000..0451e3d4 --- /dev/null +++ b/is-between-array/test/test.js @@ -0,0 +1,346 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isBetweenArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isBetweenArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided an unrecognized `left` argument', function test( t ) { + var values; + var i; + + values = [ + 'beep', + 'boop', + 'left', + 'right', + 'ope', + 'close', + 5, + NaN, + null, + undefined, + true, + false, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + isBetweenArray( [ 3, 4, 5 ], 3, 5, value, 'closed' ); + }; + } +}); + +tape( 'the function throws an error if provided an unrecognized `right` argument', function test( t ) { + var values; + var i; + + values = [ + 'beep', + 'boop', + 'left', + 'right', + 'ope', + 'close', + 5, + NaN, + null, + undefined, + true, + false, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + isBetweenArray( [ 3, 4, 5 ], 3, 5, 'closed', value ); + }; + } +}); + +tape( 'the function returns `true` if provided an array-like object where every element is between two values (default)', function test( t ) { + var arr; + + arr = new Int32Array( [ 3, 4, 5 ] ); + t.strictEqual( isBetweenArray( arr, 3, 5 ), true, 'returns true' ); + t.strictEqual( isBetweenArray( arr, 3, 5 ), true, 'returns true' ); + t.strictEqual( isBetweenArray( arr, 3, 5 ), true, 'returns true' ); + + arr = [ 'a', 'b', 'c' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c' ), true, 'returns true' ); + t.strictEqual( isBetweenArray( arr, 'a', 'c' ), true, 'returns true' ); + t.strictEqual( isBetweenArray( arr, 'a', 'c' ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if provided an array-like object where every element is between two values (closed,closed)', function test( t ) { + var arr; + + arr = new Int16Array( [ 3, 4, 5 ] ); + t.strictEqual( isBetweenArray( arr, 3, 5, 'closed', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetweenArray( arr, 3, 5, 'closed', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetweenArray( arr, 3, 5, 'closed', 'closed' ), true, 'returns true' ); + + arr = [ 'a', 'b', 'c' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'closed', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'closed', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'closed', 'closed' ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if provided an array-like object where every element is between two values (open,closed)', function test( t ) { + var arr; + + arr = new Uint32Array( [ 4, 5 ] ); + t.strictEqual( isBetweenArray( arr, 3, 5, 'open', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetweenArray( arr, 3, 5, 'open', 'closed' ), true, 'returns true' ); + + arr = [ 'b', 'c' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'open', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'open', 'closed' ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if provided an array-like object where every element is between two values (closed,open)', function test( t ) { + var arr; + + arr = new Uint16Array( [ 3, 4 ] ); + t.strictEqual( isBetweenArray( arr, 3, 5, 'closed', 'open' ), true, 'returns true' ); + t.strictEqual( isBetweenArray( arr, 3, 5, 'closed', 'open' ), true, 'returns true' ); + + arr = [ 'a', 'b' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'closed', 'open' ), true, 'returns true' ); + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'closed', 'open' ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if provided an array-like object where every element is between two values (open,open)', function test( t ) { + var arr; + + arr = new Int8Array( [ 4 ] ); + t.strictEqual( isBetweenArray( arr, 3, 5, 'open', 'open' ), true, 'returns true' ); + + arr = [ 'b' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'open', 'open' ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if not provided an array-like object where every element is between two values (default)', function test( t ) { + var arr; + + arr = new Uint8Array( [ 2 ] ); + t.strictEqual( isBetweenArray( arr, 3, 5 ), false, 'returns false' ); + + arr = new Uint8ClampedArray( [ 6 ] ); + t.strictEqual( isBetweenArray( arr, 3, 5 ), false, 'returns false' ); + + arr = [ 'a' ]; + t.strictEqual( isBetweenArray( arr, 'b', 'c' ), false, 'returns false' ); + + arr = [ 'd' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c' ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if not provided an array-like object where every element is between two values (closed,closed)', function test( t ) { + var arr; + + arr = { + 'length': 1, + '0': 2 + }; + t.strictEqual( isBetweenArray( arr, 3, 5, 'closed', 'closed' ), false, 'returns false' ); + + arr = [ 6 ]; + t.strictEqual( isBetweenArray( arr, 3, 5, 'closed', 'closed' ), false, 'returns false' ); + + arr = [ 'a' ]; + t.strictEqual( isBetweenArray( arr, 'b', 'c', 'closed', 'closed' ), false, 'returns false' ); + + arr = [ 'd' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'closed', 'closed' ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if not provided an array-like object where every element is between two values (open,closed)', function test( t ) { + var arr; + + arr = [ 2 ]; + t.strictEqual( isBetweenArray( arr, 3, 5, 'open', 'closed' ), false, 'returns false' ); + + arr = [ 3 ]; + t.strictEqual( isBetweenArray( arr, 3, 5, 'open', 'closed' ), false, 'returns false' ); + + arr = [ 6 ]; + t.strictEqual( isBetweenArray( arr, 3, 5, 'open', 'closed' ), false, 'returns false' ); + + arr = [ 'a' ]; + t.strictEqual( isBetweenArray( arr, 'b', 'c', 'open', 'closed' ), false, 'returns false' ); + + arr = [ 'a' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'open', 'closed' ), false, 'returns false' ); + + arr = [ 'd' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'open', 'closed' ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if not provided an array-like object where every element is between two values (closed,open)', function test( t ) { + var arr; + + arr = [ 2 ]; + t.strictEqual( isBetweenArray( arr, 3, 5, 'closed', 'open' ), false, 'returns false' ); + + arr = [ 5 ]; + t.strictEqual( isBetweenArray( arr, 3, 5, 'closed', 'open' ), false, 'returns false' ); + + arr = [ 6 ]; + t.strictEqual( isBetweenArray( arr, 3, 5, 'closed', 'open' ), false, 'returns false' ); + + arr = [ 'a' ]; + t.strictEqual( isBetweenArray( arr, 'b', 'c', 'closed', 'open' ), false, 'returns false' ); + + arr = [ 'c' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'closed', 'open' ), false, 'returns false' ); + + arr = [ 'd' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'closed', 'open' ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if not provided an array-like object where every element is between two values (open,open)', function test( t ) { + var arr; + + arr = [ 2 ]; + t.strictEqual( isBetweenArray( arr, 3, 5, 'open', 'open' ), false, 'returns false' ); + + arr = [ 3 ]; + t.strictEqual( isBetweenArray( arr, 3, 5, 'open', 'open' ), false, 'returns false' ); + + arr = [ 5 ]; + t.strictEqual( isBetweenArray( arr, 3, 5, 'open', 'open' ), false, 'returns false' ); + + arr = [ 6 ]; + t.strictEqual( isBetweenArray( arr, 3, 5, 'open', 'open' ), false, 'returns false' ); + + arr = [ 'a' ]; + t.strictEqual( isBetweenArray( arr, 'b', 'c', 'open', 'open' ), false, 'returns false' ); + + arr = [ 'a' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'open', 'open' ), false, 'returns false' ); + + arr = [ 'c' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'open', 'open' ), false, 'returns false' ); + + arr = [ 'd' ]; + t.strictEqual( isBetweenArray( arr, 'a', 'c', 'open', 'open' ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an empty array-like object', function test( t ) { + var arr; + + arr = []; + t.strictEqual( isBetweenArray( arr, 0.0, 1.0 ), false, 'returns false' ); + + arr = []; + t.strictEqual( isBetweenArray( arr, 0.0, 1.0, 'closed', 'closed' ), false, 'returns false' ); + + arr = []; + t.strictEqual( isBetweenArray( arr, 0.0, 1.0, 'open', 'closed' ), false, 'returns false' ); + + arr = []; + t.strictEqual( isBetweenArray( arr, 0.0, 1.0, 'closed', 'open' ), false, 'returns false' ); + + arr = []; + t.strictEqual( isBetweenArray( arr, 0.0, 1.0, 'open', 'open' ), false, 'returns false' ); + + arr = new Float64Array(); + t.strictEqual( isBetweenArray( arr, 0.0, 1.0 ), false, 'returns false' ); + + arr = { + 'length': 0 + }; + t.strictEqual( isBetweenArray( arr, 0.0, 1.0 ), false, 'returns false' ); + + arr = ''; + t.strictEqual( isBetweenArray( arr, 'a', 'c' ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if not provided an array-like object', function test( t ) { + var values; + var i; + + values = [ + 5, + NaN, + null, + undefined, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isBetweenArray( values[ i ], 0.0, 1.0 ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-between/README.md b/is-between/README.md new file mode 100644 index 00000000..1fa14bb4 --- /dev/null +++ b/is-between/README.md @@ -0,0 +1,146 @@ + + +# isBetween + +> Test if a value is between two values. + +
+ +## Usage + +```javascript +var isBetween = require( '@stdlib/assert/is-between' ); +``` + +#### isBetween( value, a, b\[, left, right] ) + +Tests if a `value` is between two values `a` (left comparison value) and `b` (right comparison value). + +```javascript +var bool = isBetween( 4, 3, 5 ); +// returns true + +bool = isBetween( 2, 3, 5 ); +// returns false + +bool = isBetween( 6, 3, 5 ); +// returns false +``` + +By default, the function assumes that `a` and `b` are inclusive. + +```javascript +var bool = isBetween( 3, 3, 5 ); +// returns true + +bool = isBetween( 3, 3, 5, 'closed', 'closed' ); +// returns true + +bool = isBetween( 5, 3, 5 ); +// returns true + +bool = isBetween( 5, 3, 5, 'closed', 'closed' ); +// returns true +``` + +To make `a` and/or `b` exclusive, set the respective arguments to `'open'`. + +```javascript +var bool = isBetween( 3, 3, 5, 'open', 'closed' ); +// returns false + +bool = isBetween( 5, 3, 5, 'closed', 'open' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- If `a` and `b` are inclusive, the comparison is equivalent to + + ```text + a <= v <= b + ``` + +- If `a` is exclusive and `b` is inclusive, the comparison is equivalent to + + ```text + a < v <= b + ``` + +- If `a` is inclusive and `b` is exclusive, the comparison is equivalent to + + ```text + a <= v < b + ``` + +- If `a` and `b` are exclusive, the comparison is equivalent to + + ```text + a < v < b + ``` + +- If provided non-numeric values, comparisons are performed according to lexicographic order. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var isBetween = require( '@stdlib/assert/is-between' ); + +var bool; +var a; +var b; +var v; +var i; + +for ( i = 0; i < 100; i++ ) { + a = round( (randu()*10.0) + 5.0 ); + b = round( (randu()*10.0) + 15.0 ); + v = round( randu()*25.0 ); + bool = isBetween( v, a, b, 'open', 'closed' ); + console.log( '%d < %d <= %d: %s', a, v, b, bool.toString() ); +} +``` + +
+ + + + + + diff --git a/is-between/benchmark/benchmark.js b/is-between/benchmark/benchmark.js new file mode 100644 index 00000000..dc7a0335 --- /dev/null +++ b/is-between/benchmark/benchmark.js @@ -0,0 +1,155 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isBetween = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( assert ) { + var bool; + var a; + var b; + var v; + var i; + + assert.tic(); + for ( i = 0; i < assert.iterations; i++ ) { + a = (randu()*10.0) + 5.0; + b = (randu()*10.0) + 15.0; + v = ( randu()*25.0 ); + bool = isBetween( v, a, b ); + if ( !isBoolean( bool ) ) { + assert.fail( 'should return a boolean' ); + } + } + assert.toc(); + if ( !isBoolean( bool ) ) { + assert.fail( 'should return a boolean' ); + } + assert.pass( 'benchmark finished' ); + assert.end(); +}); + +bench( pkg+':left=open,right=closed', function benchmark( assert ) { + var bool; + var a; + var b; + var v; + var i; + + assert.tic(); + for ( i = 0; i < assert.iterations; i++ ) { + a = (randu()*10.0) + 5.0; + b = (randu()*10.0) + 15.0; + v = ( randu()*25.0 ); + bool = isBetween( v, a, b, 'open', 'closed' ); + if ( !isBoolean( bool ) ) { + assert.fail( 'should return a boolean' ); + } + } + assert.toc(); + if ( !isBoolean( bool ) ) { + assert.fail( 'should return a boolean' ); + } + assert.pass( 'benchmark finished' ); + assert.end(); +}); + +bench( pkg+':left=open,right=open', function benchmark( assert ) { + var bool; + var a; + var b; + var v; + var i; + + assert.tic(); + for ( i = 0; i < assert.iterations; i++ ) { + a = (randu()*10.0) + 5.0; + b = (randu()*10.0) + 15.0; + v = ( randu()*25.0 ); + bool = isBetween( v, a, b, 'open', 'open' ); + if ( !isBoolean( bool ) ) { + assert.fail( 'should return a boolean' ); + } + } + assert.toc(); + if ( !isBoolean( bool ) ) { + assert.fail( 'should return a boolean' ); + } + assert.pass( 'benchmark finished' ); + assert.end(); +}); + +bench( pkg+':left=closed,right=closed', function benchmark( assert ) { + var bool; + var a; + var b; + var v; + var i; + + assert.tic(); + for ( i = 0; i < assert.iterations; i++ ) { + a = (randu()*10.0) + 5.0; + b = (randu()*10.0) + 15.0; + v = ( randu()*25.0 ); + bool = isBetween( v, a, b, 'closed', 'closed' ); + if ( !isBoolean( bool ) ) { + assert.fail( 'should return a boolean' ); + } + } + assert.toc(); + if ( !isBoolean( bool ) ) { + assert.fail( 'should return a boolean' ); + } + assert.pass( 'benchmark finished' ); + assert.end(); +}); + +bench( pkg+':left=closed,right=open', function benchmark( assert ) { + var bool; + var a; + var b; + var v; + var i; + + assert.tic(); + for ( i = 0; i < assert.iterations; i++ ) { + a = (randu()*10.0) + 5.0; + b = (randu()*10.0) + 15.0; + v = ( randu()*25.0 ); + bool = isBetween( v, a, b, 'closed', 'open' ); + if ( !isBoolean( bool ) ) { + assert.fail( 'should return a boolean' ); + } + } + assert.toc(); + if ( !isBoolean( bool ) ) { + assert.fail( 'should return a boolean' ); + } + assert.pass( 'benchmark finished' ); + assert.end(); +}); diff --git a/is-between/docs/repl.txt b/is-between/docs/repl.txt new file mode 100644 index 00000000..b3ebf0e2 --- /dev/null +++ b/is-between/docs/repl.txt @@ -0,0 +1,46 @@ + +{{alias}}( value, a, b[, left, right] ) + Tests if a value is between two values. + + Parameters + ---------- + value: any + Input value. + + a: any + Left comparison value. + + b: any + Right comparison value. + + left: string (optional) + Indicates whether the left comparison value is inclusive. Must be either + 'closed' or 'open'. Default: 'closed' (i.e., inclusive). + + right: string (optional) + Indicates whether the right comparison value is inclusive. Must be + either 'closed' or 'open'. Default: 'closed' (i.e., inclusive). + + Returns + ------- + bool: boolean + Boolean indicating whether a value is between two values. + + Examples + -------- + > var bool = {{alias}}( 3.14, 3.0, 4.0 ) + true + > bool = {{alias}}( 3.0, 3.0, 4.0 ) + true + > bool = {{alias}}( 4.0, 3.0, 4.0 ) + true + > bool = {{alias}}( 3.0, 3.14, 4.0 ) + false + > bool = {{alias}}( 3.14, 3.14, 4.0, 'open', 'closed' ) + false + > bool = {{alias}}( 3.14, 3.0, 3.14, 'closed', 'open' ) + false + + See Also + -------- + diff --git a/is-between/docs/types/index.d.ts b/is-between/docs/types/index.d.ts new file mode 100644 index 00000000..7be4767c --- /dev/null +++ b/is-between/docs/types/index.d.ts @@ -0,0 +1,65 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Comparison type. +*/ +type Comparison = 'open' | 'closed'; + +/** +* Tests if a value is between two values. +* +* @param value - value to test +* @param a - left comparison value +* @param b - right comparison value +* @param left - indicates whether the left comparison value is inclusive (default: 'closed') +* @param right - indicates whether the right comparison value is inclusive (default: 'closed') +* @returns boolean indicating whether a value is between two values +* +* @example +* var bool = isBetween( 3.14, 3.0, 4.0 ); +* // returns true +* +* @example +* var bool = isBetween( 4.5, 3.0, 4.0 ); +* // returns false +* +* @example +* var bool = isBetween( 3.14, 3.14, 4.0 ); +* // returns true +* +* @example +* var bool = isBetween( 3.14, 3.14, 4.0, 'open', 'closed' ); +* // returns false +* +* @example +* var bool = isBetween( 3.14, 3.0, 3.14 ); +* // returns true +* +* @example +* var bool = isBetween( 3.14, 3.0, 3.14, 'closed', 'open' ); +* // returns false +*/ +declare function isBetween( value: any, a: any, b: any, left?: Comparison, right?: Comparison ): boolean; // tslint-disable-line max-line-length + + +// EXPORTS // + +export = isBetween; diff --git a/is-between/docs/types/test.ts b/is-between/docs/types/test.ts new file mode 100644 index 00000000..fd59a5c0 --- /dev/null +++ b/is-between/docs/types/test.ts @@ -0,0 +1,58 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isBetween = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isBetween( 3, 1, 4 ); // $ExpectType boolean + isBetween( 3.14, 3.14, 4.0, 'open', 'closed' ); // $ExpectType boolean + isBetween( 3.14, 3.14, 4.0, 'open' ); // $ExpectType boolean +} + +// The function does not compile if provided a fourth argument that is not a recognized string... +{ + isBetween( 3.14, 3.14, 4.0, [] ); // $ExpectError + isBetween( 3.14, 3.14, 4.0, {} ); // $ExpectError + isBetween( 3.14, 3.14, 4.0, 123 ); // $ExpectError + isBetween( 3.14, 3.14, 4.0, 'abc' ); // $ExpectError + isBetween( 3.14, 3.14, 4.0, null ); // $ExpectError + isBetween( 3.14, 3.14, 4.0, true ); // $ExpectError + isBetween( 3.14, 3.14, 4.0, false ); // $ExpectError +} + +// The function does not compile if provided a fifth argument that is not a recognized string... +{ + isBetween( 3.14, 3.14, 4.0, 'open', [] ); // $ExpectError + isBetween( 3.14, 3.14, 4.0, 'open', {} ); // $ExpectError + isBetween( 3.14, 3.14, 4.0, 'open', 123 ); // $ExpectError + isBetween( 3.14, 3.14, 4.0, 'open', 'abc' ); // $ExpectError + isBetween( 3.14, 3.14, 4.0, 'open', null ); // $ExpectError + isBetween( 3.14, 3.14, 4.0, 'open', true ); // $ExpectError + isBetween( 3.14, 3.14, 4.0, 'open', false ); // $ExpectError +} + +// The function does not compile if provided insufficient arguments... +{ + isBetween(); // $ExpectError + isBetween( 1 ); // $ExpectError + isBetween( 1, 2 ); // $ExpectError +} diff --git a/is-between/examples/index.js b/is-between/examples/index.js new file mode 100644 index 00000000..e6e51753 --- /dev/null +++ b/is-between/examples/index.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var randu = require( '@stdlib/random/base/randu' ); +var round = require( '@stdlib/math/base/special/round' ); +var isBetween = require( './../lib' ); + +var bool; +var a; +var b; +var v; +var i; + +for ( i = 0; i < 100; i++ ) { + a = round( (randu()*10.0) + 5.0 ); + b = round( (randu()*10.0) + 15.0 ); + v = round( randu()*25.0 ); + bool = isBetween( v, a, b, 'open', 'closed' ); + console.log( '%d < %d <= %d: %s', a, v, b, bool.toString() ); +} diff --git a/is-between/lib/index.js b/is-between/lib/index.js new file mode 100644 index 00000000..2536953e --- /dev/null +++ b/is-between/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is between two values. +* +* @module @stdlib/assert/is-between +* +* @example +* var isBetween = require( '@stdlib/assert/is-between' ); +* +* var bool = isBetween( 3.14, 3.0, 4.0 ); +* // returns true +* +* bool = isBetween( 4.5, 3.0, 4.0 ); +* // returns false +* +* bool = isBetween( 3.14, 3.14, 4.0 ); +* // returns true +* +* bool = isBetween( 3.14, 3.14, 4.0, 'open', 'closed' ); +* // returns false +* +* bool = isBetween( 3.14, 3.0, 3.14 ); +* // returns true +* +* bool = isBetween( 3.14, 3.0, 3.14, 'closed', 'open' ); +* // returns false +*/ + +// MODULES // + +var isBetween = require( './main.js' ); + + +// EXPORTS // + +module.exports = isBetween; diff --git a/is-between/lib/main.js b/is-between/lib/main.js new file mode 100644 index 00000000..f7f0a361 --- /dev/null +++ b/is-between/lib/main.js @@ -0,0 +1,83 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests if a value is between two values. +* +* @param {*} value - value to test +* @param {*} a - left comparison value +* @param {*} b - right comparison value +* @param {string} [left="closed"] - indicates whether the left comparison value is inclusive +* @param {string} [right="closed"] - indicates whether the right comparison value is inclusive +* @throws {TypeError} `left` must be a recognized string +* @throws {TypeError} `right` must be a recognized string +* @returns {boolean} boolean indicating whether a value is between two values +* +* @example +* var bool = isBetween( 3.14, 3.0, 4.0 ); +* // returns true +* +* @example +* var bool = isBetween( 4.5, 3.0, 4.0 ); +* // returns false +* +* @example +* var bool = isBetween( 3.14, 3.14, 4.0 ); +* // returns true +* +* @example +* var bool = isBetween( 3.14, 3.14, 4.0, 'open', 'closed' ); +* // returns false +* +* @example +* var bool = isBetween( 3.14, 3.0, 3.14 ); +* // returns true +* +* @example +* var bool = isBetween( 3.14, 3.0, 3.14, 'closed', 'open' ); +* // returns false +*/ +function isBetween( value, a, b, left, right ) { + if ( arguments.length > 3 ) { + if ( left !== 'closed' && left !== 'open' ) { + throw new TypeError( 'invalid argument. `left` must be one of the following strings: \'closed\' or \'open\'. Value: `'+left+'`.' ); + } + if ( right !== 'closed' && right !== 'open' ) { + throw new TypeError( 'invalid argument. `right` must be one of the following strings: \'closed\' or \'open\'. Value: `'+right+'`.' ); + } + } + if ( left === 'closed' || left === void 0 ) { + if ( right === 'closed' || right === void 0 ) { + return ( value >= a && value <= b ); + } + return ( value >= a && value < b ); + } + if ( right === 'closed' || right === void 0 ) { + return ( value > a && value <= b ); + } + return ( value > a && value < b ); +} + + +// EXPORTS // + +module.exports = isBetween; diff --git a/is-between/package.json b/is-between/package.json new file mode 100644 index 00000000..c5b59e8b --- /dev/null +++ b/is-between/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-between", + "version": "0.0.0", + "description": "Test if a value is between two values.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "comparison", + "compare", + "order", + "interval", + "closed", + "open", + "range", + "between", + "btwn", + "is", + "isbetween", + "isbtwn", + "validate", + "valid", + "isvalid", + "test", + "check" + ] +} diff --git a/is-between/test/test.js b/is-between/test/test.js new file mode 100644 index 00000000..ae820402 --- /dev/null +++ b/is-between/test/test.js @@ -0,0 +1,189 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isBetween = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isBetween, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided an unrecognized `left` argument', function test( t ) { + var values; + var i; + + values = [ + 'beep', + 'boop', + 'left', + 'right', + 'ope', + 'close', + 5, + NaN, + null, + undefined, + true, + false, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + isBetween( 4, 3, 5, value, 'closed' ); + }; + } +}); + +tape( 'the function throws an error if provided an unrecognized `right` argument', function test( t ) { + var values; + var i; + + values = [ + 'beep', + 'boop', + 'left', + 'right', + 'ope', + 'close', + 5, + NaN, + null, + undefined, + true, + false, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + isBetween( 4, 3, 5, 'closed', value ); + }; + } +}); + +tape( 'the function returns `true` if provided a value which is between two values (default)', function test( t ) { + t.strictEqual( isBetween( 4, 3, 5 ), true, 'returns true' ); + t.strictEqual( isBetween( 3, 3, 5 ), true, 'returns true' ); + t.strictEqual( isBetween( 5, 3, 5 ), true, 'returns true' ); + t.strictEqual( isBetween( 'b', 'a', 'c' ), true, 'returns true' ); + t.strictEqual( isBetween( 'a', 'a', 'c' ), true, 'returns true' ); + t.strictEqual( isBetween( 'c', 'a', 'c' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a value which is between two values (closed,closed)', function test( t ) { + t.strictEqual( isBetween( 4, 3, 5, 'closed', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetween( 3, 3, 5, 'closed', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetween( 5, 3, 5, 'closed', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetween( 'b', 'a', 'c', 'closed', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetween( 'a', 'a', 'c', 'closed', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetween( 'c', 'a', 'c', 'closed', 'closed' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a value which is between two values (open,closed)', function test( t ) { + t.strictEqual( isBetween( 4, 3, 5, 'open', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetween( 5, 3, 5, 'open', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetween( 'b', 'a', 'c', 'open', 'closed' ), true, 'returns true' ); + t.strictEqual( isBetween( 'c', 'a', 'c', 'open', 'closed' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a value which is between two values (closed,open)', function test( t ) { + t.strictEqual( isBetween( 4, 3, 5, 'closed', 'open' ), true, 'returns true' ); + t.strictEqual( isBetween( 3, 3, 5, 'closed', 'open' ), true, 'returns true' ); + t.strictEqual( isBetween( 'b', 'a', 'c', 'closed', 'open' ), true, 'returns true' ); + t.strictEqual( isBetween( 'a', 'a', 'c', 'closed', 'open' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a value which is between two values (open,open)', function test( t ) { + t.strictEqual( isBetween( 4, 3, 5, 'open', 'open' ), true, 'returns true' ); + t.strictEqual( isBetween( 'b', 'a', 'c', 'open', 'open' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a value which is between two values (default)', function test( t ) { + t.strictEqual( isBetween( 2, 3, 5 ), false, 'returns false' ); + t.strictEqual( isBetween( 6, 3, 5 ), false, 'returns false' ); + t.strictEqual( isBetween( 'a', 'b', 'c' ), false, 'returns false' ); + t.strictEqual( isBetween( 'd', 'a', 'c' ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a value which is between two values (closed,closed)', function test( t ) { + t.strictEqual( isBetween( 2, 3, 5, 'closed', 'closed' ), false, 'returns false' ); + t.strictEqual( isBetween( 6, 3, 5, 'closed', 'closed' ), false, 'returns false' ); + t.strictEqual( isBetween( 'a', 'b', 'c', 'closed', 'closed' ), false, 'returns false' ); + t.strictEqual( isBetween( 'd', 'a', 'c', 'closed', 'closed' ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a value which is between two values (open,closed)', function test( t ) { + t.strictEqual( isBetween( 2, 3, 5, 'open', 'closed' ), false, 'returns false' ); + t.strictEqual( isBetween( 3, 3, 5, 'open', 'closed' ), false, 'returns false' ); + t.strictEqual( isBetween( 6, 3, 5, 'open', 'closed' ), false, 'returns false' ); + t.strictEqual( isBetween( 'a', 'b', 'c', 'open', 'closed' ), false, 'returns false' ); + t.strictEqual( isBetween( 'a', 'a', 'c', 'open', 'closed' ), false, 'returns false' ); + t.strictEqual( isBetween( 'd', 'a', 'c', 'open', 'closed' ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a value which is between two values (closed,open)', function test( t ) { + t.strictEqual( isBetween( 2, 3, 5, 'closed', 'open' ), false, 'returns false' ); + t.strictEqual( isBetween( 5, 3, 5, 'closed', 'open' ), false, 'returns false' ); + t.strictEqual( isBetween( 6, 3, 5, 'closed', 'open' ), false, 'returns false' ); + t.strictEqual( isBetween( 'a', 'b', 'c', 'closed', 'open' ), false, 'returns false' ); + t.strictEqual( isBetween( 'c', 'a', 'c', 'closed', 'open' ), false, 'returns false' ); + t.strictEqual( isBetween( 'd', 'a', 'c', 'closed', 'open' ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a value which is between two values (open,open)', function test( t ) { + t.strictEqual( isBetween( 2, 3, 5, 'open', 'open' ), false, 'returns false' ); + t.strictEqual( isBetween( 3, 3, 5, 'open', 'open' ), false, 'returns false' ); + t.strictEqual( isBetween( 5, 3, 5, 'open', 'open' ), false, 'returns false' ); + t.strictEqual( isBetween( 6, 3, 5, 'open', 'open' ), false, 'returns false' ); + t.strictEqual( isBetween( 'a', 'b', 'c', 'open', 'open' ), false, 'returns false' ); + t.strictEqual( isBetween( 'a', 'a', 'c', 'open', 'open' ), false, 'returns false' ); + t.strictEqual( isBetween( 'c', 'a', 'c', 'open', 'open' ), false, 'returns false' ); + t.strictEqual( isBetween( 'd', 'a', 'c', 'open', 'open' ), false, 'returns false' ); + t.end(); +}); diff --git a/is-big-endian/README.md b/is-big-endian/README.md new file mode 100644 index 00000000..5f36870e --- /dev/null +++ b/is-big-endian/README.md @@ -0,0 +1,108 @@ + + +# Big Endian + +> Check if an environment is [big endian][endianness]. + +
+ +## Usage + +```javascript +var IS_BIG_ENDIAN = require( '@stdlib/assert/is-big-endian' ); +``` + +#### IS_BIG_ENDIAN + +`Boolean` indicating if an environment is [big endian][endianness]. + +```javascript +var bool = IS_BIG_ENDIAN; +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var IS_BIG_ENDIAN = require( '@stdlib/assert/is-big-endian' ); + +console.log( IS_BIG_ENDIAN ); +// => +``` + +
+ + + +* * * + +
+## CLI + +
+ +### Usage + +```text +Usage: is-big-endian [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-big-endian + +``` + +
+ + + +
+ + + + + + diff --git a/is-big-endian/bin/cli b/is-big-endian/bin/cli new file mode 100644 index 00000000..2d19bbfe --- /dev/null +++ b/is-big-endian/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var isBigEndian = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( isBigEndian ); // eslint-disable-line no-console +} + +main(); diff --git a/is-big-endian/docs/repl.txt b/is-big-endian/docs/repl.txt new file mode 100644 index 00000000..565ea38c --- /dev/null +++ b/is-big-endian/docs/repl.txt @@ -0,0 +1,12 @@ + +{{alias}} + Boolean indicating if the environment is big endian. + + Examples + -------- + > {{alias}} + + + See Also + -------- + diff --git a/is-big-endian/docs/types/index.d.ts b/is-big-endian/docs/types/index.d.ts new file mode 100644 index 00000000..5ed92175 --- /dev/null +++ b/is-big-endian/docs/types/index.d.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Boolean indicating if the environment is big endian. +* +* @example +* var bool = IS_BIG_ENDIAN; +* // returns +*/ +declare const IS_BIG_ENDIAN: boolean; + + +// EXPORTS // + +export = IS_BIG_ENDIAN; diff --git a/is-big-endian/docs/types/test.ts b/is-big-endian/docs/types/test.ts new file mode 100644 index 00000000..2cd2d5bd --- /dev/null +++ b/is-big-endian/docs/types/test.ts @@ -0,0 +1,28 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import IS_BIG_ENDIAN = require( './index' ); + + +// TESTS // + +// The variable is a boolean... +{ + // tslint:disable-next-line:no-unused-expression + IS_BIG_ENDIAN; // $ExpectType boolean +} diff --git a/is-big-endian/docs/usage.txt b/is-big-endian/docs/usage.txt new file mode 100644 index 00000000..34f0d038 --- /dev/null +++ b/is-big-endian/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-big-endian [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-big-endian/etc/cli_opts.json b/is-big-endian/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-big-endian/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-big-endian/examples/index.js b/is-big-endian/examples/index.js new file mode 100644 index 00000000..e92589fd --- /dev/null +++ b/is-big-endian/examples/index.js @@ -0,0 +1,23 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var IS_BIG_ENDIAN = require( './../lib' ); + +console.log( IS_BIG_ENDIAN ); diff --git a/is-big-endian/lib/ctors.js b/is-big-endian/lib/ctors.js new file mode 100644 index 00000000..5d7a0e8c --- /dev/null +++ b/is-big-endian/lib/ctors.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint16Array = require( '@stdlib/array/uint16' ); + + +// MAIN // + +var ctors = { + 'uint16': Uint16Array, + 'uint8': Uint8Array +}; + + +// EXPORTS // + +module.exports = ctors; diff --git a/is-big-endian/lib/index.js b/is-big-endian/lib/index.js new file mode 100644 index 00000000..e264ab95 --- /dev/null +++ b/is-big-endian/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Boolean indicating if an environment is big endian. +* +* @module @stdlib/assert/is-big-endian +* +* @example +* var IS_BIG_ENDIAN = require( '@stdlib/assert/is-big-endian' ); +* +* var bool = IS_BIG_ENDIAN; +* // returns +*/ + +// MODULES // + +var IS_BIG_ENDIAN = require( './main.js' ); + + +// EXPORTS // + +module.exports = IS_BIG_ENDIAN; diff --git a/is-big-endian/lib/main.js b/is-big-endian/lib/main.js new file mode 100644 index 00000000..060ed95e --- /dev/null +++ b/is-big-endian/lib/main.js @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var ctors = require( './ctors.js' ); + + +// VARIABLES // + +var bool; + + +// FUNCTIONS // + +/** +* Returns a boolean indicating if an environment is big endian. +* +* @private +* @returns {boolean} boolean indicating if an environment is big endian +* +* @example +* var bool = isBigEndian(); +* // returns +*/ +function isBigEndian() { + var uint16view; + var uint8view; + + uint16view = new ctors[ 'uint16' ]( 1 ); + + /* + * Set the uint16 view to a value having distinguishable lower and higher order words. + * + * 4660 => 0x1234 => 0x12 0x34 => '00010010 00110100' => (0x12,0x34) == (18,52) + */ + uint16view[ 0 ] = 0x1234; + + // Create a uint8 view on top of the uint16 buffer: + uint8view = new ctors[ 'uint8' ]( uint16view.buffer ); + + // If big endian, the least significant byte will be last... + return ( uint8view[ 1 ] === 0x34 ); +} + + +// MAIN // + +bool = isBigEndian(); + + +// EXPORTS // + +module.exports = bool; diff --git a/is-big-endian/package.json b/is-big-endian/package.json new file mode 100644 index 00000000..0939a75a --- /dev/null +++ b/is-big-endian/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-big-endian", + "version": "0.0.0", + "description": "Check if an environment is big endian.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-big-endian": "./bin/cli" + }, + "main": "./lib", + "directories": { + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "le", + "be", + "little", + "endian", + "big", + "is", + "cli", + "byte order", + "check" + ] +} diff --git a/is-big-endian/test/test.cli.js b/is-big-endian/test/test.cli.js new file mode 100644 index 00000000..1319fc59 --- /dev/null +++ b/is-big-endian/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment is big endian', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/is-big-endian/test/test.js b/is-big-endian/test/test.js new file mode 100644 index 00000000..5ea09cc5 --- /dev/null +++ b/is-big-endian/test/test.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var IS_BIG_ENDIAN = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_BIG_ENDIAN, 'boolean', 'main export is a boolean' ); + t.end(); +}); + +tape( 'the boolean is `true` if an environment is big endian', function test( t ) { + var IS_BIG_ENDIAN = proxyquire( './../lib/main.js', { + './ctors.js': { + 'uint8': Foo + } + }); + + t.equal( IS_BIG_ENDIAN, true, 'is big endian' ); + + t.end(); + + // Mock big endian byte order, where least significant bits come last... + function Foo() { + return new Uint8Array( [ 18, 52 ] ); + } +}); + +tape( 'the boolean is `false` if an environment is not big endian (e.g., little endian)', function test( t ) { + var IS_BIG_ENDIAN = proxyquire( './../lib/main.js', { + './ctors.js': { + 'uint8': Foo + } + }); + + t.equal( IS_BIG_ENDIAN, false, 'is not big endian' ); + t.end(); + + // Mock big endian byte order, where most significant bits are last... + function Foo() { + return new Uint8Array( [ 52, 18 ] ); + } +}); diff --git a/is-bigint/README.md b/is-bigint/README.md new file mode 100644 index 00000000..c330495a --- /dev/null +++ b/is-bigint/README.md @@ -0,0 +1,134 @@ + + +# isBigInt + +> Test if a value is a [BigInt][mdn-bigint]. + +
+ +## Usage + +```javascript +var isBigInt = require( '@stdlib/assert/is-bigint' ); +``` + +#### isBigInt( value ) + +Tests if a value is a [`BigInt`][mdn-bigint]. + +```javascript +var BigInt = require( '@stdlib/bigint/ctor' ); + +var bool = isBigInt( BigInt( '1' ) ); +// returns true + +bool = isBigInt( Object( BigInt( '1' ) ) ); +// returns true +``` + +#### isBigInt.isPrimitive( value ) + +Tests if a `value` is a primitive [`BigInt`][mdn-bigint]. + +```javascript +var BigInt = require( '@stdlib/bigint/ctor' ); + +var bool = isBigInt.isPrimitive( BigInt( '1' ) ); +// returns true + +bool = isBigInt.isPrimitive( Object( BigInt( '1' ) ) ); +// returns false +``` + +#### isBigInt.isObject( value ) + +Tests if a `value` is a [`BigInt`][mdn-bigint] object. + +```javascript +var BigInt = require( '@stdlib/bigint/ctor' ); + +var bool = isBigInt.isObject( BigInt( '1' ) ); +// returns false + +bool = isBigInt.isObject( Object( BigInt( '1' ) ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' ); +var BigInt = require( '@stdlib/bigint/ctor' ); +var isBigInt = require( '@stdlib/assert/is-bigint' ); + +var bool; +if ( hasBigIntSupport() ) { + bool = isBigInt( BigInt( '1' ) ); + // returns true +} else { + console.log( 'Environment does not support BigInts.' ); +} +bool = isBigInt( 1 ); +// returns false + +bool = isBigInt( '1' ); +// returns false + +bool = isBigInt( {} ); +// returns false + +bool = isBigInt( [] ); +// returns false + +bool = isBigInt( null ); +// returns false + +bool = isBigInt( void 0 ); +// returns false + +bool = isBigInt( true ); +// returns false + +bool = isBigInt( function foo() {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-bigint/benchmark/benchmark.js b/is-bigint/benchmark/benchmark.js new file mode 100644 index 00000000..fcb6dc26 --- /dev/null +++ b/is-bigint/benchmark/benchmark.js @@ -0,0 +1,374 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* global BigInt */ + +/* eslint-disable no-new-wrappers, no-empty-function, stdlib/require-globals */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var hasBigInts = require( '@stdlib/assert/has-bigint-support' ); +var pkg = require( './../package.json' ).name; +var isBigInt = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasBigInts() +}; + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBigInt( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new String( 'beep' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBigInt( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBigInt.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new String( 'beep' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBigInt.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBigInt.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new String( 'beep' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBigInt.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives,bigints', opts, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + BigInt( '1' ), + BigInt( '2' ), + BigInt( 1 ), + BigInt( 2 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBigInt( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects,bigints', opts, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + Object( BigInt( '1' ) ), + Object( BigInt( '2' ) ), + Object( BigInt( 1 ) ), + Object( BigInt( 2 ) ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBigInt( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives,bigints:isPrimitive', opts, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + BigInt( '1' ), + BigInt( '2' ), + BigInt( 1 ), + BigInt( 2 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBigInt.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects,bigints:isPrimitive', opts, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + Object( BigInt( '1' ) ), + Object( BigInt( '2' ) ), + Object( BigInt( 1 ) ), + Object( BigInt( 2 ) ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBigInt.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives,bigints:isObject', opts, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + BigInt( '1' ), + BigInt( '2' ), + BigInt( 1 ), + BigInt( 2 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBigInt.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects,bigints:isObject', opts, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + Object( BigInt( '1' ) ), + Object( BigInt( '2' ) ), + Object( BigInt( 1 ) ), + Object( BigInt( 2 ) ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBigInt.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-bigint/docs/repl.txt b/is-bigint/docs/repl.txt new file mode 100644 index 00000000..88c5063d --- /dev/null +++ b/is-bigint/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( value ) + Tests if a value is a BigInt. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a BigInt. + + Examples + -------- + > var bool = {{alias}}( {{alias:@stdlib/bigint/ctor}}( '1' ) ) + true + > bool = {{alias}}( Object( {{alias:@stdlib/bigint/ctor}}( '1' ) ) ) + true + > bool = {{alias}}( {} ) + false + > bool = {{alias}}( null ) + false + > bool = {{alias}}( true ) + false + + See Also + -------- + diff --git a/is-bigint/docs/types/index.d.ts b/is-bigint/docs/types/index.d.ts new file mode 100644 index 00000000..88158127 --- /dev/null +++ b/is-bigint/docs/types/index.d.ts @@ -0,0 +1,145 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isBigInt` with methods for testing for primitives and objects, respectively. +*/ +interface IsBigInt { + /** + * Tests if a value is a BigInt. + * + * @param value - value to test + * @returns boolean indicating whether value is a BigInt + * + * @example + * var BigInt = require( `@stdlib/bigint/ctor` ); + * + * var bool = isBigInt( BigInt( '1' ) ); + * // returns true + * + * @example + * var BigInt = require( `@stdlib/bigint/ctor` ); + * + * var bool = isBigInt( Object( BigInt( '1' ) ) ); + * // returns true + * + * @example + * var bool = isBigInt( {} ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a BigInt primitive. + * + * @param value - value to test + * @returns boolean indicating if a value is a BigInt primitive + * + * @example + * var BigInt = require( `@stdlib/bigint/ctor` ); + * + * var bool = isBigInt.isPrimitive( BigInt( '1' ) ); + * // returns true + * + * @example + * var BigInt = require( `@stdlib/bigint/ctor` ); + * + * var bool = isBigInt.isPrimitive( Object( BigInt( '1' ) ) ); + * // returns false + * + * @example + * var bool = isBigInt.isPrimitive( {} ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a BigInt object. + * + * @param value - value to test + * @returns boolean indicating if a value is a BigInt object + * + * @example + * var BigInt = require( `@stdlib/bigint/ctor` ); + * + * var bool = isBigInt.isObject( BigInt( '1' ) ); + * // returns false + * + * @example + * var BigInt = require( `@stdlib/bigint/ctor` ); + * + * var bool = isBigInt.isObject( Object( BigInt( '1' ) ) ); + * // returns true + * + * @example + * var bool = isBigInt.isObject( {} ); + * // returns false + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a BigInt. +* +* @param value - value to test +* @returns boolean indicating whether value is a BigInt +* +* @example +* var BigInt = require( `@stdlib/bigint/ctor` ); +* +* var bool = isBigInt.isPrimitive( BigInt( '1' ) ); +* // returns true +* +* @example +* var BigInt = require( `@stdlib/bigint/ctor` ); +* +* var bool = isBigInt.isPrimitive( Object( BigInt( '1' ) ) ); +* // returns false +* +* @example +* var BigInt = require( `@stdlib/bigint/ctor` ); +* +* var bool = isBigInt.isPrimitive( BigInt( '1' ) ); +* // returns true +* +* @example +* var BigInt = require( `@stdlib/bigint/ctor` ); +* +* var bool = isBigInt.isPrimitive( Object( BigInt( '1' ) ) ); +* // returns false +* +* @example +* var BigInt = require( `@stdlib/bigint/ctor` ); +* +* var bool = isBigInt.isObject( BigInt( '1' ) ); +* // returns false +* +* @example +* var BigInt = require( `@stdlib/bigint/ctor` ); +* +* var bool = isBigInt.isObject( Object( BigInt( '1' ) ) ); +* // returns true +*/ +declare var isBigInt: IsBigInt; + + +// EXPORTS // + +export = isBigInt; diff --git a/is-bigint/docs/types/test.ts b/is-bigint/docs/types/test.ts new file mode 100644 index 00000000..75406ef8 --- /dev/null +++ b/is-bigint/docs/types/test.ts @@ -0,0 +1,58 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isBigInt = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isBigInt( 1 ); // $ExpectType boolean + isBigInt( 'abc' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isBigInt(); // $ExpectError + isBigInt( 1, 123 ); // $ExpectError +} + +// Attached to main export is an `isPrimitive` method which returns a boolean... +{ + isBigInt.isPrimitive( 1 ); // $ExpectType boolean + isBigInt.isPrimitive( Object( 1 ) ); // $ExpectType boolean +} + +// The compiler throws an error if the `isPrimitive` method is provided an unsupported number of arguments... +{ + isBigInt.isPrimitive(); // $ExpectError + isBigInt.isPrimitive( 1, 123 ); // $ExpectError +} + +// Attached to main export is an `isObject` method which returns a boolean... +{ + isBigInt.isObject( 1 ); // $ExpectType boolean + isBigInt.isObject( Object( 1 ) ); // $ExpectType boolean +} + +// The compiler throws an error if the `isObject` method is provided an unsupported number of arguments... +{ + isBigInt.isObject(); // $ExpectError + isBigInt.isObject( 1, 123 ); // $ExpectError +} diff --git a/is-bigint/examples/index.js b/is-bigint/examples/index.js new file mode 100644 index 00000000..3e1f7206 --- /dev/null +++ b/is-bigint/examples/index.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-restricted-syntax, no-empty-function */ + +'use strict'; + +var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' ); +var BigInt = require( '@stdlib/bigint/ctor' ); +var isBigInt = require( './../lib' ); + +var bool; +if ( hasBigIntSupport() ) { + bool = isBigInt( BigInt( '1' ) ); + console.log( bool ); + // => true +} else { + console.log( 'Environment does not support BigInts.' ); +} +bool = isBigInt( 1 ); +console.log( bool ); +// => false + +bool = isBigInt( '1' ); +console.log( bool ); +// => false + +bool = isBigInt( {} ); +console.log( bool ); +// => false + +bool = isBigInt( [] ); +console.log( bool ); +// => false + +bool = isBigInt( null ); +console.log( bool ); +// => false + +bool = isBigInt( void 0 ); +console.log( bool ); +// => false + +bool = isBigInt( true ); +console.log( bool ); +// => false + +bool = isBigInt( function foo() {} ); +console.log( bool ); +// => false diff --git a/is-bigint/lib/generic.js b/is-bigint/lib/generic.js new file mode 100644 index 00000000..d09df5e9 --- /dev/null +++ b/is-bigint/lib/generic.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a BigInt. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a BigInt +*/ +function isBigInt( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isBigInt; diff --git a/is-bigint/lib/index.js b/is-bigint/lib/index.js new file mode 100644 index 00000000..6455fa39 --- /dev/null +++ b/is-bigint/lib/index.js @@ -0,0 +1,83 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a BigInt. +* +* @module @stdlib/assert/is-bigint +* +* @example +* var BigInt = require( '@stdlib/bigint/ctor' ); +* var isBigInt = require( '@stdlib/assert/is-bigint' ); +* +* var bool = isBigInt( BigInt( '1' ) ); +* // returns true +* +* bool = isBigInt( Object( BigInt( '1' ) ) ); +* // returns true +* +* bool = isBigInt( {} ); +* // returns false +* +* @example +* var isBigInt = require( '@stdlib/assert/is-bigint' ).isPrimitive; +* +* var bool = isBigInt( BigInt( '1' ) ); +* // returns true +* +* bool = isBigInt( Object( BigInt( '1' ) ) ); +* // returns false +* +* bool = isBigInt( {} ); +* // returns false +* +* @example +* var isBigIntObject = require( '@stdlib/assert/is-bigint' ).isObject; +* +* var bool = isBigIntObject( BigInt( '1' ) ); +* // returns false +* +* bool = isBigIntObject( Object( BigInt( '1' ) ) ); +* // returns true +* +* bool = isBigIntObject( {} ); +* // returns false +*/ + +// MODULES // + +var hasBigInts = require( '@stdlib/assert/has-bigint-support' ); +var main = require( './main.js' ); +var polyfill = require( './polyfill.js' ); + + +// MAIN // + +var isBigInt; +if ( hasBigInts() ) { + isBigInt = main; +} else { + isBigInt = polyfill; +} + + +// EXPORTS // + +module.exports = isBigInt; diff --git a/is-bigint/lib/main.js b/is-bigint/lib/main.js new file mode 100644 index 00000000..ca8c1af3 --- /dev/null +++ b/is-bigint/lib/main.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isSymbol = require( './generic.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isSymbol, 'isPrimitive', isPrimitive ); +setReadOnly( isSymbol, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isSymbol; diff --git a/is-bigint/lib/object.js b/is-bigint/lib/object.js new file mode 100644 index 00000000..a607a69f --- /dev/null +++ b/is-bigint/lib/object.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); +var test = require( './try2valueof.js' ); + + +// MAIN // + +/** +* Tests if a value is a BigInt object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a BigInt object +*/ +function BigInt( value ) { + return ( + typeof value === 'object' && + nativeClass( value ) === '[object BigInt]' && + test( value ) + ); +} + + +// EXPORTS // + +module.exports = BigInt; diff --git a/is-bigint/lib/polyfill.js b/is-bigint/lib/polyfill.js new file mode 100644 index 00000000..9bd73ba1 --- /dev/null +++ b/is-bigint/lib/polyfill.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var constantFunction = require( '@stdlib/utils/constant-function' ); + + +// MAIN // + +var isBigInt = constantFunction( false ); +var isPrimitive = constantFunction( false ); +var isObject = constantFunction( false ); + +setReadOnly( isBigInt, 'isPrimitive', isPrimitive ); +setReadOnly( isBigInt, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isBigInt; diff --git a/is-bigint/lib/primitive.js b/is-bigint/lib/primitive.js new file mode 100644 index 00000000..8158c5b2 --- /dev/null +++ b/is-bigint/lib/primitive.js @@ -0,0 +1,34 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Tests if a value is a BigInt primitive. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a BigInt primitive +*/ +function isBigInt( value ) { + return ( typeof value === 'bigint' ); +} + + +// EXPORTS // + +module.exports = isBigInt; diff --git a/is-bigint/lib/try2valueof.js b/is-bigint/lib/try2valueof.js new file mode 100644 index 00000000..21ffd1aa --- /dev/null +++ b/is-bigint/lib/try2valueof.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Attempts to call a `BigInt` method. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating if able to call a `BigInt` method +*/ +function test( value ) { + try { + return ( + // Objects created via `Object.create( null )` have no `valueOf()` method: + typeof value.valueOf() === 'bigint' + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + return false; + } +} + + +// EXPORTS // + +module.exports = test; diff --git a/is-bigint/package.json b/is-bigint/package.json new file mode 100644 index 00000000..19336925 --- /dev/null +++ b/is-bigint/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-bigint", + "version": "0.0.0", + "description": "Test if a value is a BigInt.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "bigint", + "integer", + "is", + "isbigint", + "es2020", + "type", + "check", + "primitive", + "object", + "valid", + "validate", + "test", + "isvalid" + ] +} diff --git a/is-bigint/test/test.generic.js b/is-bigint/test/test.generic.js new file mode 100644 index 00000000..4eace48f --- /dev/null +++ b/is-bigint/test/test.generic.js @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var BigInt = require( '@stdlib/bigint/ctor' ); +var hasBigInts = require( '@stdlib/assert/has-bigint-support' ); +var isBigInt = require( './../lib/generic.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasBigInts() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isBigInt, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a BigInt', opts, function test( t ) { + t.strictEqual( isBigInt( BigInt( '1' ) ), true, 'returns true' ); + t.strictEqual( isBigInt( Object( BigInt( '1' ) ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a BigInt', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isBigInt( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-bigint/test/test.js b/is-bigint/test/test.js new file mode 100644 index 00000000..e603fe9d --- /dev/null +++ b/is-bigint/test/test.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var isBigInt = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isBigInt, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive BigInt', function test( t ) { + t.strictEqual( typeof isBigInt.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a BigInt object', function test( t ) { + t.strictEqual( typeof isBigInt.isObject, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'if an environment does not support BigInts, the main export is a polyfill which always returns `false`', function test( t ) { + var isBigInt; + var values; + var i; + + isBigInt = proxyquire( './../lib', { + '@stdlib/assert/has-bigint-support': hasSupport + }); + + t.strictEqual( isBigInt, require( './../lib/polyfill.js' ), 'exports a polyfill' ); + + values = [ + '5', + 5, + NaN, + null, + void 0, + false, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isBigInt( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + t.strictEqual( isBigInt.isObject( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + t.strictEqual( isBigInt.isPrimitive( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); + + function hasSupport() { + return false; + } +}); + +tape( 'if an environment does support BigInts, the main export is not the polyfill', function test( t ) { + var isBigInt = proxyquire( './../lib', { + '@stdlib/assert/has-bigint-support': hasSupport, + './main.js': isBigInteger + }); + + t.strictEqual( isBigInt, isBigInteger, 'exports expected function' ); + t.end(); + + function hasSupport() { + return true; + } + + function isBigInteger() { + return false; + } +}); diff --git a/is-bigint/test/test.main.js b/is-bigint/test/test.main.js new file mode 100644 index 00000000..6d86c482 --- /dev/null +++ b/is-bigint/test/test.main.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isBigInt = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isBigInt, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive BigInt', function test( t ) { + t.strictEqual( typeof isBigInt.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a BigInt object', function test( t ) { + t.strictEqual( typeof isBigInt.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-bigint/test/test.object.js b/is-bigint/test/test.object.js new file mode 100644 index 00000000..48bfa580 --- /dev/null +++ b/is-bigint/test/test.object.js @@ -0,0 +1,108 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var BigInt = require( '@stdlib/bigint/ctor' ); +var hasBigInts = require( '@stdlib/assert/has-bigint-support' ); +var isBigInt = require( './../lib/object.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasBigInts() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isBigInt, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a BigInt object', opts, function test( t ) { + t.strictEqual( isBigInt( Object( BigInt( '1' ) ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive BigInt', opts, function test( t ) { + t.strictEqual( isBigInt( BigInt( '1' ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a BigInt', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isBigInt( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a BigInt, even if `toStringTag` is overridden (in environments supporting `toStringTag`)', function test( t ) { + var isBigInt; + var values; + var i; + + isBigInt = proxyquire( './../lib/object.js', { + '@stdlib/utils/native-class': nativeClass + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isBigInt( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); + + function nativeClass() { + return '[object BigInt]'; + } +}); diff --git a/is-bigint/test/test.polyfill.js b/is-bigint/test/test.polyfill.js new file mode 100644 index 00000000..73ebd310 --- /dev/null +++ b/is-bigint/test/test.polyfill.js @@ -0,0 +1,112 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isBigInt = require( './../lib/polyfill.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isBigInt, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive BigInt', function test( t ) { + t.strictEqual( typeof isBigInt.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a BigInt object', function test( t ) { + t.strictEqual( typeof isBigInt.isObject, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the main export always returns `false`', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + void 0, + false, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isBigInt( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the `isPrimitive` method always returns `false`', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + void 0, + false, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isBigInt.isPrimitive( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the `isObject` method always returns `false`', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + void 0, + false, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isBigInt.isObject( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); diff --git a/is-bigint/test/test.primitive.js b/is-bigint/test/test.primitive.js new file mode 100644 index 00000000..e6588f2e --- /dev/null +++ b/is-bigint/test/test.primitive.js @@ -0,0 +1,75 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var BigInt = require( '@stdlib/bigint/ctor' ); +var hasBigInts = require( '@stdlib/assert/has-bigint-support' ); +var isBigInt = require( './../lib/primitive.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasBigInts() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isBigInt, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive BigInt', opts, function test( t ) { + t.strictEqual( isBigInt( BigInt( '1' ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a BigInt object', opts, function test( t ) { + t.strictEqual( isBigInt( Object( BigInt( '1' ) ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a BigInt', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isBigInt( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-bigint/test/test.try2valueof.js b/is-bigint/test/test.try2valueof.js new file mode 100644 index 00000000..d60409a7 --- /dev/null +++ b/is-bigint/test/test.try2valueof.js @@ -0,0 +1,89 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var BigInt = require( '@stdlib/bigint/ctor' ); +var hasBigInts = require( '@stdlib/assert/has-bigint-support' ); +var tryValueOf = require( './../lib/try2valueof.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasBigInts() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof tryValueOf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a BigInt', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( tryValueOf( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided an empty object without a prototype', function test( t ) { + var o = Object.create( null ); + t.strictEqual( tryValueOf( o ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `true` if able to successfully call a BigInt method', opts, function test( t ) { + var o = Object.create( null ); + o.valueOf = valueOf; + + t.strictEqual( tryValueOf( o ), true, 'returns true' ); + t.end(); + + function valueOf() { + return BigInt( '1' ); + } +}); + +tape( 'if an environment supports BigInts, the function returns `true`', opts, function test( t ) { + t.strictEqual( tryValueOf( BigInt( '1' ) ), true, 'returns true' ); + t.strictEqual( tryValueOf( Object( BigInt( '1' ) ) ), true, 'returns true' ); + t.end(); +}); diff --git a/is-binary-string/README.md b/is-binary-string/README.md new file mode 100644 index 00000000..47728557 --- /dev/null +++ b/is-binary-string/README.md @@ -0,0 +1,137 @@ + + +# isBinaryString + +> Test if a value is a binary string. + +
+ +## Usage + +```javascript +var isBinaryString = require( '@stdlib/assert/is-binary-string' ); +``` + +#### isBinaryString( value ) + +Tests if a `value` is a binary `string`; i.e., a character sequence of `1`'s and `0`'s. + +```javascript +var bool = isBinaryString( '1000101' ); +// returns true + +bool = isBinaryString( 'beep' ); +// returns false + +bool = isBinaryString( '' ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isBinaryString = require( '@stdlib/assert/is-binary-string' ); + +var bool = isBinaryString( '1' ); +// returns true + +bool = isBinaryString( '0' ); +// returns true + +bool = isBinaryString( '101010101001' ); +// returns true + +bool = isBinaryString( '' ); +// returns false + +bool = isBinaryString( 'beep' ); +// returns false + +bool = isBinaryString( null ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-binary-string [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-binary-string 01234 +false +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n '0110' | is-binary-string +true +``` + +
+ + + +
+ + + + + + diff --git a/is-binary-string/benchmark/benchmark.js b/is-binary-string/benchmark/benchmark.js new file mode 100644 index 00000000..b250dc2d --- /dev/null +++ b/is-binary-string/benchmark/benchmark.js @@ -0,0 +1,106 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var isBinaryString = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - string length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var str; + var i; + + str = ''; + for ( i = 0; i < len-1; i++ ) { + if ( randu() < 0.5 ) { + str += '0'; + } else { + str += '1'; + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBinaryString( str + (i%10) ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/is-binary-string/bin/cli b/is-binary-string/bin/cli new file mode 100644 index 00000000..40c53202 --- /dev/null +++ b/is-binary-string/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isBinaryString = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isBinaryString( String( args[ 0 ] ) ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isBinaryString( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-binary-string/docs/repl.txt b/is-binary-string/docs/repl.txt new file mode 100644 index 00000000..a0468b9b --- /dev/null +++ b/is-binary-string/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( value ) + Tests if a value is a binary string. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a binary string. + + Examples + -------- + > var bool = {{alias}}( '1000101' ) + true + > bool = {{alias}}( 'beep' ) + false + > bool = {{alias}}( '' ) + false + + See Also + -------- + diff --git a/is-binary-string/docs/types/index.d.ts b/is-binary-string/docs/types/index.d.ts new file mode 100644 index 00000000..5d14b05b --- /dev/null +++ b/is-binary-string/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a binary string. +* +* @param value - value to test +* @returns boolean indicating if an input value is a binary string +* +* @example +* var bool = isBinaryString( '1000101' ); +* // returns true +* +* @example +* var bool = isBinaryString( 'beep' ); +* // returns false +* +* @example +* var bool = isBinaryString( '' ); +* // returns false +*/ +declare function isBinaryString( value: any ): boolean; + + +// EXPORTS // + +export = isBinaryString; diff --git a/is-binary-string/docs/types/test.ts b/is-binary-string/docs/types/test.ts new file mode 100644 index 00000000..798438cd --- /dev/null +++ b/is-binary-string/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isBinaryString = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isBinaryString( 'beep' ); // $ExpectType boolean + isBinaryString( '00011' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isBinaryString(); // $ExpectError + isBinaryString( undefined, 123 ); // $ExpectError +} diff --git a/is-binary-string/docs/usage.txt b/is-binary-string/docs/usage.txt new file mode 100644 index 00000000..85c73b79 --- /dev/null +++ b/is-binary-string/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-binary-string [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-binary-string/etc/cli_opts.json b/is-binary-string/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-binary-string/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-binary-string/examples/index.js b/is-binary-string/examples/index.js new file mode 100644 index 00000000..476973bc --- /dev/null +++ b/is-binary-string/examples/index.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isBinaryString = require( './../lib' ); + +console.log( isBinaryString( '1' ) ); +// => true + +console.log( isBinaryString( '0' ) ); +// => true + +console.log( isBinaryString( '101010101001' ) ); +// => true + +console.log( isBinaryString( '' ) ); +// => false + +console.log( isBinaryString( 'beep' ) ); +// => false + +console.log( isBinaryString( null ) ); +// => false diff --git a/is-binary-string/lib/index.js b/is-binary-string/lib/index.js new file mode 100644 index 00000000..fb767f78 --- /dev/null +++ b/is-binary-string/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a binary string. +* +* @module @stdlib/assert/is-binary-string +* +* @example +* var isBinaryString = require( '@stdlib/assert/is-binary-string' ); +* +* var bool = isBinaryString( '1000101' ); +* // returns true +* +* bool = isBinaryString( 'beep' ); +* // returns false +* +* bool = isBinaryString( '' ); +* // returns false +*/ + +// MODULES // + +var isBinaryString = require( './main.js' ); + + +// EXPORTS // + +module.exports = isBinaryString; diff --git a/is-binary-string/lib/main.js b/is-binary-string/lib/main.js new file mode 100644 index 00000000..197c13f8 --- /dev/null +++ b/is-binary-string/lib/main.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a binary string. +* +* @param {*} str - value to test +* @returns {boolean} boolean indicating if an input value is a binary string +* +* @example +* var bool = isBinaryString( '1000101' ); +* // returns true +* +* @example +* var bool = isBinaryString( 'beep' ); +* // returns false +* +* @example +* var bool = isBinaryString( '' ); +* // returns false +*/ +function isBinaryString( str ) { + var ch; + var i; + if ( !isString( str ) ) { + return false; + } + if ( str.length === 0 ) { + return false; + } + for ( i = 0; i < str.length; i++ ) { + ch = str[ i ]; + if ( ch !== '1' && ch !== '0' ) { + return false; + } + } + return true; +} + + +// EXPORTS // + +module.exports = isBinaryString; diff --git a/is-binary-string/package.json b/is-binary-string/package.json new file mode 100644 index 00000000..3fec6f92 --- /dev/null +++ b/is-binary-string/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-binary-string", + "version": "0.0.0", + "description": "Test if a value is a binary string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-binary-string": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "binary", + "string", + "isstring", + "isbinary", + "binarystring", + "isvalid", + "check" + ] +} diff --git a/is-binary-string/test/fixtures/stdin_error.js.txt b/is-binary-string/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-binary-string/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-binary-string/test/test.cli.js b/is-binary-string/test/test.cli.js new file mode 100644 index 00000000..6728bdfe --- /dev/null +++ b/is-binary-string/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument contains only zeros and ones', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'0111011\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "01111011\n123456"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\nfalse\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-binary-string/test/test.js b/is-binary-string/test/test.js new file mode 100644 index 00000000..62e7f126 --- /dev/null +++ b/is-binary-string/test/test.js @@ -0,0 +1,81 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isBinaryString = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isBinaryString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a binary string', function test( t ) { + var values; + var i; + + values = [ + '0', + '1', + '10101', + '000000000000000000000000000000000000000000000000000000000000000000000000000000', + '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111' + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isBinaryString( values[i] ), true, 'returns true' ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided a `String` object, even if the value of the `String` object is a binary string', function test( t ) { + t.equal( isBinaryString( new String( '0101' ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a binary string', function test( t ) { + var values; + var i; + + values = [ + '5', + 'beep', + '', + '1010102', + 5, + null, + NaN, + true, + undefined, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isBinaryString( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-boolean-array/README.md b/is-boolean-array/README.md new file mode 100644 index 00000000..70845cf2 --- /dev/null +++ b/is-boolean-array/README.md @@ -0,0 +1,109 @@ + + +# isBooleanArray + +> Test if a value is an array-like object of booleans. + +
+ +## Usage + +```javascript +var isBooleanArray = require( '@stdlib/assert/is-boolean-array' ); +``` + +#### isBooleanArray( value ) + +Tests if a `value` is an array-like object of `booleans`. + +```javascript +var bool = isBooleanArray( [ true, false, false, true ] ); +// returns true +``` + +#### isBooleanArray.primitives( value ) + +Tests if a `value` is an array-like object containing **only** `boolean` primitives. + + + +```javascript +var bool = isBooleanArray.primitives( [ true, false ] ); +// returns true + +bool = isBooleanArray.primitives( [ false, new Boolean( true ) ] ); +// returns false +``` + +#### isBooleanArray.objects( value ) + +Tests if a `value` is an array-like object containing **only** `Boolean` objects. + + + +```javascript +var bool = isBooleanArray.objects( [ new Boolean( false ), new Boolean( true ) ] ); +// returns true + +bool = isBooleanArray.objects( [ new Boolean( false ), true ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isBooleanArray = require( '@stdlib/assert/is-boolean-array' ); + +var bool = isBooleanArray( [ true, false ] ); +// returns true + +bool = isBooleanArray( [ true, new Boolean( false ) ] ); +// returns true + +bool = isBooleanArray( [ true, 'false' ] ); +// returns false + +bool = isBooleanArray( [] ); +// returns false + +bool = isBooleanArray( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-boolean-array/benchmark/benchmark.js b/is-boolean-array/benchmark/benchmark.js new file mode 100644 index 00000000..ee4dd5ae --- /dev/null +++ b/is-boolean-array/benchmark/benchmark.js @@ -0,0 +1,127 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var isBooleanArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + if ( randu() < 0.5 ) { + x.push( false ); + } else { + x.push( true ); + } + } + } else { + for ( i = 0; i < len; i++ ) { + if ( randu() < 0.5 ) { + x.push( new Boolean( false ) ); // eslint-disable-line no-new-wrappers + } else { + x.push( new Boolean( true ) ); // eslint-disable-line no-new-wrappers + } + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isBooleanArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isBooleanArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isBooleanArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-boolean-array/docs/repl.txt b/is-boolean-array/docs/repl.txt new file mode 100644 index 00000000..7a93f461 --- /dev/null +++ b/is-boolean-array/docs/repl.txt @@ -0,0 +1,68 @@ + +{{alias}}( value ) + Tests if a value is an array-like object of booleans. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object of booleans. + + Examples + -------- + > var bool = {{alias}}( [ true, false, true ] ) + true + > bool = {{alias}}( [ true, 'abc', false ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only boolean primitives. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + boolean primitives. + + Examples + -------- + > var bool = {{alias}}.primitives( [ true, false ] ) + true + > bool = {{alias}}.primitives( [ false, new Boolean( true ) ] ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only Boolean objects. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + Boolean objects. + + Examples + -------- + > var bool = {{alias}}.objects( [ new Boolean( false ), true ] ) + false + > bool = {{alias}}.objects( [ new Boolean( false ), new Boolean( true ) ] ) + true + + See Also + -------- + diff --git a/is-boolean-array/docs/types/index.d.ts b/is-boolean-array/docs/types/index.d.ts new file mode 100644 index 00000000..c7eb6b0c --- /dev/null +++ b/is-boolean-array/docs/types/index.d.ts @@ -0,0 +1,101 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isBooleanArray` with methods for testing for primitives and objects, respectively. +*/ +interface IsBooleanArray { + /** + * Tests if a value is an array-like object of booleans. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object of booleans + * + * @example + * var bool = isBooleanArray( [ true, false, true ] ); + * // returns true + * + * @example + * var bool = isBooleanArray( [ true, 'abc', false ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only boolean primitives. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only boolean primitives + * + * @example + * var bool = isBooleanArray.primitives( [ true, false ] ); + * // returns true + * + * @example + * var bool = isBooleanArray.primitives( [ false, new Boolean( true ) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only Boolean objects. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only Boolean objects + * + * @example + * var bool = isBooleanArray.objects( [ new Boolean( false ), new Boolean( true ) ] ); + * // returns true + * + * @example + * var bool = isBooleanArray.objects( [ new Boolean( false ), true ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object of booleans. +* +* @param value - value to test +* @returns boolean indicating whether value is an array-like object of booleans +* +* @example +* var bool = isBooleanArray( [ true, false, true ] ); +* // returns true +* +* @example +* var bool = isBooleanArray( [ true, 'abc', false ] ); +* // returns false +* +* @example +* var bool = isBooleanArray.primitives( [ true, false ] ); +* // returns true +* +* @example +* var bool = isBooleanArray.objects( [ new Boolean( false ), new Boolean( true ) ] ); +* // returns true +*/ +declare var isBooleanArray: IsBooleanArray; + + +// EXPORTS // + +export = isBooleanArray; diff --git a/is-boolean-array/docs/types/test.ts b/is-boolean-array/docs/types/test.ts new file mode 100644 index 00000000..6e825da3 --- /dev/null +++ b/is-boolean-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isBooleanArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isBooleanArray( [ 'beep' ] ); // $ExpectType boolean + isBooleanArray( [ true, false, true ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isBooleanArray(); // $ExpectError + isBooleanArray( [ true, false ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isBooleanArray.primitives( [ new Boolean( true ) ] ); // $ExpectType boolean + isBooleanArray.primitives( [ true ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isBooleanArray.primitives(); // $ExpectError + isBooleanArray.primitives( [ true ], 123 ); // $ExpectError +} + + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isBooleanArray.objects( [ new Boolean( true ) ] ); // $ExpectType boolean + isBooleanArray.objects( [ true ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isBooleanArray.objects(); // $ExpectError + isBooleanArray.objects( [ true ], 123 ); // $ExpectError +} diff --git a/is-boolean-array/examples/index.js b/is-boolean-array/examples/index.js new file mode 100644 index 00000000..180433d6 --- /dev/null +++ b/is-boolean-array/examples/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var isBooleanArray = require( './../lib' ); + +var bool = isBooleanArray( [ true, false ] ); +console.log( bool ); +// => true + +bool = isBooleanArray( [ true, new Boolean( false ) ] ); +console.log( bool ); +// => true + +bool = isBooleanArray( [ true, 'false' ] ); +console.log( bool ); +// => false + +bool = isBooleanArray( [] ); +console.log( bool ); +// => false + +bool = isBooleanArray( null ); +console.log( bool ); +// => false diff --git a/is-boolean-array/lib/index.js b/is-boolean-array/lib/index.js new file mode 100644 index 00000000..a89254fc --- /dev/null +++ b/is-boolean-array/lib/index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object of booleans. +* +* @module @stdlib/assert/is-boolean-array +* +* @example +* var isBooleanArray = require( '@stdlib/assert/is-boolean-array' ); +* +* var bool = isBooleanArray( [ true, false, true ] ); +* // returns true +* +* bool = isBooleanArray( [ true, 'abc', false ] ); +* // returns false +* +* @example +* var isBooleanArray = require( '@stdlib/assert/is-boolean-array' ).primitives; +* +* var bool = isBooleanArray( [ true, false ] ); +* // returns true +* +* bool = isBooleanArray( [ false, new Boolean( true ) ] ); +* // returns false +* +* @example +* var isBooleanArray = require( '@stdlib/assert/is-boolean-array' ).objects; +* +* var bool = isBooleanArray( [ new Boolean( false ), new Boolean( true ) ] ); +* // returns true +* +* bool = isBooleanArray( [ new Boolean( false ), true ] ); +* // returns false +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ); + + +// MAIN // + +var isBooleanArray = arrayfun( isBoolean ); +setReadOnly( isBooleanArray, 'primitives', arrayfun( isBoolean.isPrimitive ) ); +setReadOnly( isBooleanArray, 'objects', arrayfun( isBoolean.isObject ) ); + + +// EXPORTS // + +module.exports = isBooleanArray; diff --git a/is-boolean-array/package.json b/is-boolean-array/package.json new file mode 100644 index 00000000..04c08519 --- /dev/null +++ b/is-boolean-array/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-boolean-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object of booleans.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "boolean", + "array", + "is", + "isboolean", + "isarray", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-boolean-array/test/test.js b/is-boolean-array/test/test.js new file mode 100644 index 00000000..4f25f48c --- /dev/null +++ b/is-boolean-array/test/test.js @@ -0,0 +1,90 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isBooleanArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isBooleanArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only booleans', function test( t ) { + var arr; + + arr = [ true, new Boolean( true ), false, new Boolean( false ) ]; + t.equal( isBooleanArray( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': true, + '1': false + }; + t.equal( isBooleanArray( arr ), true, 'returns true' ); + + arr = [ true, 'true', null ]; + t.equal( isBooleanArray( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only boolean primitives', function test( t ) { + var arr; + + arr = [ true, false, true, false ]; + t.equal( isBooleanArray.primitives( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': false, + '1': true + }; + t.equal( isBooleanArray.primitives( arr ), true, 'returns true' ); + + arr = [ new Boolean( true ), false, false ]; + t.equal( isBooleanArray.primitives( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only `Boolean` objects', function test( t ) { + var arr; + + arr = [ new Boolean( true ), new Boolean( false ), new Boolean( true ) ]; + t.equal( isBooleanArray.objects( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': new Boolean( true ), + '1': new Boolean( true ) + }; + t.equal( isBooleanArray.objects( arr ), true, 'returns true' ); + + arr = [ new Boolean( true ), false, false ]; + t.equal( isBooleanArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-boolean/README.md b/is-boolean/README.md new file mode 100644 index 00000000..cd89b9de --- /dev/null +++ b/is-boolean/README.md @@ -0,0 +1,120 @@ + + +# isBoolean + +> Test if a value is a boolean. + +
+ +### Usage + +```javascript +var isBoolean = require( '@stdlib/assert/is-boolean' ); +``` + +#### isBoolean( value ) + +Tests if a `value` is a `boolean`. + + + +```javascript +var bool = isBoolean( false ); +// returns true + +bool = isBoolean( true ); +// returns true + +bool = isBoolean( new Boolean( false ) ); +// returns true + +bool = isBoolean( new Boolean( true ) ); +// returns true +``` + +#### isBoolean.isPrimitive( value ) + +Tests if a `value` is a primitive `boolean`. + + + +```javascript +var bool = isBoolean.isPrimitive( true ); +// returns true + +bool = isBoolean.isPrimitive( false ); +// returns true + +bool = isBoolean.isPrimitive( new Boolean( true ) ); +// returns false +``` + +#### isBoolean.isObject( value ) + +Tests if a `value` is a `Boolean` object. + + + +```javascript +var bool = isBoolean.isObject( true ); +// returns false + +bool = isBoolean.isObject( new Boolean( false ) ); +// returns true +``` + +
+ + + +
+ +### Examples + + + + + +```javascript +var isBoolean = require( '@stdlib/assert/is-boolean' ); + +var bool = isBoolean( false ); +// returns true + +bool = isBoolean( new Boolean( false ) ); +// returns true + +bool = isBoolean( 'true' ); +// returns false + +bool = isBoolean( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-boolean/benchmark/benchmark.js b/is-boolean/benchmark/benchmark.js new file mode 100644 index 00000000..b56dcf70 --- /dev/null +++ b/is-boolean/benchmark/benchmark.js @@ -0,0 +1,201 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var isBoolean = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBoolean( values[ i % values.length ] ); + if ( !isBoolean.isPrimitive( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean.isPrimitive( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Boolean( true ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBoolean( values[ i % values.length ] ); + if ( !isBoolean.isPrimitive( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean.isPrimitive( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBoolean.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean.isPrimitive( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean.isPrimitive( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Boolean( false ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBoolean.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean.isPrimitive( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean.isPrimitive( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBoolean.isObject( values[ i % values.length ] ); + if ( !isBoolean.isPrimitive( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean.isPrimitive( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Boolean( true ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBoolean.isObject( values[ i % values.length ] ); + if ( !isBoolean.isPrimitive( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean.isPrimitive( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-boolean/docs/repl.txt b/is-boolean/docs/repl.txt new file mode 100644 index 00000000..6cb6364d --- /dev/null +++ b/is-boolean/docs/repl.txt @@ -0,0 +1,68 @@ + +{{alias}}( value ) + Tests if a value is a boolean. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a boolean. + + Examples + -------- + > var bool = {{alias}}( false ) + true + > bool = {{alias}}( new Boolean( false ) ) + true + + +{{alias}}.isPrimitive( value ) + Tests if a value is a boolean primitive. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a boolean primitive. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( true ) + true + > bool = {{alias}}.isPrimitive( false ) + true + > bool = {{alias}}.isPrimitive( new Boolean( true ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a boolean object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a boolean object. + + Examples + -------- + > var bool = {{alias}}.isObject( true ) + false + > bool = {{alias}}.isObject( new Boolean( false ) ) + true + + See Also + -------- + diff --git a/is-boolean/docs/types/index.d.ts b/is-boolean/docs/types/index.d.ts new file mode 100644 index 00000000..1d469622 --- /dev/null +++ b/is-boolean/docs/types/index.d.ts @@ -0,0 +1,113 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isBoolean` with methods for testing for primitives and objects, respectively. +*/ +interface IsBoolean { + /** + * Tests if a value is a boolean. + * + * @param value - value to test + * @returns boolean indicating whether value is a boolean + * + * @example + * var bool = isBoolean( false ); + * // returns true + * + * @example + * var bool = isBoolean( true ); + * // returns true + * + * @example + * var bool = isBoolean( new Boolean( false ) ); + * // returns true + * + * @example + * var bool = isBoolean( new Boolean( true ) ); + * // returns true + */ + ( value: any ): boolean; + + /** + * Tests if a value is a boolean primitive. + * + * @param value - value to test + * @returns boolean indicating if a value is a boolean primitive + * + * @example + * var bool = isBoolean.isPrimitive( true ); + * // returns true + * + * @example + * var bool = isBoolean.isPrimitive( false ); + * // returns true + * + * @example + * var bool = isBoolean.isPrimitive( new Boolean( true ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a boolean object. + * + * @param value - value to test + * @returns boolean indicating if a value is a boolean object + * + * @example + * var bool = isBoolean.isObject( true ); + * // returns false + * + * @example + * var bool = isBoolean.isObject( new Boolean( false ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a boolean. +* +* @param value - value to test +* @returns boolean indicating whether value is a boolean +* +* @example +* var bool = isBoolean( false ); +* // returns true +* +* @example +* var bool = isBoolean( true ); +* // returns true +* +* @example +* var bool = isBoolean.isPrimitive( new Boolean( true ) ); +* // returns false +* +* @example +* var bool = isBoolean.isObject( new Boolean( false ) ); +* // returns true +*/ +declare var isBoolean: IsBoolean; + + +// EXPORTS // + +export = isBoolean; diff --git a/is-boolean/docs/types/test.ts b/is-boolean/docs/types/test.ts new file mode 100644 index 00000000..98ecb4ed --- /dev/null +++ b/is-boolean/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isBoolean = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isBoolean( 'beep' ); // $ExpectType boolean + isBoolean( true ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isBoolean(); // $ExpectError + isBoolean( true, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isBoolean.isPrimitive( new Boolean( true ) ); // $ExpectType boolean + isBoolean.isPrimitive( true ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isBoolean.isPrimitive(); // $ExpectError + isBoolean.isPrimitive( true, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isBoolean.isObject( new Boolean( true ) ); // $ExpectType boolean + isBoolean.isObject( true ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isBoolean.isObject(); // $ExpectError + isBoolean.isObject( true, 123 ); // $ExpectError +} diff --git a/is-boolean/examples/index.js b/is-boolean/examples/index.js new file mode 100644 index 00000000..ebd860fd --- /dev/null +++ b/is-boolean/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var isBoolean = require( './../lib' ); + +console.log( isBoolean( false ) ); +// => true + +console.log( isBoolean( new Boolean( false ) ) ); +// => true + +console.log( isBoolean( 'true' ) ); +// => false + +console.log( isBoolean( null ) ); +// => false diff --git a/is-boolean/lib/index.js b/is-boolean/lib/index.js new file mode 100644 index 00000000..f6cb4a7b --- /dev/null +++ b/is-boolean/lib/index.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a boolean. +* +* @module @stdlib/assert/is-boolean +* +* @example +* var isBoolean = require( '@stdlib/assert/is-boolean' ); +* +* var bool = isBoolean( false ); +* // returns true +* +* bool = isBoolean( new Boolean( false ) ); +* // returns true +* +* @example +* // Use interface to check for boolean primitives... +* var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +* +* var bool = isBoolean( false ); +* // returns true +* +* bool = isBoolean( new Boolean( true ) ); +* // returns false +* +* @example +* // Use interface to check for boolean objects... +* var isBoolean = require( '@stdlib/assert/is-boolean' ).isObject; +* +* var bool = isBoolean( true ); +* // returns false +* +* bool = isBoolean( new Boolean( false ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isBoolean = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isBoolean, 'isPrimitive', isPrimitive ); +setReadOnly( isBoolean, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isBoolean; diff --git a/is-boolean/lib/main.js b/is-boolean/lib/main.js new file mode 100644 index 00000000..a6c63003 --- /dev/null +++ b/is-boolean/lib/main.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a boolean. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a boolean +* +* @example +* var bool = isBoolean( false ); +* // returns true +* +* @example +* var bool = isBoolean( true ); +* // returns true +* +* @example +* var bool = isBoolean( new Boolean( false ) ); +* // returns true +* +* @example +* var bool = isBoolean( new Boolean( true ) ); +* // returns true +*/ +function isBoolean( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isBoolean; diff --git a/is-boolean/lib/object.js b/is-boolean/lib/object.js new file mode 100644 index 00000000..cfbf29b7 --- /dev/null +++ b/is-boolean/lib/object.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); +var nativeClass = require( '@stdlib/utils/native-class' ); +var test = require( './try2serialize.js' ); + + +// VARIABLES // + +var FLG = hasToStringTag(); + + +// MAIN // + +/** +* Tests if a value is a boolean object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a boolean object +* +* @example +* var bool = isBoolean( true ); +* // returns false +* +* @example +* var bool = isBoolean( new Boolean( false ) ); +* // returns true +*/ +function isBoolean( value ) { + if ( typeof value === 'object' ) { + if ( value instanceof Boolean ) { + return true; + } + if ( FLG ) { + return test( value ); + } + return ( nativeClass( value ) === '[object Boolean]' ); + } + return false; +} + + +// EXPORTS // + +module.exports = isBoolean; diff --git a/is-boolean/lib/primitive.js b/is-boolean/lib/primitive.js new file mode 100644 index 00000000..601146af --- /dev/null +++ b/is-boolean/lib/primitive.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Tests if a value is a boolean primitive. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a boolean primitive +* +* @example +* var bool = isBoolean( true ); +* // returns true +* +* @example +* var bool = isBoolean( false ); +* // returns true +* +* @example +* var bool = isBoolean( new Boolean( true ) ); +* // returns false +*/ +function isBoolean( value ) { + return ( typeof value === 'boolean' ); +} + + +// EXPORTS // + +module.exports = isBoolean; diff --git a/is-boolean/lib/tostring.js b/is-boolean/lib/tostring.js new file mode 100644 index 00000000..6aeddbd1 --- /dev/null +++ b/is-boolean/lib/tostring.js @@ -0,0 +1,27 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// eslint-disable-next-line stdlib/no-redeclare +var toString = Boolean.prototype.toString; // non-generic + + +// EXPORTS // + +module.exports = toString; diff --git a/is-boolean/lib/try2serialize.js b/is-boolean/lib/try2serialize.js new file mode 100644 index 00000000..556559c4 --- /dev/null +++ b/is-boolean/lib/try2serialize.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var toString = require( './tostring.js' ); // eslint-disable-line stdlib/no-redeclare + + +// MAIN // + +/** +* Attempts to serialize a value to a string. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value can be serialized +*/ +function test( value ) { + try { + toString.call( value ); + return true; + } catch ( err ) { // eslint-disable-line no-unused-vars + return false; + } +} + + +// EXPORTS // + +module.exports = test; diff --git a/is-boolean/package.json b/is-boolean/package.json new file mode 100644 index 00000000..a3d774ba --- /dev/null +++ b/is-boolean/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-boolean", + "version": "0.0.0", + "description": "Test if a value is a boolean.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "boolean", + "bool", + "true", + "false", + "isboolean", + "isbool", + "is", + "type", + "check", + "primitive", + "object" + ] +} diff --git a/is-boolean/test/test.js b/is-boolean/test/test.js new file mode 100644 index 00000000..00bb8bd5 --- /dev/null +++ b/is-boolean/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isBoolean = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isBoolean, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive boolean', function test( t ) { + t.equal( typeof isBoolean.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a boolean object', function test( t ) { + t.equal( typeof isBoolean.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-boolean/test/test.main.js b/is-boolean/test/test.main.js new file mode 100644 index 00000000..43174669 --- /dev/null +++ b/is-boolean/test/test.main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isBoolean = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isBoolean, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a boolean', function test( t ) { + t.equal( isBoolean( false ), true, 'returns true' ); + t.equal( isBoolean( new Boolean( false ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.equal( isBoolean( true ), true, 'returns true' ); + t.equal( isBoolean( new Boolean( true ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a boolean', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + 5, + NaN, + undefined, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isBoolean( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-boolean/test/test.object.js b/is-boolean/test/test.object.js new file mode 100644 index 00000000..1f085c3b --- /dev/null +++ b/is-boolean/test/test.object.js @@ -0,0 +1,123 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); +var isBoolean = require( './../lib/object.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isBoolean, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a boolean object', function test( t ) { + t.equal( isBoolean( new Boolean( false ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.equal( isBoolean( new Boolean( true ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive boolean', function test( t ) { + t.equal( isBoolean( true ), false, 'returns false' ); + t.equal( isBoolean( false ), false, 'returns false' ); + t.end(); +}); + +tape( 'if `Symbol.toStringTag` is supported, the function guards against objects masquerading as `Boolean` objects', opts, function test( t ) { + var isBoolean; + var mock; + + isBoolean = proxyquire( './../lib/object.js', { + '@stdlib/assert/has-tostringtag-support': detect + }); + + mock = { + 'toString': toString, + 'valueOf': valueOf + }; + if ( hasToStringTag() ) { + mock[ Symbol.toStringTag ] = 'Boolean'; + } + t.equal( isBoolean( mock ), false, 'returns false' ); + t.end(); + + function detect() { + return true; + } + function toString() { + return 'true'; + } + function valueOf() { + return true; + } +}); + +tape( 'if `Symbol.toStringTag` is not supported, the function attempts to determine the native class', function test( t ) { + var isBoolean = proxyquire( './../lib/object.js', { + '@stdlib/assert/has-tostringtag-support': detect + }); + + t.equal( isBoolean( new Boolean( true ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.equal( isBoolean( {} ), false, 'returns false' ); + + t.end(); + + function detect() { + return false; + } +}); + +tape( 'the function returns `false` if not provided a boolean', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + 5, + NaN, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isBoolean( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-boolean/test/test.primitive.js b/is-boolean/test/test.primitive.js new file mode 100644 index 00000000..2fc7c212 --- /dev/null +++ b/is-boolean/test/test.primitive.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isBoolean = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isBoolean, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive boolean', function test( t ) { + t.equal( isBoolean( true ), true, 'returns true' ); + t.equal( isBoolean( false ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a boolean object', function test( t ) { + t.equal( isBoolean( new Boolean( true ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.equal( isBoolean( new Boolean( false ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a boolean', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + 5, + NaN, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isBoolean( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-boolean/test/test.try2serialize.js b/is-boolean/test/test.try2serialize.js new file mode 100644 index 00000000..50c17e81 --- /dev/null +++ b/is-boolean/test/test.try2serialize.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var serialize = require( './../lib/try2serialize.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof serialize, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if able to serialize', function test( t ) { + t.equal( serialize( true ), true, 'returns true' ); + t.equal( serialize( new Boolean( true ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.equal( serialize( false ), true, 'returns true' ); + t.equal( serialize( new Boolean( false ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if unable to serialize', function test( t ) { + t.equal( serialize( {} ), false, 'returns false' ); + t.end(); +}); diff --git a/is-boxed-primitive/README.md b/is-boxed-primitive/README.md new file mode 100644 index 00000000..087bf35a --- /dev/null +++ b/is-boxed-primitive/README.md @@ -0,0 +1,117 @@ + + +# isBoxedPrimitive + +> Test if a value is a JavaScript boxed primitive. + +
+ +## Usage + +```javascript +var isBoxedPrimitive = require( '@stdlib/assert/is-boxed-primitive' ); +``` + +#### isBoxedPrimitive( value ) + +Tests if a `value` is a JavaScript boxed primitive. + + + +```javascript +var bool = isBoxedPrimitive( new Boolean( false ) ); +// returns true + +bool = isBoxedPrimitive( true ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Boxed primitive objects can be created with one of the following: + + - `new Boolean()` + - `new Number()` + - `new String()` + - `Object( Symbol() )` (ES6/ES2015) + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isBoxedPrimitive = require( '@stdlib/assert/is-boxed-primitive' ); + +var bool = isBoxedPrimitive( new Boolean( false ) ); +// returns true + +bool = isBoxedPrimitive( new String( 'beep' ) ); +// returns true + +bool = isBoxedPrimitive( new Number( 3.14 ) ); +// returns true + +bool = isBoxedPrimitive( false ); +// returns false + +bool = isBoxedPrimitive( 0 ); +// returns false + +bool = isBoxedPrimitive( '' ); +// returns false + +bool = isBoxedPrimitive( null ); +// returns false + +bool = isBoxedPrimitive( void 0 ); +// returns false + +bool = isBoxedPrimitive( [] ); +// returns false + +bool = isBoxedPrimitive( {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-boxed-primitive/benchmark/benchmark.js b/is-boxed-primitive/benchmark/benchmark.js new file mode 100644 index 00000000..11ea824d --- /dev/null +++ b/is-boxed-primitive/benchmark/benchmark.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isBoxedPrimitive = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + new String( '5' ), // eslint-disable-line no-new-wrappers + 5, + new Number( 5 ), // eslint-disable-line no-new-wrappers + NaN, + true, + new Boolean( false ), // eslint-disable-line no-new-wrappers + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isBoxedPrimitive( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-boxed-primitive/docs/repl.txt b/is-boxed-primitive/docs/repl.txt new file mode 100644 index 00000000..bc2f9c2e --- /dev/null +++ b/is-boxed-primitive/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( value ) + Tests if a value is a JavaScript boxed primitive. + + Boxed primitive objects can be created with one of the following: + + - new Boolean() + - new Number() + - new String() + - Object( Symbol() ) (ES6/ES2015) + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a JavaScript boxed primitive. + + Examples + -------- + > var bool = {{alias}}( new Boolean( false ) ) + true + > bool = {{alias}}( true ) + false + + See Also + -------- + diff --git a/is-boxed-primitive/docs/types/index.d.ts b/is-boxed-primitive/docs/types/index.d.ts new file mode 100644 index 00000000..c93069b1 --- /dev/null +++ b/is-boxed-primitive/docs/types/index.d.ts @@ -0,0 +1,58 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a JavaScript boxed primitive. +* +* @param value - value to test +* @returns boolean indicating if a value is a JavaScript boxed primitive +* +* @example +* var bool = isBoxedPrimitive( new String( 'beep' ) ); +* // returns true +* +* @example +* var bool = isBoxedPrimitive( new Number( 3.21 ) ); +* // returns true +* +* @example +* var Symbol = require( `@stdlib/symbol/ctor` ); +* var bool = isBoxedPrimitive( Object( Symbol( 'beep' ) ) ); +* // returns true +* +* @example +* var bool = isBoxedPrimitive( true ); +* // returns false +* +* @example +* var bool = isBoxedPrimitive( {} ); +* // returns false +* +* @example +* var Symbol = require( `@stdlib/symbol/ctor` ); +* var bool = isBoxedPrimitive( Symbol( 'beep' ) ); +* // returns false +*/ +declare function isBoxedPrimitive( value: any ): boolean; + + +// EXPORTS // + +export = isBoxedPrimitive; diff --git a/is-boxed-primitive/docs/types/test.ts b/is-boxed-primitive/docs/types/test.ts new file mode 100644 index 00000000..34b20fda --- /dev/null +++ b/is-boxed-primitive/docs/types/test.ts @@ -0,0 +1,41 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isBoxedPrimitive = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + // tslint:disable-next-line:no-construct + isBoxedPrimitive( new String( 'beep' ) ); // $ExpectType boolean + isBoxedPrimitive( 'beep' ); // $ExpectType boolean + isBoxedPrimitive( [] ); // $ExpectType boolean + + // tslint:disable-next-line:no-construct + isBoxedPrimitive( new Number( 3.21 ) ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isBoxedPrimitive(); // $ExpectError + + // tslint:disable-next-line:no-construct + isBoxedPrimitive( new String( 'beep' ), 123 ); // $ExpectError +} diff --git a/is-boxed-primitive/examples/index.js b/is-boxed-primitive/examples/index.js new file mode 100644 index 00000000..d48f80e4 --- /dev/null +++ b/is-boxed-primitive/examples/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isBoxedPrimitive = require( './../lib' ); + +console.log( isBoxedPrimitive( new Boolean( false ) ) ); +// => true + +console.log( isBoxedPrimitive( new String( 'beep' ) ) ); +// => true + +console.log( isBoxedPrimitive( new Number( 3.14 ) ) ); +// => true + +console.log( isBoxedPrimitive( false ) ); +// => false + +console.log( isBoxedPrimitive( 0 ) ); +// => false + +console.log( isBoxedPrimitive( '' ) ); +// => false + +console.log( isBoxedPrimitive( null ) ); +// => false + +console.log( isBoxedPrimitive( void 0 ) ); +// => false + +console.log( isBoxedPrimitive( [] ) ); +// => false + +console.log( isBoxedPrimitive( {} ) ); +// => false diff --git a/is-boxed-primitive/lib/index.js b/is-boxed-primitive/lib/index.js new file mode 100644 index 00000000..c88cc73f --- /dev/null +++ b/is-boxed-primitive/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a JavaScript boxed primitive. +* +* @module @stdlib/assert/is-boxed-primitive +* +* @example +* var isBoxedPrimitive = require( '@stdlib/assert/is-boxed-primitive' ); +* +* var bool = isBoxedPrimitive( new Boolean( true ) ); +* // returns true +* +* bool = isBoxedPrimitive( true ); +* // returns false +*/ + +// MODULES // + +var isBoxedPrimitive = require( './main.js' ); + + +// EXPORTS // + +module.exports = isBoxedPrimitive; diff --git a/is-boxed-primitive/lib/main.js b/is-boxed-primitive/lib/main.js new file mode 100644 index 00000000..de899601 --- /dev/null +++ b/is-boxed-primitive/lib/main.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isBoolean = require( '@stdlib/assert/is-boolean' ).isObject; +var isNumber = require( '@stdlib/assert/is-number' ).isObject; +var isString = require( '@stdlib/assert/is-string' ).isObject; +var isSymbol = require( '@stdlib/assert/is-symbol' ).isObject; + + +// MAIN // + +/** +* Tests if a value is a JavaScript boxed primitive. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a JavaScript boxed primitive +* +* @example +* var bool = isBoxedPrimitive( new String( 'beep' ) ); +* // returns true +* +* @example +* var bool = isBoxedPrimitive( new Number( 3.21 ) ); +* // returns true +* +* @example +* var Symbol = require( '@stdlib/symbol/ctor' ); +* var bool = isBoxedPrimitive( Object( Symbol( 'beep' ) ) ); +* // returns true +* +* @example +* var bool = isBoxedPrimitive( true ); +* // returns false +* +* @example +* var bool = isBoxedPrimitive( {} ); +* // returns false +* +* @example +* var Symbol = require( '@stdlib/symbol/ctor' ); +* var bool = isBoxedPrimitive( Symbol( 'beep' ) ); +* // returns false +*/ +function isBoxedPrimitive( value ) { + if ( typeof value !== 'object' ) { + return false; + } + return ( + isBoolean( value ) || + isNumber( value ) || + isString( value ) || + isSymbol( value ) + ); +} + + +// EXPORTS // + +module.exports = isBoxedPrimitive; diff --git a/is-boxed-primitive/package.json b/is-boxed-primitive/package.json new file mode 100644 index 00000000..e31d487a --- /dev/null +++ b/is-boxed-primitive/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-boxed-primitive", + "version": "0.0.0", + "description": "Test if a value is a JavaScript boxed primitive.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "primitive", + "object", + "string", + "number", + "boolean", + "symbol", + "is", + "isprimitive", + "isboxed", + "isboxedprimitive", + "type", + "check", + "validate", + "test" + ] +} diff --git a/is-boxed-primitive/test/test.js b/is-boxed-primitive/test/test.js new file mode 100644 index 00000000..3f39ecd2 --- /dev/null +++ b/is-boxed-primitive/test/test.js @@ -0,0 +1,89 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var Number = require( '@stdlib/number/ctor' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var isBoxedPrimitive = require( './../lib' ); + + +// VARIABLES // + +var hasSymbols = hasSymbolSupport(); +var opts = { + 'skip': !hasSymbols +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isBoxedPrimitive, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a boxed primitive', function test( t ) { + var values; + var i; + + values = [ + new Boolean(), + new String(), + new Number() + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isBoxedPrimitive( values[i] ), true, 'returns true' ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided a boxed `Symbol`', opts, function test( t ) { + t.equal( isBoxedPrimitive( Object( Symbol( 'symbol' ) ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive `Symbol`', opts, function test( t ) { + t.equal( isBoxedPrimitive( Symbol( 'symbol' ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a boxed primitive', function test( t ) { + var values; + var i; + + values = [ + 'beep', + 3.14, + false, + function noop() {}, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isBoxedPrimitive( values[i] ), false, 'returns false for value: '+values[i] ); + } + t.end(); +}); diff --git a/is-browser/README.md b/is-browser/README.md new file mode 100644 index 00000000..7fff351a --- /dev/null +++ b/is-browser/README.md @@ -0,0 +1,79 @@ + + +# IS_BROWSER + +> Check if the runtime is a web browser. + +
+ +## Usage + +```javascript +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +``` + +#### IS_BROWSER + +`Boolean` indicating if the runtime is a web browser. + +```javascript +var bool = IS_BROWSER; +// returns +``` + +
+ + + +
+ +## Notes + +- In order to determine whether the runtime is a web browser, the implementation must resolve the global scope, thus requiring function generation. The use of function generation may be problematic in browser contexts enforcing a strict [content security policy][mdn-csp] (CSP). + +
+ + + +
+ +## Examples + + + +```javascript +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); + +console.log( IS_BROWSER ); +// => +``` + +
+ + + + + + diff --git a/is-browser/docs/repl.txt b/is-browser/docs/repl.txt new file mode 100644 index 00000000..d061960f --- /dev/null +++ b/is-browser/docs/repl.txt @@ -0,0 +1,12 @@ + +{{alias}} + Boolean indicating if the runtime is a web browser. + + Examples + -------- + > {{alias}} + + + See Also + -------- + diff --git a/is-browser/docs/types/index.d.ts b/is-browser/docs/types/index.d.ts new file mode 100644 index 00000000..9ddd6d5e --- /dev/null +++ b/is-browser/docs/types/index.d.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Boolean indicating if the runtime is a web browser. +* +* @example +* var bool = IS_BROWSER; +* // returns +*/ +declare const IS_BROWSER: boolean; + + +// EXPORTS // + +export = IS_BROWSER; diff --git a/is-browser/docs/types/test.ts b/is-browser/docs/types/test.ts new file mode 100644 index 00000000..9fc7fd55 --- /dev/null +++ b/is-browser/docs/types/test.ts @@ -0,0 +1,28 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import IS_BROWSER = require( './index' ); + + +// TESTS // + +// The variable is a boolean... +{ + // tslint:disable-next-line:no-unused-expression + IS_BROWSER; // $ExpectType boolean +} diff --git a/is-browser/examples/index.js b/is-browser/examples/index.js new file mode 100644 index 00000000..fb694e79 --- /dev/null +++ b/is-browser/examples/index.js @@ -0,0 +1,24 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var IS_BROWSER = require( './../lib' ); + +console.log( IS_BROWSER ); +// => diff --git a/is-browser/lib/browser.js b/is-browser/lib/browser.js new file mode 100644 index 00000000..06c53a1d --- /dev/null +++ b/is-browser/lib/browser.js @@ -0,0 +1,23 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// EXPORTS // + +module.exports = true; diff --git a/is-browser/lib/global_scope.js b/is-browser/lib/global_scope.js new file mode 100644 index 00000000..f354c8e9 --- /dev/null +++ b/is-browser/lib/global_scope.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-func */ + +'use strict'; + +// VARIABLES // + +var scope; + + +// FUNCTIONS // + +/** +* Test if the global scope is bound to the "window" variable present in browser environments. When creating a new function using the `Function(){}` constructor, the execution scope aliased by the `this` variable is the global scope. +* +* @private +* @returns {boolean} boolean indicating if global scope is bound to "window" variable +*/ +function globalScope() { + var fcn = ''; + fcn += 'try {'; + fcn += 'return this === window;'; + fcn += '} catch ( err ) {'; + fcn += 'return false;'; + fcn += '}'; + return (new Function( fcn ))(); +} + + +// MAIN // + +scope = globalScope(); + + +// EXPORTS // + +module.exports = scope; diff --git a/is-browser/lib/index.js b/is-browser/lib/index.js new file mode 100644 index 00000000..f5b1e3fd --- /dev/null +++ b/is-browser/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Boolean indicating if the runtime is a web browser. +* +* @module @stdlib/assert/is-browser +* +* @example +* var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +* +* var bool = IS_BROWSER; +* // returns +*/ + +// MODULES // + +var isBrowser = require( './main.js' ); + + +// MAIN // + +var bool = isBrowser(); + + +// EXPORTS // + +module.exports = bool; diff --git a/is-browser/lib/main.js b/is-browser/lib/main.js new file mode 100644 index 00000000..dc44e3db --- /dev/null +++ b/is-browser/lib/main.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* global window */ + +'use strict'; + +// MODULES // + +var getGlobal = require( '@stdlib/utils/global' ); +var IS_NODE = require( '@stdlib/assert/is-node' ); +var globalScope = require( './global_scope.js' ); + + +// VARIABLES // + +var Global = getGlobal(); + + +// MAIN // + +/** +* Returns a boolean indicating if the runtime is a web browser. +* +* @returns {boolean} boolean indicating if runtime is a web browser +* +* @example +* var bool = isBrowser(); +* // returns +*/ +function isBrowser() { + return ( + // Check that we are not running in a Node.js runtime: + IS_NODE === false && + + // Check for presence of `window` variable: + typeof window === 'object' && + + // Check that the `window` variable matches the determined global variable: + window === Global && + + // Check that the `window` variable is equal to the global scope: + globalScope === true + ); +} + + +// EXPORTS // + +module.exports = isBrowser; diff --git a/is-browser/package.json b/is-browser/package.json new file mode 100644 index 00000000..41ee1060 --- /dev/null +++ b/is-browser/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/assert/is-browser", + "version": "0.0.0", + "description": "Check if the runtime is a web browser.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "browser": "./lib/browser.js", + "directories": { + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "runtime", + "environment", + "web", + "browser", + "node.js" + ] +} diff --git a/is-browser/test/test.browser.js b/is-browser/test/test.browser.js new file mode 100644 index 00000000..fd2f5c46 --- /dev/null +++ b/is-browser/test/test.browser.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var IS_BROWSER = require( './../lib/browser.js' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_BROWSER, 'boolean', 'main export is a boolean' ); + t.end(); +}); + +tape( 'the exported value is always `true`', function test( t ) { + t.equal( IS_BROWSER, true, 'returns true' ); + t.end(); +}); diff --git a/is-browser/test/test.js b/is-browser/test/test.js new file mode 100644 index 00000000..605838dd --- /dev/null +++ b/is-browser/test/test.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var IS_BROWSER = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_BROWSER, 'boolean', 'main export is a boolean' ); + t.end(); +}); diff --git a/is-browser/test/test.main.js b/is-browser/test/test.main.js new file mode 100644 index 00000000..a9e6b8d2 --- /dev/null +++ b/is-browser/test/test.main.js @@ -0,0 +1,130 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* global global */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var isBrowser = require( './../lib/main.js' ); + + +// FIXTURES // + +var Global; +if ( typeof global === 'undefined' ) { + Global = {}; +} else { + Global = global; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isBrowser, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if the runtime is a web browser', function test( t ) { + var isBrowser; + var win; + + win = {}; + Global.window = win; + + isBrowser = proxyquire( './../lib/main.js', { + '@stdlib/utils/global': getGlobal, + '@stdlib/assert/is-node': false, + './global_scope.js': true + }); + + t.equal( isBrowser(), true, 'returns true' ); + t.end(); + + function getGlobal() { + return win; + } +}); + +tape( 'the function returns `false` if the runtime is not a web browser (`window` global is not an object)', function test( t ) { + var isBrowser; + var win; + + win = true; + Global.window = win; + + isBrowser = proxyquire( './../lib/main.js', { + '@stdlib/utils/global': getGlobal, + '@stdlib/assert/is-node': false, + './global_scope.js': true + }); + + t.equal( isBrowser(), false, 'returns false' ); + t.end(); + + function getGlobal() { + return win; + } +}); + +tape( 'the function returns `false` if the runtime is not a web browser (`window` global is not the detected global variable)', function test( t ) { + var isBrowser; + var win; + + win = {}; + Global.window = win; + + isBrowser = proxyquire( './../lib/main.js', { + '@stdlib/utils/global': getGlobal, + '@stdlib/assert/is-node': false, + './global_scope.js': true + }); + + t.equal( isBrowser(), false, 'returns false' ); + t.end(); + + function getGlobal() { + return {}; + } +}); + +tape( 'the function returns `false` if the runtime is not a web browser (`window` global is not equal to the global scope)', function test( t ) { + var isBrowser; + var win; + + win = {}; + Global.window = win; + + isBrowser = proxyquire( './../lib/main.js', { + '@stdlib/utils/global': getGlobal, + '@stdlib/assert/is-node': false, + './global_scope.js': false + }); + + t.equal( isBrowser(), false, 'returns false' ); + t.end(); + + function getGlobal() { + return win; + } +}); diff --git a/is-buffer/README.md b/is-buffer/README.md new file mode 100644 index 00000000..6d98a64b --- /dev/null +++ b/is-buffer/README.md @@ -0,0 +1,122 @@ + + +# isBuffer + +> Test if a value is a [Buffer][node-buffer] object. + +
+ +## Usage + +```javascript +var isBuffer = require( '@stdlib/assert/is-buffer' ); +``` + +#### isBuffer( value ) + +Tests if a `value` is a [`Buffer`][node-buffer] object. + + + + + +```javascript +var Buffer = require( '@stdlib/buffer/ctor' ); + +var value = new Buffer( [ 1, 2, 3, 4 ] ); + +var bool = isBuffer( value ); +// returns true +``` + +
+ + + +
+ +## Notes + +- The implementation supports both [Node.js][node-buffer] and [browser polyfill][browser-buffer] `Buffer` objects. + +
+ + + +
+ +## Examples + + + + + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Buffer = require( '@stdlib/buffer/ctor' ); +var isBuffer = require( '@stdlib/assert/is-buffer' ); + +var bool = isBuffer( new Buffer( [ 1, 2, 3, 4 ] ) ); +// returns true + +bool = isBuffer( new Buffer( 'beep' ) ); +// returns true + +bool = isBuffer( [] ); +// returns false + +bool = isBuffer( {} ); +// returns false + +bool = isBuffer( new Int8Array() ); +// returns false + +bool = isBuffer( function foo() {} ); +// returns false + +bool = isBuffer( null ); +// returns false + +bool = isBuffer( void 0 ); +// returns false + +bool = isBuffer( 'beep' ); +// returns false + +bool = isBuffer( 5 ); +// returns false +``` + +
+ + + + + + diff --git a/is-buffer/benchmark/benchmark.js b/is-buffer/benchmark/benchmark.js new file mode 100644 index 00000000..62731afa --- /dev/null +++ b/is-buffer/benchmark/benchmark.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Buffer = require( '@stdlib/buffer/ctor' ); +var pkg = require( './../package.json' ).name; +var isBuffer = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var obj; + var i; + + function Buff() { + return this; + } + + function isBuff() { + return false; + } + + Buffer.prototype.isBuffer = isBuff; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = new Buff(); + obj[ 0 ] = i; + obj[ 1 ] = i + 1; + bool = isBuffer( obj ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-buffer/docs/repl.txt b/is-buffer/docs/repl.txt new file mode 100644 index 00000000..8a2d68b9 --- /dev/null +++ b/is-buffer/docs/repl.txt @@ -0,0 +1,28 @@ + +{{alias}}( value ) + Tests if a value is a Buffer instance. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a Buffer instance. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/buffer/ctor}}( 'beep' ) ) + true + > bool = {{alias}}( new {{alias:@stdlib/buffer/ctor}}( [ 1, 2, 3, 4 ] ) ) + true + > bool = {{alias}}( {} ) + false + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-buffer/docs/types/index.d.ts b/is-buffer/docs/types/index.d.ts new file mode 100644 index 00000000..46bd2d77 --- /dev/null +++ b/is-buffer/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a Buffer instance. +* +* @param value - value to validate +* @returns boolean indicating if a value is a Buffer instance +* +* @example +* var v = isBuffer( new Buffer( 'beep' ) ); +* // returns true +* +* @example +* var v = isBuffer( new Buffer( [1,2,3,4] ) ); +* // returns true +* +* @example +* var v = isBuffer( {} ); +* // returns false +* +* @example +* var v = isBuffer( [] ); +* // returns false +*/ +declare function isBuffer( value: any ): boolean; + + +// EXPORTS // + +export = isBuffer; diff --git a/is-buffer/docs/types/test.ts b/is-buffer/docs/types/test.ts new file mode 100644 index 00000000..7e395c9e --- /dev/null +++ b/is-buffer/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isBuffer = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isBuffer( [ 1, 2, 3, 4 ] ); // $ExpectType boolean + isBuffer( {} ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isBuffer(); // $ExpectError + isBuffer( {}, 123 ); // $ExpectError +} diff --git a/is-buffer/examples/index.js b/is-buffer/examples/index.js new file mode 100644 index 00000000..08163847 --- /dev/null +++ b/is-buffer/examples/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-restricted-syntax, no-empty-function */ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Buffer = require( '@stdlib/buffer/ctor' ); +var isBuffer = require( './../lib' ); + +console.log( isBuffer( new Buffer( [ 1, 2, 3, 4 ] ) ) ); +// => true + +console.log( isBuffer( new Buffer( 'beep' ) ) ); +// => true + +console.log( isBuffer( [] ) ); +// => false + +console.log( isBuffer( {} ) ); +// => false + +console.log( isBuffer( new Int8Array() ) ); +// => false + +console.log( isBuffer( function foo() {} ) ); +// => false + +console.log( isBuffer( null ) ); +// => false + +console.log( isBuffer( void 0 ) ); +// => false + +console.log( isBuffer( 'beep' ) ); +// => false + +console.log( isBuffer( 5 ) ); +// => false diff --git a/is-buffer/lib/index.js b/is-buffer/lib/index.js new file mode 100644 index 00000000..325dbadb --- /dev/null +++ b/is-buffer/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a Buffer instance. +* +* @module @stdlib/assert/is-buffer +* +* @example +* var isBuffer = require( '@stdlib/assert/is-buffer' ); +* +* var v = isBuffer( new Buffer( 'beep' ) ); +* // returns true +* +* v = isBuffer( {} ); +* // returns false +*/ + +// MODULES // + +var isBuffer = require( './main.js' ); + + +// EXPORTS // + +module.exports = isBuffer; diff --git a/is-buffer/lib/main.js b/is-buffer/lib/main.js new file mode 100644 index 00000000..4931bb06 --- /dev/null +++ b/is-buffer/lib/main.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isObjectLike = require( '@stdlib/assert/is-object-like' ); + + +// MAIN // + +/** +* Tests if a value is a Buffer instance. +* +* @param {*} value - value to validate +* @returns {boolean} boolean indicating if a value is a Buffer instance +* +* @example +* var v = isBuffer( new Buffer( 'beep' ) ); +* // returns true +* +* @example +* var v = isBuffer( new Buffer( [1,2,3,4] ) ); +* // returns true +* +* @example +* var v = isBuffer( {} ); +* // returns false +* +* @example +* var v = isBuffer( [] ); +* // returns false +*/ +function isBuffer( value ) { + return ( + isObjectLike( value ) && + ( + // eslint-disable-next-line no-underscore-dangle + value._isBuffer || // for envs missing Object.prototype.constructor (e.g., Safari 5-7) + ( + value.constructor && + + // WARNING: `typeof` is not a foolproof check, as certain envs consider RegExp and NodeList instances to be functions + typeof value.constructor.isBuffer === 'function' && + value.constructor.isBuffer( value ) + ) + ) + ); +} + + +// EXPORTS // + +module.exports = isBuffer; diff --git a/is-buffer/package.json b/is-buffer/package.json new file mode 100644 index 00000000..a19c7dc4 --- /dev/null +++ b/is-buffer/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/assert/is-buffer", + "version": "0.0.0", + "description": "Test if a value is a Buffer object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "node", + "buffer", + "is", + "isbuffer", + "type", + "check" + ] +} diff --git a/is-buffer/test/test.js b/is-buffer/test/test.js new file mode 100644 index 00000000..e4e1ec41 --- /dev/null +++ b/is-buffer/test/test.js @@ -0,0 +1,80 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Buffer = require( '@stdlib/buffer/ctor' ); +var isBuffer = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isBuffer, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'if provided a Buffer object, the function returns `true`', function test( t ) { + var values; + var i; + + values = [ + new Buffer( [ 1, 2, 3, 4 ] ), + new Buffer( 'beep' ), + new Buffer( new Buffer(4) ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isBuffer( values[i] ), true, 'returns `true` for value ' + i ); + } + t.end(); +}); + +tape( 'if provided any value other than a Buffer object, the function returns `false`', function test( t ) { + var values; + var i; + + values = [ + 5, + '5', + NaN, + null, + undefined, + true, + function noop() {}, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isBuffer( values[i] ), false, 'returns `false` for value: ' + values[i] ); + } + + function Foo() { + return this; + } + Foo.prototype.isBuffer = null; + + t.equal( isBuffer( new Foo() ), false, 'returns `false` when provided a class with an `isBuffer` method' ); + + t.end(); +}); diff --git a/is-capitalized/README.md b/is-capitalized/README.md new file mode 100644 index 00000000..2af03857 --- /dev/null +++ b/is-capitalized/README.md @@ -0,0 +1,141 @@ + + +# isCapitalized + +> Test if a value is a string having an uppercase first character. + +
+ +## Usage + +```javascript +var isCapitalized = require( '@stdlib/assert/is-capitalized' ); +``` + +#### isCapitalized( value ) + +Tests if a `value` is a `string` having an uppercase first character. + +```javascript +var bool = isCapitalized( 'Every noble work is at first impossible.' ); +// returns true + +bool = isCapitalized( 'HELLO WORLD!' ); +// returns true + +bool = isCapitalized( 'salt and light' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- The function validates that a `value` is a `string`. For all other types, the function returns `false`. + +
+ + + +
+ +## Examples + + + +```javascript +var isCapitalized = require( '@stdlib/assert/is-capitalized' ); + +var bool = isCapitalized( 'Hello' ); +// returns true + +bool = isCapitalized( 'HELLO' ); +// returns true + +bool = isCapitalized( '' ); +// returns false + +bool = isCapitalized( 'hello' ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-capitalized [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-capitalized Beep +true +``` + +
+ +To use as a [standard stream][standard-streams], + +```bash +$ echo -n 'boop' | is-capitalized +false +``` + + + +
+ + + + + + diff --git a/is-capitalized/benchmark/benchmark.js b/is-capitalized/benchmark/benchmark.js new file mode 100644 index 00000000..03e9877d --- /dev/null +++ b/is-capitalized/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isCapitalized = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isCapitalized( fromCodePoint( i%126 ) ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-capitalized/bin/cli b/is-capitalized/bin/cli new file mode 100644 index 00000000..39f131a4 --- /dev/null +++ b/is-capitalized/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isCapitalized = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isCapitalized( String( args[ 0 ] ) ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isCapitalized( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-capitalized/docs/repl.txt b/is-capitalized/docs/repl.txt new file mode 100644 index 00000000..0a37b467 --- /dev/null +++ b/is-capitalized/docs/repl.txt @@ -0,0 +1,25 @@ + +{{alias}}( value ) + Tests if a value is a string having an uppercase first character. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a string with an uppercase first + character. + + Examples + -------- + > var bool = {{alias}}( 'Hello' ) + true + > bool = {{alias}}( 'world' ) + false + + See Also + -------- + diff --git a/is-capitalized/docs/types/index.d.ts b/is-capitalized/docs/types/index.d.ts new file mode 100644 index 00000000..0b2d10df --- /dev/null +++ b/is-capitalized/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a string having an uppercase first character. +* +* @param value - value to test +* @returns boolean indicating whether a value is a string with an uppercase first character +* +* @example +* var bool = isCapitalized( 'Hello' ); +* // returns true +* +* @example +* var bool = isCapitalized( 'WORLD' ); +* // returns true +* +* @example +* var bool = isCapitalized( '!' ); +* // returns false +* +* @example +* var bool = isCapitalized( 'salt and light' ); +* // returns false +*/ +declare function isCapitalized( value: any ): boolean; + + +// EXPORTS // + +export = isCapitalized; diff --git a/is-capitalized/docs/types/test.ts b/is-capitalized/docs/types/test.ts new file mode 100644 index 00000000..191deb0f --- /dev/null +++ b/is-capitalized/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isCapitalized = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isCapitalized( 'Hello' ); // $ExpectType boolean + isCapitalized( 'world' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isCapitalized(); // $ExpectError + isCapitalized( undefined, 123 ); // $ExpectError +} diff --git a/is-capitalized/docs/usage.txt b/is-capitalized/docs/usage.txt new file mode 100644 index 00000000..1c23672c --- /dev/null +++ b/is-capitalized/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-capitalized [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-capitalized/etc/cli_opts.json b/is-capitalized/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-capitalized/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-capitalized/examples/index.js b/is-capitalized/examples/index.js new file mode 100644 index 00000000..67fd4d0a --- /dev/null +++ b/is-capitalized/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isCapitalized = require( './../lib' ); + +console.log( isCapitalized( 'Hello' ) ); +// => true + +console.log( isCapitalized( 'HELLO' ) ); +// => true + +console.log( isCapitalized( '' ) ); +// => false + +console.log( isCapitalized( 'hello' ) ); +// => false diff --git a/is-capitalized/lib/index.js b/is-capitalized/lib/index.js new file mode 100644 index 00000000..1761d583 --- /dev/null +++ b/is-capitalized/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a string having an uppercase first character. +* +* @module @stdlib/assert/is-capitalized +* +* @example +* var isCapitalized = require( '@stdlib/assert/is-capitalized' ); +* +* var bool = isCapitalized( 'Hello' ); +* // returns true +* +* bool = isCapitalized( 'world' ); +* // returns false +*/ + +// MODULES // + +var isCapitalized = require( './main.js' ); + + +// EXPORTS // + +module.exports = isCapitalized; diff --git a/is-capitalized/lib/main.js b/is-capitalized/lib/main.js new file mode 100644 index 00000000..cd3aaedc --- /dev/null +++ b/is-capitalized/lib/main.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var lowercase = require( '@stdlib/string/lowercase' ); +var uppercase = require( '@stdlib/string/uppercase' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a string having an uppercase first character. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is a string with an uppercase first character +* +* @example +* var bool = isCapitalized( 'Hello' ); +* // returns true +* +* @example +* var bool = isCapitalized( 'WORLD' ); +* // returns true +* +* @example +* var bool = isCapitalized( '!' ); +* // returns false +* +* @example +* var bool = isCapitalized( 'salt and light' ); +* // returns false +*/ +function isCapitalized( value ) { + var ch; + if ( isString( value ) && value !== '' ) { + ch = value[ 0 ]; + return ( ch === uppercase( ch ) && ch !== lowercase( ch ) ); + } + return false; +} + + +// EXPORTS // + +module.exports = isCapitalized; diff --git a/is-capitalized/package.json b/is-capitalized/package.json new file mode 100644 index 00000000..2f00dc59 --- /dev/null +++ b/is-capitalized/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-capitalized", + "version": "0.0.0", + "description": "Test if a value is a string having an uppercase first character.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-capitalized": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "iscapitalized", + "capitalized", + "string", + "valid", + "validate", + "test" + ] +} diff --git a/is-capitalized/test/fixtures/stdin_error.js.txt b/is-capitalized/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-capitalized/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-capitalized/test/test.cli.js b/is-capitalized/test/test.cli.js new file mode 100644 index 00000000..58ca3d50 --- /dev/null +++ b/is-capitalized/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument is capitalized', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'Hello\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "Beep\nboop"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\nfalse\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-capitalized/test/test.js b/is-capitalized/test/test.js new file mode 100644 index 00000000..f57af8c3 --- /dev/null +++ b/is-capitalized/test/test.js @@ -0,0 +1,77 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isCapitalized = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isCapitalized, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a string having an uppercase first character', function test( t ) { + var values; + var bool; + var i; + + values = [ + 'Hello World!', + 'The end.' + ]; + + for ( i = 0; i < values.length; i++ ) { + bool = isCapitalized( values[ i ] ); + t.strictEqual( bool, true, 'returns true when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a string having an uppercase first character', function test( t ) { + var values; + var i; + + values = [ + 'abc', + 'hello World', + '', + '1139094843', + '!', + void 0, + 0, + NaN, + null, + false, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isCapitalized( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-centrosymmetric-matrix/README.md b/is-centrosymmetric-matrix/README.md new file mode 100644 index 00000000..c41a8bfe --- /dev/null +++ b/is-centrosymmetric-matrix/README.md @@ -0,0 +1,92 @@ + + +# isCentrosymmetricMatrix + +> Test if a value is a [centrosymmetric matrix][centrosymmetric-matrix]. + +
+ +## Usage + +```javascript +var isCentrosymmetricMatrix = require( '@stdlib/assert/is-centrosymmetric-matrix' ); +``` + +#### isCentrosymmetricMatrix( value ) + +Tests if a value is a [centrosymmetric matrix][centrosymmetric-matrix]. + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var buffer = [ + 1, 2, 3, + 4, 5, 4, + 3, 2, 1 +]; +var arr = ndarray( 'generic', buffer, [ 3, 3 ], [ 3, 1 ], 0, 'row-major' ); + +var bool = isCentrosymmetricMatrix( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isCentrosymmetricMatrix = require( '@stdlib/assert/is-centrosymmetric-matrix' ); + +var arr = ndarray( 'generic', [ 2, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +var out = isCentrosymmetricMatrix( arr ); +// returns true + +out = isCentrosymmetricMatrix( [ 1, 2, 3, 4 ] ); +// returns false + +out = isCentrosymmetricMatrix( {} ); +// returns false + +out = isCentrosymmetricMatrix( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-centrosymmetric-matrix/benchmark/benchmark.js b/is-centrosymmetric-matrix/benchmark/benchmark.js new file mode 100644 index 00000000..6562b985 --- /dev/null +++ b/is-centrosymmetric-matrix/benchmark/benchmark.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var isCentrosymmetricMatrix = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = [ 0, 0, 0, 0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + + values = [ + arr, + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isCentrosymmetricMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-centrosymmetric-matrix/benchmark/benchmark.size.js b/is-centrosymmetric-matrix/benchmark/benchmark.size.js new file mode 100644 index 00000000..e318fee8 --- /dev/null +++ b/is-centrosymmetric-matrix/benchmark/benchmark.size.js @@ -0,0 +1,122 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isOdd = require( '@stdlib/math/base/assert/is-odd' ); +var pkg = require( './../package.json' ).name; +var isCentrosymmetricMatrix = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} size - matrix size +* @param {PositiveInteger} N - matrix order +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, size, N ) { + var strides; + var buffer; + var shape; + var x; + + buffer = new Float64Array( size ); + shape = [ N, N ]; + strides = [ N, 1 ]; + x = ndarray( 'float64', buffer, shape, strides, 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var n; + var i; + var j; + + // Compute the index of the last checked element... + n = floor( N/2.0 ); + if ( isOdd( N ) ) { + j = ( n*N ) + n - 1; + } else { + j = n * N; + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire matrix before finding a failing value. + buffer[ j ] = i + 3.14; + bool = fcn( x ); + if ( bool !== false ) { + b.fail( 'should return false' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var sz; + var N; + var f; + var i; + + min = 4; // minimum number of elements + + for ( i = 0; i <= 16; i += 2 ) { + sz = min * pow( 2, i ); + N = sqrt( sz ); + f = createBenchmark( isCentrosymmetricMatrix, sz, N ); + bench( pkg+':size='+sz+',dims='+N+'x'+N, f ); + } +} + +main(); diff --git a/is-centrosymmetric-matrix/docs/repl.txt b/is-centrosymmetric-matrix/docs/repl.txt new file mode 100644 index 00000000..25a996ce --- /dev/null +++ b/is-centrosymmetric-matrix/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( value ) + Tests if a value is a matrix which is symmetric about its center. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a centrosymmetric matrix. + + Examples + -------- + > var buf = [ 2, 1, 1, 2 ]; + > var M = {{alias:@stdlib/ndarray/ctor}}( 'generic', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-centrosymmetric-matrix/docs/types/index.d.ts b/is-centrosymmetric-matrix/docs/types/index.d.ts new file mode 100644 index 00000000..faeb4260 --- /dev/null +++ b/is-centrosymmetric-matrix/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a centrosymmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} = M_{N-i-1,N-j-1}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param v - value to test +* @returns boolean indicating if a value is a centrosymmetric matrix +* +* @example +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'generic', [ 2, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isCentrosymmetricMatrix( arr ); +* // returns true +* +* bool = isCentrosymmetricMatrix( [] ); +* // returns false +*/ +declare function isCentrosymmetricMatrix( v: any ): boolean; + + +// EXPORTS // + +export = isCentrosymmetricMatrix; diff --git a/is-centrosymmetric-matrix/docs/types/test.ts b/is-centrosymmetric-matrix/docs/types/test.ts new file mode 100644 index 00000000..48d29ee2 --- /dev/null +++ b/is-centrosymmetric-matrix/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* tslint:disable:no-invalid-this */ + +import isCentrosymmetricMatrix = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const matrix = { + 'data': [ 2, 1, 1, 2 ], + 'ndims': 2, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': function get( i: number, j: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + return this.data[ idx ]; + }, + 'set': function set( i: number, j: number, v: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + this.data[ idx ] = v; + return v; + } + }; + isCentrosymmetricMatrix( matrix ); // $ExpectType boolean + isCentrosymmetricMatrix( [] ); // $ExpectType boolean + isCentrosymmetricMatrix( false ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isCentrosymmetricMatrix(); // $ExpectError +} diff --git a/is-centrosymmetric-matrix/examples/index.js b/is-centrosymmetric-matrix/examples/index.js new file mode 100644 index 00000000..3ed227e1 --- /dev/null +++ b/is-centrosymmetric-matrix/examples/index.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isCentrosymmetricMatrix = require( './../lib' ); + +var arr = ndarray( 'generic', [ 2, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +console.log( isCentrosymmetricMatrix( arr ) ); +// => true + +console.log( isCentrosymmetricMatrix( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isCentrosymmetricMatrix( {} ) ); +// => false + +console.log( isCentrosymmetricMatrix( null ) ); +// => false diff --git a/is-centrosymmetric-matrix/lib/index.js b/is-centrosymmetric-matrix/lib/index.js new file mode 100644 index 00000000..423e184d --- /dev/null +++ b/is-centrosymmetric-matrix/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a centrosymmetric matrix. +* +* @module @stdlib/assert/is-centrosymmetric-matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isCentrosymmetricMatrix = require( '@stdlib/assert/is-centrosymmetric-matrix' ); +* +* var arr = ndarray( 'generic', [ 2, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isCentrosymmetricMatrix( arr ); +* // returns true +* +* bool = isCentrosymmetricMatrix( [] ); +* // returns false +*/ + +// MODULES // + +var isCentrosymmetricMatrix = require( './main.js' ); + + +// EXPORTS // + +module.exports = isCentrosymmetricMatrix; diff --git a/is-centrosymmetric-matrix/lib/main.js b/is-centrosymmetric-matrix/lib/main.js new file mode 100644 index 00000000..7f8573c1 --- /dev/null +++ b/is-centrosymmetric-matrix/lib/main.js @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isSquareMatrix = require( '@stdlib/assert/is-square-matrix' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var isOdd = require( '@stdlib/math/base/assert/is-odd' ); + + +// MAIN // + +/** +* Tests if a value is a centrosymmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} = M_{N-i-1,N-j-1}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a centrosymmetric matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var arr = ndarray( 'generic', [ 2, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isCentrosymmetricMatrix( arr ); +* // returns true +* +* bool = isCentrosymmetricMatrix( [] ); +* // returns false +*/ +function isCentrosymmetricMatrix( v ) { + var m1; + var M; + var N; + var n; + var i; + var j; + if ( !isSquareMatrix( v ) ) { + return false; + } + M = v.shape[ 0 ]; + N = floor( M/2.0 ); // corresponds to a row index + 1 + m1 = M - 1; + for ( i = 0; i < N; i++ ) { + n = m1 - i; + for ( j = 0; j < M; j++ ) { + if ( v.get( i, j ) !== v.get( n, m1-j ) ) { + return false; + } + } + } + if ( isOdd( M ) ) { + // Only need to examine the first half of the row (up until the center element) due to symmetry... + for ( j = 0; j < N; j++ ) { + if ( v.get( i, j ) !== v.get( N, m1-j ) ) { + return false; + } + } + } + return true; +} + + +// EXPORTS // + +module.exports = isCentrosymmetricMatrix; diff --git a/is-centrosymmetric-matrix/package.json b/is-centrosymmetric-matrix/package.json new file mode 100644 index 00000000..1eae7642 --- /dev/null +++ b/is-centrosymmetric-matrix/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-centrosymmetric-matrix", + "version": "0.0.0", + "description": "Test if a value is a centrosymmetric matrix.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "matrix", + "ismatrix", + "2d", + "square", + "symmetric", + "symm", + "centrosymmetric", + "bisymmetric", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-centrosymmetric-matrix/test/test.js b/is-centrosymmetric-matrix/test/test.js new file mode 100644 index 00000000..bc8f9698 --- /dev/null +++ b/is-centrosymmetric-matrix/test/test.js @@ -0,0 +1,201 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable array-element-newline */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var isCentrosymmetricMatrix = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isCentrosymmetricMatrix, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a centrosymmetric matrix (odd order)', function test( t ) { + var buffer; + var arr; + + buffer = [ + 0, 1, 2, 3, 4, + 5, 6, 7, 8, 9, + 10, 11, 12, 11, 10, + 9, 8, 7, 6, 5, + 4, 3, 2, 1, 0 + ]; + arr = ndarray( 'generic', buffer, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); + + t.equal( isCentrosymmetricMatrix( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a centrosymmetric matrix (even order)', function test( t ) { + var buffer; + var arr; + + buffer = [ + 0, 1, 2, 3, 4, 5, + 6, 7, 8, 9, 10, 11, + 12, 13, 14, 15, 16, 17, + 17, 16, 15, 14, 13, 12, + 11, 10, 9, 8, 7, 6, + 5, 4, 3, 2, 1, 0 + ]; + arr = ndarray( 'generic', buffer, [ 6, 6 ], [ 6, 1 ], 0, 'row-major' ); + + t.equal( isCentrosymmetricMatrix( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray-like object which is centrosymmetric', function test( t ) { + var arr = { + 'data': [ + 0, 1, 2, + 3, 4, 3, + 2, 1, 0 + ], + 'shape': [ 3, 3 ], + 'strides': [ 3, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 9, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isCentrosymmetricMatrix( arr ), true, 'returns true' ); + t.end(); + + function get( i, j ) { + return arr.data[ (arr.strides[0]*i) + j ]; + } +}); + +tape( 'the function returns `false` if not provided a square matrix', function test( t ) { + var arr = ndarray( 'generic', [ 0, 0, 0, 0, 0, 0 ], [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + + t.equal( isCentrosymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a centrosymmetric matrix (even order)', function test( t ) { + var arr = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + t.equal( isCentrosymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a centrosymmetric matrix (odd order)', function test( t ) { + var buffer; + var arr; + + buffer = [ + 1, 2, 3, + 4, 5, 6, + 3, 2, 1 + ]; + arr = ndarray( 'generic', buffer, [ 3, 3 ], [ 3, 1 ], 0, 'row-major' ); + + t.equal( isCentrosymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray-like object not having equal dimensions', function test( t ) { + var arr; + + arr = { + 'data': [ 0, 0, 0, 0, 0, 0 ], + 'shape': [ 3, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 6, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isCentrosymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray-like object which is centrosymmetric', function test( t ) { + var arr; + + arr = { + 'data': [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], + 'shape': [ 3, 3 ], + 'strides': [ 3, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 9, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isCentrosymmetricMatrix( arr ), false, 'returns false' ); + t.end(); + + function get( i, j ) { + return arr.data[ (arr.strides[0]*i) + j ]; + } +}); + +tape( 'the function returns `false` if not provided a 2-dimensional ndarray-like object', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 0, 0, 0 ], [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + values = [ + arr, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isCentrosymmetricMatrix( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-circular-array/README.md b/is-circular-array/README.md new file mode 100644 index 00000000..e9916e2e --- /dev/null +++ b/is-circular-array/README.md @@ -0,0 +1,92 @@ + + +# isCircularArray + +> Test if a value is an array containing a circular reference. + +
+ +## Usage + +```javascript +var isCircularArray = require( '@stdlib/assert/is-circular-array' ); +``` + +#### isCircularArray( value ) + +Tests if a `value` is an `array` containing a circular reference. + +```javascript +var arr = [ 1, 2, 3 ]; +var bool = isCircularArray( arr ); +// returns false + +arr.push( arr ); +bool = isCircularArray( arr ); +// returns true + +arr.pop(); +arr.self = arr; +bool = isCircularArray( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isCircularArray = require( '@stdlib/assert/is-circular-array' ); + +var arr = [ 1, 2, 3 ]; +arr.push( arr ); +console.log( isCircularArray( arr ) ); +// => true + +var obj = { + 'beep': 'boop' +}; +obj.self = obj; +console.log( isCircularArray( obj ) ); +// => false + +console.log( isCircularArray( [] ) ); +// => false + +console.log( isCircularArray( null ) ); +// => false +``` + +
+ + + + + + diff --git a/is-circular-array/benchmark/benchmark.js b/is-circular-array/benchmark/benchmark.js new file mode 100644 index 00000000..c807e7f5 --- /dev/null +++ b/is-circular-array/benchmark/benchmark.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isCircularArray = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var obj1; + var obj2; + var arr1; + var arr2; + var i; + + obj1 = { + 'a': 'beep', + 'b': { + 'c': 'boop' + } + }; + obj1.b.self = obj1; + obj2 = { + 'a': {}, + 'b': obj1 + }; + arr1 = [ 1, 2, 3 ]; + arr1.push( arr1 ); + + arr2 = [ 1, 2, 3 ]; + arr2.self = arr2; + values = [ + null, + {}, + obj1, + obj2, + [], + arr1, + arr2 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isCircularArray( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-circular-array/docs/repl.txt b/is-circular-array/docs/repl.txt new file mode 100644 index 00000000..bb2076ae --- /dev/null +++ b/is-circular-array/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( value ) + Tests if a value is an array containing a circular reference. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array containing a + circular reference. + + Examples + -------- + > var arr = [ 1, 2, 3 ]; + > arr.push( arr ); + > var bool = {{alias}}( arr ) + true + > bool = {{alias}}( [] ) + false + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-circular-array/docs/types/index.d.ts b/is-circular-array/docs/types/index.d.ts new file mode 100644 index 00000000..841a256d --- /dev/null +++ b/is-circular-array/docs/types/index.d.ts @@ -0,0 +1,57 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an array containing a circular reference. +* +* @param value - value to test +* @returns boolean indicating whether value is an array containing a circular reference +* +* @example +* var arr = [ 1, 2, 3 ]; +* arr.push( arr ); +* var bool = isCircularArray( arr ); +* // returns true +* +* @example +* var obj = { +* 'a': 'beep', +* 'b': { +* 'c': 'boop' +* } +* }; +* obj.b.self = obj; +* var bool = isCircularArray( obj ); +* // returns false +* +* @example +* var bool = isCircularArray( [] ); +* // returns false +* +* @example +* var bool = isCircularArray( null ); +* // returns false +*/ +declare function isCircularArray( value: any ): boolean; + + +// EXPORTS // + +export = isCircularArray; diff --git a/is-circular-array/docs/types/test.ts b/is-circular-array/docs/types/test.ts new file mode 100644 index 00000000..c23426e5 --- /dev/null +++ b/is-circular-array/docs/types/test.ts @@ -0,0 +1,36 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isCircularArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const arr: Array = [ 1, 2, 3 ]; + arr.push( arr ); + isCircularArray( arr ); // $ExpectType boolean + isCircularArray( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isCircularArray(); // $ExpectError + isCircularArray( [], 123 ); // $ExpectError +} diff --git a/is-circular-array/examples/index.js b/is-circular-array/examples/index.js new file mode 100644 index 00000000..16646c89 --- /dev/null +++ b/is-circular-array/examples/index.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isCircularArray = require( './../lib' ); + +var arr = [ 1, 2, 3 ]; +arr.push( arr ); +console.log( isCircularArray( arr ) ); +// => true + +var obj = { + 'beep': 'boop' +}; +obj.self = obj; +console.log( isCircularArray( obj ) ); +// => false + +console.log( isCircularArray( [] ) ); +// => false + +console.log( isCircularArray( null ) ); +// => false diff --git a/is-circular-array/lib/index.js b/is-circular-array/lib/index.js new file mode 100644 index 00000000..0849fc43 --- /dev/null +++ b/is-circular-array/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array containing a circular reference. +* +* @module @stdlib/assert/is-circular-array +* +* @example +* var isCircularArray = require( '@stdlib/assert/is-circular-array' ); +* +* var arr = [ 1, 2, 3 ]; +* arr.push( arr ); +* +* var bool = isCircularArray( arr ); +* // returns true +* +* bool = isCircularArray( null ); +* // returns false +*/ + +// MODULES // + +var isCircularArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isCircularArray; diff --git a/is-circular-array/lib/main.js b/is-circular-array/lib/main.js new file mode 100644 index 00000000..01f06541 --- /dev/null +++ b/is-circular-array/lib/main.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isArray = require( '@stdlib/assert/is-array' ); +var isCircular = require( '@stdlib/assert/is-circular' ); + + +// MAIN // + +/** +* Tests if a value is an array containing a circular reference. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an array containing a circular reference +* +* @example +* var arr = [ 1, 2, 3 ]; +* arr.push( arr ); +* var bool = isCircularArray( arr ); +* // returns true +* +* @example +* var obj = { +* 'a': 'beep', +* 'b': { +* 'c': 'boop' +* } +* }; +* obj.b.self = obj; +* var bool = isCircularArray( obj ); +* // returns false +* +* @example +* var bool = isCircularArray( [] ); +* // returns false +* +* @example +* var bool = isCircularArray( null ); +* // returns false +*/ +function isCircularArray( value ) { + if ( !isArray( value ) ) { + return false; + } + return isCircular( value ); +} + + +// EXPORTS // + +module.exports = isCircularArray; diff --git a/is-circular-array/package.json b/is-circular-array/package.json new file mode 100644 index 00000000..31f7eaa0 --- /dev/null +++ b/is-circular-array/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-circular-array", + "version": "0.0.0", + "description": "Test if a value is an array containing a circular reference.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "array", + "arr", + "is", + "isarray", + "circular", + "cyclic", + "reference", + "ref", + "type", + "check", + "validate", + "test" + ] +} diff --git a/is-circular-array/test/test.js b/is-circular-array/test/test.js new file mode 100644 index 00000000..b54e573a --- /dev/null +++ b/is-circular-array/test/test.js @@ -0,0 +1,133 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isCircularArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isCircularArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an array', function test( t ) { + var values; + var obj1; + var obj2; + var obj3; + var i; + + obj1 = { + 'a': 'beep', + 'b': { + 'c': 'boop' + } + }; + + obj2 = { + 'a': obj1, + 'b': obj1 + }; + + obj3 = {}; + obj3.self = obj1; + + values = [ + '5', + 5, + true, + void 0, + null, + NaN, + function noop() {}, + {}, + obj1, + obj2, + obj3 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isCircularArray( values[i] ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided an array not containing a circular reference', function test( t ) { + var values; + var arr1; + var arr2; + var arr3; + var i; + + arr1 = [ 'beep', 'boop' ]; + + arr2 = [ arr1, arr1 ]; + + arr3 = [ + { + 'self': arr1 + } + ]; + + values = [ + [], + arr1, + arr2, + arr3 + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isCircularArray( values[i] ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an array containing a circular reference', function test( t ) { + var values; + var arr1; + var arr2; + var arr3; + var i; + + arr1 = [ 'beep', 'boop' ]; + arr1.push( arr1 ); + + arr2 = [ arr1, arr1 ]; + + arr3 = [ + { + 'self': arr1 + } + ]; + + values = [ + arr1, + arr2, + arr3 + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isCircularArray( values[i] ), true, 'returns true' ); + } + t.end(); +}); diff --git a/is-circular-plain-object/README.md b/is-circular-plain-object/README.md new file mode 100644 index 00000000..b65f9b2f --- /dev/null +++ b/is-circular-plain-object/README.md @@ -0,0 +1,108 @@ + + +# isCircularPlainObject + +> Test if a value is a plain object containing a circular reference. + +
+ +## Usage + +```javascript +var isCircularPlainObject = require( '@stdlib/assert/is-circular' ); +``` + +#### isCircularPlainObject( value ) + +Tests if a `value` is a plain `object` containing a circular reference. + +```javascript +var obj = { + 'beep': 'boop' +}; +var bool = isCircularPlainObject( obj ); +// returns false + +obj.self = obj; +bool = isCircularPlainObject( obj ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isCircularPlainObject = require( '@stdlib/assert/is-circular' ); + +var obj1 = { + 'a': 'beep', + 'b': { + 'c': 'boop' + } +}; +obj1.b.self = obj1; +var bool = isCircularPlainObject( obj1 ); +// returns true + +var obj2 = { + 'a': {}, + 'b': obj1 +}; +bool = isCircularPlainObject( obj2 ); +// returns true + +var arr = [ 1, 2, 3 ]; +arr.push( arr ); +bool = isCircularPlainObject( arr ); +// returns false + +var obj3 = { + 'beep': 'boop' +}; +bool = isCircularPlainObject({ + 'a': obj3, + 'b': obj3 +}); +// returns false + +bool = isCircularPlainObject( {} ); +// returns false + +bool = isCircularPlainObject( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-circular-plain-object/benchmark/benchmark.js b/is-circular-plain-object/benchmark/benchmark.js new file mode 100644 index 00000000..06543d17 --- /dev/null +++ b/is-circular-plain-object/benchmark/benchmark.js @@ -0,0 +1,74 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isCircularPlainObject = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var obj1; + var obj2; + var arr; + var i; + + obj1 = { + 'a': 'beep', + 'b': { + 'c': 'boop' + } + }; + obj1.b.self = obj1; + obj2 = { + 'a': {}, + 'b': obj1 + }; + arr = [ 1, 2, 3 ]; + arr.push( arr ); + values = [ + null, + {}, + obj1, + obj2, + [], + arr + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isCircularPlainObject( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-circular-plain-object/docs/repl.txt b/is-circular-plain-object/docs/repl.txt new file mode 100644 index 00000000..131aeecb --- /dev/null +++ b/is-circular-plain-object/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( value ) + Tests if a value is a plain object containing a circular reference. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a plain object containing a + circular reference. + + Examples + -------- + > var obj = { 'beep': 'boop' }; + > obj.self = obj; + > var bool = {{alias}}( obj ) + true + > bool = {{alias}}( {} ) + false + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-circular-plain-object/docs/types/index.d.ts b/is-circular-plain-object/docs/types/index.d.ts new file mode 100644 index 00000000..6a52c3cd --- /dev/null +++ b/is-circular-plain-object/docs/types/index.d.ts @@ -0,0 +1,57 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a plain object containing a circular reference. +* +* @param value - value to test +* @returns boolean indicating whether value is a plain object containing a circular reference +* +* @example +* var obj = { +* 'a': 'beep', +* 'b': { +* 'c': 'boop' +* } +* }; +* obj.b.self = obj; +* var bool = isCircularPlainObject( obj ); +* // returns true +* +* @example +* var arr = [ 1, 2, 3 ]; +* arr.push( arr ); +* var bool = isCircularPlainObject( arr ); +* // returns false +* +* @example +* var bool = isCircularPlainObject( {} ); +* // returns false +* +* @example +* var bool = isCircularPlainObject( null ); +* // returns false +*/ +declare function isCircularPlainObject( value: any ): boolean; + + +// EXPORTS // + +export = isCircularPlainObject; diff --git a/is-circular-plain-object/docs/types/test.ts b/is-circular-plain-object/docs/types/test.ts new file mode 100644 index 00000000..6ba595c0 --- /dev/null +++ b/is-circular-plain-object/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isCircularPlainObject = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isCircularPlainObject( {} ); // $ExpectType boolean + isCircularPlainObject( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isCircularPlainObject(); // $ExpectError + isCircularPlainObject( [], 123 ); // $ExpectError +} diff --git a/is-circular-plain-object/examples/index.js b/is-circular-plain-object/examples/index.js new file mode 100644 index 00000000..ebda17be --- /dev/null +++ b/is-circular-plain-object/examples/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isCircularPlainObject = require( './../lib' ); + +var obj1 = { + 'a': 'beep', + 'b': { + 'c': 'boop' + } +}; +obj1.b.self = obj1; +console.log( isCircularPlainObject( obj1 ) ); +// => true + +var obj2 = { + 'a': {}, + 'b': obj1 +}; +console.log( isCircularPlainObject( obj2 ) ); +// => true + +var arr = [ 1, 2, 3 ]; +arr.push( arr ); +console.log( isCircularPlainObject( arr ) ); +// => false + +var obj3 = { + 'beep': 'boop' +}; +console.log( isCircularPlainObject({ + 'a': obj3, + 'b': obj3 +}) ); +// => false + +console.log( isCircularPlainObject( {} ) ); +// => false + +console.log( isCircularPlainObject( null ) ); +// => false diff --git a/is-circular-plain-object/lib/index.js b/is-circular-plain-object/lib/index.js new file mode 100644 index 00000000..60837f7c --- /dev/null +++ b/is-circular-plain-object/lib/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a plain object containing a circular reference. +* +* @module @stdlib/assert/is-circular-plain-object +* +* @example +* var isCircular = require( '@stdlib/assert/is-circular-plain-object' ); +* +* var obj = { +* 'a': 'beep', +* 'b': { +* 'c': 'boop' +* } +* }; +* obj.b.self = obj; +* +* var bool = isCircularPlainObject( obj ); +* // returns true +* +* bool = isCircularPlainObject( null ); +* // returns false +*/ + +// MODULES // + +var isCircularPlainObject = require( './main.js' ); + + +// EXPORTS // + +module.exports = isCircularPlainObject; diff --git a/is-circular-plain-object/lib/main.js b/is-circular-plain-object/lib/main.js new file mode 100644 index 00000000..a4757386 --- /dev/null +++ b/is-circular-plain-object/lib/main.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isCircular = require( '@stdlib/assert/is-circular' ); + + +// MAIN // + +/** +* Tests if a value is a plain object containing a circular reference. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a plain object containing a circular reference +* +* @example +* var obj = { +* 'a': 'beep', +* 'b': { +* 'c': 'boop' +* } +* }; +* obj.b.self = obj; +* var bool = isCircularPlainObject( obj ); +* // returns true +* +* @example +* var arr = [ 1, 2, 3 ]; +* arr.push( arr ); +* var bool = isCircularPlainObject( arr ); +* // returns false +* +* @example +* var bool = isCircularPlainObject( {} ); +* // returns false +* +* @example +* var bool = isCircularPlainObject( null ); +* // returns false +*/ +function isCircularPlainObject( value ) { + if ( !isPlainObject( value ) ) { + return false; + } + return isCircular( value, [] ); +} + + +// EXPORTS // + +module.exports = isCircularPlainObject; diff --git a/is-circular-plain-object/package.json b/is-circular-plain-object/package.json new file mode 100644 index 00000000..9275374c --- /dev/null +++ b/is-circular-plain-object/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-circular-plain-object", + "version": "0.0.0", + "description": "Test if a value is a plain object containing a circular reference.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "object", + "obj", + "is", + "isobject", + "circular", + "cyclic", + "reference", + "ref", + "type", + "check", + "validate", + "test" + ] +} diff --git a/is-circular-plain-object/test/test.js b/is-circular-plain-object/test/test.js new file mode 100644 index 00000000..ab5ba5a3 --- /dev/null +++ b/is-circular-plain-object/test/test.js @@ -0,0 +1,123 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isCircularPlainObject = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isCircularPlainObject, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a plain object', function test( t ) { + var values; + var i; + values = [ + '5', + 5, + true, + void 0, + null, + NaN, + function noop() {}, + [], + new Date(), + new RegExp( '[0-9]' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isCircularPlainObject( values[i] ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided a plain object not containing a circular reference', function test( t ) { + var values; + var obj1; + var obj2; + var obj3; + var i; + + obj1 = { + 'a': 'beep', + 'b': { + 'c': 'boop' + } + }; + + obj2 = { + 'a': obj1, + 'b': obj1 + }; + + obj3 = {}; + obj3.self = obj1; + + values = [ + {}, + obj1, + obj2, + obj3 + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isCircularPlainObject( values[i] ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided a plain object containing a circular reference', function test( t ) { + var values; + var obj1; + var obj2; + var obj3; + var i; + + obj1 = { + 'a': 'beep', + 'b': { + 'c': 'boop' + } + }; + obj1.b.self = obj1; + + obj2 = { + 'a': {}, + 'b': obj1 + }; + + obj3 = {}; + obj3.self = obj3; + + values = [ + obj1, + obj2, + obj3 + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isCircularPlainObject( values[i] ), true, 'returns true' ); + } + t.end(); +}); diff --git a/is-circular/README.md b/is-circular/README.md new file mode 100644 index 00000000..5525fed2 --- /dev/null +++ b/is-circular/README.md @@ -0,0 +1,127 @@ + + +# isCircular + +> Test if an object-like value contains a circular reference. + +
+ +## Usage + +```javascript +var isCircular = require( '@stdlib/assert/is-circular' ); +``` + +#### isCircular( value ) + +Test if an object-like `value` contains a circular reference. + +```javascript +var obj = { + 'beep': 'boop' +}; +var bool = isCircular( obj ); +// returns false + +obj.self = obj; +bool = isCircular( obj ); +// returns true +``` + +
+ + + + + +
+ +## Notes + +- Object-like values include `objects` (except for `null`), `arrays`, `functions`, regular expressions, `Date` objects, and any other JavaScript object to which properties may be bound. + +
+ + + +
+ +## Examples + + + + + +```javascript +var isCircular = require( '@stdlib/assert/is-circular' ); + +var obj1 = { + 'a': 'beep', + 'b': { + 'c': 'boop' + } +}; +obj1.b.self = obj1; +var bool = isCircular( obj1 ); +// returns true + +var obj2 = { + 'a': {}, + 'b': obj1 +}; +bool = isCircular( obj2 ); +// returns true + +var arr = [ 1, 2, 3 ]; +arr.push( arr ); +bool = isCircular( arr ); +// returns true + +function circ() {} +circ.self = circ; +bool = isCircular( circ ); +// returns true + +var obj3 = { + 'beep': 'boop' +}; +bool = isCircular({ + 'a': obj3, + 'b': obj3 +}); +// returns false + +bool = isCircular( {} ); +// returns false + +bool = isCircular( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-circular/benchmark/benchmark.js b/is-circular/benchmark/benchmark.js new file mode 100644 index 00000000..0e8549d9 --- /dev/null +++ b/is-circular/benchmark/benchmark.js @@ -0,0 +1,141 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isCircular = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Tests if an object contains a circular reference by trying to stringify the input object. +* +* @private +* @param {Object} obj - object to test +* @returns {boolean} boolean indicating whether object contains a circular reference +*/ +function stringifyCheck( obj ) { + var bool; + try { + JSON.stringify( obj ); + bool = false; + } catch ( err ) { + bool = err.message === 'Converting circular structure to JSON'; + } + return bool; +} + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var obj1; + var obj2; + var arr; + var i; + + obj1 = { + 'a': 'beep', + 'b': { + 'c': 'boop' + } + }; + obj1.b.self = obj1; + obj2 = { + 'a': {}, + 'b': obj1 + }; + arr = [ 1, 2, 3 ]; + arr.push( arr ); + values = [ + null, + {}, + obj1, + obj2, + [], + arr + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isCircular( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::built-in', function benchmark( b ) { + var values; + var bool; + var obj1; + var obj2; + var arr; + var i; + + obj1 = { + 'a': 'beep', + 'b': { + 'c': 'boop' + } + }; + obj1.b.self = obj1; + obj2 = { + 'a': {}, + 'b': obj1 + }; + arr = [ 1, 2, 3 ]; + arr.push( arr ); + + values = [ + null, + {}, + obj1, + obj2, + [], + arr + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = stringifyCheck( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-circular/docs/repl.txt b/is-circular/docs/repl.txt new file mode 100644 index 00000000..20acce62 --- /dev/null +++ b/is-circular/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( value ) + Tests if an object-like value contains a circular reference. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether an object-like value contains a circular + reference. + + Examples + -------- + > var obj = { 'beep': 'boop' }; + > obj.self = obj; + > var bool = {{alias}}( obj ) + true + > bool = {{alias}}( {} ) + false + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-circular/docs/types/index.d.ts b/is-circular/docs/types/index.d.ts new file mode 100644 index 00000000..fe898878 --- /dev/null +++ b/is-circular/docs/types/index.d.ts @@ -0,0 +1,57 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object-like value contains a circular reference. +* +* @param value - value to test +* @returns boolean indicating whether value is object-like and contains a circular reference +* +* @example +* var obj = { +* 'a': 'beep', +* 'b': { +* 'c': 'boop' +* } +* }; +* obj.b.self = obj; +* var bool = isCircular( obj ); +* // returns true +* +* @example +* var arr = [ 1, 2, 3 ]; +* arr.push( arr ); +* var bool = isCircular( arr ); +* // returns true +* +* @example +* var bool = isCircular( {} ); +* // returns false +* +* @example +* var bool = isCircular( null ); +* // returns false +*/ +declare function isCircular( value: any ): boolean; + + +// EXPORTS // + +export = isCircular; diff --git a/is-circular/docs/types/test.ts b/is-circular/docs/types/test.ts new file mode 100644 index 00000000..f0c21945 --- /dev/null +++ b/is-circular/docs/types/test.ts @@ -0,0 +1,47 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isCircular = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'a': 'beep', + 'b': { + 'c': 'boop', + 'self': {} + } + }; + obj.b.self = obj; + isCircular( obj ); // $ExpectType boolean + + const arr: Array = [ 1, 2, 3 ]; + arr.push( arr ); + isCircular( arr ); // $ExpectType boolean + + isCircular( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isCircular(); // $ExpectError + isCircular( [], 123 ); // $ExpectError +} diff --git a/is-circular/examples/index.js b/is-circular/examples/index.js new file mode 100644 index 00000000..c267758d --- /dev/null +++ b/is-circular/examples/index.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isCircular = require( './../lib' ); + +var obj1 = { + 'a': 'beep', + 'b': { + 'c': 'boop' + } +}; +obj1.b.self = obj1; +console.log( isCircular( obj1 ) ); +// => true + +var obj2 = { + 'a': {}, + 'b': obj1 +}; +console.log( isCircular( obj2 ) ); +// => true + +var arr = [ 1, 2, 3 ]; +arr.push( arr ); +console.log( isCircular( arr ) ); +// => true + +function circ() {} // eslint-disable-line no-empty-function +circ.self = circ; +console.log( isCircular( circ ) ); +// => true + +var obj3 = { + 'beep': 'boop' +}; +console.log( isCircular({ + 'a': obj3, + 'b': obj3 +}) ); +// => false + +console.log( isCircular( {} ) ); +// => false + +console.log( isCircular( null ) ); +// => false diff --git a/is-circular/lib/index.js b/is-circular/lib/index.js new file mode 100644 index 00000000..0c20f260 --- /dev/null +++ b/is-circular/lib/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if an object-like value contains a circular reference. +* +* @module @stdlib/assert/is-circular +* +* @example +* var isCircular = require( '@stdlib/assert/is-circular' ); +* +* var obj = { +* 'a': 'beep', +* 'b': { +* 'c': 'boop' +* } +* }; +* obj.b.self = obj; +* +* var bool = isCircular( obj ); +* // returns true +* +* bool = isCircular( null ); +* // returns false +*/ + +// MODULES // + +var isCircular = require( './main.js' ); + + +// EXPORTS // + +module.exports = isCircular; diff --git a/is-circular/lib/main.js b/is-circular/lib/main.js new file mode 100644 index 00000000..9b0ba8ee --- /dev/null +++ b/is-circular/lib/main.js @@ -0,0 +1,134 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var objectKeys = require( '@stdlib/utils/keys' ); + + +// FUNCTIONS // + +/** +* Tests if a value is an object. +* +* ## Notes +* +* - The function excludes `null`. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is an object +*/ +function isObject( value ) { + var type = typeof value; + return ( value !== null && ( type === 'object' || type === 'function' ) ); +} + +/** +* Tests if the seen array contains a search value. +* +* @private +* @param {Array} seen - array of seen objects +* @param {*} searchValue - search value +* @returns {boolean} boolean indicating whether array contains search value +*/ +function contains( seen, searchValue ) { + var i; + for ( i = 0; i < seen.length; i++ ) { + if ( seen[ i ] === searchValue ) { + return true; + } + } + return false; +} + +/** +* Tests if an object contains a circular reference by recursively traversing object keys. +* +* @private +* @param {Object} obj - object to test +* @param {Array} seen - array of seen objects +* @returns {boolean} boolean indicating whether object contains a circular reference +*/ +function isCircObj( obj, seen ) { + var keys; + var val; + var i; + + seen.push( obj ); + keys = objectKeys( obj ); + if ( keys.length === 0 ) { + return false; + } + for ( i = 0; i < keys.length; i++ ) { + val = obj[ keys[ i ] ]; + if ( isObject( val ) && ( contains( seen, val ) || isCircObj( val, seen ) ) ) { // eslint-disable-line max-len + return true; + } + } + seen.pop( obj ); + return false; +} + + +// MAIN // + +/** +* Tests if an object-like value contains a circular reference. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is object-like and contains a circular reference +* +* @example +* var obj = { +* 'a': 'beep', +* 'b': { +* 'c': 'boop' +* } +* }; +* obj.b.self = obj; +* var bool = isCircular( obj ); +* // returns true +* +* @example +* var arr = [ 1, 2, 3 ]; +* arr.push( arr ); +* var bool = isCircular( arr ); +* // returns true +* +* @example +* var bool = isCircular( {} ); +* // returns false +* +* @example +* var bool = isCircular( null ); +* // returns false +*/ +function isCircular( value ) { + if ( !isObject( value ) ) { + return false; + } + return isCircObj( value, [] ); +} + + +// EXPORTS // + +module.exports = isCircular; diff --git a/is-circular/package.json b/is-circular/package.json new file mode 100644 index 00000000..71041475 --- /dev/null +++ b/is-circular/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/assert/is-circular", + "version": "0.0.0", + "description": "Test if an object-like value contains a circular reference.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "object", + "obj", + "array", + "arr", + "function", + "fcn", + "is", + "isobject", + "isarray", + "isfunction", + "circular", + "cyclic", + "reference", + "ref", + "type", + "check", + "validate", + "test" + ] +} diff --git a/is-circular/test/test.js b/is-circular/test/test.js new file mode 100644 index 00000000..c33ae0ad --- /dev/null +++ b/is-circular/test/test.js @@ -0,0 +1,241 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isCircular = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isCircular, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an object-like value', function test( t ) { + var values; + var i; + values = [ + '5', + 5, + true, + void 0, + null, + NaN + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isCircular( values[i] ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided an object not containing a circular reference', function test( t ) { + var values; + var obj1; + var obj2; + var obj3; + var i; + + obj1 = { + 'a': 'beep', + 'b': { + 'c': 'boop' + } + }; + + obj2 = { + 'a': obj1, + 'b': obj1 + }; + + obj3 = {}; + obj3.self = obj1; + + values = [ + {}, + obj1, + obj2, + obj3, + new Date(), + new RegExp( '[0-9]' ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isCircular( values[i] ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided an array not containing a circular reference', function test( t ) { + var values; + var arr1; + var arr2; + var arr3; + var i; + + arr1 = [ 'beep', 'boop' ]; + + arr2 = [ arr1, arr1 ]; + + arr3 = [ + { + 'self': arr1 + } + ]; + + values = [ + [], + arr1, + arr2, + arr3 + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isCircular( values[i] ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided a function not containing a circular reference', function test( t ) { + var values; + var i; + + function fcn1() {} + + function fcn2() {} + fcn2.other = fcn1; + + function fcn3() {} + fcn3.arr = [ + fcn1 + ]; + + values = [ + fcn1, + fcn2, + fcn3 + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isCircular( values[i] ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an object containing a circular reference', function test( t ) { + var values; + var date; + var obj1; + var obj2; + var obj3; + var re; + var i; + + obj1 = { + 'a': 'beep', + 'b': { + 'c': 'boop' + } + }; + obj1.b.self = obj1; + + obj2 = { + 'a': {}, + 'b': obj1 + }; + + obj3 = {}; + obj3.self = obj3; + + re = new RegExp( '[0-9]' ); + re.self = re; + + date = new Date(); + date.self = date; + + values = [ + obj1, + obj2, + obj3, + re, + date + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isCircular( values[i] ), true, 'returns true' ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an array containing a circular reference', function test( t ) { + var values; + var arr1; + var arr2; + var arr3; + var i; + + arr1 = [ 'beep', 'boop' ]; + arr1.push( arr1 ); + + arr2 = [ arr1, arr1 ]; + + arr3 = [ + { + 'self': arr1 + } + ]; + + values = [ + arr1, + arr2, + arr3 + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isCircular( values[i] ), true, 'returns true' ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided a function containing a circular reference', function test( t ) { + var values; + var i; + + function fcn1() {} + fcn1.self = fcn1; + + function fcn2() {} + fcn2.other = fcn1; + fcn2.other.self = fcn2; + + function fcn3() {} + fcn3.arr = [ + fcn3 + ]; + + values = [ + fcn1, + fcn2, + fcn3 + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isCircular( values[i] ), true, 'returns true' ); + } + t.end(); +}); diff --git a/is-collection/README.md b/is-collection/README.md new file mode 100644 index 00000000..e47000cf --- /dev/null +++ b/is-collection/README.md @@ -0,0 +1,140 @@ + + +# isCollection + +> Test if a value is a collection. + +
+ +A collection is defined as either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (excluding `strings` and `functions`). + +
+ + + +
+ +## Usage + +```javascript +var isCollection = require( '@stdlib/assert/is-collection' ); +``` + +#### isCollection( value ) + +Tests if a value is a collection. + +```javascript +var bool = isCollection( [] ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isCollection = require( '@stdlib/assert/is-collection' ); + +var bool = isCollection( [] ); +// returns true + +bool = isCollection( new Float64Array( 10 ) ); +// returns true + +bool = isCollection( new Float32Array( 10 ) ); +// returns true + +bool = isCollection( new Int32Array( 10 ) ); +// returns true + +bool = isCollection( new Uint32Array( 10 ) ); +// returns true + +bool = isCollection( new Int16Array( 10 ) ); +// returns true + +bool = isCollection( new Uint16Array( 10 ) ); +// returns true + +bool = isCollection( new Int8Array( 10 ) ); +// returns true + +bool = isCollection( new Uint8Array( 10 ) ); +// returns true + +bool = isCollection( new Uint8ClampedArray( 10 ) ); +// returns true + +bool = isCollection( { 'length': 0 } ); +// returns true + +bool = isCollection( {} ); +// returns false + +bool = isCollection( 'beep' ); +// returns false + +bool = isCollection( isCollection ); +// returns false + +bool = isCollection( null ); +// returns false + +bool = isCollection( void 0 ); +// returns false + +bool = isCollection( 3.14 ); +// returns false +``` + +
+ + + + + + diff --git a/is-collection/benchmark/benchmark.js b/is-collection/benchmark/benchmark.js new file mode 100644 index 00000000..85ae1e3b --- /dev/null +++ b/is-collection/benchmark/benchmark.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isCollection = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::array', function benchmark( b ) { + var bool; + var obj; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = [ i, i+1 ]; + bool = isCollection( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::typed_array', function benchmark( b ) { + var values; + var bool; + var obj; + var N; + var i; + + values = [ + new Float64Array( [ 1.0, 2.0 ] ), + new Float64Array( [ 3.0, 4.0 ] ) + ]; + N = values.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = values[ i%N ]; + bool = isCollection( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array_like_object', function benchmark( b ) { + var bool; + var obj; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = { + 'length': 2, + '0': i, + '1': i + 1 + }; + bool = isCollection( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-collection/docs/repl.txt b/is-collection/docs/repl.txt new file mode 100644 index 00000000..5c2daab5 --- /dev/null +++ b/is-collection/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( value ) + Tests if a value is a collection. + + A collection is defined as an array, typed array, or an array-like object + (excluding strings and functions). + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a collection. + + Examples + -------- + > var bool = {{alias}}( [] ) + true + > bool = {{alias}}( { 'length': 0 } ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-collection/docs/types/index.d.ts b/is-collection/docs/types/index.d.ts new file mode 100644 index 00000000..29cbb013 --- /dev/null +++ b/is-collection/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a collection. +* +* @param value - value to test +* @returns boolean indicating whether a value is a collection +* +* @example +* var bool = isCollection( [] ); +* // returns true +* +* @example +* var bool = isCollection( {} ); +* // returns false +*/ +declare function isCollection( value: any ): boolean; + + +// EXPORTS // + +export = isCollection; diff --git a/is-collection/docs/types/test.ts b/is-collection/docs/types/test.ts new file mode 100644 index 00000000..72714b1a --- /dev/null +++ b/is-collection/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isCollection = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isCollection( [] ); // $ExpectType boolean + isCollection( {} ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isCollection(); // $ExpectError + isCollection( [], 123 ); // $ExpectError +} diff --git a/is-collection/examples/index.js b/is-collection/examples/index.js new file mode 100644 index 00000000..19370c01 --- /dev/null +++ b/is-collection/examples/index.js @@ -0,0 +1,83 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isCollection = require( './../lib' ); + +console.log( isCollection( [] ) ); +// => true + +console.log( isCollection( new Float64Array( 10 ) ) ); +// => true + +console.log( isCollection( new Float32Array( 10 ) ) ); +// => true + +console.log( isCollection( new Int32Array( 10 ) ) ); +// => true + +console.log( isCollection( new Uint32Array( 10 ) ) ); +// => true + +console.log( isCollection( new Int16Array( 10 ) ) ); +// => true + +console.log( isCollection( new Uint16Array( 10 ) ) ); +// => true + +console.log( isCollection( new Int8Array( 10 ) ) ); +// => true + +console.log( isCollection( new Uint8Array( 10 ) ) ); +// => true + +console.log( isCollection( new Uint8ClampedArray( 10 ) ) ); +// => true + +console.log( isCollection( { 'length': 0 } ) ); +// => true + +console.log( isCollection( {} ) ); +// => false + +console.log( isCollection( 'beep' ) ); +// => false + +console.log( isCollection( isCollection ) ); +// => false + +console.log( isCollection( null ) ); +// => false + +console.log( isCollection( void 0 ) ); +// => false + +console.log( isCollection( 3.14 ) ); +// => false diff --git a/is-collection/lib/index.js b/is-collection/lib/index.js new file mode 100644 index 00000000..e156f384 --- /dev/null +++ b/is-collection/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a collection. +* +* @module @stdlib/assert/is-collection +* +* @example +* var isCollection = require( '@stdlib/assert/is-collection' ); +* +* var bool = isCollection( [] ); +* // returns true +* +* bool = isCollection( {} ); +* // returns false +*/ + +// MODULES // + +var isCollection = require( './main.js' ); + + +// EXPORTS // + +module.exports = isCollection; diff --git a/is-collection/lib/main.js b/is-collection/lib/main.js new file mode 100644 index 00000000..3f7fa718 --- /dev/null +++ b/is-collection/lib/main.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/math/base/assert/is-integer' ); +var MAX_LENGTH = require( '@stdlib/constants/array/max-typed-array-length' ); + + +// MAIN // + +/** +* Tests if a value is a collection. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is a collection +* +* @example +* var bool = isCollection( [] ); +* // returns true +* +* @example +* var bool = isCollection( {} ); +* // returns false +*/ +function isCollection( value ) { + return ( + typeof value === 'object' && + value !== null && + typeof value.length === 'number' && + isInteger( value.length ) && + value.length >= 0 && + value.length <= MAX_LENGTH + ); +} + + +// EXPORTS // + +module.exports = isCollection; diff --git a/is-collection/package.json b/is-collection/package.json new file mode 100644 index 00000000..6b5bef4d --- /dev/null +++ b/is-collection/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/assert/is-collection", + "version": "0.0.0", + "description": "Test if a value is a collection.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "array", + "typed array", + "collection", + "float64array", + "float32array", + "int32array", + "uint32array", + "int16array", + "uint16array", + "int8array", + "uint8array", + "uint8clampedarray", + "is", + "isarray", + "type", + "check", + "test", + "valid", + "validate" + ] +} diff --git a/is-collection/test/test.js b/is-collection/test/test.js new file mode 100644 index 00000000..5249b3a2 --- /dev/null +++ b/is-collection/test/test.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline, no-unused-vars */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var constructorName = require( '@stdlib/utils/constructor-name' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' ); +var isCollection = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isCollection, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a collection', function test( t ) { + var values; + var i; + + values = [ + [], + { 'length': 10 }, + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ), + allocUnsafe( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isCollection( values[i] ), true, 'returns true when provided '+constructorName( values[i] ) ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a collection', function test( t ) { + var values; + var i; + + values = [ + 'beep', + 5, + null, + undefined, + NaN, + true, + false, + {}, + function boop( a, b, c ) {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isCollection( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-complex-like/README.md b/is-complex-like/README.md new file mode 100644 index 00000000..5214da52 --- /dev/null +++ b/is-complex-like/README.md @@ -0,0 +1,111 @@ + + +# isComplexLike + +> Test if a value is a complex number-like object. + +
+ +## Usage + +```javascript +var isComplexLike = require( '@stdlib/assert/is-complex-like' ); +``` + +#### isComplexLike( value ) + +Tests if a value is a complex number-like `object`. + +```javascript +var Complex128 = require( '@stdlib/complex/float64' ); +var Complex64 = require( '@stdlib/complex/float32' ); + +var x = new Complex128( 1.0, 3.0 ); +var bool = isComplexLike( x ); +// returns true + +x = new Complex64( 3.0, 1.0 ); +bool = isComplexLike( x ); +// returns true + +x = { + 're': 1.0, + 'im': -1.0 +}; +bool = isComplexLike( x ); +// returns true +``` + +
+ + + +
+ +## Notes + +- A complex number-like `object` is defined as an `object` having the following properties assigned to numeric values: + + - **re**: real component. + - **im**: imaginary component. + +
+ + + +
+ +## Examples + + + + + +```javascript +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var isComplexLike = require( '@stdlib/assert/is-complex-like' ); + +var out = isComplexLike( new Complex64( 2.0, 2.0 ) ); +// returns true + +out = isComplexLike( new Complex128( 3.0, 1.0 ) ); +// returns true + +out = isComplexLike( { 're': 1.0, 'im': -1.0 } ); +// returns true + +out = isComplexLike( {} ); +// returns false + +out = isComplexLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-complex-like/benchmark/benchmark.js b/is-complex-like/benchmark/benchmark.js new file mode 100644 index 00000000..f4599d6f --- /dev/null +++ b/is-complex-like/benchmark/benchmark.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var pkg = require( './../package.json' ).name; +var isComplexLike = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var z; + var N; + var i; + + values = [ + new Complex128( 1.0, -1.0 ), + new Complex64( 2.0, -2.0 ), + { + 're': 3.0, + 'im': -3.0 + } + ]; + N = values.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = values[ i%N ]; + bool = isComplexLike( z ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var z; + var N; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + [], + {} + ]; + N = values.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = values[ i%N ]; + bool = isComplexLike( z ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-complex-like/docs/repl.txt b/is-complex-like/docs/repl.txt new file mode 100644 index 00000000..9437ec6d --- /dev/null +++ b/is-complex-like/docs/repl.txt @@ -0,0 +1,28 @@ + +{{alias}}( value ) + Tests if a value is a complex number-like object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a complex number-like object. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/complex/float32}}( 2.0, 2.0 ) ) + true + > bool = {{alias}}( new {{alias:@stdlib/complex/float64}}( 3.0, 1.0 ) ) + true + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-complex-like/docs/types/index.d.ts b/is-complex-like/docs/types/index.d.ts new file mode 100644 index 00000000..2bb3e5c5 --- /dev/null +++ b/is-complex-like/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a complex number-like object. +* +* @param value - value to test +* @returns boolean indicating if a value is a complex number-like object. +* +* @example +* var Complex128 = require( `@stdlib/complex/float64` ); +* var Complex64 = require( `@stdlib/complex/float32` ); +* +* var x = new Complex128( 4.0, 2.0 ); +* var bool = isComplexLike( x ); +* // returns true +* +* x = new Complex64( 4.0, 2.0 ); +* bool = isComplexLike( x ); +* // returns true +*/ +declare function isComplexLike( value: any ): boolean; + + +// EXPORTS // + +export = isComplexLike; diff --git a/is-complex-like/docs/types/test.ts b/is-complex-like/docs/types/test.ts new file mode 100644 index 00000000..a5afc767 --- /dev/null +++ b/is-complex-like/docs/types/test.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isComplexLike = require( './index' ); +import Complex128 = require( '@stdlib/complex/float64' ); +import Complex64 = require( '@stdlib/complex/float32' ); + + +// TESTS // + +// The function returns a boolean... +{ + isComplexLike( new Complex128( 5.0, 3.0 ) ); // $ExpectType boolean + isComplexLike( new Complex64( 5.0, 3.0 ) ); // $ExpectType boolean + isComplexLike( { 're': 5.0, 'im': 3.0 } ); // $ExpectType boolean + isComplexLike( 123 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isComplexLike(); // $ExpectError + isComplexLike( new Complex128( 5.0, 3.0 ), 123 ); // $ExpectError +} diff --git a/is-complex-like/examples/index.js b/is-complex-like/examples/index.js new file mode 100644 index 00000000..ed168095 --- /dev/null +++ b/is-complex-like/examples/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline, object-property-newline */ + +'use strict'; + +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var isComplexLike = require( './../lib' ); + +console.log( isComplexLike( new Complex64( 2.0, 2.0 ) ) ); +// => true + +console.log( isComplexLike( new Complex128( 3.0, 1.0 ) ) ); +// => true + +console.log( isComplexLike( { 're': 1.0, 'im': -1.0 } ) ); +// => true + +console.log( isComplexLike( {} ) ); +// => false + +console.log( isComplexLike( null ) ); +// => false diff --git a/is-complex-like/lib/index.js b/is-complex-like/lib/index.js new file mode 100644 index 00000000..bb4b8147 --- /dev/null +++ b/is-complex-like/lib/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a complex number-like object. +* +* @module @stdlib/assert/is-complex-like +* +* @example +* var Complex128 = require( '@stdlib/complex/float64' ); +* var Complex64 = require( '@stdlib/complex/float32' ); +* var isComplexLike = require( '@stdlib/assert/is-complex-like' ); +* +* var x = new Complex128( 4.0, 2.0 ); +* var bool = isComplexLike( x ); +* // returns true +* +* x = new Complex64( 4.0, 2.0 ); +* bool = isComplexLike( x ); +* // returns true +*/ + +// MODULES // + +var isComplexLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isComplexLike; diff --git a/is-complex-like/lib/main.js b/is-complex-like/lib/main.js new file mode 100644 index 00000000..3bae836c --- /dev/null +++ b/is-complex-like/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Complex128 = require( '@stdlib/complex/float64' ); +var Complex64 = require( '@stdlib/complex/float32' ); + + +// MAIN // + +/** +* Tests if a value is a complex number-like object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a complex number-like object. +* +* @example +* var Complex128 = require( '@stdlib/complex/float64' ); +* var Complex64 = require( '@stdlib/complex/float32' ); +* +* var x = new Complex128( 4.0, 2.0 ); +* var bool = isComplexLike( x ); +* // returns true +* +* x = new Complex64( 4.0, 2.0 ); +* bool = isComplexLike( x ); +* // returns true +*/ +function isComplexLike( value ) { + if ( value instanceof Complex128 || value instanceof Complex64 ) { + return true; + } + return ( + typeof value === 'object' && + value !== null && + typeof value.re === 'number' && + typeof value.im === 'number' + ); +} + + +// EXPORTS // + +module.exports = isComplexLike; diff --git a/is-complex-like/package.json b/is-complex-like/package.json new file mode 100644 index 00000000..3e30ef86 --- /dev/null +++ b/is-complex-like/package.json @@ -0,0 +1,81 @@ +{ + "name": "@stdlib/assert/is-complex-like", + "version": "0.0.0", + "description": "Test if a value is a complex number-like object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "iscomplex", + "complex", + "complex64", + "complex128", + "64-bit", + "128-bit", + "float32", + "float64", + "single", + "single-precision", + "double", + "double-precision", + "ieee754", + "type", + "check", + "test", + "validate", + "valid", + "isvalid" + ] +} diff --git a/is-complex-like/test/test.js b/is-complex-like/test/test.js new file mode 100644 index 00000000..a1b8f0cc --- /dev/null +++ b/is-complex-like/test/test.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var isComplexLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isComplexLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a complex number-like object', function test( t ) { + var v; + + v = new Complex64( 3.0, 2.0 ); + t.equal( isComplexLike( v ), true, 'returns true' ); + + v = new Complex128( 3.0, 2.0 ); + t.equal( isComplexLike( v ), true, 'returns true' ); + + v = { + 're': 1.0, + 'im': -1.0 + }; + t.equal( isComplexLike( v ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a complex number-like object', function test( t ) { + var values; + var i; + + values = [ + 5, + '5', + null, + void 0, + NaN, + true, + false, + [], + [ 1.0, -1.0 ], + {}, + { + 're': 1.0 + }, + { + 'im': -1.0 + }, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isComplexLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-complex-typed-array-like/README.md b/is-complex-typed-array-like/README.md new file mode 100644 index 00000000..5f0c1304 --- /dev/null +++ b/is-complex-typed-array-like/README.md @@ -0,0 +1,113 @@ + + +# isComplexTypedArrayLike + +> Test if a value is complex-typed-array-like. + +
+ +## Usage + +```javascript +var isComplexTypedArrayLike = require( '@stdlib/assert/is-complex-typed-array-like' ); +``` + +#### isComplexTypedArrayLike( value ) + +Tests if a value is complex-typed-array-like. + + + +```javascript +var Complex64Array = require( '@stdlib/array/complex64' ); + +var bool = isComplexTypedArrayLike( new Complex64Array() ); +// returns true + +bool = isComplexTypedArrayLike({ + 'length': 10, + 'byteOffset': 0, + 'byteLength': 10, + 'BYTES_PER_ELEMENT': 4, + 'get': function get() {}, + 'set': function set() {} +}); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Complex64Array = require( '@stdlib/array/complex64' ); +var isComplexTypedArrayLike = require( '@stdlib/assert/is-complex-typed-array-like' ); + +var bool; +var arr; + +arr = { + 'BYTES_PER_ELEMENT': 8, + 'length': 10, + 'byteOffset': 0, + 'byteLength': 10, + 'get': function get() {}, + 'set': function set() {} +}; +bool = isComplexTypedArrayLike( arr ); +// returns true + +bool = isComplexTypedArrayLike( new Complex64Array( 4 ) ); +// returns true + +bool = isComplexTypedArrayLike( [] ); +// returns false + +bool = isComplexTypedArrayLike( {} ); +// returns false + +bool = isComplexTypedArrayLike( null ); +// returns false + +bool = isComplexTypedArrayLike( 'beep' ); +// returns false + +bool = isComplexTypedArrayLike( function foo( a, b ) {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-complex-typed-array-like/benchmark/benchmark.js b/is-complex-typed-array-like/benchmark/benchmark.js new file mode 100644 index 00000000..dbb55ce4 --- /dev/null +++ b/is-complex-typed-array-like/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var noop = require( '@stdlib/utils/noop' ); +var pkg = require( './../package.json' ).name; +var isComplexTypedArrayLike = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + new Date() + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplexTypedArrayLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var arr; + var i; + + arr = {}; + arr.BYTES_PER_ELEMENT = 4; + arr.length = 10; + arr.byteOffset = 0; + arr.byteLength = 10; + arr.get = noop; + arr.set = noop; + + values = [ + arr, + new Complex64Array( 10 ), + new Complex128Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplexTypedArrayLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-complex-typed-array-like/docs/repl.txt b/is-complex-typed-array-like/docs/repl.txt new file mode 100644 index 00000000..55eef32c --- /dev/null +++ b/is-complex-typed-array-like/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( value ) + Tests if a value is complex-typed-array-like. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is complex-typed-array-like. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/complex128}}() ) + true + > bool = {{alias}}({ + ... 'length': 10, + ... 'byteOffset': 0, + ... 'byteLength': 10, + ... 'BYTES_PER_ELEMENT': 4, + ... 'get': function get() {}, + ... 'set': function set() {} + ... }) + true + + See Also + -------- + diff --git a/is-complex-typed-array-like/docs/types/index.d.ts b/is-complex-typed-array-like/docs/types/index.d.ts new file mode 100644 index 00000000..f6f0286c --- /dev/null +++ b/is-complex-typed-array-like/docs/types/index.d.ts @@ -0,0 +1,66 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is complex-typed-array-like. +* +* @param value - value to test +* @returns boolean indicating if a value is complex-typed-array-like +* +* @example +* var arr = { +* 'BYTES_PER_ELEMENT': 8, +* 'length': 10, +* 'byteOffset': 0, +* 'byteLength': 10, +* 'get': function get() {}, +* 'set': function set() {} +* }; +* var val = isComplexTypedArrayLike( arr ); +* // returns true +* +* @example +* var Complex128Array = require( `@stdlib/array/complex128` ); +* +* var val = isComplexTypedArrayLike( new Complex128Array( 4 ) ); +* // returns true +* +* @example +* var val = isComplexTypedArrayLike( [] ); +* // returns false +* +* @example +* var val = isComplexTypedArrayLike( {} ); +* // returns false +* +* @example +* var val = isComplexTypedArrayLike( null ); +* // returns false +* +* @example +* var val = isComplexTypedArrayLike( 'beep' ); +* // returns false +*/ +declare function isComplexTypedArrayLike( value: any ): boolean; + + +// EXPORTS // + +export = isComplexTypedArrayLike; diff --git a/is-complex-typed-array-like/docs/types/test.ts b/is-complex-typed-array-like/docs/types/test.ts new file mode 100644 index 00000000..c980a4a3 --- /dev/null +++ b/is-complex-typed-array-like/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isComplexTypedArrayLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isComplexTypedArrayLike( {} ); // $ExpectType boolean + isComplexTypedArrayLike( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isComplexTypedArrayLike(); // $ExpectError + isComplexTypedArrayLike( [], 123 ); // $ExpectError +} diff --git a/is-complex-typed-array-like/examples/index.js b/is-complex-typed-array-like/examples/index.js new file mode 100644 index 00000000..aacd7d04 --- /dev/null +++ b/is-complex-typed-array-like/examples/index.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-restricted-syntax, no-empty-function, no-unused-vars */ + +'use strict'; + +var Complex64Array = require( '@stdlib/array/complex64' ); +var isComplexTypedArrayLike = require( './../lib' ); + +var arr = { + 'BYTES_PER_ELEMENT': 8, + 'length': 10, + 'byteOffset': 0, + 'byteLength': 10, + 'get': function get() {}, + 'set': function set() {} +}; +console.log( isComplexTypedArrayLike( arr ) ); +// => true + +console.log( isComplexTypedArrayLike( new Complex64Array( 4 ) ) ); +// => true + +console.log( isComplexTypedArrayLike( [] ) ); +// => false + +console.log( isComplexTypedArrayLike( {} ) ); +// => false + +console.log( isComplexTypedArrayLike( null ) ); +// => false + +console.log( isComplexTypedArrayLike( 'beep' ) ); +// => false + +console.log( isComplexTypedArrayLike( function foo( a, b ) {} ) ); +// => false diff --git a/is-complex-typed-array-like/lib/index.js b/is-complex-typed-array-like/lib/index.js new file mode 100644 index 00000000..ba521158 --- /dev/null +++ b/is-complex-typed-array-like/lib/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is complex-typed-array-like. +* +* @module @stdlib/assert/is-complex-typed-array-like +* +* @example +* var Complex64Array = require( '@stdlib/array/complex64' ); +* var isComplexTypedArrayLike = require( '@stdlib/assert/is-complex-typed-array-like' ); +* +* var bool = isComplexTypedArrayLike( new Complex64Array() ); +* // returns true +* +* bool = isComplexTypedArrayLike({ +* 'length': 10, +* 'byteOffset': 0, +* 'byteLength': 10, +* 'BYTES_PER_ELEMENT': 4, +* 'get': function get() {}, +* 'set': function set() {} +* }); +* // returns true +*/ + +// MODULES // + +var isComplexTypedArrayLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isComplexTypedArrayLike; diff --git a/is-complex-typed-array-like/lib/main.js b/is-complex-typed-array-like/lib/main.js new file mode 100644 index 00000000..acae8c5e --- /dev/null +++ b/is-complex-typed-array-like/lib/main.js @@ -0,0 +1,95 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var MAX = require( '@stdlib/constants/array/max-typed-array-length' ); + + +// VARIABLES // + +var MAX_LENGTH = MAX / 2; // every complex array element has both a real and imaginary component stored as separate numbers, so the maximum length is half that of a normal typed array + + +// MAIN // + +/** +* Tests if a value is complex-typed-array-like. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is complex-typed-array-like +* +* @example +* var arr = { +* 'BYTES_PER_ELEMENT': 8, +* 'length': 10, +* 'byteOffset': 0, +* 'byteLength': 10, +* 'get': function get() {}, +* 'set': function set() {} +* }; +* var val = isComplexTypedArrayLike( arr ); +* // returns true +* +* @example +* var Complex64Array = require( '@stdlib/array/complex64' ); +* +* var val = isComplexTypedArrayLike( new Complex64Array( 4 ) ); +* // returns true +* +* @example +* var val = isComplexTypedArrayLike( [] ); +* // returns false +* +* @example +* var val = isComplexTypedArrayLike( {} ); +* // returns false +* +* @example +* var val = isComplexTypedArrayLike( null ); +* // returns false +* +* @example +* var val = isComplexTypedArrayLike( 'beep' ); +* // returns false +*/ +function isComplexTypedArrayLike( value ) { + return ( + value !== null && + typeof value === 'object' && + + // Check for standard typed array properties: + isNonNegativeInteger( value.length ) && + value.length <= MAX_LENGTH && + typeof value.BYTES_PER_ELEMENT === 'number' && + typeof value.byteOffset === 'number' && + typeof value.byteLength === 'number' && + + // Check for properties necessary for complex typed arrays: + typeof value.get === 'function' && + typeof value.set === 'function' + ); +} + + +// EXPORTS // + +module.exports = isComplexTypedArrayLike; diff --git a/is-complex-typed-array-like/package.json b/is-complex-typed-array-like/package.json new file mode 100644 index 00000000..5e42de0a --- /dev/null +++ b/is-complex-typed-array-like/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-complex-typed-array-like", + "version": "0.0.0", + "description": "Test if a value is complex-typed-array-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "complex", + "cmplx", + "typed", + "array", + "typed-array-like", + "like", + "is", + "istypedarraylike", + "length", + "duck type", + "duck", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-complex-typed-array-like/test/test.js b/is-complex-typed-array-like/test/test.js new file mode 100644 index 00000000..3ca02b7d --- /dev/null +++ b/is-complex-typed-array-like/test/test.js @@ -0,0 +1,190 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var isComplexTypedArrayLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isComplexTypedArrayLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a complex-typed-array-like object', function test( t ) { + var values; + var i; + + values = [ + new Complex64Array( 2 ), + new Complex128Array( 2 ), + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0, + 'byteLength': 10, + 'get': function get() {}, + 'set': function set() {} + } + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isComplexTypedArrayLike( values[i] ), true, 'returns true when provided '+values[i] ); + } + + t.end(); +}); + +tape( 'the function returns `false` if not provided a complex-typed-array-like object', function test( t ) { + var values; + var i; + + values = [ + 'beep', + 5, + null, + void 0, + NaN, + true, + false, + {}, + [], + new Int8Array(), + new Uint8Array(), + new Int16Array(), + new Uint16Array(), + new Int32Array(), + new Uint32Array(), + new Float32Array(), + new Float64Array(), + new Uint8ClampedArray(), + function boop() {}, + { + 'length': 3.14, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0, + 'byteLength': 10, + 'get': function get() {}, + 'set': function set() {} + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': true, + 'byteOffset': 0, + 'byteLength': 10, + 'get': function get() {}, + 'set': function set() {} + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': [], + 'byteLength': 10, + 'get': function get() {}, + 'set': function set() {} + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0, + 'byteLength': null, + 'get': function get() {}, + 'set': function set() {} + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0, + 'byteLength': 10, + 'get': null, + 'set': function set() {} + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0, + 'byteLength': 10, + 'get': function get() {}, + 'set': 'set' + }, + { + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0, + 'byteLength': 10, + 'get': function get() {}, + 'set': function set() {} + }, + { + 'length': 10, + 'byteOffset': 0, + 'byteLength': 10, + 'get': function get() {}, + 'set': function set() {} + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteLength': 10, + 'get': function get() {}, + 'set': function set() {} + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0, + 'get': function get() {}, + 'set': function set() {} + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0, + 'byteLength': 10, + 'set': function set() {} + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0, + 'byteLength': 10, + 'get': function get() {} + } + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isComplexTypedArrayLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-complex-typed-array/README.md b/is-complex-typed-array/README.md new file mode 100644 index 00000000..cca18b22 --- /dev/null +++ b/is-complex-typed-array/README.md @@ -0,0 +1,133 @@ + + +# isComplexTypedArray + +> Test if a value is a complex typed array. + +
+ +## Usage + +```javascript +var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' ); +``` + +#### isComplexTypedArray( value ) + +Tests if a value is a complex typed array. + +```javascript +var Complex128Array = require( '@stdlib/array/complex128' ); + +var bool = isComplexTypedArray( new Complex128Array( 10 ) ); +// returns true + +bool = isComplexTypedArray( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' ); + +var arr = new Complex64Array( 10 ); +var bool = isComplexTypedArray( arr ); +// returns true + +arr = new Complex128Array( 10 ); +bool = isComplexTypedArray( arr ); +// returns true + +arr = new Int8Array( 10 ); +bool = isComplexTypedArray( arr ); +// returns false + +arr = new Uint8Array( 10 ); +bool = isComplexTypedArray( arr ); +// returns false + +arr = new Uint8ClampedArray( 10 ); +bool = isComplexTypedArray( arr ); +// returns false + +arr = new Int16Array( 10 ); +bool = isComplexTypedArray( arr ); +// returns false + +arr = new Uint16Array( 10 ); +bool = isComplexTypedArray( arr ); +// returns false + +arr = new Int32Array( 10 ); +bool = isComplexTypedArray( arr ); +// returns false + +arr = new Uint32Array( 10 ); +bool = isComplexTypedArray( arr ); +// returns false + +arr = new Float32Array( 10 ); +bool = isComplexTypedArray( arr ); +// returns false + +arr = new Float64Array( 10 ); +bool = isComplexTypedArray( arr ); +// returns false + +bool = isComplexTypedArray( [] ); +// returns false + +bool = isComplexTypedArray( {} ); +// returns false + +bool = isComplexTypedArray( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-complex-typed-array/benchmark/benchmark.js b/is-complex-typed-array/benchmark/benchmark.js new file mode 100644 index 00000000..d528b7a0 --- /dev/null +++ b/is-complex-typed-array/benchmark/benchmark.js @@ -0,0 +1,144 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var pkg = require( './../package.json' ).name; +var isComplexTypedArray = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + new Date(), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ), + new Complex64Array( 10 ), + new Complex128Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplexTypedArray( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplexTypedArray( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Complex64Array( 10 ), + new Complex128Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplexTypedArray( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-complex-typed-array/docs/repl.txt b/is-complex-typed-array/docs/repl.txt new file mode 100644 index 00000000..8c990db6 --- /dev/null +++ b/is-complex-typed-array/docs/repl.txt @@ -0,0 +1,22 @@ + +{{alias}}( value ) + Tests if a value is a complex typed array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a complex typed array. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/complex64}}( 10 ) ) + true + + See Also + -------- + diff --git a/is-complex-typed-array/docs/types/index.d.ts b/is-complex-typed-array/docs/types/index.d.ts new file mode 100644 index 00000000..b471bdd7 --- /dev/null +++ b/is-complex-typed-array/docs/types/index.d.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a complex typed array. +* +* @param value - value to test +* @returns boolean indicating if a value is a complex typed array +* +* @example +* var Complex128Array = require( `@stdlib/array/complex128` ); +* +* var bool = isComplexTypedArray( new Complex128Array( 10 ) ); +* // returns true +*/ +declare function isComplexTypedArray( value: any ): boolean; + + +// EXPORTS // + +export = isComplexTypedArray; diff --git a/is-complex-typed-array/docs/types/test.ts b/is-complex-typed-array/docs/types/test.ts new file mode 100644 index 00000000..6a84506e --- /dev/null +++ b/is-complex-typed-array/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isComplexTypedArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isComplexTypedArray( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isComplexTypedArray(); // $ExpectError + isComplexTypedArray( [], 123 ); // $ExpectError +} diff --git a/is-complex-typed-array/examples/index.js b/is-complex-typed-array/examples/index.js new file mode 100644 index 00000000..ec8877cd --- /dev/null +++ b/is-complex-typed-array/examples/index.js @@ -0,0 +1,85 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var isComplexTypedArray = require( './../lib' ); + +var arr = new Complex64Array( 10 ); +console.log( isComplexTypedArray( arr ) ); +// => true + +arr = new Complex128Array( 10 ); +console.log( isComplexTypedArray( arr ) ); +// => true + +arr = new Int8Array( 10 ); +console.log( isComplexTypedArray( arr ) ); +// => false + +arr = new Uint8Array( 10 ); +console.log( isComplexTypedArray( arr ) ); +// => false + +arr = new Uint8ClampedArray( 10 ); +console.log( isComplexTypedArray( arr ) ); +// => false + +arr = new Int16Array( 10 ); +console.log( isComplexTypedArray( arr ) ); +// => false + +arr = new Uint16Array( 10 ); +console.log( isComplexTypedArray( arr ) ); +// => false + +arr = new Int32Array( 10 ); +console.log( isComplexTypedArray( arr ) ); +// => false + +arr = new Uint32Array( 10 ); +console.log( isComplexTypedArray( arr ) ); +// => false + +arr = new Float32Array( 10 ); +console.log( isComplexTypedArray( arr ) ); +// => false + +arr = new Float64Array( 10 ); +console.log( isComplexTypedArray( arr ) ); +// => false + +console.log( isComplexTypedArray( [] ) ); +// => false + +console.log( isComplexTypedArray( {} ) ); +// => false + +console.log( isComplexTypedArray( null ) ); +// => false diff --git a/is-complex-typed-array/lib/ctors.js b/is-complex-typed-array/lib/ctors.js new file mode 100644 index 00000000..a8644740 --- /dev/null +++ b/is-complex-typed-array/lib/ctors.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); + + +// MAIN // + +var CTORS = [ + Complex128Array, + Complex64Array +]; + + +// EXPORTS // + +module.exports = CTORS; diff --git a/is-complex-typed-array/lib/index.js b/is-complex-typed-array/lib/index.js new file mode 100644 index 00000000..2c2df6cd --- /dev/null +++ b/is-complex-typed-array/lib/index.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a complex typed array. +* +* @module @stdlib/assert/is-complex-typed-array +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' ); +* +* var bool = isComplexTypedArray( new Complex128Array( 10 ) ); +* // returns true +*/ + +// MODULES // + +var isComplexTypedArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isComplexTypedArray; diff --git a/is-complex-typed-array/lib/main.js b/is-complex-typed-array/lib/main.js new file mode 100644 index 00000000..609e314e --- /dev/null +++ b/is-complex-typed-array/lib/main.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var ctorName = require( '@stdlib/utils/constructor-name' ); +var getPrototypeOf = require( '@stdlib/utils/get-prototype-of' ); +var CTORS = require( './ctors.js' ); +var NAMES = require( './names.json' ); + + +// MAIN // + +/** +* Tests if a value is a complex typed array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a complex typed array +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* +* var bool = isComplexTypedArray( new Complex128Array( 10 ) ); +* // returns true +*/ +function isComplexTypedArray( value ) { + var v; + var i; + + if ( typeof value !== 'object' || value === null ) { + return false; + } + // Check for complex typed array objects from the same realm (same Node.js `vm` or same `Window` object)... + for ( i = 0; i < CTORS.length; i++ ) { + if ( value instanceof CTORS[ i ] ) { + return true; + } + } + // Walk the prototype tree until we find an object having a desired class... + while ( value ) { + v = ctorName( value ); + for ( i = 0; i < NAMES.length; i++ ) { + if ( NAMES[ i ] === v ) { + return true; + } + } + value = getPrototypeOf( value ); + } + + return false; +} + + +// EXPORTS // + +module.exports = isComplexTypedArray; diff --git a/is-complex-typed-array/lib/names.json b/is-complex-typed-array/lib/names.json new file mode 100644 index 00000000..b1f6dc23 --- /dev/null +++ b/is-complex-typed-array/lib/names.json @@ -0,0 +1,4 @@ +[ + "Complex64Array", + "Complex128Array" +] diff --git a/is-complex-typed-array/package.json b/is-complex-typed-array/package.json new file mode 100644 index 00000000..d0910e75 --- /dev/null +++ b/is-complex-typed-array/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-complex-typed-array", + "version": "0.0.0", + "description": "Test if a value is a complex typed array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "complex", + "cmplx", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "class", + "constructor", + "name", + "type", + "check" + ] +} diff --git a/is-complex-typed-array/test/test.js b/is-complex-typed-array/test/test.js new file mode 100644 index 00000000..17842944 --- /dev/null +++ b/is-complex-typed-array/test/test.js @@ -0,0 +1,157 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var vm = require( 'vm' ); // TODO: handle in-browser tests +var tape = require( 'tape' ); +var inherit = require( '@stdlib/utils/inherit' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var isComplexTypedArray = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': IS_BROWSER +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isComplexTypedArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a complex typed array', function test( t ) { + var values; + var i; + + values = [ + new Complex128Array( 10 ), + new Complex64Array( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isComplexTypedArray( values[i] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided a non-complex typed array', function test( t ) { + var values; + var i; + + values = [ + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Float32Array( 10 ), + new Float64Array( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isComplexTypedArray( values[i] ), false, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an object inheriting from a complex typed array', function test( t ) { + function CustomArray( data ) { + var i; + for ( i = 0; i < data.length; i++ ) { + this[ i ] = data[ i ]; + } + return this; + } + + inherit( CustomArray, Complex64Array ); + + t.strictEqual( isComplexTypedArray( new CustomArray( [ 5.0, 3.0 ] ) ), true, 'returns true when provided a value which inherits from a complex typed array' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a complex typed array from a different realm', opts, function test( t ) { + var ctx; + var arr; + + // FIXME: this test does not test what it is supposed to test. Since `require` is from the same realm, we generate objects from the same realm. In order to fix this, we need to create a "new" require function with a fresh cache. This has been done elsewhere in the project, but requires a bit of code to achieve. + ctx = { + 'require': require + }; + arr = vm.runInNewContext( 'new ( require( "@stdlib/array/complex64" ) )( [ 5.0, 3.0 ] )', ctx ); + t.strictEqual( isComplexTypedArray( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if provided an object from a different realm which inherits from a complex typed array', opts, function test( t ) { + var ctx; + var arr; + + // FIXME: this test does not test what it is supposed to test. Since `require` is from the same realm, we generate objects from the same realm. In order to fix this, we need to create a "new" require function with a fresh cache. This has been done elsewhere in the project, but requires a bit of code to achieve. + ctx = { + 'require': require + }; + arr = vm.runInNewContext( 'function Arr() { return this; }; Arr.prototype = Object.create( require( "@stdlib/array/complex64" ).prototype ); Arr.prototype.constructor = Arr; new Arr( [ 5.0, 3.0 ] );', ctx ); + t.strictEqual( isComplexTypedArray( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if not provided a complex typed array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isComplexTypedArray( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-complex/README.md b/is-complex/README.md new file mode 100644 index 00000000..70cdb8d8 --- /dev/null +++ b/is-complex/README.md @@ -0,0 +1,90 @@ + + +# isComplex + +> Test if a value is a [64-bit][@stdlib/complex/float32] or [128-bit][@stdlib/complex/float64] complex number. + +
+ +## Usage + +```javascript +var isComplex = require( '@stdlib/assert/is-complex' ); +``` + +#### isComplex( value ) + +Tests if a value is a [64-bit][@stdlib/complex/float32] or [128-bit][@stdlib/complex/float64] complex number. + +```javascript +var Complex128 = require( '@stdlib/complex/float64' ); +var Complex64 = require( '@stdlib/complex/float32' ); + +var x = new Complex128( 1.0, 3.0 ); +var bool = isComplex( x ); +// returns true + +x = new Complex64( 3.0, 1.0 ); +bool = isComplex( x ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var isComplex = require( '@stdlib/assert/is-complex' ); + +var out = isComplex( new Complex64( 2.0, 2.0 ) ); +// returns true + +out = isComplex( new Complex128( 3.0, 1.0 ) ); +// returns true + +out = isComplex( {} ); +// returns false + +out = isComplex( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-complex/benchmark/benchmark.js b/is-complex/benchmark/benchmark.js new file mode 100644 index 00000000..a829c13e --- /dev/null +++ b/is-complex/benchmark/benchmark.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var pkg = require( './../package.json' ).name; +var isComplex = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var z; + var N; + var i; + + values = [ + new Complex128( 1.0, -1.0 ), + new Complex64( 2.0, -2.0 ) + ]; + N = values.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = values[ i%N ]; + bool = isComplex( z ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-complex/docs/repl.txt b/is-complex/docs/repl.txt new file mode 100644 index 00000000..291e1d30 --- /dev/null +++ b/is-complex/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( value ) + Tests if a value is a 64-bit or 128-bit complex number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a 64-bit or 128-bit complex + number. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/complex/float32}}( 2.0, 2.0 ) ) + true + > bool = {{alias}}( new {{alias:@stdlib/complex/float64}}( 3.0, 1.0 ) ) + true + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-complex/docs/types/index.d.ts b/is-complex/docs/types/index.d.ts new file mode 100644 index 00000000..8897da39 --- /dev/null +++ b/is-complex/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a 64-bit or 128-bit complex number. +* +* @param value - value to test +* @returns boolean indicating if a value is a 64-bit or 128-bit complex number +* +* @example +* var Complex128 = require( `@stdlib/complex/float64` ); +* var Complex64 = require( `@stdlib/complex/float32` ); +* +* var x = new Complex128( 4.0, 2.0 ); +* var bool = isComplex( x ); +* // returns true +* +* x = new Complex64( 4.0, 2.0 ); +* bool = isComplex( x ); +* // returns true +*/ +declare function isComplex( value: any ): boolean; + + +// EXPORTS // + +export = isComplex; diff --git a/is-complex/docs/types/test.ts b/is-complex/docs/types/test.ts new file mode 100644 index 00000000..cc29749e --- /dev/null +++ b/is-complex/docs/types/test.ts @@ -0,0 +1,37 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isComplex = require( './index' ); +import Complex128 = require( '@stdlib/complex/float64' ); +import Complex64 = require( '@stdlib/complex/float32' ); + + +// TESTS // + +// The function returns a boolean... +{ + isComplex( new Complex128( 5.0, 3.0 ) ); // $ExpectType boolean + isComplex( new Complex64( 5.0, 3.0 ) ); // $ExpectType boolean + isComplex( 123 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isComplex(); // $ExpectError + isComplex( new Complex128( 5.0, 3.0 ), 123 ); // $ExpectError +} diff --git a/is-complex/examples/index.js b/is-complex/examples/index.js new file mode 100644 index 00000000..3930829b --- /dev/null +++ b/is-complex/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var isComplex = require( './../lib' ); + +console.log( isComplex( new Complex64( 2.0, 2.0 ) ) ); +// => true + +console.log( isComplex( new Complex128( 3.0, 1.0 ) ) ); +// => true + +console.log( isComplex( {} ) ); +// => false + +console.log( isComplex( null ) ); +// => false diff --git a/is-complex/lib/index.js b/is-complex/lib/index.js new file mode 100644 index 00000000..87906ba6 --- /dev/null +++ b/is-complex/lib/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a 64-bit or 128-bit complex number. +* +* @module @stdlib/assert/is-complex +* +* @example +* var Complex128 = require( '@stdlib/complex/float64' ); +* var Complex64 = require( '@stdlib/complex/float32' ); +* var isComplex = require( '@stdlib/assert/is-complex' ); +* +* var x = new Complex128( 4.0, 2.0 ); +* var bool = isComplex( x ); +* // returns true +* +* x = new Complex64( 4.0, 2.0 ); +* bool = isComplex( x ); +* // returns true +*/ + +// MODULES // + +var isComplex = require( './main.js' ); + + +// EXPORTS // + +module.exports = isComplex; diff --git a/is-complex/lib/main.js b/is-complex/lib/main.js new file mode 100644 index 00000000..3806cb3b --- /dev/null +++ b/is-complex/lib/main.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Complex128 = require( '@stdlib/complex/float64' ); +var Complex64 = require( '@stdlib/complex/float32' ); +var constructorName = require( '@stdlib/utils/constructor-name' ); + + +// MAIN // + +/** +* Tests if a value is a 64-bit or 128-bit complex number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a 64-bit or 128-bit complex number +* +* @example +* var Complex128 = require( '@stdlib/complex/float64' ); +* var Complex64 = require( '@stdlib/complex/float32' ); +* +* var x = new Complex128( 4.0, 2.0 ); +* var bool = isComplex( x ); +* // returns true +* +* x = new Complex64( 4.0, 2.0 ); +* bool = isComplex( x ); +* // returns true +*/ +function isComplex( value ) { + var type; + if ( value instanceof Complex128 || value instanceof Complex64 ) { + return true; + } + type = constructorName( value ); + return ( type === 'Complex64' || type === 'Complex128' ); +} + + +// EXPORTS // + +module.exports = isComplex; diff --git a/is-complex/package.json b/is-complex/package.json new file mode 100644 index 00000000..69eac49a --- /dev/null +++ b/is-complex/package.json @@ -0,0 +1,81 @@ +{ + "name": "@stdlib/assert/is-complex", + "version": "0.0.0", + "description": "Test if a value is a 64-bit or 128-bit complex number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "iscomplex", + "complex", + "complex64", + "complex128", + "64-bit", + "128-bit", + "float32", + "float64", + "single", + "single-precision", + "double", + "double-precision", + "ieee754", + "type", + "check", + "test", + "validate", + "valid", + "isvalid" + ] +} diff --git a/is-complex/test/test.js b/is-complex/test/test.js new file mode 100644 index 00000000..91a4d808 --- /dev/null +++ b/is-complex/test/test.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var isComplex = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isComplex, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 64-bit or 128-bit complex number', function test( t ) { + t.equal( isComplex( new Complex64( 3.0, 2.0 ) ), true, 'returns true' ); + t.equal( isComplex( new Complex128( 3.0, 2.0 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a complex number', function test( t ) { + var values; + var i; + + values = [ + 5, + '5', + null, + void 0, + NaN, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isComplex( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-complex128/README.md b/is-complex128/README.md new file mode 100644 index 00000000..d98aed9e --- /dev/null +++ b/is-complex128/README.md @@ -0,0 +1,84 @@ + + +# isComplex128 + +> Test if a value is a [128-bit complex number][@stdlib/complex/float64]. + +
+ +## Usage + +```javascript +var isComplex128 = require( '@stdlib/assert/is-complex128' ); +``` + +#### isComplex128( value ) + +Tests if a value is a [128-bit complex number][@stdlib/complex/float64]. + +```javascript +var Complex128 = require( '@stdlib/complex/float64' ); + +var x = new Complex128( 1.0, 3.0 ); + +var bool = isComplex128( x ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var isComplex128 = require( '@stdlib/assert/is-complex128' ); + +var out = isComplex128( new Complex128( 3.0, 1.0 ) ); +// returns true + +out = isComplex128( new Complex64( 2.0, 2.0 ) ); +// returns false + +out = isComplex128( {} ); +// returns false + +out = isComplex128( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-complex128/benchmark/benchmark.js b/is-complex128/benchmark/benchmark.js new file mode 100644 index 00000000..e49f5007 --- /dev/null +++ b/is-complex128/benchmark/benchmark.js @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Complex128 = require( '@stdlib/complex/float64' ); +var pkg = require( './../package.json' ).name; +var isComplex128 = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Complex128( 1.0, 2.0 ), + new Complex128( 3.0, 4.0 ), + new Complex128( 5.0, 6.0 ), + new Complex128( 7.0, 8.0 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplex128( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + [], + {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplex128( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-complex128/docs/repl.txt b/is-complex128/docs/repl.txt new file mode 100644 index 00000000..4b8fd49b --- /dev/null +++ b/is-complex128/docs/repl.txt @@ -0,0 +1,28 @@ + +{{alias}}( value ) + Tests if a value is a 128-bit complex number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a 128-bit complex number. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/complex/float64}}( 3.0, 1.0 ) ) + true + > bool = {{alias}}( new {{alias:@stdlib/complex/float32}}( 2.0, 2.0 ) ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-complex128/docs/types/index.d.ts b/is-complex128/docs/types/index.d.ts new file mode 100644 index 00000000..f893a179 --- /dev/null +++ b/is-complex128/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a 128-bit complex number. +* +* @param value - value to test +* @returns boolean indicating if a value is a 128-bit complex number +* +* @example +* var Complex128 = require( `@stdlib/complex/float64` ); +* +* var x = new Complex128( 4.0, 2.0 ); +* +* var bool = isComplex128( x ); +* // returns true +*/ +declare function isComplex128( value: any ): boolean; + + +// EXPORTS // + +export = isComplex128; diff --git a/is-complex128/docs/types/test.ts b/is-complex128/docs/types/test.ts new file mode 100644 index 00000000..af3d10da --- /dev/null +++ b/is-complex128/docs/types/test.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isComplex128 = require( './index' ); +import Complex128 = require( '@stdlib/complex/float64' ); +import Complex64 = require( '@stdlib/complex/float32' ); + + +// TESTS // + +// The function returns a boolean... +{ + isComplex128( new Complex128( 5.0, 3.0 ) ); // $ExpectType boolean + isComplex128( new Complex64( 5.0, 3.0 ) ); // $ExpectType boolean + isComplex128( { 're': 5.0, 'im': 3.0 } ); // $ExpectType boolean + isComplex128( 123 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isComplex128(); // $ExpectError + isComplex128( new Complex128( 5.0, 3.0 ), 123 ); // $ExpectError +} diff --git a/is-complex128/examples/index.js b/is-complex128/examples/index.js new file mode 100644 index 00000000..d094c835 --- /dev/null +++ b/is-complex128/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var isComplex128 = require( './../lib' ); + +console.log( isComplex128( new Complex128( 3.0, 1.0 ) ) ); +// => true + +console.log( isComplex128( new Complex64( 2.0, 2.0 ) ) ); +// => false + +console.log( isComplex128( {} ) ); +// => false + +console.log( isComplex128( null ) ); +// => false diff --git a/is-complex128/lib/index.js b/is-complex128/lib/index.js new file mode 100644 index 00000000..d7745e33 --- /dev/null +++ b/is-complex128/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a 128-bit complex number. +* +* @module @stdlib/assert/is-complex128 +* +* @example +* var Complex128 = require( '@stdlib/complex/float64' ); +* var isComplex128 = require( '@stdlib/assert/is-complex128' ); +* +* var x = new Complex128( 4.0, 2.0 ); +* +* var bool = isComplex128( x ); +* // returns true +*/ + +// MODULES // + +var isComplex128 = require( './main.js' ); + + +// EXPORTS // + +module.exports = isComplex128; diff --git a/is-complex128/lib/main.js b/is-complex128/lib/main.js new file mode 100644 index 00000000..956c60b4 --- /dev/null +++ b/is-complex128/lib/main.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Complex128 = require( '@stdlib/complex/float64' ); +var constructorName = require( '@stdlib/utils/constructor-name' ); + + +// MAIN // + +/** +* Tests if a value is a 128-bit complex number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a 128-bit complex number +* +* @example +* var Complex128 = require( '@stdlib/complex/float64' ); +* +* var x = new Complex128( 4.0, 2.0 ); +* +* var bool = isComplex128( x ); +* // returns true +*/ +function isComplex128( value ) { + return ( + value instanceof Complex128 || + constructorName( value ) === 'Complex128' + ); +} + + +// EXPORTS // + +module.exports = isComplex128; diff --git a/is-complex128/package.json b/is-complex128/package.json new file mode 100644 index 00000000..b9cc561c --- /dev/null +++ b/is-complex128/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-complex128", + "version": "0.0.0", + "description": "Test if a value is a 128-bit complex number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "iscomplex", + "complex128", + "128-bit", + "float64", + "double", + "double-precision", + "ieee754", + "type", + "check", + "validate", + "isvalid", + "test" + ] +} diff --git a/is-complex128/test/test.js b/is-complex128/test/test.js new file mode 100644 index 00000000..7a3e7e33 --- /dev/null +++ b/is-complex128/test/test.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var isComplex128 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isComplex128, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 128-bit complex number', function test( t ) { + t.equal( isComplex128( new Complex128( 3.0, 2.0 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a 128-bit complex number', function test( t ) { + var values; + var i; + + values = [ + 5, + '5', + null, + void 0, + NaN, + true, + false, + [], + {}, + new Complex64( 2.0, 2.0 ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isComplex128( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-complex128array/README.md b/is-complex128array/README.md new file mode 100644 index 00000000..132bce4e --- /dev/null +++ b/is-complex128array/README.md @@ -0,0 +1,124 @@ + + +# isComplex128Array + +> Test if a value is a [Complex128Array][@stdlib/array/complex128]. + +
+ +## Usage + +```javascript +var isComplex128Array = require( '@stdlib/assert/is-complex128array' ); +``` + +#### isComplex128Array( value ) + +Tests if a value is a [`Complex128Array`][@stdlib/array/complex128]. + +```javascript +var Complex128Array = require( '@stdlib/array/complex128' ); + +var bool = isComplex128Array( new Complex128Array( 10 ) ); +// returns true + +bool = isComplex128Array( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var isComplex128Array = require( '@stdlib/assert/is-complex128array' ); + +var bool = isComplex128Array( new Complex128Array( 10 ) ); +// returns true + +bool = isComplex128Array( new Complex64Array( 10 ) ); +// returns false + +bool = isComplex128Array( new Float64Array( 10 ) ); +// returns false + +bool = isComplex128Array( new Int8Array( 10 ) ); +// returns false + +bool = isComplex128Array( new Uint8Array( 10 ) ); +// returns false + +bool = isComplex128Array( new Uint8ClampedArray( 10 ) ); +// returns false + +bool = isComplex128Array( new Int16Array( 10 ) ); +// returns false + +bool = isComplex128Array( new Uint16Array( 10 ) ); +// returns false + +bool = isComplex128Array( new Int32Array( 10 ) ); +// returns false + +bool = isComplex128Array( new Uint32Array( 10 ) ); +// returns false + +bool = isComplex128Array( new Float32Array( 10 ) ); +// returns false + +bool = isComplex128Array( new Array( 10 ) ); +// returns false + +bool = isComplex128Array( {} ); +// returns false + +bool = isComplex128Array( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-complex128array/benchmark/benchmark.js b/is-complex128array/benchmark/benchmark.js new file mode 100644 index 00000000..068cffe0 --- /dev/null +++ b/is-complex128array/benchmark/benchmark.js @@ -0,0 +1,129 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isComplex128Array = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ), + new Complex128Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplex128Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Complex128Array( 10 ), + new Complex128Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplex128Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplex128Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-complex128array/docs/repl.txt b/is-complex128array/docs/repl.txt new file mode 100644 index 00000000..f5465f6f --- /dev/null +++ b/is-complex128array/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a Complex128Array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a Complex128Array. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/complex128}}( 10 ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-complex128array/docs/types/index.d.ts b/is-complex128array/docs/types/index.d.ts new file mode 100644 index 00000000..e11a8544 --- /dev/null +++ b/is-complex128array/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a Complex128Array. +* +* @param value - value to test +* @returns boolean indicating whether value is a Complex128Array +* +* @example +* var bool = isComplex128Array( new Complex128Array( 10 ) ); +* // returns true +* +* @example +* var bool = isComplex128Array( [] ); +* // returns false +*/ +declare function isComplex128Array( value: any ): boolean; + + +// EXPORTS // + +export = isComplex128Array; diff --git a/is-complex128array/docs/types/test.ts b/is-complex128array/docs/types/test.ts new file mode 100644 index 00000000..0721bbf4 --- /dev/null +++ b/is-complex128array/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isComplex128Array = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isComplex128Array( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isComplex128Array(); // $ExpectError + isComplex128Array( [], 123 ); // $ExpectError +} diff --git a/is-complex128array/examples/index.js b/is-complex128array/examples/index.js new file mode 100644 index 00000000..31c2ddce --- /dev/null +++ b/is-complex128array/examples/index.js @@ -0,0 +1,88 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var isComplex128Array = require( './../lib' ); + +var bool = isComplex128Array( new Complex128Array( 10 ) ); +console.log( bool ); +// => true + +bool = isComplex128Array( new Complex64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex128Array( new Float64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex128Array( new Int8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex128Array( new Uint8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex128Array( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex128Array( new Int16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex128Array( new Uint16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex128Array( new Int32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex128Array( new Uint32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex128Array( new Float32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex128Array( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex128Array( {} ); +console.log( bool ); +// => false + +bool = isComplex128Array( null ); +console.log( bool ); +// => false diff --git a/is-complex128array/lib/index.js b/is-complex128array/lib/index.js new file mode 100644 index 00000000..ef7a1ba1 --- /dev/null +++ b/is-complex128array/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a Complex128Array. +* +* @module @stdlib/assert/is-complex128array +* +* @example +* var isComplex128Array = require( '@stdlib/assert/is-complex128array' ); +* +* var bool = isComplex128Array( new Complex128Array( 10 ) ); +* // returns true +* +* bool = isComplex128Array( [] ); +* // returns false +*/ + +// MODULES // + +var isComplex128Array = require( './main.js' ); + + +// EXPORTS // + +module.exports = isComplex128Array; diff --git a/is-complex128array/lib/main.js b/is-complex128array/lib/main.js new file mode 100644 index 00000000..16b45330 --- /dev/null +++ b/is-complex128array/lib/main.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Complex128Array = require( '@stdlib/array/complex128' ); +var constructorName = require( '@stdlib/utils/constructor-name' ); + + +// MAIN // + +/** +* Tests if a value is a Complex128Array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a Complex128Array +* +* @example +* var bool = isComplex128Array( new Complex128Array( 10 ) ); +* // returns true +* +* @example +* var bool = isComplex128Array( [] ); +* // returns false +*/ +function isComplex128Array( value ) { + return ( + value instanceof Complex128Array || + constructorName( value ) === 'Complex128Array' + ); +} + + +// EXPORTS // + +module.exports = isComplex128Array; diff --git a/is-complex128array/package.json b/is-complex128array/package.json new file mode 100644 index 00000000..cb11cb53 --- /dev/null +++ b/is-complex128array/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/assert/is-complex128array", + "version": "0.0.0", + "description": "Test if a value is a Complex128Array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "complex128array", + "complex128", + "double", + "double-precision", + "float64", + "ieee754", + "typed", + "typed array", + "typed-array", + "array", + "is", + "isarray", + "istypedarray", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-complex128array/test/test.js b/is-complex128array/test/test.js new file mode 100644 index 00000000..2e285836 --- /dev/null +++ b/is-complex128array/test/test.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var isComplex128Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isComplex128Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a Complex128Array', function test( t ) { + t.strictEqual( isComplex128Array( new Complex128Array( 10 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a Complex128Array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + undefined, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Uint32Array( 10 ), + new Int32Array( 10 ), + new Uint16Array( 10 ), + new Int16Array( 10 ), + new Uint8Array( 10 ), + new Int8Array( 10 ), + new Uint8ClampedArray( 10 ), + new Complex64Array( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isComplex128Array( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-complex64/README.md b/is-complex64/README.md new file mode 100644 index 00000000..d34c9362 --- /dev/null +++ b/is-complex64/README.md @@ -0,0 +1,84 @@ + + +# isComplex64 + +> Test if a value is a [64-bit complex number][@stdlib/complex/float32]. + +
+ +## Usage + +```javascript +var isComplex64 = require( '@stdlib/assert/is-complex64' ); +``` + +#### isComplex64( value ) + +Tests if a value is a [64-bit complex number][@stdlib/complex/float32]. + +```javascript +var Complex64 = require( '@stdlib/complex/float32' ); + +var x = new Complex64( 1.0, 3.0 ); + +var bool = isComplex64( x ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var isComplex64 = require( '@stdlib/assert/is-complex64' ); + +var out = isComplex64( new Complex64( 2.0, 2.0 ) ); +// returns true + +out = isComplex64( new Complex128( 3.0, 1.0 ) ); +// returns false + +out = isComplex64( {} ); +// returns false + +out = isComplex64( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-complex64/benchmark/benchmark.js b/is-complex64/benchmark/benchmark.js new file mode 100644 index 00000000..30fc6a0e --- /dev/null +++ b/is-complex64/benchmark/benchmark.js @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Complex64 = require( '@stdlib/complex/float32' ); +var pkg = require( './../package.json' ).name; +var isComplex64 = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Complex64( 1.0, 2.0 ), + new Complex64( 3.0, 4.0 ), + new Complex64( 5.0, 6.0 ), + new Complex64( 7.0, 8.0 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplex64( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + [], + {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplex64( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-complex64/docs/repl.txt b/is-complex64/docs/repl.txt new file mode 100644 index 00000000..a6866ee8 --- /dev/null +++ b/is-complex64/docs/repl.txt @@ -0,0 +1,28 @@ + +{{alias}}( value ) + Tests if a value is a 64-bit complex number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a 64-bit complex number. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/complex/float32}}( 2.0, 2.0 ) ) + true + > bool = {{alias}}( new {{alias:@stdlib/complex/float64}}( 3.0, 1.0 ) ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-complex64/docs/types/index.d.ts b/is-complex64/docs/types/index.d.ts new file mode 100644 index 00000000..1a4e178c --- /dev/null +++ b/is-complex64/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a 64-bit complex number. +* +* @param value - value to test +* @returns boolean indicating if a value is a 64-bit complex number +* +* @example +* var Complex64 = require( `@stdlib/complex/float32` ); +* +* var x = new Complex64( 4.0, 2.0 ); +* +* var bool = isComplex64( x ); +* // returns true +*/ +declare function isComplex64( value: any ): boolean; + + +// EXPORTS // + +export = isComplex64; diff --git a/is-complex64/docs/types/test.ts b/is-complex64/docs/types/test.ts new file mode 100644 index 00000000..08ecebb3 --- /dev/null +++ b/is-complex64/docs/types/test.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isComplex64 = require( './index' ); +import Complex128 = require( '@stdlib/complex/float64' ); +import Complex64 = require( '@stdlib/complex/float32' ); + + +// TESTS // + +// The function returns a boolean... +{ + isComplex64( new Complex64( 5.0, 3.0 ) ); // $ExpectType boolean + isComplex64( new Complex128( 5.0, 3.0 ) ); // $ExpectType boolean + isComplex64( { 're': 5.0, 'im': 3.0 } ); // $ExpectType boolean + isComplex64( 123 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isComplex64(); // $ExpectError + isComplex64( new Complex64( 5.0, 3.0 ), 123 ); // $ExpectError +} diff --git a/is-complex64/examples/index.js b/is-complex64/examples/index.js new file mode 100644 index 00000000..2e57e110 --- /dev/null +++ b/is-complex64/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var isComplex64 = require( './../lib' ); + +console.log( isComplex64( new Complex64( 2.0, 2.0 ) ) ); +// => true + +console.log( isComplex64( new Complex128( 3.0, 1.0 ) ) ); +// => false + +console.log( isComplex64( {} ) ); +// => false + +console.log( isComplex64( null ) ); +// => false diff --git a/is-complex64/lib/index.js b/is-complex64/lib/index.js new file mode 100644 index 00000000..e7889acd --- /dev/null +++ b/is-complex64/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a 64-bit complex number. +* +* @module @stdlib/assert/is-complex64 +* +* @example +* var Complex64 = require( '@stdlib/complex/float32' ); +* var isComplex64 = require( '@stdlib/assert/is-complex64' ); +* +* var x = new Complex64( 4.0, 2.0 ); +* +* var bool = isComplex64( x ); +* // returns true +*/ + +// MODULES // + +var isComplex64 = require( './main.js' ); + + +// EXPORTS // + +module.exports = isComplex64; diff --git a/is-complex64/lib/main.js b/is-complex64/lib/main.js new file mode 100644 index 00000000..36a37767 --- /dev/null +++ b/is-complex64/lib/main.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Complex64 = require( '@stdlib/complex/float32' ); +var constructorName = require( '@stdlib/utils/constructor-name' ); + + +// MAIN // + +/** +* Tests if a value is a 64-bit complex number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a 64-bit complex number +* +* @example +* var Complex64 = require( '@stdlib/complex/float32' ); +* +* var x = new Complex64( 4.0, 2.0 ); +* +* var bool = isComplex64( x ); +* // returns true +*/ +function isComplex64( value ) { + return ( + value instanceof Complex64 || + constructorName( value ) === 'Complex64' + ); +} + + +// EXPORTS // + +module.exports = isComplex64; diff --git a/is-complex64/package.json b/is-complex64/package.json new file mode 100644 index 00000000..51b85d6c --- /dev/null +++ b/is-complex64/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-complex64", + "version": "0.0.0", + "description": "Test if a value is a 64-bit complex number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "iscomplex", + "complex64", + "64-bit", + "float32", + "single", + "single-precision", + "ieee754", + "type", + "check", + "test", + "validate", + "isvalid", + "valid" + ] +} diff --git a/is-complex64/test/test.js b/is-complex64/test/test.js new file mode 100644 index 00000000..ea4ce4ee --- /dev/null +++ b/is-complex64/test/test.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var isComplex64 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isComplex64, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 64-bit complex number', function test( t ) { + t.equal( isComplex64( new Complex64( 3.0, 2.0 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a 64-bit complex number', function test( t ) { + var values; + var i; + + values = [ + 5, + '5', + null, + undefined, + NaN, + true, + [], + {}, + new Complex128( 2.0, 2.0 ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isComplex64( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-complex64array/README.md b/is-complex64array/README.md new file mode 100644 index 00000000..18c925a9 --- /dev/null +++ b/is-complex64array/README.md @@ -0,0 +1,124 @@ + + +# isComplex64Array + +> Test if a value is a [Complex64Array][@stdlib/array/complex64]. + +
+ +## Usage + +```javascript +var isComplex64Array = require( '@stdlib/assert/is-complex64array' ); +``` + +#### isComplex64Array( value ) + +Tests if a value is a [`Complex64Array`][@stdlib/array/complex64]. + +```javascript +var Complex64Array = require( '@stdlib/array/complex64' ); + +var bool = isComplex64Array( new Complex64Array( 10 ) ); +// returns true + +bool = isComplex64Array( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var isComplex64Array = require( '@stdlib/assert/is-complex64array' ); + +var bool = isComplex64Array( new Complex64Array( 10 ) ); +// returns true + +bool = isComplex64Array( new Complex128Array( 10 ) ); +// returns false + +bool = isComplex64Array( new Float64Array( 10 ) ); +// returns false + +bool = isComplex64Array( new Int8Array( 10 ) ); +// returns false + +bool = isComplex64Array( new Uint8Array( 10 ) ); +// returns false + +bool = isComplex64Array( new Uint8ClampedArray( 10 ) ); +// returns false + +bool = isComplex64Array( new Int16Array( 10 ) ); +// returns false + +bool = isComplex64Array( new Uint16Array( 10 ) ); +// returns false + +bool = isComplex64Array( new Int32Array( 10 ) ); +// returns false + +bool = isComplex64Array( new Uint32Array( 10 ) ); +// returns false + +bool = isComplex64Array( new Float32Array( 10 ) ); +// returns false + +bool = isComplex64Array( new Array( 10 ) ); +// returns false + +bool = isComplex64Array( {} ); +// returns false + +bool = isComplex64Array( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-complex64array/benchmark/benchmark.js b/is-complex64array/benchmark/benchmark.js new file mode 100644 index 00000000..d8e73149 --- /dev/null +++ b/is-complex64array/benchmark/benchmark.js @@ -0,0 +1,129 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isComplex64Array = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ), + new Complex64Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplex64Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Complex64Array( 10 ), + new Complex64Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplex64Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComplex64Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-complex64array/docs/repl.txt b/is-complex64array/docs/repl.txt new file mode 100644 index 00000000..e07b2392 --- /dev/null +++ b/is-complex64array/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a Complex64Array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a Complex64Array. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/complex64}}( 10 ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-complex64array/docs/types/index.d.ts b/is-complex64array/docs/types/index.d.ts new file mode 100644 index 00000000..54f06e37 --- /dev/null +++ b/is-complex64array/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a Complex64Array. +* +* @param value - value to test +* @returns boolean indicating whether value is a Complex64Array +* +* @example +* var bool = isComplex64Array( new Complex64Array( 10 ) ); +* // returns true +* +* @example +* var bool = isComplex64Array( [] ); +* // returns false +*/ +declare function isComplex64Array( value: any ): boolean; + + +// EXPORTS // + +export = isComplex64Array; diff --git a/is-complex64array/docs/types/test.ts b/is-complex64array/docs/types/test.ts new file mode 100644 index 00000000..54e74807 --- /dev/null +++ b/is-complex64array/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isComplex64Array = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isComplex64Array( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isComplex64Array(); // $ExpectError + isComplex64Array( [], 123 ); // $ExpectError +} diff --git a/is-complex64array/examples/index.js b/is-complex64array/examples/index.js new file mode 100644 index 00000000..059dd5ea --- /dev/null +++ b/is-complex64array/examples/index.js @@ -0,0 +1,88 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var isComplex64Array = require( './../lib' ); + +var bool = isComplex64Array( new Complex64Array( 10 ) ); +console.log( bool ); +// => true + +bool = isComplex64Array( new Complex128Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex64Array( new Float64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex64Array( new Int8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex64Array( new Uint8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex64Array( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex64Array( new Int16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex64Array( new Uint16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex64Array( new Int32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex64Array( new Uint32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex64Array( new Float32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex64Array( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isComplex64Array( {} ); +console.log( bool ); +// => false + +bool = isComplex64Array( null ); +console.log( bool ); +// => false diff --git a/is-complex64array/lib/index.js b/is-complex64array/lib/index.js new file mode 100644 index 00000000..eaeac434 --- /dev/null +++ b/is-complex64array/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a Complex64Array. +* +* @module @stdlib/assert/is-complex64array +* +* @example +* var isComplex64Array = require( '@stdlib/assert/is-complex64array' ); +* +* var bool = isComplex64Array( new Complex64Array( 10 ) ); +* // returns true +* +* bool = isComplex64Array( [] ); +* // returns false +*/ + +// MODULES // + +var isComplex64Array = require( './main.js' ); + + +// EXPORTS // + +module.exports = isComplex64Array; diff --git a/is-complex64array/lib/main.js b/is-complex64array/lib/main.js new file mode 100644 index 00000000..b02bb610 --- /dev/null +++ b/is-complex64array/lib/main.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Complex64Array = require( '@stdlib/array/complex64' ); +var constructorName = require( '@stdlib/utils/constructor-name' ); + + +// MAIN // + +/** +* Tests if a value is a Complex64Array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a Complex64Array +* +* @example +* var bool = isComplex64Array( new Complex64Array( 10 ) ); +* // returns true +* +* @example +* var bool = isComplex64Array( [] ); +* // returns false +*/ +function isComplex64Array( value ) { + return ( + value instanceof Complex64Array || + constructorName( value ) === 'Complex64Array' + ); +} + + +// EXPORTS // + +module.exports = isComplex64Array; diff --git a/is-complex64array/package.json b/is-complex64array/package.json new file mode 100644 index 00000000..04f05951 --- /dev/null +++ b/is-complex64array/package.json @@ -0,0 +1,81 @@ +{ + "name": "@stdlib/assert/is-complex64array", + "version": "0.0.0", + "description": "Test if a value is a Complex64Array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "complex64array", + "complex64", + "single", + "single-precision", + "float", + "float32", + "ieee754", + "typed", + "typed array", + "typed-array", + "array", + "is", + "isarray", + "istypedarray", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-complex64array/test/test.js b/is-complex64array/test/test.js new file mode 100644 index 00000000..14524d69 --- /dev/null +++ b/is-complex64array/test/test.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var isComplex64Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isComplex64Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a Complex64Array', function test( t ) { + t.strictEqual( isComplex64Array( new Complex64Array( 10 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a Complex64Array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + undefined, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Uint32Array( 10 ), + new Int32Array( 10 ), + new Uint16Array( 10 ), + new Int16Array( 10 ), + new Uint8Array( 10 ), + new Int8Array( 10 ), + new Uint8ClampedArray( 10 ), + new Complex128Array( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isComplex64Array( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-composite/README.md b/is-composite/README.md new file mode 100644 index 00000000..01d9944b --- /dev/null +++ b/is-composite/README.md @@ -0,0 +1,153 @@ + + +# isComposite + +> Test if a value is a composite number. + +
+ +A **composite number** is defined as a positive integer value greater than `1` which has **at least** one divisor other than `1` and itself (i.e., an integer value which can be formed by multiplying two smaller positive integers). + +
+ + + +
+ +## Usage + +```javascript +var isComposite = require( '@stdlib/assert/is-composite' ); +``` + +#### isComposite( value ) + +Tests if a `value` is a composite number. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isComposite( 4.0 ); +// returns true + +bool = isComposite( new Number( 4.0 ) ); +// returns true + +bool = isComposite( 3.14 ); +// returns false + +bool = isComposite( -4.0 ); +// returns false + +bool = isComposite( NaN ); +// returns false + +bool = isComposite( null ); +// returns false +``` + +#### isComposite.isPrimitive( value ) + +Tests if a `value` is a primitive composite number. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isComposite.isPrimitive( 4.0 ); +// returns true + +bool = isComposite.isPrimitive( new Number( 4.0 ) ); +// returns false +``` + +#### isComposite.isObject( value ) + +Tests if a `value` is a `Number` object having a value which is a composite number. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isComposite.isObject( 4.0 ); +// returns false + +bool = isComposite.isObject( new Number( 4.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isComposite = require( '@stdlib/assert/is-composite' ); + +var bool = isComposite( 4.0 ); +// returns true + +bool = isComposite( new Number( 4.0 ) ); +// returns true + +bool = isComposite( 10.0 ); +// returns true + +bool = isComposite( 7.0 ); +// returns false + +bool = isComposite( 3.14 ); +// returns false + +bool = isComposite( -4.0 ); +// returns false + +bool = isComposite( NaN ); +// returns false + +bool = isComposite( '0.5' ); +// returns false + +bool = isComposite( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-composite/benchmark/benchmark.js b/is-composite/benchmark/benchmark.js new file mode 100644 index 00000000..50df1154 --- /dev/null +++ b/is-composite/benchmark/benchmark.js @@ -0,0 +1,233 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isComposite = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 0.5, + 1.0, + 0.0, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComposite( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 4.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComposite( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComposite.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 4.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComposite.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComposite.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 4.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComposite.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-composite/benchmark/benchmark.primes.js b/is-composite/benchmark/benchmark.primes.js new file mode 100644 index 00000000..6e111de5 --- /dev/null +++ b/is-composite/benchmark/benchmark.primes.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var log10 = require( '@stdlib/math/base/special/log10' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var PRIMES = require( './../test/fixtures/primes.js' ); +var isComposite = require( './../lib' ).isPrimitive; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} prime - prime number +* @returns {Function} benchmark function +*/ +function createBenchmark( prime ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isComposite( prime ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var max; + var f; + var p; + var i; + var j; + + min = 1; // 10^min + max = floor( log10( PRIMES.length ) ); // 10^max + + for ( i = min; i <= max; i++ ) { + j = pow( 10, i ) - 1; + p = PRIMES[ j ]; + f = createBenchmark( p ); + bench( pkg+':prime='+p, f ); + } +} + +main(); diff --git a/is-composite/docs/repl.txt b/is-composite/docs/repl.txt new file mode 100644 index 00000000..92c7e775 --- /dev/null +++ b/is-composite/docs/repl.txt @@ -0,0 +1,76 @@ + +{{alias}}( value ) + Tests if a value is a composite number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a composite number. + + Examples + -------- + > var bool = {{alias}}( 4.0 ) + true + > bool = {{alias}}( new Number( 4.0 ) ) + true + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( -4.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive which is a composite number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive which is a + composite number. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 4.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( 4.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a value which is a composite + number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a value which + is a composite number. + + Examples + -------- + > var bool = {{alias}}.isObject( 4.0 ) + false + > bool = {{alias}}.isObject( new Number( 4.0 ) ) + true + + + See Also + -------- + diff --git a/is-composite/docs/types/index.d.ts b/is-composite/docs/types/index.d.ts new file mode 100644 index 00000000..5b06490b --- /dev/null +++ b/is-composite/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface describing methods for testing for primitives and objects. +*/ +interface IsComposite { + /** + * Tests if a value is a composite number. + * + * @param value - value to test + * @returns boolean indicating whether value is a composite number + * + * @example + * var bool = isComposite( 4.0 ); + * // returns true + * + * @example + * var bool = isComposite( new Number( 4.0 ) ); + * // returns true + * + * @example + * var bool = isComposite( 3.14 ); + * // returns false + * + * @example + * var bool = isComposite( -4.0 ); + * // returns false + * + * @example + * var bool = isComposite( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a value which is a composite number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a value which is a composite number + * + * @example + * var bool = isComposite.isPrimitive( 4.0 ); + * // returns true + * + * @example + * var bool = isComposite.isPrimitive( new Number( 4.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a value which is a composite number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a value which is a composite number + * + * @example + * var bool = isComposite.isObject( 4.0 ); + * // returns false + * + * @example + * var bool = isComposite.isObject( new Number( 4.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a composite number. +* +* @param value - value to test +* @returns boolean indicating whether value is a composite number +* +* @example +* var bool = isComposite( 4.0 ); +* // returns true +* +* @example +* var bool = isComposite( new Number( 4.0 ) ); +* // returns true +* +* @example +* var bool = isComposite( 3.14 ); +* // returns false +* +* @example +* var bool = isComposite( -4.0 ); +* // returns false +* +* @example +* var bool = isComposite( null ); +* // returns false +* +* @example +* var bool = isComposite.isPrimitive( 4.0 ); +* // returns true +* +* @example +* var bool = isComposite.isObject( new Number( 4.0 ) ); +* // returns true +*/ +declare var isComposite: IsComposite; + + +// EXPORTS // + +export = isComposite; diff --git a/is-composite/docs/types/test.ts b/is-composite/docs/types/test.ts new file mode 100644 index 00000000..70107454 --- /dev/null +++ b/is-composite/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isComposite = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isComposite( 3 ); // $ExpectType boolean + isComposite( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isComposite(); // $ExpectError + isComposite( 0.2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isComposite.isPrimitive( new Number( 0.2 ) ); // $ExpectType boolean + isComposite.isPrimitive( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isComposite.isPrimitive(); // $ExpectError + isComposite.isPrimitive( 0.2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isComposite.isObject( new Number( 0.2 ) ); // $ExpectType boolean + isComposite.isObject( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isComposite.isObject(); // $ExpectError + isComposite.isObject( 0.2, 123 ); // $ExpectError +} diff --git a/is-composite/examples/index.js b/is-composite/examples/index.js new file mode 100644 index 00000000..799236eb --- /dev/null +++ b/is-composite/examples/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isComposite = require( './../lib' ); + +console.log( isComposite( 4.0 ) ); +// => true + +console.log( isComposite( new Number( 4.0 ) ) ); +// => true + +console.log( isComposite( 10.0 ) ); +// => true + +console.log( isComposite( 7.0 ) ); +// => false + +console.log( isComposite( 3.14 ) ); +// => false + +console.log( isComposite( -4.0 ) ); +// => false + +console.log( isComposite( NaN ) ); +// => false + +console.log( isComposite( '0.5' ) ); +// => false + +console.log( isComposite( null ) ); +// => false diff --git a/is-composite/lib/index.js b/is-composite/lib/index.js new file mode 100644 index 00000000..5e1801f3 --- /dev/null +++ b/is-composite/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a composite number. +* +* @module @stdlib/assert/is-composite +* +* @example +* var isComposite = require( '@stdlib/assert/is-composite' ); +* +* var bool = isComposite( 4.0 ); +* // returns true +* +* bool = isComposite( new Number( 4.0 ) ); +* // returns true +* +* bool = isComposite( 3.14 ); +* // returns false +* +* bool = isComposite( -4.0 ); +* // returns false +* +* bool = isComposite( null ); +* // returns false +* +* @example +* var isComposite = require( '@stdlib/assert/is-composite' ).isPrimitive; +* +* var bool = isComposite( 4.0 ); +* // returns true +* +* bool = isComposite( new Number( 4.0 ) ); +* // returns false +* +* @example +* var isComposite = require( '@stdlib/assert/is-composite' ).isObject; +* +* var bool = isComposite( 4.0 ); +* // returns false +* +* bool = isComposite( new Number( 4.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isComposite = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isComposite, 'isPrimitive', isPrimitive ); +setReadOnly( isComposite, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isComposite; diff --git a/is-composite/lib/main.js b/is-composite/lib/main.js new file mode 100644 index 00000000..4342e209 --- /dev/null +++ b/is-composite/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a composite number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a composite number +* +* @example +* var bool = isComposite( 4.0 ); +* // returns true +* +* @example +* var bool = isComposite( new Number( 4.0 ) ); +* // returns true +* +* @example +* var bool = isComposite( 3.14 ); +* // returns false +* +* @example +* var bool = isComposite( -4.0 ); +* // returns false +* +* @example +* var bool = isComposite( null ); +* // returns false +*/ +function isComposite( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isComposite; diff --git a/is-composite/lib/object.js b/is-composite/lib/object.js new file mode 100644 index 00000000..d38de25c --- /dev/null +++ b/is-composite/lib/object.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; +var iscomposite = require( '@stdlib/math/base/assert/is-composite' ); + + +// MAIN // + +/** +* Tests if a value is a number object having a value which is a composite number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a value which is a composite number +* +* @example +* var bool = isComposite( 4.0 ); +* // returns false +* +* @example +* var bool = isComposite( new Number( 4.0 ) ); +* // returns true +*/ +function isComposite( value ) { + if ( !isNumber( value ) ) { + return false; + } + return iscomposite( value.valueOf() ); +} + + +// EXPORTS // + +module.exports = isComposite; diff --git a/is-composite/lib/primitive.js b/is-composite/lib/primitive.js new file mode 100644 index 00000000..5af794d1 --- /dev/null +++ b/is-composite/lib/primitive.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var iscomposite = require( '@stdlib/math/base/assert/is-composite' ); + + +// MAIN // + +/** +* Tests if a value is a number primitive having a value which is a composite number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a value which is a composite number +* +* @example +* var bool = isComposite( 4.0 ); +* // returns true +* +* @example +* var bool = isComposite( new Number( 4.0 ) ); +* // returns false +*/ +function isComposite( value ) { + if ( !isNumber( value ) ) { + return false; + } + return iscomposite( value ); +} + + +// EXPORTS // + +module.exports = isComposite; diff --git a/is-composite/package.json b/is-composite/package.json new file mode 100644 index 00000000..302bdff8 --- /dev/null +++ b/is-composite/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-composite", + "version": "0.0.0", + "description": "Test if a value is a composite number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "integer", + "int", + "composite", + "prime", + "primality", + "is", + "iscomposite", + "type", + "check", + "valid", + "validate", + "test", + "primitive", + "object" + ] +} diff --git a/is-composite/test/fixtures/primes.js b/is-composite/test/fixtures/primes.js new file mode 100644 index 00000000..8acba9a1 --- /dev/null +++ b/is-composite/test/fixtures/primes.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var primes = require( '@stdlib/datasets/primes-100k' ); + + +// MAIN // + +var PRIMES = primes(); + + +// EXPORTS // + +module.exports = PRIMES; diff --git a/is-composite/test/test.js b/is-composite/test/test.js new file mode 100644 index 00000000..a5985461 --- /dev/null +++ b/is-composite/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isComposite = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isComposite, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test primitives', function test( t ) { + t.equal( typeof isComposite.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test objects', function test( t ) { + t.equal( typeof isComposite.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-composite/test/test.main.js b/is-composite/test/test.main.js new file mode 100644 index 00000000..df51cc00 --- /dev/null +++ b/is-composite/test/test.main.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var PRIMES = require( './fixtures/primes.js' ); +var isComposite = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isComposite, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a composite number', function test( t ) { + t.equal( isComposite( 4 ), true, 'returns expected value' ); + t.equal( isComposite( 6 ), true, 'returns expected value' ); + t.equal( isComposite( 8 ), true, 'returns expected value' ); + t.equal( isComposite( 9 ), true, 'returns expected value' ); + t.equal( isComposite( 10 ), true, 'returns expected value' ); + t.equal( isComposite( 15 ), true, 'returns expected value' ); + + t.equal( isComposite( new Number( 4 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 6 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 8 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 9 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 10 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 15 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + + t.end(); +}); + +tape( 'the function returns `false` if provided a prime number', function test( t ) { + var N; + var p; + var i; + var j; + + t.equal( isComposite( 2 ), false, 'returns expected value' ); + t.equal( isComposite( 3 ), false, 'returns expected value' ); + t.equal( isComposite( 5 ), false, 'returns expected value' ); + t.equal( isComposite( 7 ), false, 'returns expected value' ); + + t.equal( isComposite( new Number( 2 ) ), false, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 3 ) ), false, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 5 ) ), false, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 7 ) ), false, 'returns expected value' ); // eslint-disable-line no-new-wrappers + + N = PRIMES.length - 1; + for ( i = 0; i < 1e3; i++ ) { + j = discreteUniform( 0, N ); + p = PRIMES[ j ]; + t.equal( isComposite( p ), false, 'returns expected value for '+p ); + t.equal( isComposite( new Number( p ) ), false, 'returns expected value for Number('+p+')' ); // eslint-disable-line no-new-wrappers + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a composite number', function test( t ) { + var values; + var i; + + values = [ + '5', + 1.0, + -5.0, + -3.14, + -1.0, + NaN, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isComposite( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-composite/test/test.object.js b/is-composite/test/test.object.js new file mode 100644 index 00000000..00b11717 --- /dev/null +++ b/is-composite/test/test.object.js @@ -0,0 +1,101 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var PRIMES = require( './fixtures/primes.js' ); +var isComposite = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isComposite, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object whose value is a composite number', function test( t ) { + t.equal( isComposite( new Number( 4 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 6 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 8 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 9 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 10 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 15 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + + t.end(); +}); + +tape( 'the function returns `false` if provided a number object whose value is a prime number', function test( t ) { + var N; + var p; + var i; + var j; + + t.equal( isComposite( new Number( 2 ) ), false, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 3 ) ), false, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 5 ) ), false, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 7 ) ), false, 'returns expected value' ); // eslint-disable-line no-new-wrappers + + N = PRIMES.length - 1; + for ( i = 0; i < 1e3; i++ ) { + j = discreteUniform( 0, N ); + p = PRIMES[ j ]; + t.equal( isComposite( new Number( p ) ), false, 'returns expected value for Number('+p+')' ); // eslint-disable-line no-new-wrappers + } + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive composite number', function test( t ) { + t.equal( isComposite( 6.0 ), false, 'returns false' ); + t.equal( isComposite( 10.0 ), false, 'returns false' ); + t.equal( isComposite( 49.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a composite number', function test( t ) { + var values; + var i; + + values = [ + '5', + new Number( 2.0 ), // eslint-disable-line no-new-wrappers + new Number( 3.0 ), // eslint-disable-line no-new-wrappers + new Number( 5.0 ), // eslint-disable-line no-new-wrappers + null, + true, + NaN, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), // eslint-disable-line prefer-regex-literals + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isComposite( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-composite/test/test.primitive.js b/is-composite/test/test.primitive.js new file mode 100644 index 00000000..30dbd5c9 --- /dev/null +++ b/is-composite/test/test.primitive.js @@ -0,0 +1,96 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var PRIMES = require( './fixtures/primes.js' ); +var isComposite = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isComposite, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive composite number', function test( t ) { + t.equal( isComposite( 4 ), true, 'returns expected value' ); + t.equal( isComposite( 6 ), true, 'returns expected value' ); + t.equal( isComposite( 8 ), true, 'returns expected value' ); + t.equal( isComposite( 9 ), true, 'returns expected value' ); + t.equal( isComposite( 10 ), true, 'returns expected value' ); + t.equal( isComposite( 15 ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive prime number', function test( t ) { + var N; + var p; + var i; + var j; + + t.equal( isComposite( 2 ), false, 'returns expected value' ); + t.equal( isComposite( 3 ), false, 'returns expected value' ); + t.equal( isComposite( 5 ), false, 'returns expected value' ); + t.equal( isComposite( 7 ), false, 'returns expected value' ); + + N = PRIMES.length - 1; + for ( i = 0; i < 1e3; i++ ) { + j = discreteUniform( 0, N ); + p = PRIMES[ j ]; + t.equal( isComposite( p ), false, 'returns expected value for '+p ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided a number object whose value is a composite number', function test( t ) { + t.equal( isComposite( new Number( 6.0 ) ), false, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 8.0 ) ), false, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isComposite( new Number( 10.0 ) ), false, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a composite number', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + -5.0, + null, + true, + NaN, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isComposite( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-configurable-property-in/README.md b/is-configurable-property-in/README.md new file mode 100644 index 00000000..8caf7240 --- /dev/null +++ b/is-configurable-property-in/README.md @@ -0,0 +1,153 @@ + + +# isConfigurablePropertyIn + +> Test if an object's own or inherited property is configurable. + +
+ +## Usage + +```javascript +var isConfigurablePropertyIn = require( '@stdlib/assert/is-configurable-property-in' ); +``` + +#### isConfigurablePropertyIn( value, property ) + +Returns a `boolean` indicating if a `value` has a configurable `property` (i.e., a property which may be deleted or whose descriptor may be changed). + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var bool; +var obj; + +function Foo() { + this.foo = 'bar'; + return this; +} + +defineProperty( Foo.prototype, 'beep', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': true +}); + +defineProperty( Foo.prototype, 'boop', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': true +}); + +obj = new Foo(); + +bool = isConfigurablePropertyIn( obj, 'foo' ); +// returns true + +bool = isConfigurablePropertyIn( obj, 'beep' ); +// returns true + +bool = isConfigurablePropertyIn( obj, 'boop' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isConfigurablePropertyIn( 'beep', 'toString' ); + // returns true + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var obj = { + 'null': 'foo' + }; + + var bool = isConfigurablePropertyIn( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isConfigurablePropertyIn = require( '@stdlib/assert/is-configurable-property-in' ); + +var bool = isConfigurablePropertyIn( { 'a': 'b' }, 'a' ); +// returns true + +bool = isConfigurablePropertyIn( [ 'a' ], 0 ); +// returns true + +bool = isConfigurablePropertyIn( { 'null': false }, null ); +// returns true + +bool = isConfigurablePropertyIn( { '[object Object]': false }, {} ); +// returns true + +bool = isConfigurablePropertyIn( {}, 'toString' ); +// returns true + +bool = isConfigurablePropertyIn( {}, 'hasOwnProperty' ); +// returns true + +bool = isConfigurablePropertyIn( [ 'a' ], 'length' ); +// returns false + +bool = isConfigurablePropertyIn( null, 'a' ); +// returns false + +bool = isConfigurablePropertyIn( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-configurable-property-in/benchmark/benchmark.js b/is-configurable-property-in/benchmark/benchmark.js new file mode 100644 index 00000000..72a2fe9b --- /dev/null +++ b/is-configurable-property-in/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isConfigurablePropertyIn = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isConfigurablePropertyIn( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-configurable-property-in/docs/repl.txt b/is-configurable-property-in/docs/repl.txt new file mode 100644 index 00000000..cec14dba --- /dev/null +++ b/is-configurable-property-in/docs/repl.txt @@ -0,0 +1,38 @@ + +{{alias}}( value, property ) + Tests if an object's own or inherited property is configurable. + + A property is configurable if the property may be deleted or its descriptor + may be changed. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own or inherited property is + configurable. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = true; + > desc.writable = true; + > desc.value = true; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + true + > bool = {{alias}}( obj, 'beep' ) + false + + See Also + -------- + diff --git a/is-configurable-property-in/docs/types/index.d.ts b/is-configurable-property-in/docs/types/index.d.ts new file mode 100644 index 00000000..d0817c8a --- /dev/null +++ b/is-configurable-property-in/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own or inherited property is configurable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is configurable +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isConfigurablePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isConfigurablePropertyIn( obj, 'beep' ); +* // returns false +*/ +declare function isConfigurablePropertyIn( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isConfigurablePropertyIn; diff --git a/is-configurable-property-in/docs/types/test.ts b/is-configurable-property-in/docs/types/test.ts new file mode 100644 index 00000000..4d3f4a27 --- /dev/null +++ b/is-configurable-property-in/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isConfigurablePropertyIn = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': 'beep' + } ); + isConfigurablePropertyIn( obj, 'beep' ); // $ExpectType boolean + isConfigurablePropertyIn( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isConfigurablePropertyIn(); // $ExpectError + isConfigurablePropertyIn( {} ); // $ExpectError + isConfigurablePropertyIn( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-configurable-property-in/examples/index.js b/is-configurable-property-in/examples/index.js new file mode 100644 index 00000000..d3123b9a --- /dev/null +++ b/is-configurable-property-in/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isConfigurablePropertyIn = require( './../lib' ); + +var bool = isConfigurablePropertyIn( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = isConfigurablePropertyIn( [ 'a' ], 0 ); +console.log( bool ); +// => true + +bool = isConfigurablePropertyIn( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = isConfigurablePropertyIn( { '[object Object]': false }, {} ); +console.log( bool ); +// => true + +bool = isConfigurablePropertyIn( {}, 'toString' ); +console.log( bool ); +// => true + +bool = isConfigurablePropertyIn( {}, 'hasOwnProperty' ); +console.log( bool ); +// => true + +bool = isConfigurablePropertyIn( [ 'a' ], 'length' ); +console.log( bool ); +// => false + +bool = isConfigurablePropertyIn( null, 'a' ); +console.log( bool ); +// => false + +bool = isConfigurablePropertyIn( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-configurable-property-in/lib/index.js b/is-configurable-property-in/lib/index.js new file mode 100644 index 00000000..2c326d83 --- /dev/null +++ b/is-configurable-property-in/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own or inherited property is configurable. +* +* @module @stdlib/assert/is-configurable-property-in +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isConfigurablePropertyIn = require( '@stdlib/assert/is-configurable-property-in' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isConfigurablePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isConfigurablePropertyIn( obj, 'beep' ); +* // returns false +*/ + +// MODULES // + +var isConfigurablePropertyIn = require( './main.js' ); + + +// EXPORTS // + +module.exports = isConfigurablePropertyIn; diff --git a/is-configurable-property-in/lib/main.js b/is-configurable-property-in/lib/main.js new file mode 100644 index 00000000..2b4e7c9e --- /dev/null +++ b/is-configurable-property-in/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptorIn = require( '@stdlib/utils/property-descriptor-in' ); + + +// MAIN // + +/** +* Tests if an object's own or inherited property is configurable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is configurable +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isConfigurablePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isConfigurablePropertyIn( obj, 'beep' ); +* // returns false +*/ +function isConfigurablePropertyIn( value, property ) { + var desc = propertyDescriptorIn( value, property ); + return ( desc !== null && desc.configurable === true ); +} + + +// EXPORTS // + +module.exports = isConfigurablePropertyIn; diff --git a/is-configurable-property-in/package.json b/is-configurable-property-in/package.json new file mode 100644 index 00000000..a27fd8b3 --- /dev/null +++ b/is-configurable-property-in/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-configurable-property-in", + "version": "0.0.0", + "description": "Test if an object's own or inherited property is configurable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "inherited", + "in", + "prototype", + "proto", + "configurable", + "descriptor", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-configurable-property-in/test/test.js b/is-configurable-property-in/test/test.js new file mode 100644 index 00000000..75c76c38 --- /dev/null +++ b/is-configurable-property-in/test/test.js @@ -0,0 +1,161 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isConfigurablePropertyIn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isConfigurablePropertyIn, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property is configurable', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isConfigurablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isConfigurablePropertyIn( [ 1, 2, 3 ], '1' ); + t.equal( bool, true, 'returns true' ); + + bool = isConfigurablePropertyIn( [ 1, 2, 3 ], 1 ); + t.equal( bool, true, 'returns true' ); + + bool = isConfigurablePropertyIn( new Foo(), 'bar' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': 'b' + }); + + bool = isConfigurablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if provided a configurable inherited property', function test( t ) { + var bool; + + bool = isConfigurablePropertyIn( {}, 'hasOwnProperty' ); + t.equal( bool, true, 'returns true' ); + + bool = isConfigurablePropertyIn( {}, 'toString' ); + t.equal( bool, true, 'returns true' ); + + bool = isConfigurablePropertyIn( {}, 'constructor' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isConfigurablePropertyIn( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isConfigurablePropertyIn( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a configurable property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + defineProperty( obj, 'd', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': true + }); + + bool = isConfigurablePropertyIn( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isConfigurablePropertyIn( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + + bool = isConfigurablePropertyIn( [ 'a' ], 'length' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a non-configurable inherited property', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + defineProperty( Foo.prototype, 'bar', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': true + }); + + obj = new Foo(); + + bool = isConfigurablePropertyIn( obj, 'bar' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isConfigurablePropertyIn( 'beep', 'length' ); + t.equal( bool, false, 'returns false' ); + + bool = isConfigurablePropertyIn( 'beep', 'toString' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/is-configurable-property/README.md b/is-configurable-property/README.md new file mode 100644 index 00000000..6bb42a16 --- /dev/null +++ b/is-configurable-property/README.md @@ -0,0 +1,147 @@ + + +# isConfigurableProperty + +> Test if an object's own property is configurable. + +
+ +## Usage + +```javascript +var isConfigurableProperty = require( '@stdlib/assert/is-configurable-property' ); +``` + +#### isConfigurableProperty( value, property ) + +Returns a `boolean` indicating if a `value` has a configurable `property` (i.e., a property which may be deleted or whose descriptor may be changed). + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var obj = { + 'foo': 'bar' +}; + +defineProperty( obj, 'beep', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': true +}); + +defineProperty( obj, 'boop', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': true +}); + +var bool = isConfigurableProperty( obj, 'foo' ); +// returns true + +bool = isConfigurableProperty( obj, 'beep' ); +// returns true + +bool = isConfigurableProperty( obj, 'boop' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isConfigurableProperty( 'beep', 'length' ); + // returns false + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var obj = { + 'null': 'foo' + }; + + var bool = isConfigurableProperty( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isConfigurableProperty = require( '@stdlib/assert/is-configurable-property' ); + +var bool = isConfigurableProperty( { 'a': 'b' }, 'a' ); +// returns true + +bool = isConfigurableProperty( [ 'a' ], 0 ); +// returns true + +bool = isConfigurableProperty( { 'null': false }, null ); +// returns true + +bool = isConfigurableProperty( { '[object Object]': false }, {} ); +// returns true + +bool = isConfigurableProperty( [ 'a' ], 'length' ); +// returns false + +bool = isConfigurableProperty( {}, 'toString' ); +// returns false + +bool = isConfigurableProperty( {}, 'hasOwnProperty' ); +// returns false + +bool = isConfigurableProperty( null, 'a' ); +// returns false + +bool = isConfigurableProperty( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-configurable-property/benchmark/benchmark.js b/is-configurable-property/benchmark/benchmark.js new file mode 100644 index 00000000..d63c25b8 --- /dev/null +++ b/is-configurable-property/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isConfigurableProperty = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isConfigurableProperty( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-configurable-property/docs/repl.txt b/is-configurable-property/docs/repl.txt new file mode 100644 index 00000000..9a4e0af7 --- /dev/null +++ b/is-configurable-property/docs/repl.txt @@ -0,0 +1,37 @@ + +{{alias}}( value, property ) + Tests if an object's own property is configurable. + + A property is configurable if the property may be deleted or its descriptor + may be changed. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own property is configurable. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = true; + > desc.writable = true; + > desc.value = true; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + true + > bool = {{alias}}( obj, 'beep' ) + false + + See Also + -------- + diff --git a/is-configurable-property/docs/types/index.d.ts b/is-configurable-property/docs/types/index.d.ts new file mode 100644 index 00000000..826f4758 --- /dev/null +++ b/is-configurable-property/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own property is configurable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is configurable +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isConfigurableProperty( obj, 'boop' ); +* // returns true +* +* bool = isConfigurableProperty( obj, 'beep' ); +* // returns false +*/ +declare function isConfigurableProperty( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isConfigurableProperty; diff --git a/is-configurable-property/docs/types/test.ts b/is-configurable-property/docs/types/test.ts new file mode 100644 index 00000000..7422a90d --- /dev/null +++ b/is-configurable-property/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isConfigurableProperty = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': 'beep' + } ); + isConfigurableProperty( obj, 'beep' ); // $ExpectType boolean + isConfigurableProperty( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isConfigurableProperty(); // $ExpectError + isConfigurableProperty( {} ); // $ExpectError + isConfigurableProperty( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-configurable-property/examples/index.js b/is-configurable-property/examples/index.js new file mode 100644 index 00000000..ed3cde59 --- /dev/null +++ b/is-configurable-property/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isConfigurableProperty = require( './../lib' ); + +var bool = isConfigurableProperty( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = isConfigurableProperty( [ 'a' ], 0 ); +console.log( bool ); +// => true + +bool = isConfigurableProperty( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = isConfigurableProperty( { '[object Object]': false }, {} ); +console.log( bool ); +// => true + +bool = isConfigurableProperty( [ 'a' ], 'length' ); +console.log( bool ); +// => false + +bool = isConfigurableProperty( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isConfigurableProperty( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isConfigurableProperty( null, 'a' ); +console.log( bool ); +// => false + +bool = isConfigurableProperty( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-configurable-property/lib/index.js b/is-configurable-property/lib/index.js new file mode 100644 index 00000000..63a3412b --- /dev/null +++ b/is-configurable-property/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own property is configurable. +* +* @module @stdlib/assert/is-configurable-property +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isConfigurableProperty = require( '@stdlib/assert/is-configurable-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isConfigurableProperty( obj, 'boop' ); +* // returns true +* +* bool = isConfigurableProperty( obj, 'beep' ); +* // returns false +*/ + +// MODULES // + +var isConfigurableProperty = require( './main.js' ); + + +// EXPORTS // + +module.exports = isConfigurableProperty; diff --git a/is-configurable-property/lib/main.js b/is-configurable-property/lib/main.js new file mode 100644 index 00000000..1ebf8e6c --- /dev/null +++ b/is-configurable-property/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptor = require( '@stdlib/utils/property-descriptor' ); + + +// MAIN // + +/** +* Tests if an object's own property is configurable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is configurable +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isConfigurableProperty( obj, 'boop' ); +* // returns true +* +* bool = isConfigurableProperty( obj, 'beep' ); +* // returns false +*/ +function isConfigurableProperty( value, property ) { + var desc = propertyDescriptor( value, property ); + return ( desc !== null && desc.configurable === true ); +} + + +// EXPORTS // + +module.exports = isConfigurableProperty; diff --git a/is-configurable-property/package.json b/is-configurable-property/package.json new file mode 100644 index 00000000..4f5c904f --- /dev/null +++ b/is-configurable-property/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-configurable-property", + "version": "0.0.0", + "description": "Test if an object's own property is configurable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "configurable", + "descriptor", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-configurable-property/test/test.js b/is-configurable-property/test/test.js new file mode 100644 index 00000000..d287fb43 --- /dev/null +++ b/is-configurable-property/test/test.js @@ -0,0 +1,135 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isConfigurableProperty = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isConfigurableProperty, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property is configurable', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isConfigurableProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isConfigurableProperty( [ 1, 2, 3 ], '1' ); + t.equal( bool, true, 'returns true' ); + + bool = isConfigurableProperty( [ 1, 2, 3 ], 1 ); + t.equal( bool, true, 'returns true' ); + + bool = isConfigurableProperty( new Foo(), 'bar' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': 'b' + }); + + bool = isConfigurableProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isConfigurableProperty( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isConfigurableProperty( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a configurable property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + defineProperty( obj, 'd', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': true + }); + + bool = isConfigurableProperty( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isConfigurableProperty( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + + bool = isConfigurableProperty( [ 'a' ], 'length' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an inherited property', function test( t ) { + var bool; + + bool = isConfigurableProperty( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isConfigurableProperty( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isConfigurableProperty( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isConfigurableProperty( 'beep', 'length' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); diff --git a/is-cube-number/README.md b/is-cube-number/README.md new file mode 100644 index 00000000..820e20f8 --- /dev/null +++ b/is-cube-number/README.md @@ -0,0 +1,153 @@ + + +# isCubeNumber + +> Test if a value is a cube number. + +
+ +A **cube number** is defined as an integer value which is the cube of an integer. + +
+ + + +
+ +## Usage + +```javascript +var isCubeNumber = require( '@stdlib/assert/is-cube-number' ); +``` + +#### isCubeNumber( value ) + +Tests if a `value` is a cube number. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isCubeNumber( 8.0 ); +// returns true + +bool = isCubeNumber( new Number( 8.0 ) ); +// returns true + +bool = isCubeNumber( 3.14 ); +// returns false + +bool = isCubeNumber( -5.0 ); +// returns false + +bool = isCubeNumber( NaN ); +// returns false + +bool = isCubeNumber( null ); +// returns false +``` + +#### isCubeNumber.isPrimitive( value ) + +Tests if a `value` is a primitive cube number. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isCubeNumber.isPrimitive( 8.0 ); +// returns true + +bool = isCubeNumber.isPrimitive( new Number( 8.0 ) ); +// returns false +``` + +#### isCubeNumber.isObject( value ) + +Tests if a `value` is a `Number` object having a value which is a cube number. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isCubeNumber.isObject( 8.0 ); +// returns false + +bool = isCubeNumber.isObject( new Number( 8.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isCubeNumber = require( '@stdlib/assert/is-cube-number' ); + +var bool = isCubeNumber( 8.0 ); +// returns true + +bool = isCubeNumber( new Number( 8.0 ) ); +// returns true + +bool = isCubeNumber( 0.0 ); +// returns true + +bool = isCubeNumber( 1.0 ); +// returns true + +bool = isCubeNumber( 3.14 ); +// returns false + +bool = isCubeNumber( -5.0 ); +// returns false + +bool = isCubeNumber( NaN ); +// returns false + +bool = isCubeNumber( '0.5' ); +// returns false + +bool = isCubeNumber( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-cube-number/benchmark/benchmark.js b/is-cube-number/benchmark/benchmark.js new file mode 100644 index 00000000..d549dea0 --- /dev/null +++ b/is-cube-number/benchmark/benchmark.js @@ -0,0 +1,233 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isCubeNumber = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 0.5, + 1.0, + 0.0, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isCubeNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 8.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isCubeNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isCubeNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 8.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isCubeNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isCubeNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 8.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isCubeNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-cube-number/docs/repl.txt b/is-cube-number/docs/repl.txt new file mode 100644 index 00000000..4342bedf --- /dev/null +++ b/is-cube-number/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is a cube number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a cube number. + + Examples + -------- + > var bool = {{alias}}( 8.0 ) + true + > bool = {{alias}}( new Number( 8.0 ) ) + true + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( -5.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive which is a cube number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive which is a cube + number. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 8.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( 8.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a value which is a cube number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a value which + is a cube number. + + Examples + -------- + > var bool = {{alias}}.isObject( 8.0 ) + false + > bool = {{alias}}.isObject( new Number( 8.0 ) ) + true + + + See Also + -------- + diff --git a/is-cube-number/docs/types/index.d.ts b/is-cube-number/docs/types/index.d.ts new file mode 100644 index 00000000..a1f32208 --- /dev/null +++ b/is-cube-number/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface describing methods for testing for primitives and objects. +*/ +interface IsCubeNumber { + /** + * Tests if a value is a cube number. + * + * @param value - value to test + * @returns boolean indicating whether value is a cube number + * + * @example + * var bool = isCubeNumber( 8.0 ); + * // returns true + * + * @example + * var bool = isCubeNumber( new Number( 8.0 ) ); + * // returns true + * + * @example + * var bool = isCubeNumber( 3.14 ); + * // returns false + * + * @example + * var bool = isCubeNumber( -5.0 ); + * // returns false + * + * @example + * var bool = isCubeNumber( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a value which is a cube number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a value which is a cube number + * + * @example + * var bool = isCubeNumber.isPrimitive( 8.0 ); + * // returns true + * + * @example + * var bool = isCubeNumber.isPrimitive( new Number( 8.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a value which is a cube number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a value which is a cube number + * + * @example + * var bool = isCubeNumber.isObject( 8.0 ); + * // returns false + * + * @example + * var bool = isCubeNumber.isObject( new Number( 8.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a cube number. +* +* @param value - value to test +* @returns boolean indicating whether value is a cube number +* +* @example +* var bool = isCubeNumber( 8.0 ); +* // returns true +* +* @example +* var bool = isCubeNumber( new Number( 8.0 ) ); +* // returns true +* +* @example +* var bool = isCubeNumber( 3.14 ); +* // returns false +* +* @example +* var bool = isCubeNumber( -5.0 ); +* // returns false +* +* @example +* var bool = isCubeNumber( null ); +* // returns false +* +* @example +* var bool = isCubeNumber.isPrimitive( 8.0 ); +* // returns true +* +* @example +* var bool = isCubeNumber.isObject( new Number( 8.0 ) ); +* // returns true +*/ +declare var isCubeNumber: IsCubeNumber; + + +// EXPORTS // + +export = isCubeNumber; diff --git a/is-cube-number/docs/types/test.ts b/is-cube-number/docs/types/test.ts new file mode 100644 index 00000000..7eca496a --- /dev/null +++ b/is-cube-number/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isCubeNumber = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isCubeNumber( 3 ); // $ExpectType boolean + isCubeNumber( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isCubeNumber(); // $ExpectError + isCubeNumber( 0.2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isCubeNumber.isPrimitive( new Number( 0.2 ) ); // $ExpectType boolean + isCubeNumber.isPrimitive( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isCubeNumber.isPrimitive(); // $ExpectError + isCubeNumber.isPrimitive( 0.2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isCubeNumber.isObject( new Number( 0.2 ) ); // $ExpectType boolean + isCubeNumber.isObject( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isCubeNumber.isObject(); // $ExpectError + isCubeNumber.isObject( 0.2, 123 ); // $ExpectError +} diff --git a/is-cube-number/examples/index.js b/is-cube-number/examples/index.js new file mode 100644 index 00000000..5d090368 --- /dev/null +++ b/is-cube-number/examples/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isCubeNumber = require( './../lib' ); + +console.log( isCubeNumber( 8.0 ) ); +// => true + +console.log( isCubeNumber( new Number( 8.0 ) ) ); +// => true + +console.log( isCubeNumber( 0.0 ) ); +// => true + +console.log( isCubeNumber( 1.0 ) ); +// => true + +console.log( isCubeNumber( 3.14 ) ); +// => false + +console.log( isCubeNumber( -5.0 ) ); +// => false + +console.log( isCubeNumber( NaN ) ); +// => false + +console.log( isCubeNumber( '0.5' ) ); +// => false + +console.log( isCubeNumber( null ) ); +// => false diff --git a/is-cube-number/lib/index.js b/is-cube-number/lib/index.js new file mode 100644 index 00000000..385ce49f --- /dev/null +++ b/is-cube-number/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a cube number. +* +* @module @stdlib/assert/is-cube-number +* +* @example +* var isCubeNumber = require( '@stdlib/assert/is-cube-number' ); +* +* var bool = isCubeNumber( 8.0 ); +* // returns true +* +* bool = isCubeNumber( new Number( 8.0 ) ); +* // returns true +* +* bool = isCubeNumber( 3.14 ); +* // returns false +* +* bool = isCubeNumber( -5.0 ); +* // returns false +* +* bool = isCubeNumber( null ); +* // returns false +* +* @example +* var isCubeNumber = require( '@stdlib/assert/is-cube-number' ).isPrimitive; +* +* var bool = isCubeNumber( 8.0 ); +* // returns true +* +* bool = isCubeNumber( new Number( 8.0 ) ); +* // returns false +* +* @example +* var isCubeNumber = require( '@stdlib/assert/is-cube-number' ).isObject; +* +* var bool = isCubeNumber( 8.0 ); +* // returns false +* +* bool = isCubeNumber( new Number( 8.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isCubeNumber = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isCubeNumber, 'isPrimitive', isPrimitive ); +setReadOnly( isCubeNumber, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isCubeNumber; diff --git a/is-cube-number/lib/main.js b/is-cube-number/lib/main.js new file mode 100644 index 00000000..85d18712 --- /dev/null +++ b/is-cube-number/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a cube number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a cube number +* +* @example +* var bool = isCubeNumber( 8.0 ); +* // returns true +* +* @example +* var bool = isCubeNumber( new Number( 8.0 ) ); +* // returns true +* +* @example +* var bool = isCubeNumber( 3.14 ); +* // returns false +* +* @example +* var bool = isCubeNumber( -5.0 ); +* // returns false +* +* @example +* var bool = isCubeNumber( null ); +* // returns false +*/ +function isCubeNumber( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isCubeNumber; diff --git a/is-cube-number/lib/object.js b/is-cube-number/lib/object.js new file mode 100644 index 00000000..3ba836c1 --- /dev/null +++ b/is-cube-number/lib/object.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isObject; +var cbrt = require( '@stdlib/math/base/special/cbrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); + + +// MAIN // + +/** +* Tests if a value is a number object having a value which is a cube number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a value which is a cube number +* +* @example +* var bool = isCubeNumber( 8.0 ); +* // returns false +* +* @example +* var bool = isCubeNumber( new Number( 8.0 ) ); +* // returns true +*/ +function isCubeNumber( value ) { + var n; + var x; + if ( !isInteger( value ) ) { + return false; + } + x = value.valueOf(); + n = floor( cbrt( x ) + 0.5 ); + return n*n*n === x; +} + + +// EXPORTS // + +module.exports = isCubeNumber; diff --git a/is-cube-number/lib/primitive.js b/is-cube-number/lib/primitive.js new file mode 100644 index 00000000..ba9a9323 --- /dev/null +++ b/is-cube-number/lib/primitive.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var cbrt = require( '@stdlib/math/base/special/cbrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); + + +// MAIN // + +/** +* Tests if a value is a number primitive having a value which is a cube number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a value which is a cube number +* +* @example +* var bool = isCubeNumber( 8.0 ); +* // returns true +* +* @example +* var bool = isCubeNumber( new Number( 8.0 ) ); +* // returns false +*/ +function isCubeNumber( value ) { + var n; + if ( !isInteger( value ) ) { + return false; + } + n = floor( cbrt( value ) + 0.5 ); + return n*n*n === value; +} + + +// EXPORTS // + +module.exports = isCubeNumber; diff --git a/is-cube-number/package.json b/is-cube-number/package.json new file mode 100644 index 00000000..e78deae2 --- /dev/null +++ b/is-cube-number/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-cube-number", + "version": "0.0.0", + "description": "Test if a value is a cube number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "perfect", + "cube", + "is", + "iscube", + "type", + "check", + "valid", + "validate", + "test", + "primitive", + "object" + ] +} diff --git a/is-cube-number/test/test.js b/is-cube-number/test/test.js new file mode 100644 index 00000000..6bdf74a8 --- /dev/null +++ b/is-cube-number/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isCubeNumber = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isCubeNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test primitives', function test( t ) { + t.equal( typeof isCubeNumber.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test objects', function test( t ) { + t.equal( typeof isCubeNumber.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-cube-number/test/test.main.js b/is-cube-number/test/test.main.js new file mode 100644 index 00000000..6e8e5617 --- /dev/null +++ b/is-cube-number/test/test.main.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isCubeNumber = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isCubeNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a cube number', function test( t ) { + t.equal( isCubeNumber( 8.0 ), true, 'returns true' ); + t.equal( isCubeNumber( new Number( 8.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a cube number', function test( t ) { + var values; + var i; + + values = [ + '5', + 5.0, + -3.14, + -2.0, + NaN, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isCubeNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-cube-number/test/test.object.js b/is-cube-number/test/test.object.js new file mode 100644 index 00000000..86a9c01c --- /dev/null +++ b/is-cube-number/test/test.object.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isCubeNumber = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isCubeNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a cube number', function test( t ) { + t.equal( isCubeNumber( new Number( 8.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive cube number', function test( t ) { + t.equal( isCubeNumber( 8.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a cube number', function test( t ) { + var values; + var i; + + values = [ + '5', + new Number( 5.0 ), // eslint-disable-line no-new-wrappers + null, + true, + NaN, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), // eslint-disable-line prefer-regex-literals + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isCubeNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-cube-number/test/test.primitive.js b/is-cube-number/test/test.primitive.js new file mode 100644 index 00000000..4786660f --- /dev/null +++ b/is-cube-number/test/test.primitive.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isCubeNumber = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isCubeNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive cube number', function test( t ) { + t.equal( isCubeNumber( 8.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object', function test( t ) { + t.equal( isCubeNumber( new Number( 8.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a cube number', function test( t ) { + var values; + var i; + + values = [ + '5', + 5.0, + null, + true, + NaN, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isCubeNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-darwin/README.md b/is-darwin/README.md new file mode 100644 index 00000000..dcbb6e70 --- /dev/null +++ b/is-darwin/README.md @@ -0,0 +1,73 @@ + + +# IS_DARWIN + +> Boolean indicating if the current process is running on [Darwin][darwin]. + +
+ +## Usage + +```javascript +var IS_DARWIN = require( '@stdlib/assert/is-darwin' ); +``` + +#### IS_DARWIN + +`Boolean` indicating if the current process is running on [Darwin][darwin]. + +```javascript +console.log( IS_DARWIN ); +// => +``` + +
+ + + +
+ +## Examples + + + +```javascript +var PLATFORM = require( '@stdlib/os/platform' ); +var IS_DARWIN = require( '@stdlib/assert/is-darwin' ); + +if ( IS_DARWIN ) { + console.log( 'Running on Darwin...' ); +} else { + console.log( 'Running on %s...', PLATFORM ); +} +``` + +
+ + + + + + diff --git a/is-darwin/docs/repl.txt b/is-darwin/docs/repl.txt new file mode 100644 index 00000000..3d7fc4ef --- /dev/null +++ b/is-darwin/docs/repl.txt @@ -0,0 +1,12 @@ + +{{alias}} + Boolean indicating if the current process is running on Darwin. + + Examples + -------- + > {{alias}} + + + See Also + -------- + diff --git a/is-darwin/docs/types/index.d.ts b/is-darwin/docs/types/index.d.ts new file mode 100644 index 00000000..a127f295 --- /dev/null +++ b/is-darwin/docs/types/index.d.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Boolean indicating if the current process is running on Darwin. +* +* @example +* var PLATFORM = require( `@stdlib/os/platform` ); +* +* if ( IS_DARWIN ) { +* console.log( 'Running on Darwin...' ); +* } else { +* console.log( 'Running on %s...', PLATFORM ); +* } +*/ +declare const IS_DARWIN: boolean; + + +// EXPORTS // + +export = IS_DARWIN; diff --git a/is-darwin/docs/types/test.ts b/is-darwin/docs/types/test.ts new file mode 100644 index 00000000..efe4281d --- /dev/null +++ b/is-darwin/docs/types/test.ts @@ -0,0 +1,28 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import IS_DARWIN = require( './index' ); + + +// TESTS // + +// The variable is a boolean... +{ + // tslint:disable-next-line:no-unused-expression + IS_DARWIN; // $ExpectType boolean +} diff --git a/is-darwin/examples/index.js b/is-darwin/examples/index.js new file mode 100644 index 00000000..8caea05f --- /dev/null +++ b/is-darwin/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var PLATFORM = require( '@stdlib/os/platform' ); +var IS_DARWIN = require( './../lib' ); + +if ( IS_DARWIN ) { + console.log( 'Running on Darwin...' ); +} else { + console.log( 'Running on %s...', PLATFORM ); +} diff --git a/is-darwin/lib/index.js b/is-darwin/lib/index.js new file mode 100644 index 00000000..188fdb43 --- /dev/null +++ b/is-darwin/lib/index.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Boolean indicating if the current process is running on Darwin. +* +* @module @stdlib/assert/is-darwin +* @type {boolean} +* +* @example +* var PLATFORM = require( '@stdlib/os/platform' ); +* var IS_DARWIN = require( '@stdlib/assert/is-darwin' ); +* +* if ( IS_DARWIN ) { +* console.log( 'Running on Darwin...' ); +* } else { +* console.log( 'Running on %s...', PLATFORM ); +* } +*/ + +// MODULES // + +var PLATFORM = require( '@stdlib/os/platform' ); + + +// MAIN // + +/** +* Boolean indicating if the current process is running on Darwin. +* +* @constant +* @type {boolean} +*/ +var IS_DARWIN = ( PLATFORM === 'darwin' ); + + +// EXPORTS // + +module.exports = IS_DARWIN; diff --git a/is-darwin/package.json b/is-darwin/package.json new file mode 100644 index 00000000..43a538f9 --- /dev/null +++ b/is-darwin/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-darwin", + "version": "0.0.0", + "description": "Boolean indicating if the current process is running on Darwin.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utils", + "util", + "utilities", + "utility", + "process", + "platform", + "os", + "check", + "windows", + "posix", + "linux", + "darwin", + "is", + "isdarwin", + "win32" + ] +} diff --git a/is-darwin/test/test.js b/is-darwin/test/test.js new file mode 100644 index 00000000..abc3c6c4 --- /dev/null +++ b/is-darwin/test/test.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var IS_DARWIN = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_DARWIN, 'boolean', 'main export is a boolean' ); + t.end(); +}); + +tape( 'the main export is `true` if the current process is running on Darwin', function test( t ) { + var IS_DARWIN; + + IS_DARWIN = proxyquire( './../lib', { + '@stdlib/os/platform': 'darwin' + }); + t.equal( IS_DARWIN, true, 'is true' ); + t.end(); +}); + +tape( 'the main export is `false` if the current process is not running on Darwin', function test( t ) { + var IS_DARWIN; + + IS_DARWIN = proxyquire( './../lib', { + '@stdlib/os/platform': 'linux' + }); + t.equal( IS_DARWIN, false, 'is false' ); + t.end(); +}); diff --git a/is-data-property-in/README.md b/is-data-property-in/README.md new file mode 100644 index 00000000..d1a6ff61 --- /dev/null +++ b/is-data-property-in/README.md @@ -0,0 +1,157 @@ + + +# isDataPropertyIn + +> Test if an object's own or inherited property has a data descriptor. + +
+ +## Usage + +```javascript +var isDataPropertyIn = require( '@stdlib/assert/is-data-property-in' ); +``` + +#### isDataPropertyIn( value, property ) + +Returns a `boolean` indicating if a `value` has a data `property` (either own or inherited). + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var bool; +var obj; + +function Foo() { + this.foo = 'bar'; + return this; +} + +defineProperty( Foo.prototype, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'boop' +}); + +defineProperty( Foo.prototype, 'accessor', { + 'configurable': false, + 'enumerable': false, + 'get': function getter() { + return obj.foo; + }, + 'set': function setter( v ) { + obj.foo = v; + } +}); + +obj = new Foo(); + +bool = isDataPropertyIn( obj, 'foo' ); +// returns true + +bool = isDataPropertyIn( obj, 'beep' ); +// returns true + +bool = isDataPropertyIn( obj, 'accessor' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isDataPropertyIn( 'beep', 'length' ); + // returns true + ``` + +- Non-symbol property arguments are coerced to `strings`. + + ```javascript + var obj = { + 'null': 'foo' + }; + + var bool = isDataPropertyIn( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isDataPropertyIn = require( '@stdlib/assert/is-data-property-in' ); + +var bool = isDataPropertyIn( [ 'a' ], 'length' ); +// returns true + +bool = isDataPropertyIn( { 'a': 'b' }, 'a' ); +// returns true + +bool = isDataPropertyIn( [ 'a' ], 0 ); +// returns true + +bool = isDataPropertyIn( { 'null': false }, null ); +// returns true + +bool = isDataPropertyIn( { '[object Object]': false }, {} ); +// returns true + +bool = isDataPropertyIn( {}, 'toString' ); +// returns true + +bool = isDataPropertyIn( {}, 'hasOwnProperty' ); +// returns true + +bool = isDataPropertyIn( null, 'a' ); +// returns false + +bool = isDataPropertyIn( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-data-property-in/benchmark/benchmark.js b/is-data-property-in/benchmark/benchmark.js new file mode 100644 index 00000000..52ec0c14 --- /dev/null +++ b/is-data-property-in/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isDataPropertyIn = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isDataPropertyIn( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-data-property-in/docs/repl.txt b/is-data-property-in/docs/repl.txt new file mode 100644 index 00000000..b8164859 --- /dev/null +++ b/is-data-property-in/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, property ) + Tests if an object's own or inherited property has a data descriptor. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own or inherited property has a data + descriptor. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = false; + > desc.get = function getter() { return 'beep'; }; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + true + > bool = {{alias}}( obj, 'beep' ) + false + + See Also + -------- + diff --git a/is-data-property-in/docs/types/index.d.ts b/is-data-property-in/docs/types/index.d.ts new file mode 100644 index 00000000..397e89eb --- /dev/null +++ b/is-data-property-in/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own or inherited property has a data descriptor. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property has a data descriptor +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* function getter() { +* return 'beep'; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'get': getter +* }); +* +* var bool = isDataPropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isDataPropertyIn( obj, 'beep' ); +* // returns false +*/ +declare function isDataPropertyIn( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isDataPropertyIn; diff --git a/is-data-property-in/docs/types/test.ts b/is-data-property-in/docs/types/test.ts new file mode 100644 index 00000000..c4123a2d --- /dev/null +++ b/is-data-property-in/docs/types/test.ts @@ -0,0 +1,49 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isDataPropertyIn = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + + const getter = () => { + return 'beep'; + }; + + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'get': getter + } ); + + isDataPropertyIn( obj, 'boop' ); // $ExpectType boolean + isDataPropertyIn( obj, 'beep' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isDataPropertyIn(); // $ExpectError + isDataPropertyIn( {} ); // $ExpectError + isDataPropertyIn( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-data-property-in/examples/index.js b/is-data-property-in/examples/index.js new file mode 100644 index 00000000..21e39e82 --- /dev/null +++ b/is-data-property-in/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isDataPropertyIn = require( './../lib' ); + +var bool = isDataPropertyIn( [ 'a' ], 'length' ); +console.log( bool ); +// => true + +bool = isDataPropertyIn( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = isDataPropertyIn( [ 'a' ], 0 ); +console.log( bool ); +// => true + +bool = isDataPropertyIn( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = isDataPropertyIn( { '[object Object]': false }, {} ); +console.log( bool ); +// => true + +bool = isDataPropertyIn( {}, 'toString' ); +console.log( bool ); +// => true + +bool = isDataPropertyIn( {}, 'hasOwnProperty' ); +console.log( bool ); +// => true + +bool = isDataPropertyIn( null, 'a' ); +console.log( bool ); +// => false + +bool = isDataPropertyIn( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-data-property-in/lib/index.js b/is-data-property-in/lib/index.js new file mode 100644 index 00000000..ab3aa88f --- /dev/null +++ b/is-data-property-in/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own or inherited property has a data descriptor. +* +* @module @stdlib/assert/is-data-property-in +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isDataPropertyIn = require( '@stdlib/assert/is-data-property-in' ); +* +* var obj = { +* 'boop': true +* }; +* +* function getter() { +* return 'beep'; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'get': getter +* }); +* +* var bool = isDataPropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isDataPropertyIn( obj, 'beep' ); +* // returns false +*/ + +// MODULES // + +var isDataPropertyIn = require( './main.js' ); + + +// EXPORTS // + +module.exports = isDataPropertyIn; diff --git a/is-data-property-in/lib/main.js b/is-data-property-in/lib/main.js new file mode 100644 index 00000000..4595166b --- /dev/null +++ b/is-data-property-in/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptorIn = require( '@stdlib/utils/property-descriptor-in' ); + + +// MAIN // + +/** +* Tests if an object's own or inherited property has a data descriptor. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property has a data descriptor +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function getter() { +* return 'beep'; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'get': getter +* }); +* +* var bool = isDataPropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isDataPropertyIn( obj, 'beep' ); +* // returns false +*/ +function isDataPropertyIn( value, property ) { + var desc = propertyDescriptorIn( value, property ); + return ( desc !== null && typeof desc.writable === 'boolean' ); +} + + +// EXPORTS // + +module.exports = isDataPropertyIn; diff --git a/is-data-property-in/package.json b/is-data-property-in/package.json new file mode 100644 index 00000000..76dd94d1 --- /dev/null +++ b/is-data-property-in/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-data-property-in", + "version": "0.0.0", + "description": "Test if an object's own or inherited property has a data descriptor.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "inherited", + "in", + "prototype", + "proto", + "data", + "descriptor", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-data-property-in/test/test.js b/is-data-property-in/test/test.js new file mode 100644 index 00000000..f8643d7d --- /dev/null +++ b/is-data-property-in/test/test.js @@ -0,0 +1,175 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isDataPropertyIn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isDataPropertyIn, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property has a data descriptor', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isDataPropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isDataPropertyIn( [ 1, 2, 3 ], '1' ); + t.equal( bool, true, 'returns true' ); + + bool = isDataPropertyIn( [ 1, 2, 3 ], 1 ); + t.equal( bool, true, 'returns true' ); + + bool = isDataPropertyIn( new Foo(), 'bar' ); + t.equal( bool, true, 'returns true' ); + + bool = isDataPropertyIn( [ 'a' ], 'length' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'b' + }); + + bool = isDataPropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isDataPropertyIn( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isDataPropertyIn( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a property having a data descriptor', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + bool = isDataPropertyIn( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'get': getter, + 'set': setter + }); + + bool = isDataPropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function getter() { + // No-op... + } + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `true` if provided an inherited property having a data descriptor', function test( t ) { + var bool; + + bool = isDataPropertyIn( {}, 'hasOwnProperty' ); + t.equal( bool, true, 'returns true' ); + + bool = isDataPropertyIn( {}, 'toString' ); + t.equal( bool, true, 'returns true' ); + + bool = isDataPropertyIn( {}, 'constructor' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an inherited property not having a data descriptor', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + defineProperty( Foo.prototype, 'bar', { + 'configurable': true, + 'enumerable': true, + 'get': getter, + 'set': setter + }); + + obj = new Foo(); + + bool = isDataPropertyIn( obj, 'bar' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function getter() { + // No-op... + } + + function setter() { + // No-op... + } +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isDataPropertyIn( 'beep', 'length' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/is-data-property/README.md b/is-data-property/README.md new file mode 100644 index 00000000..052193fa --- /dev/null +++ b/is-data-property/README.md @@ -0,0 +1,151 @@ + + +# isDataProperty + +> Test if an object's own property has a data descriptor. + +
+ +## Usage + +```javascript +var isDataProperty = require( '@stdlib/assert/is-data-property' ); +``` + +#### isDataProperty( value, property ) + +Returns a `boolean` indicating if a `value` has a data `property`. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var obj = { + 'foo': 'bar' +}; + +defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'boop' +}); + +defineProperty( obj, 'accessor', { + 'configurable': false, + 'enumerable': false, + 'get': function getter() { + return obj.foo; + }, + 'set': function setter( v ) { + obj.foo = v; + } +}); + +var bool = isDataProperty( obj, 'foo' ); +// returns true + +bool = isDataProperty( obj, 'beep' ); +// returns true + +bool = isDataProperty( obj, 'accessor' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isDataProperty( 'beep', 'length' ); + // returns true + ``` + +- Non-symbol property arguments are coerced to `strings`. + + ```javascript + var obj = { + 'null': 'foo' + }; + + var bool = isDataProperty( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isDataProperty = require( '@stdlib/assert/is-data-property' ); + +var bool = isDataProperty( [ 'a' ], 'length' ); +// returns true + +bool = isDataProperty( { 'a': 'b' }, 'a' ); +// returns true + +bool = isDataProperty( [ 'a' ], 0 ); +// returns true + +bool = isDataProperty( { 'null': false }, null ); +// returns true + +bool = isDataProperty( { '[object Object]': false }, {} ); +// returns true + +bool = isDataProperty( {}, 'toString' ); +// returns false + +bool = isDataProperty( {}, 'hasOwnProperty' ); +// returns false + +bool = isDataProperty( null, 'a' ); +// returns false + +bool = isDataProperty( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-data-property/benchmark/benchmark.js b/is-data-property/benchmark/benchmark.js new file mode 100644 index 00000000..adb4adfd --- /dev/null +++ b/is-data-property/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isDataProperty = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isDataProperty( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-data-property/docs/repl.txt b/is-data-property/docs/repl.txt new file mode 100644 index 00000000..10cd8e39 --- /dev/null +++ b/is-data-property/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( value, property ) + Tests if an object's own property has a data descriptor. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own property has a data descriptor. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = false; + > desc.get = function getter() { return 'beep'; }; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + true + > bool = {{alias}}( obj, 'beep' ) + false + + See Also + -------- + diff --git a/is-data-property/docs/types/index.d.ts b/is-data-property/docs/types/index.d.ts new file mode 100644 index 00000000..6d9bcdab --- /dev/null +++ b/is-data-property/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own property has a data descriptor. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property has a data descriptor +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* function getter() { +* return 'beep'; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'get': getter +* }); +* +* var bool = isDataProperty( obj, 'boop' ); +* // returns true +* +* bool = isDataProperty( obj, 'beep' ); +* // returns false +*/ +declare function isDataProperty( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isDataProperty; diff --git a/is-data-property/docs/types/test.ts b/is-data-property/docs/types/test.ts new file mode 100644 index 00000000..01515ff8 --- /dev/null +++ b/is-data-property/docs/types/test.ts @@ -0,0 +1,49 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isDataProperty = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + + const getter = () => { + return 'beep'; + }; + + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'get': getter + } ); + + isDataProperty( obj, 'boop' ); // $ExpectType boolean + isDataProperty( obj, 'beep' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isDataProperty(); // $ExpectError + isDataProperty( {} ); // $ExpectError + isDataProperty( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-data-property/examples/index.js b/is-data-property/examples/index.js new file mode 100644 index 00000000..a5638e11 --- /dev/null +++ b/is-data-property/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isDataProperty = require( './../lib' ); + +var bool = isDataProperty( [ 'a' ], 'length' ); +console.log( bool ); +// => true + +bool = isDataProperty( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = isDataProperty( [ 'a' ], 0 ); +console.log( bool ); +// => true + +bool = isDataProperty( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = isDataProperty( { '[object Object]': false }, {} ); +console.log( bool ); +// => true + +bool = isDataProperty( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isDataProperty( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isDataProperty( null, 'a' ); +console.log( bool ); +// => false + +bool = isDataProperty( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-data-property/lib/index.js b/is-data-property/lib/index.js new file mode 100644 index 00000000..46fada10 --- /dev/null +++ b/is-data-property/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own property has a data descriptor. +* +* @module @stdlib/assert/is-data-property +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isDataProperty = require( '@stdlib/assert/is-data-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function getter() { +* return 'beep'; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'get': getter +* }); +* +* var bool = isDataProperty( obj, 'boop' ); +* // returns true +* +* bool = isDataProperty( obj, 'beep' ); +* // returns false +*/ + +// MODULES // + +var isDataProperty = require( './main.js' ); + + +// EXPORTS // + +module.exports = isDataProperty; diff --git a/is-data-property/lib/main.js b/is-data-property/lib/main.js new file mode 100644 index 00000000..30fa3d8e --- /dev/null +++ b/is-data-property/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptor = require( '@stdlib/utils/property-descriptor' ); + + +// MAIN // + +/** +* Tests if an object's own property has a data descriptor. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property has a data descriptor +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function getter() { +* return 'beep'; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'get': getter +* }); +* +* var bool = isDataProperty( obj, 'boop' ); +* // returns true +* +* bool = isDataProperty( obj, 'beep' ); +* // returns false +*/ +function isDataProperty( value, property ) { + var desc = propertyDescriptor( value, property ); + return ( desc !== null && typeof desc.writable === 'boolean' ); +} + + +// EXPORTS // + +module.exports = isDataProperty; diff --git a/is-data-property/package.json b/is-data-property/package.json new file mode 100644 index 00000000..51ad861f --- /dev/null +++ b/is-data-property/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-data-property", + "version": "0.0.0", + "description": "Test if an object's own property has a data descriptor.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "data", + "descriptor", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-data-property/test/test.js b/is-data-property/test/test.js new file mode 100644 index 00000000..87ec6cb2 --- /dev/null +++ b/is-data-property/test/test.js @@ -0,0 +1,144 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isDataProperty = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isDataProperty, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property has a data descriptor', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isDataProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isDataProperty( [ 1, 2, 3 ], '1' ); + t.equal( bool, true, 'returns true' ); + + bool = isDataProperty( [ 1, 2, 3 ], 1 ); + t.equal( bool, true, 'returns true' ); + + bool = isDataProperty( new Foo(), 'bar' ); + t.equal( bool, true, 'returns true' ); + + bool = isDataProperty( [ 'a' ], 'length' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'b' + }); + + bool = isDataProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isDataProperty( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isDataProperty( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a property having a data descriptor', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + bool = isDataProperty( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'get': getter, + 'set': setter + }); + + bool = isDataProperty( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function getter() { + // No-op... + } + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided an inherited property', function test( t ) { + var bool; + + bool = isDataProperty( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isDataProperty( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isDataProperty( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isDataProperty( 'beep', 'length' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/is-dataview/README.md b/is-dataview/README.md new file mode 100644 index 00000000..f6db80da --- /dev/null +++ b/is-dataview/README.md @@ -0,0 +1,125 @@ + + +# isDataView + +> Test if a value is a [DataView][mdn-dataview]. + +
+ +## Usage + +```javascript +var isDataView = require( '@stdlib/assert/is-dataview' ); +``` + +#### isDataView( value ) + +Tests if a value is a [`DataView`][mdn-dataview]. + +```javascript +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var DataView = require( '@stdlib/array/dataview' ); + +var bool = isDataView( new DataView( new ArrayBuffer( 10 ) ) ); +// returns true + +bool = isDataView( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var DataView = require( '@stdlib/array/dataview' ); +var isDataView = require( '@stdlib/assert/is-dataview' ); + +var bool = isDataView( new DataView( new ArrayBuffer( 10 ) ) ); +// returns true + +bool = isDataView( new Float32Array( 10 ) ); +// returns false + +bool = isDataView( new Int8Array( 10 ) ); +// returns false + +bool = isDataView( new Uint8Array( 10 ) ); +// returns false + +bool = isDataView( new Uint8ClampedArray( 10 ) ); +// returns false + +bool = isDataView( new Int16Array( 10 ) ); +// returns false + +bool = isDataView( new Uint16Array( 10 ) ); +// returns false + +bool = isDataView( new Int32Array( 10 ) ); +// returns false + +bool = isDataView( new Uint32Array( 10 ) ); +// returns false + +bool = isDataView( new Float64Array( 10 ) ); +// returns false + +bool = isDataView( new Array( 10 ) ); +// returns false + +bool = isDataView( new ArrayBuffer( 10 ) ); +// returns false + +bool = isDataView( {} ); +// returns false + +bool = isDataView( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-dataview/benchmark/benchmark.js b/is-dataview/benchmark/benchmark.js new file mode 100644 index 00000000..6e72af43 --- /dev/null +++ b/is-dataview/benchmark/benchmark.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var DataView = require( '@stdlib/array/dataview' ); +var pkg = require( './../package.json' ).name; +var isDataView = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ), + new ArrayBuffer( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isDataView( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new DataView( new ArrayBuffer( 10 ) ), + new DataView( new ArrayBuffer( 10 ) ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isDataView( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-dataview/docs/repl.txt b/is-dataview/docs/repl.txt new file mode 100644 index 00000000..1e93671f --- /dev/null +++ b/is-dataview/docs/repl.txt @@ -0,0 +1,25 @@ + +{{alias}}( value ) + Tests if a value is a DataView. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a DataView. + + Examples + -------- + > var buf = new {{alias:@stdlib/array/buffer}}( 10 ); + > var bool = {{alias}}( new {{alias:@stdlib/array/dataview}}( buf ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-dataview/docs/types/index.d.ts b/is-dataview/docs/types/index.d.ts new file mode 100644 index 00000000..218ac86b --- /dev/null +++ b/is-dataview/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a `DataView`. +* +* @param value - value to test +* @returns boolean indicating whether value is a DataView +* +* @example +* var ArrayBuffer = require( `@stdlib/array/buffer` ); +* var DataView = require( `@stdlib/array/dataview` ); +* +* var bool = isDataView( new DataView( new ArrayBuffer( 10 ) ) ); +* // returns true +* +* @example +* var bool = isDataView( [] ); +* // returns false +*/ +declare function isDataView( value: any ): boolean; + + +// EXPORTS // + +export = isDataView; diff --git a/is-dataview/docs/types/test.ts b/is-dataview/docs/types/test.ts new file mode 100644 index 00000000..86ccf3dc --- /dev/null +++ b/is-dataview/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isDataView = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isDataView( new DataView( new ArrayBuffer( 10 ) ) ); // $ExpectType boolean + isDataView( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isDataView(); // $ExpectError + isDataView( [], 123 ); // $ExpectError +} diff --git a/is-dataview/examples/index.js b/is-dataview/examples/index.js new file mode 100644 index 00000000..f7780667 --- /dev/null +++ b/is-dataview/examples/index.js @@ -0,0 +1,88 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var DataView = require( '@stdlib/array/dataview' ); +var isDataView = require( './../lib' ); + +var bool = isDataView( new DataView( new ArrayBuffer( 10 ) ) ); +console.log( bool ); +// => true + +bool = isDataView( new Float32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isDataView( new Int8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isDataView( new Uint8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isDataView( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => false + +bool = isDataView( new Int16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isDataView( new Uint16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isDataView( new Int32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isDataView( new Uint32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isDataView( new Float64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isDataView( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isDataView( new ArrayBuffer( 10 ) ); +console.log( bool ); +// => false + +bool = isDataView( {} ); +console.log( bool ); +// => false + +bool = isDataView( null ); +console.log( bool ); +// => false diff --git a/is-dataview/lib/index.js b/is-dataview/lib/index.js new file mode 100644 index 00000000..f4b0bb83 --- /dev/null +++ b/is-dataview/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a `DataView`. +* +* @module @stdlib/assert/is-dataview +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* var DataView = require( '@stdlib/array/dataview' ); +* var isDataView = require( '@stdlib/assert/is-dataview' ); +* +* var bool = isDataView( new DataView( new ArrayBuffer( 10 ) ) ); +* // returns true +* +* bool = isDataView( [] ); +* // returns false +*/ + +// MODULES // + +var isDataView = require( './main.js' ); + + +// EXPORTS // + +module.exports = isDataView; diff --git a/is-dataview/lib/main.js b/is-dataview/lib/main.js new file mode 100644 index 00000000..5398c4ab --- /dev/null +++ b/is-dataview/lib/main.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var hasDataView = ( typeof DataView === 'function' ); // eslint-disable-line stdlib/require-globals + + +// MAIN // + +/** +* Tests if a value is a `DataView`. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a DataView +* +* @example +* var ArrayBuffer = require( '@stdlib/array/buffer' ); +* var DataView = require( '@stdlib/array/dataview' ); +* +* var bool = isDataView( new DataView( new ArrayBuffer( 10 ) ) ); +* // returns true +* +* @example +* var bool = isDataView( [] ); +* // returns false +*/ +function isDataView( value ) { + return ( + ( hasDataView && value instanceof DataView ) || // eslint-disable-line stdlib/require-globals + nativeClass( value ) === '[object DataView]' + ); +} + + +// EXPORTS // + +module.exports = isDataView; diff --git a/is-dataview/package.json b/is-dataview/package.json new file mode 100644 index 00000000..0a521afc --- /dev/null +++ b/is-dataview/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-dataview", + "version": "0.0.0", + "description": "Test if a value is a DataView.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "arraybuffer", + "array", + "buffer", + "dataview", + "typed", + "typed array", + "typed-array", + "is", + "isview", + "isdataview", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-dataview/test/test.js b/is-dataview/test/test.js new file mode 100644 index 00000000..6c8854db --- /dev/null +++ b/is-dataview/test/test.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var DataView = require( '@stdlib/array/dataview' ); +var isDataView = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isDataView, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a DataView', function test( t ) { + t.strictEqual( isDataView( new DataView( new ArrayBuffer( 10 ) ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a DataView', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + void 0, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Uint32Array( 10 ), + new Int32Array( 10 ), + new Uint16Array( 10 ), + new Int16Array( 10 ), + new Uint8Array( 10 ), + new Int8Array( 10 ), + new Uint8ClampedArray( 10 ), + new ArrayBuffer( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isDataView( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-date-object-array/README.md b/is-date-object-array/README.md new file mode 100644 index 00000000..2904b16e --- /dev/null +++ b/is-date-object-array/README.md @@ -0,0 +1,103 @@ + + +# isDateObjectArray + +> Test if a value is an array-like object containing only [Date][@stdlib/assert/is-date-object] objects. + +
+ +## Usage + +```javascript +var isDateObjectArray = require( '@stdlib/assert/is-date-object-array' ); +``` + +#### isDateObjectArray( value ) + +Tests if a `value` is an array-like object containing **only** [`Date`][@stdlib/assert/is-date-object] objects. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isDateObjectArray( [ new Date(), new Date() ] ); +// returns true + +bool = isDateObjectArray( [ {}, new Number( 3.0 ) ] ); +// returns false + +bool = isDateObjectArray( [ {}, '3.0' ] ); +// returns false + +bool = isDateObjectArray( [] ); +// returns false + +bool = isDateObjectArray( [ null, {} ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isDateObjectArray = require( '@stdlib/assert/is-date-object-array' ); + +var bool = isDateObjectArray( [ new Date(), new Date(), new Date() ] ); +// returns true + +bool = isDateObjectArray( [ new Date(), new Number( 3 ) ] ); +// returns false + +bool = isDateObjectArray( [ new Date(), new String( 'abc' ), {} ] ); +// returns false + +bool = isDateObjectArray( [ [], {} ] ); +// returns false + +bool = isDateObjectArray( [ 'a', 'b' ] ); +// returns false + +bool = isDateObjectArray( [] ); +// returns false +``` + +
+ + + + + + diff --git a/is-date-object-array/benchmark/benchmark.js b/is-date-object-array/benchmark/benchmark.js new file mode 100644 index 00000000..60640961 --- /dev/null +++ b/is-date-object-array/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isDateObjectArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( new Date() ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i; + bool = isDateObjectArray( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/is-date-object-array/docs/repl.txt b/is-date-object-array/docs/repl.txt new file mode 100644 index 00000000..24b44e71 --- /dev/null +++ b/is-date-object-array/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only Date objects. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating if value is an array-like object containing only Date + objects. + + Examples + -------- + > var bool = {{alias}}( [ new Date(), new Date() ] ) + true + > bool = {{alias}}( [ {}, {} ] ) + false + > bool = {{alias}}( [ new Date(), '2011-01-01' ] ) + false + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-date-object-array/docs/types/index.d.ts b/is-date-object-array/docs/types/index.d.ts new file mode 100644 index 00000000..29312150 --- /dev/null +++ b/is-date-object-array/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Test if a value is an array-like object containing only `Date` objects. +* +* @param value - value to test +* @returns boolean indicating if a value is an array-like object containing only `Date` objects +* +* @example +* var bool = isDateObjectArray( [ new Date(), new Date() ] ); +* // returns true +* +* bool = isDateObjectArray( [ {}, new Number( 3.0 ) ] ); +* // returns false +* +* bool = isDateObjectArray( [ {}, '3.0' ] ); +* // returns false +*/ + +declare function isDateObjectArray( value: any ): boolean; + + +// EXPORTS // + +export = isDateObjectArray; diff --git a/is-date-object-array/docs/types/test.ts b/is-date-object-array/docs/types/test.ts new file mode 100644 index 00000000..7b5070bd --- /dev/null +++ b/is-date-object-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isDateObjectArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isDateObjectArray( new Date() ); // $ExpectType boolean + isDateObjectArray( [ new Date(), new Date() ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isDateObjectArray(); // $ExpectError + isDateObjectArray( [ new Date() ], 123 ); // $ExpectError +} diff --git a/is-date-object-array/examples/index.js b/is-date-object-array/examples/index.js new file mode 100644 index 00000000..ff794c3b --- /dev/null +++ b/is-date-object-array/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isDateObjectArray = require( './../lib' ); + +var bool = isDateObjectArray( [ new Date(), new Date(), new Date() ] ); +console.log( bool ); +// => true + +bool = isDateObjectArray( [ new Date(), new Number( 3 ) ] ); +console.log( bool ); +// => false + +bool = isDateObjectArray( [ new Date(), new String( '2017-01-01' ), {} ] ); +console.log( bool ); +// => false + +bool = isDateObjectArray( [ [], {} ] ); +console.log( bool ); +// => false + +bool = isDateObjectArray( [ 'a', 'b' ] ); +console.log( bool ); +// => false + +bool = isDateObjectArray( [] ); +console.log( bool ); +// => false diff --git a/is-date-object-array/lib/index.js b/is-date-object-array/lib/index.js new file mode 100644 index 00000000..7c2e1250 --- /dev/null +++ b/is-date-object-array/lib/index.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only `Date` objects. +* +* @module @stdlib/assert/is-date-object-array +* +* @example +* var isDateObjectArray = require( '@stdlib/assert/is-date-object-array' ); +* +* var bool = isDateObjectArray( [ new Date(), new Date() ] ); +* // returns true +* +* bool = isDateObjectArray( [ {}, new Number( 3.0 ) ] ); +* // returns false +* +* bool = isDateObjectArray( [ {}, '3.0' ] ); +* // returns false +*/ + +// MODULES // + +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); +var isDateObject = require( '@stdlib/assert/is-date-object' ); + + +// MAIN // + +var isDateObjectArray = arrayfun( isDateObject ); + + +// EXPORTS // + +module.exports = isDateObjectArray; diff --git a/is-date-object-array/package.json b/is-date-object-array/package.json new file mode 100644 index 00000000..441d4cc8 --- /dev/null +++ b/is-date-object-array/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-date-object-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only Date objects.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "array", + "is", + "isarray", + "object", + "obj", + "isobject", + "date", + "isdate", + "isdateobject", + "type", + "check", + "validate", + "isvalid", + "test" + ] +} diff --git a/is-date-object-array/test/test.js b/is-date-object-array/test/test.js new file mode 100644 index 00000000..8ac2b378 --- /dev/null +++ b/is-date-object-array/test/test.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isDateObjectArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isDateObjectArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only `Date` objects', function test( t ) { + var arr; + + arr = [ new Date(), new Date(), new Date() ]; + t.strictEqual( isDateObjectArray( arr ), true, 'returns true' ); + + arr = [ + { 'type': 'object' }, // eslint-disable-line object-curly-newline + /.*/, + {} + ]; + t.strictEqual( isDateObjectArray( arr ), false, 'returns false' ); + + arr = [ 5.0, {}, new Date() ]; + t.strictEqual( isDateObjectArray( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': new Date(), + '1': new Date() + }; + t.strictEqual( isDateObjectArray( arr ), true, 'returns true' ); + + arr = []; + t.strictEqual( isDateObjectArray( arr ), false, 'returns false when provided an empty array' ); + + t.end(); +}); diff --git a/is-date-object/README.md b/is-date-object/README.md new file mode 100644 index 00000000..71653b83 --- /dev/null +++ b/is-date-object/README.md @@ -0,0 +1,95 @@ + + +# isDateObject + +> Test if a value is a [Date][mdn-date-object] object. + +
+ +## Usage + +```javascript +var isDateObject = require( '@stdlib/assert/is-date-object' ); +``` + +#### isDateObject( value ) + +Tests if a value is a [`Date`][mdn-date-object] object. + +```javascript +var bool = isDateObject( new Date() ); +// returns true + +bool = isDateObject( '2017-01-01' ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isDateObject = require( '@stdlib/assert/is-date-object' ); + +var bool = isDateObject( new Date() ); +// returns true + +bool = isDateObject( '2017-01-01' ); +// returns false + +bool = isDateObject( 5 ); +// returns false + +bool = isDateObject( null ); +// returns false + +bool = isDateObject( void 0 ); +// returns false + +bool = isDateObject( {} ); +// returns false + +bool = isDateObject( [] ); +// returns false + +bool = isDateObject( function Date() {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-date-object/benchmark/benchmark.js b/is-date-object/benchmark/benchmark.js new file mode 100644 index 00000000..75d12408 --- /dev/null +++ b/is-date-object/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isDateObject = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isDateObject( new Date() ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-date-object/docs/repl.txt b/is-date-object/docs/repl.txt new file mode 100644 index 00000000..384caaaf --- /dev/null +++ b/is-date-object/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a Date object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a Date object. + + Examples + -------- + > var bool = {{alias}}( new Date() ) + true + > bool = {{alias}}( '2017-01-01' ) + false + + See Also + -------- + diff --git a/is-date-object/docs/types/index.d.ts b/is-date-object/docs/types/index.d.ts new file mode 100644 index 00000000..da8c2f0d --- /dev/null +++ b/is-date-object/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a `Date` object. +* +* @param value - value to test +* @returns boolean indicating if a value is a `Date` object +* +* @example +* var bool = isDateObject( new Date() ); +* // returns true +* +* @example +* var bool = isDateObject( '2017-01-01' ); +* // returns false +*/ +declare function isDateObject( value: any ): boolean; + + +// EXPORTS // + +export = isDateObject; diff --git a/is-date-object/docs/types/test.ts b/is-date-object/docs/types/test.ts new file mode 100644 index 00000000..c4b1d80f --- /dev/null +++ b/is-date-object/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isDateObject = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isDateObject( new Date() ); // $ExpectType boolean + isDateObject( 'date' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isDateObject(); // $ExpectError + isDateObject( new Date(), 123 ); // $ExpectError +} diff --git a/is-date-object/examples/index.js b/is-date-object/examples/index.js new file mode 100644 index 00000000..81c14fb5 --- /dev/null +++ b/is-date-object/examples/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-restricted-syntax, no-empty-function */ + +'use strict'; + +var isDateObject = require( './../lib' ); + +console.log( isDateObject( new Date() ) ); +// => true + +console.log( isDateObject( '2017-01-01' ) ); +// => false + +console.log( isDateObject( 5 ) ); +// => false + +console.log( isDateObject( null ) ); +// => false + +console.log( isDateObject( void 0 ) ); +// => false + +console.log( isDateObject( {} ) ); +// => false + +console.log( isDateObject( [] ) ); +// => false + +console.log( isDateObject( function Date() {} ) ); +// => false diff --git a/is-date-object/lib/getday.js b/is-date-object/lib/getday.js new file mode 100644 index 00000000..83a76b7b --- /dev/null +++ b/is-date-object/lib/getday.js @@ -0,0 +1,26 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var getDay = Date.prototype.getDay; // non-generic + + +// EXPORTS // + +module.exports = getDay; diff --git a/is-date-object/lib/index.js b/is-date-object/lib/index.js new file mode 100644 index 00000000..81a4c3d5 --- /dev/null +++ b/is-date-object/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a `Date` object. +* +* @module @stdlib/assert/is-date-object +* +* @example +* var isDateObject = require( '@stdlib/assert/is-date-object' ); +* +* var bool = isDateObject( new Date() ); +* // returns true +* +* bool = isDateObject( '2017-01-01' ); +* // returns false +*/ + +// MODULES // + +var isDateObject = require( './main.js' ); + + +// EXPORTS // + +module.exports = isDateObject; diff --git a/is-date-object/lib/main.js b/is-date-object/lib/main.js new file mode 100644 index 00000000..0dc5d540 --- /dev/null +++ b/is-date-object/lib/main.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); +var nativeClass = require( '@stdlib/utils/native-class' ); +var test = require( './try2getday.js' ); + + +// VARIABLES // + +var FLG = hasToStringTag(); + + +// MAIN // + +/** +* Tests if a value is a `Date` object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a `Date` object +* +* @example +* var bool = isDateObject( new Date() ); +* // returns true +* +* @example +* var bool = isDateObject( '2017-01-01' ); +* // returns false +*/ +function isDateObject( value ) { + if ( typeof value === 'object' ) { + if ( value instanceof Date ) { + return true; + } + if ( FLG ) { + return test( value ); + } + return ( nativeClass( value ) === '[object Date]' ); + } + return false; +} + + +// EXPORTS // + +module.exports = isDateObject; diff --git a/is-date-object/lib/try2getday.js b/is-date-object/lib/try2getday.js new file mode 100644 index 00000000..98a1e60c --- /dev/null +++ b/is-date-object/lib/try2getday.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var getDay = require( './getday.js' ); + + +// MAIN // + +/** +* Attempts to call a `Date` method. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating if able to call a `Date` method +*/ +function test( value ) { + try { + getDay.call( value ); + return true; + } catch ( err ) { // eslint-disable-line no-unused-vars + return false; + } +} + + +// EXPORTS // + +module.exports = test; diff --git a/is-date-object/package.json b/is-date-object/package.json new file mode 100644 index 00000000..7e1036ef --- /dev/null +++ b/is-date-object/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-date-object", + "version": "0.0.0", + "description": "Test if a value is a Date object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "date", + "object", + "instance", + "is", + "isdate", + "type", + "check", + "valid", + "validate", + "test", + "isvalid" + ] +} diff --git a/is-date-object/test/test.js b/is-date-object/test/test.js new file mode 100644 index 00000000..30b99faf --- /dev/null +++ b/is-date-object/test/test.js @@ -0,0 +1,135 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); +var isDateObject = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isDateObject, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a `Date` object', function test( t ) { + t.strictEqual( isDateObject( new Date() ), true, 'returns true' ); + t.end(); +}); + +tape( 'if `Symbol.toStringTag` is supported, the function guards against objects masquerading as `Date` objects', opts, function test( t ) { + var isDateObject; + var mock; + + isDateObject = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-tostringtag-support': detect + }); + + mock = { + 'toString': toString, + 'getDay': getDay + }; + if ( hasToStringTag() ) { + mock[ Symbol.toStringTag ] = 'Date'; + } + t.strictEqual( isDateObject( mock ), false, 'returns false' ); + + t.end(); + + function detect() { + return true; + } + function toString() { + return 'a'; + } + function getDay() { + return 'a'; + } +}); + +tape( 'if `Symbol.toStringTag` is supported, the function returns `true` if provided a `Date` object', opts, function test( t ) { + var isDateObject = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-tostringtag-support': detect + }); + + t.strictEqual( isDateObject( new Date() ), true, 'returns true' ); + + t.end(); + + function detect() { + return true; + } +}); + +tape( 'if `Symbol.toStringTag` is not supported, the function attempts to determine the native class', function test( t ) { + var isDateObject = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-tostringtag-support': detect + }); + + t.strictEqual( isDateObject( new Date() ), true, 'returns true' ); + t.strictEqual( isDateObject( {} ), false, 'returns false' ); + + t.end(); + + function detect() { + return false; + } +}); + +tape( 'the function returns `false` if not provided a `Date` object', function test( t ) { + var values; + var i; + + values = [ + '2017-01-01', + new String( '2017-01-01' ), + 5, + NaN, + null, + true, + void 0, + [], + {}, + /./, + new RegExp( '.' ), + function noop() {}, + function Date() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isDateObject( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-digit-string/README.md b/is-digit-string/README.md new file mode 100644 index 00000000..2d710f80 --- /dev/null +++ b/is-digit-string/README.md @@ -0,0 +1,135 @@ + + +# isDigitString + +> Test whether a string contains only numeric digits. + +
+ +## Usage + +```javascript +var isDigitString = require( '@stdlib/assert/is-digit-string' ); +``` + +#### isDigitString( value ) + +Tests whether a string contains only numeric digits. + +```javascript +var bool = isDigitString( '0123456789' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- For non-string values, the function returns `false`. + +
+ + + +
+ +## Examples + + + +```javascript +var isDigitString = require( '@stdlib/assert/is-digit-string' ); + +var out = isDigitString( '0123456789' ); +// returns true + +out = isDigitString( '' ); +// returns false + +out = isDigitString( '0xffffff' ); +// returns false + +out = isDigitString( 123 ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-digit-string [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-digit-string 0123456789 +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n '0123456789' | is-digit-string +true +``` + +
+ + + +
+ + + + + + diff --git a/is-digit-string/benchmark/benchmark.js b/is-digit-string/benchmark/benchmark.js new file mode 100644 index 00000000..b2d36e97 --- /dev/null +++ b/is-digit-string/benchmark/benchmark.js @@ -0,0 +1,118 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isDigitString = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::single-character', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = fromCodePoint( (i%20)+48 ); + bool = isDigitString( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::string', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = '0123456789' + fromCodePoint( (i%20)+48 ); + bool = isDigitString( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::regexp-single-character', function benchmark( b ) { + var bool; + var str; + var RE; + var i; + + RE = /[0-9]+/; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = fromCodePoint( (i%20)+48 ); + bool = RE.test( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::regexp-string', function benchmark( b ) { + var bool; + var str; + var RE; + var i; + + RE = /[0-9]+/; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = '0123456789' + fromCodePoint( (i%20)+48 ); + bool = RE.test( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-digit-string/bin/cli b/is-digit-string/bin/cli new file mode 100644 index 00000000..1ee8f57f --- /dev/null +++ b/is-digit-string/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isDigitString = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isDigitString( String( args[ 0 ] ) ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isDigitString( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-digit-string/docs/repl.txt b/is-digit-string/docs/repl.txt new file mode 100644 index 00000000..c555362c --- /dev/null +++ b/is-digit-string/docs/repl.txt @@ -0,0 +1,28 @@ + +{{alias}}( str ) + Tests whether a string contains only numeric digits. + + Parameters + ---------- + str: string + Input string. + + Returns + ------- + bool: boolean + Boolean indicating whether a string contains only numeric digits. + + Examples + -------- + > var bool = {{alias}}( '0123456789' ) + true + > bool = {{alias}}( 'abcdef' ) + false + > bool = {{alias}}( '0xff' ) + false + > bool = {{alias}}( '' ) + false + + See Also + -------- + diff --git a/is-digit-string/docs/types/index.d.ts b/is-digit-string/docs/types/index.d.ts new file mode 100644 index 00000000..d99e5d99 --- /dev/null +++ b/is-digit-string/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests whether a string contains only numeric digits. +* +* @param value - value to test +* @returns boolean indicating if a string contains only numeric digits +* +* @example +* var out = isDigitString( '0123456789' ); +* // returns true +* +* @example +* var out = isDigitString( '0xffffff' ); +* // returns false +* +* @example +* var out = isDigitString( '' ); +* // returns false +* +* @example +* var out = isDigitString( 123 ); +* // returns false +*/ +declare function isDigitString( value: any ): boolean; + + +// EXPORTS // + +export = isDigitString; diff --git a/is-digit-string/docs/types/test.ts b/is-digit-string/docs/types/test.ts new file mode 100644 index 00000000..47a12f19 --- /dev/null +++ b/is-digit-string/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isDigitString = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isDigitString( '123' ); // $ExpectType boolean + isDigitString( 'abc' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isDigitString(); // $ExpectError + isDigitString( undefined, 123 ); // $ExpectError +} diff --git a/is-digit-string/docs/usage.txt b/is-digit-string/docs/usage.txt new file mode 100644 index 00000000..c9bf73e1 --- /dev/null +++ b/is-digit-string/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-digit-string [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-digit-string/etc/cli_opts.json b/is-digit-string/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-digit-string/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-digit-string/examples/index.js b/is-digit-string/examples/index.js new file mode 100644 index 00000000..cba6a74a --- /dev/null +++ b/is-digit-string/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isDigitString = require( './../lib' ); + +console.log( isDigitString( '0123456789' ) ); +// => true + +console.log( isDigitString( '' ) ); +// => false + +console.log( isDigitString( '0xffffff' ) ); +// => false + +console.log( isDigitString( 123 ) ); +// => false diff --git a/is-digit-string/lib/index.js b/is-digit-string/lib/index.js new file mode 100644 index 00000000..5fd6f98f --- /dev/null +++ b/is-digit-string/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether a string contains only numeric digits. +* +* @module @stdlib/assert/is-digit-string +* +* @example +* var isDigitString = require( '@stdlib/assert/is-digit-string' ); +* +* var out = isDigitString( '0123456789' ); +* // returns true +* +* out = isDigitString( '0xffffff' ); +* // returns false +* +* out = isDigitString( '' ); +* // returns false +*/ + +// MODULES // + +var isDigitString = require( './main.js' ); + + +// EXPORTS // + +module.exports = isDigitString; diff --git a/is-digit-string/lib/main.js b/is-digit-string/lib/main.js new file mode 100644 index 00000000..85af1cd5 --- /dev/null +++ b/is-digit-string/lib/main.js @@ -0,0 +1,80 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; + + +// VARIABLES // + +// Character codes: +var ZERO = 48; +var NINE = 57; + + +// MAIN // + +/** +* Tests whether a string contains only numeric digits. +* +* @param {*} x - value to test +* @returns {boolean} boolean indicating if a string contains only numeric digits +* +* @example +* var out = isDigitString( '0123456789' ); +* // returns true +* +* @example +* var out = isDigitString( '0xffffff' ); +* // returns false +* +* @example +* var out = isDigitString( '' ); +* // returns false +* +* @example +* var out = isDigitString( 123 ); +* // returns false +*/ +function isDigitString( x ) { + var len; + var ch; + var i; + if ( !isString( x ) ) { + return false; + } + len = x.length; + if ( len === 0 ) { + return false; + } + for ( i = 0; i < len; i++ ) { + ch = x.charCodeAt( i ); + if ( ch < ZERO || ch > NINE ) { + return false; + } + } + return true; +} + + +// EXPORTS // + +module.exports = isDigitString; diff --git a/is-digit-string/package.json b/is-digit-string/package.json new file mode 100644 index 00000000..1b54f935 --- /dev/null +++ b/is-digit-string/package.json @@ -0,0 +1,81 @@ +{ + "name": "@stdlib/assert/is-digit-string", + "version": "0.0.0", + "description": "Test whether a string contains only numeric digits.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-digit-string": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "string", + "str", + "digit", + "numeric", + "number", + "num", + "is", + "isstring", + "isdigit", + "isnumber", + "isnum", + "validate", + "valid", + "isvalid", + "test", + "check" + ] +} diff --git a/is-digit-string/test/fixtures/stdin_error.js.txt b/is-digit-string/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-digit-string/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-digit-string/test/test.cli.js b/is-digit-string/test/test.cli.js new file mode 100644 index 00000000..497f5cfe --- /dev/null +++ b/is-digit-string/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument consists only of numeric digits', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'0123456789\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "0123456789\nbeep"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\nfalse\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-digit-string/test/test.js b/is-digit-string/test/test.js new file mode 100644 index 00000000..bedaf757 --- /dev/null +++ b/is-digit-string/test/test.js @@ -0,0 +1,114 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isDigitString = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isDigitString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a string containing all numeric digits', function test( t ) { + t.strictEqual( isDigitString( '0123456789' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a string containing all numeric digits', function test( t ) { + var values; + var i; + + values = [ + '', + 'È', + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i', + 'j', + 'k', + 'l', + 'm', + 'n', + 'o', + 'p', + 'q', + 'r', + 's', + 't', + 'u', + 'v', + 'w', + 'x', + 'y', + 'z', + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', + 'G', + 'H', + 'I', + 'J', + 'K', + 'L', + 'M', + 'N', + 'O', + 'P', + 'Q', + 'R', + 'S', + 'T', + 'U', + 'V', + 'W', + 'X', + 'Y', + 'Z', + new String( '0123456789' ), // eslint-disable-line no-new-wrappers + 5, + null, + NaN, + undefined, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isDigitString( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-electron-main/README.md b/is-electron-main/README.md new file mode 100644 index 00000000..c89d504c --- /dev/null +++ b/is-electron-main/README.md @@ -0,0 +1,69 @@ + + +# IS_ELECTRON_MAIN + +> Check if the runtime is the main [Electron][electron] process. + +
+ +## Usage + +```javascript +var IS_ELECTRON_MAIN = require( '@stdlib/assert/is-electron-main' ); +``` + +#### IS_ELECTRON_MAIN + +`Boolean` indicating if the runtime is the main [Electron][electron] process. + +```javascript +var bool = IS_ELECTRON_MAIN; +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var IS_ELECTRON_MAIN = require( '@stdlib/assert/is-electron-main' ); + +console.log( IS_ELECTRON_MAIN ); +// => +``` + +
+ + + + + + diff --git a/is-electron-main/docs/repl.txt b/is-electron-main/docs/repl.txt new file mode 100644 index 00000000..5257fb88 --- /dev/null +++ b/is-electron-main/docs/repl.txt @@ -0,0 +1,12 @@ + +{{alias}} + Boolean indicating if the runtime is the main Electron process. + + Examples + -------- + > {{alias}} + + + See Also + -------- + diff --git a/is-electron-main/docs/types/index.d.ts b/is-electron-main/docs/types/index.d.ts new file mode 100644 index 00000000..f6d43d5f --- /dev/null +++ b/is-electron-main/docs/types/index.d.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Boolean indicating if the runtime is the main Electron process. +* +* @example +* var bool = IS_ELECTRON_MAIN; +* // returns +*/ +declare const IS_ELECTRON_MAIN: boolean; + + +// EXPORTS // + +export = IS_ELECTRON_MAIN; diff --git a/is-electron-main/docs/types/test.ts b/is-electron-main/docs/types/test.ts new file mode 100644 index 00000000..c7a12030 --- /dev/null +++ b/is-electron-main/docs/types/test.ts @@ -0,0 +1,28 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import IS_ELECTRON_MAIN = require( './index' ); + + +// TESTS // + +// The variable is a boolean... +{ + // tslint:disable-next-line:no-unused-expression + IS_ELECTRON_MAIN; // $ExpectType boolean +} diff --git a/is-electron-main/examples/index.js b/is-electron-main/examples/index.js new file mode 100644 index 00000000..654034c8 --- /dev/null +++ b/is-electron-main/examples/index.js @@ -0,0 +1,24 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var IS_ELECTRON_MAIN = require( './../lib' ); + +console.log( IS_ELECTRON_MAIN ); +// => diff --git a/is-electron-main/lib/browser.js b/is-electron-main/lib/browser.js new file mode 100644 index 00000000..5f913971 --- /dev/null +++ b/is-electron-main/lib/browser.js @@ -0,0 +1,23 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// EXPORTS // + +module.exports = false; diff --git a/is-electron-main/lib/index.js b/is-electron-main/lib/index.js new file mode 100644 index 00000000..4a70ccb7 --- /dev/null +++ b/is-electron-main/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Boolean indicating if the runtime is the main Electron process. +* +* @module @stdlib/assert/is-electron-main +* +* @example +* var IS_ELECTRON_MAIN = require( '@stdlib/assert/is-electron-main' ); +* +* var bool = IS_ELECTRON_MAIN; +* // returns +*/ + +// MODULES // + +var isElectronMain = require( './main.js' ); + + +// MAIN // + +var bool = isElectronMain(); + + +// EXPORTS // + +module.exports = bool; diff --git a/is-electron-main/lib/main.js b/is-electron-main/lib/main.js new file mode 100644 index 00000000..000a3272 --- /dev/null +++ b/is-electron-main/lib/main.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isElectron = require( '@stdlib/assert/is-electron' ); +var proc = require( './process.js' ); + + +// MAIN // + +/** +* Returns a boolean indicating if the runtime is the main Electron process. +* +* @returns {boolean} boolean indicating if runtime is the main Electron process +* +* @example +* var bool = isElectronMain(); +* // returns +*/ +function isElectronMain() { + return ( + isElectron && + proc.type === 'browser' + ); +} + + +// EXPORTS // + +module.exports = isElectronMain; diff --git a/is-electron-main/lib/process.js b/is-electron-main/lib/process.js new file mode 100644 index 00000000..ac1bb1c4 --- /dev/null +++ b/is-electron-main/lib/process.js @@ -0,0 +1,23 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// EXPORTS // + +module.exports = process; diff --git a/is-electron-main/package.json b/is-electron-main/package.json new file mode 100644 index 00000000..56f1ea82 --- /dev/null +++ b/is-electron-main/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/assert/is-electron-main", + "version": "0.0.0", + "description": "Check if the runtime is the main Electron process.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "browser": "./lib/browser.js", + "directories": { + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "runtime", + "environment", + "electron", + "renderer", + "atom" + ] +} diff --git a/is-electron-main/test/test.browser.js b/is-electron-main/test/test.browser.js new file mode 100644 index 00000000..242e4042 --- /dev/null +++ b/is-electron-main/test/test.browser.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var IS_ELECTRON_MAIN = require( './../lib/browser.js' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_ELECTRON_MAIN, 'boolean', 'main export is a boolean' ); + t.end(); +}); + +tape( 'the exported value is always `false`', function test( t ) { + t.equal( IS_ELECTRON_MAIN, false, 'returns false' ); + t.end(); +}); diff --git a/is-electron-main/test/test.js b/is-electron-main/test/test.js new file mode 100644 index 00000000..7a388176 --- /dev/null +++ b/is-electron-main/test/test.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var IS_ELECTRON_MAIN = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_ELECTRON_MAIN, 'boolean', 'main export is a boolean' ); + t.end(); +}); diff --git a/is-electron-main/test/test.main.js b/is-electron-main/test/test.main.js new file mode 100644 index 00000000..002196bd --- /dev/null +++ b/is-electron-main/test/test.main.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var ENV = require( '@stdlib/process/env' ); +var isElectronMain = require( './../lib/main.js' ); + + +// FIXTURES // + +function Process() { + this.type = 'browser'; + this.versions = { + 'electron': '1.0.0', + 'chrome': '42.1.34' + }; + this.env = ENV; + return this; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isElectronMain, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if runtime is the main Electron process', function test( t ) { + var isElectronMain; + var proc; + var bool; + + proc = new Process(); + + isElectronMain = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-electron': true, + './process.js': proc + }); + + bool = isElectronMain(); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if runtime is not the main Electron process (not Electron)', function test( t ) { + var isElectronMain; + var proc; + var bool; + + proc = new Process(); + + isElectronMain = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-electron': false, + './process.js': proc + }); + + bool = isElectronMain(); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if runtime is not the main Electron process (process type does not equal `browser`)', function test( t ) { + var isElectronMain; + var proc; + var bool; + + proc = new Process(); + proc.type = 'renderer'; + + isElectronMain = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-electron': true, + './process.js': proc + }); + + bool = isElectronMain(); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); diff --git a/is-electron-renderer/README.md b/is-electron-renderer/README.md new file mode 100644 index 00000000..da060521 --- /dev/null +++ b/is-electron-renderer/README.md @@ -0,0 +1,69 @@ + + +# IS_ELECTRON_RENDERER + +> Check if the runtime is the [Electron][electron] renderer process. + +
+ +## Usage + +```javascript +var IS_ELECTRON_RENDERER = require( '@stdlib/assert/is-electron-renderer' ); +``` + +#### IS_ELECTRON_RENDERER + +`Boolean` indicating if the runtime is the [Electron][electron] renderer process. + +```javascript +var bool = IS_ELECTRON_RENDERER; +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var IS_ELECTRON_RENDERER = require( '@stdlib/assert/is-electron-renderer' ); + +console.log( IS_ELECTRON_RENDERER ); +// => +``` + +
+ + + + + + diff --git a/is-electron-renderer/docs/repl.txt b/is-electron-renderer/docs/repl.txt new file mode 100644 index 00000000..f490b4bb --- /dev/null +++ b/is-electron-renderer/docs/repl.txt @@ -0,0 +1,12 @@ + +{{alias}} + Boolean indicating if the runtime is the Electron renderer process. + + Examples + -------- + > {{alias}} + + + See Also + -------- + diff --git a/is-electron-renderer/docs/types/index.d.ts b/is-electron-renderer/docs/types/index.d.ts new file mode 100644 index 00000000..18df04b4 --- /dev/null +++ b/is-electron-renderer/docs/types/index.d.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Boolean indicating if the runtime is the Electron renderer process. +* +* @example +* var bool = IS_ELECTRON_RENDERER; +* // returns +*/ +declare const IS_ELECTRON_RENDERER: boolean; + + +// EXPORTS // + +export = IS_ELECTRON_RENDERER; diff --git a/is-electron-renderer/docs/types/test.ts b/is-electron-renderer/docs/types/test.ts new file mode 100644 index 00000000..abc9bf55 --- /dev/null +++ b/is-electron-renderer/docs/types/test.ts @@ -0,0 +1,28 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import IS_ELECTRON_RENDERER = require( './index' ); + + +// TESTS // + +// The variable is a boolean... +{ + // tslint:disable-next-line:no-unused-expression + IS_ELECTRON_RENDERER; // $ExpectType boolean +} diff --git a/is-electron-renderer/examples/index.js b/is-electron-renderer/examples/index.js new file mode 100644 index 00000000..3244cab3 --- /dev/null +++ b/is-electron-renderer/examples/index.js @@ -0,0 +1,24 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var IS_ELECTRON_RENDERER = require( './../lib' ); + +console.log( IS_ELECTRON_RENDERER ); +// => diff --git a/is-electron-renderer/lib/browser.js b/is-electron-renderer/lib/browser.js new file mode 100644 index 00000000..5f913971 --- /dev/null +++ b/is-electron-renderer/lib/browser.js @@ -0,0 +1,23 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// EXPORTS // + +module.exports = false; diff --git a/is-electron-renderer/lib/index.js b/is-electron-renderer/lib/index.js new file mode 100644 index 00000000..0066af5a --- /dev/null +++ b/is-electron-renderer/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Boolean indicating if the runtime is the Electron renderer process. +* +* @module @stdlib/assert/is-electron-renderer +* +* @example +* var IS_ELECTRON_RENDERER = require( '@stdlib/assert/is-electron-renderer' ); +* +* var bool = IS_ELECTRON_RENDERER; +* // returns +*/ + +// MODULES // + +var isElectronRenderer = require( './main.js' ); + + +// MAIN // + +var bool = isElectronRenderer(); + + +// EXPORTS // + +module.exports = bool; diff --git a/is-electron-renderer/lib/main.js b/is-electron-renderer/lib/main.js new file mode 100644 index 00000000..a5998619 --- /dev/null +++ b/is-electron-renderer/lib/main.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isElectron = require( '@stdlib/assert/is-electron' ); +var proc = require( './process.js' ); + + +// MAIN // + +/** +* Returns a boolean indicating if the runtime is the Electron renderer process. +* +* @returns {boolean} boolean indicating if runtime is the Electron renderer process +* +* @example +* var bool = isElectronRenderer(); +* // returns +*/ +function isElectronRenderer() { + return ( + isElectron && + proc.type === 'renderer' + ); +} + + +// EXPORTS // + +module.exports = isElectronRenderer; diff --git a/is-electron-renderer/lib/process.js b/is-electron-renderer/lib/process.js new file mode 100644 index 00000000..ac1bb1c4 --- /dev/null +++ b/is-electron-renderer/lib/process.js @@ -0,0 +1,23 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// EXPORTS // + +module.exports = process; diff --git a/is-electron-renderer/package.json b/is-electron-renderer/package.json new file mode 100644 index 00000000..c5df6ac4 --- /dev/null +++ b/is-electron-renderer/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/assert/is-electron-renderer", + "version": "0.0.0", + "description": "Check if the runtime is the Electron renderer process.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "browser": "./lib/browser.js", + "directories": { + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "runtime", + "environment", + "electron", + "renderer", + "atom" + ] +} diff --git a/is-electron-renderer/test/test.browser.js b/is-electron-renderer/test/test.browser.js new file mode 100644 index 00000000..e2fb666b --- /dev/null +++ b/is-electron-renderer/test/test.browser.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var IS_ELECTRON_RENDERER = require( './../lib/browser.js' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_ELECTRON_RENDERER, 'boolean', 'main export is a boolean' ); + t.end(); +}); + +tape( 'the exported value is always `false`', function test( t ) { + t.equal( IS_ELECTRON_RENDERER, false, 'returns false' ); + t.end(); +}); diff --git a/is-electron-renderer/test/test.js b/is-electron-renderer/test/test.js new file mode 100644 index 00000000..47f7dead --- /dev/null +++ b/is-electron-renderer/test/test.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var IS_ELECTRON_RENDERER = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_ELECTRON_RENDERER, 'boolean', 'main export is a boolean' ); + t.end(); +}); diff --git a/is-electron-renderer/test/test.main.js b/is-electron-renderer/test/test.main.js new file mode 100644 index 00000000..bee046ae --- /dev/null +++ b/is-electron-renderer/test/test.main.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var ENV = require( '@stdlib/process/env' ); +var isElectronRenderer = require( './../lib/main.js' ); + + +// FIXTURES // + +function Process() { + this.type = 'renderer'; + this.versions = { + 'electron': '1.0.0', + 'chrome': '42.1.34' + }; + this.env = ENV; + return this; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isElectronRenderer, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if runtime is the Electron renderer process', function test( t ) { + var isElectronRenderer; + var proc; + var bool; + + proc = new Process(); + + isElectronRenderer = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-electron': true, + './process.js': proc + }); + + bool = isElectronRenderer(); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if runtime is not the Electron renderer process (not Electron)', function test( t ) { + var isElectronRenderer; + var proc; + var bool; + + proc = new Process(); + + isElectronRenderer = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-electron': false, + './process.js': proc + }); + + bool = isElectronRenderer(); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if runtime is not the Electron renderer process (process type does not equal `renderer`)', function test( t ) { + var isElectronRenderer; + var proc; + var bool; + + proc = new Process(); + proc.type = 'browser'; + + isElectronRenderer = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-electron': true, + './process.js': proc + }); + + bool = isElectronRenderer(); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); diff --git a/is-electron/README.md b/is-electron/README.md new file mode 100644 index 00000000..8238c122 --- /dev/null +++ b/is-electron/README.md @@ -0,0 +1,69 @@ + + +# IS_ELECTRON + +> Check if the runtime is [Electron][electron]. + +
+ +## Usage + +```javascript +var IS_ELECTRON = require( '@stdlib/assert/is-electron' ); +``` + +#### IS_ELECTRON + +`Boolean` indicating if the runtime is [Electron][electron]. + +```javascript +var bool = IS_ELECTRON; +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var IS_ELECTRON = require( '@stdlib/assert/is-electron' ); + +console.log( IS_ELECTRON ); +// => +``` + +
+ + + + + + diff --git a/is-electron/docs/repl.txt b/is-electron/docs/repl.txt new file mode 100644 index 00000000..81dbe8fa --- /dev/null +++ b/is-electron/docs/repl.txt @@ -0,0 +1,12 @@ + +{{alias}} + Boolean indicating if the runtime is Electron. + + Examples + -------- + > {{alias}} + + + See Also + -------- + diff --git a/is-electron/docs/types/index.d.ts b/is-electron/docs/types/index.d.ts new file mode 100644 index 00000000..737d735d --- /dev/null +++ b/is-electron/docs/types/index.d.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Boolean indicating if the runtime is Electron. +* +* @example +* var bool = IS_ELECTRON; +* // returns +*/ +declare const IS_ELECTRON: boolean; + + +// EXPORTS // + +export = IS_ELECTRON; diff --git a/is-electron/docs/types/test.ts b/is-electron/docs/types/test.ts new file mode 100644 index 00000000..233ae9be --- /dev/null +++ b/is-electron/docs/types/test.ts @@ -0,0 +1,28 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import IS_ELECTRON = require( './index' ); + + +// TESTS // + +// The variable is a boolean... +{ + // tslint:disable-next-line:no-unused-expression + IS_ELECTRON; // $ExpectType boolean +} diff --git a/is-electron/examples/index.js b/is-electron/examples/index.js new file mode 100644 index 00000000..cbb8b3a3 --- /dev/null +++ b/is-electron/examples/index.js @@ -0,0 +1,24 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var IS_ELECTRON = require( './../lib' ); + +console.log( IS_ELECTRON ); +// => diff --git a/is-electron/lib/browser.js b/is-electron/lib/browser.js new file mode 100644 index 00000000..5f913971 --- /dev/null +++ b/is-electron/lib/browser.js @@ -0,0 +1,23 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// EXPORTS // + +module.exports = false; diff --git a/is-electron/lib/index.js b/is-electron/lib/index.js new file mode 100644 index 00000000..ab8152b4 --- /dev/null +++ b/is-electron/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Boolean indicating if the runtime is Electron. +* +* @module @stdlib/assert/is-electron +* +* @example +* var IS_ELECTRON = require( '@stdlib/assert/is-electron' ); +* +* var bool = IS_ELECTRON; +* // returns +*/ + +// MODULES // + +var isElectron = require( './main.js' ); + + +// MAIN // + +var bool = isElectron(); + + +// EXPORTS // + +module.exports = bool; diff --git a/is-electron/lib/main.js b/is-electron/lib/main.js new file mode 100644 index 00000000..8269838c --- /dev/null +++ b/is-electron/lib/main.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); +var isObject = require( '@stdlib/assert/is-plain-object' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var proc = require( './process.js' ); + + +// MAIN // + +/** +* Returns a boolean indicating if the runtime is Electron. +* +* @returns {boolean} boolean indicating if runtime is Electron +* +* @example +* var bool = isElectron(); +* // returns +*/ +function isElectron() { + return ( + // Check for a `process` global variable: + typeof proc === 'object' && + + // Check that the `process` global variable has the expected internal class: + nativeClass( proc ) === '[object process]' && + + // Check for a `versions` property: + isObject( proc.versions ) && + + // Check for an `electron` property: + isString( proc.versions.electron ) && + + // Check for a `chrome` property: + isString( proc.versions.chrome ) && + + // Check for a `type` property: + ( + proc.type === 'browser' || + proc.type === 'renderer' + ) + ); +} + + +// EXPORTS // + +module.exports = isElectron; diff --git a/is-electron/lib/process.js b/is-electron/lib/process.js new file mode 100644 index 00000000..ac1bb1c4 --- /dev/null +++ b/is-electron/lib/process.js @@ -0,0 +1,23 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// EXPORTS // + +module.exports = process; diff --git a/is-electron/package.json b/is-electron/package.json new file mode 100644 index 00000000..3e2905b8 --- /dev/null +++ b/is-electron/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/assert/is-electron", + "version": "0.0.0", + "description": "Check if the runtime is Electron.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "browser": "./lib/browser.js", + "directories": { + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "runtime", + "environment", + "electron", + "renderer", + "atom" + ] +} diff --git a/is-electron/test/test.browser.js b/is-electron/test/test.browser.js new file mode 100644 index 00000000..c72540be --- /dev/null +++ b/is-electron/test/test.browser.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var IS_ELECTRON = require( './../lib/browser.js' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_ELECTRON, 'boolean', 'main export is a boolean' ); + t.end(); +}); + +tape( 'the exported value is always `false`', function test( t ) { + t.equal( IS_ELECTRON, false, 'returns false' ); + t.end(); +}); diff --git a/is-electron/test/test.js b/is-electron/test/test.js new file mode 100644 index 00000000..e0bd6d7d --- /dev/null +++ b/is-electron/test/test.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var IS_ELECTRON = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_ELECTRON, 'boolean', 'main export is a boolean' ); + t.end(); +}); diff --git a/is-electron/test/test.main.js b/is-electron/test/test.main.js new file mode 100644 index 00000000..b5915998 --- /dev/null +++ b/is-electron/test/test.main.js @@ -0,0 +1,209 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var ENV = require( '@stdlib/process/env' ); +var isElectron = require( './../lib/main.js' ); + + +// FIXTURES // + +function Process() { + this.type = 'browser'; + this.versions = { + 'electron': '1.0.0', + 'chrome': '42.1.34' + }; + this.env = ENV; + return this; +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isElectron, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if runtime is Electron (browser/main)', function test( t ) { + var isElectron; + var proc; + var bool; + + proc = new Process(); + proc.type = 'browser'; + + isElectron = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + './process.js': proc + }); + + bool = isElectron(); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); + + function nativeClass() { + return '[object process]'; + } +}); + +tape( 'the function returns `true` if runtime is Electron (renderer)', function test( t ) { + var isElectron; + var proc; + var bool; + + proc = new Process(); + proc.type = 'renderer'; + + isElectron = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + './process.js': proc + }); + + bool = isElectron(); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); + + function nativeClass() { + return '[object process]'; + } +}); + +tape( 'the function returns `false` if runtime is not Electron (`process` variable has wrong class)', function test( t ) { + var isElectron; + var proc; + var bool; + + function nativeClass() { + return '[object beeeeeep]'; + } + + proc = new Process(); + proc.type = 'browser'; + + isElectron = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + './process.js': proc + }); + + bool = isElectron(); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if runtime is not Electron (`process.versions` is not an object)', function test( t ) { + var isElectron; + var proc; + var bool; + + proc = new Process(); + proc.versions = true; + + isElectron = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + './process.js': proc + }); + + bool = isElectron(); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); + + function nativeClass() { + return '[object process]'; + } +}); + +tape( 'the function returns `false` if runtime is not Electron (`process.versions.electron` is not a string primitive)', function test( t ) { + var isElectron; + var proc; + var bool; + + proc = new Process(); + proc.versions.electron = null; + + isElectron = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + './process.js': proc + }); + + bool = isElectron(); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); + + function nativeClass() { + return '[object process]'; + } +}); + +tape( 'the function returns `false` if runtime is not Electron (`process.versions.chrome` is not a string primitive)', function test( t ) { + var isElectron; + var proc; + var bool; + + proc = new Process(); + proc.versions.chrome = {}; + + isElectron = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + './process.js': proc + }); + + bool = isElectron(); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); + + function nativeClass() { + return '[object process]'; + } +}); + +tape( 'the function returns `false` if runtime is not Electron (`process.type` is not equal to `browser` or `renderer`)', function test( t ) { + var isElectron; + var proc; + var bool; + + proc = new Process(); + proc.type = 'beeeeep'; + + isElectron = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + './process.js': proc + }); + + bool = isElectron(); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); + + function nativeClass() { + return '[object process]'; + } +}); diff --git a/is-email-address/README.md b/is-email-address/README.md new file mode 100644 index 00000000..66fc2e2d --- /dev/null +++ b/is-email-address/README.md @@ -0,0 +1,144 @@ + + +# isEmail + +> Test if a value is an email address. + +
+ +
+ + + +
+ +## Usage + +```javascript +var isEmail = require( '@stdlib/assert/is-email-address' ); +``` + +#### isEmail( value ) + +Tests if a `value` is an [email address][validate-email-address]. + +```javascript +var bool = isEmail( 'beep@boop.com' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Validation is **not** rigorous, nor [should it be][validate-email-address]. **9** RFCs relate to email addresses, and accounting for all of them is a fool's errand. This module performs the simplest validation; i.e., requiring **at least** one `@` symbol. +- For rigorous validation, send a confirmation email. If the email bounces, consider the email invalid. + + + +
+ +## Examples + + + +```javascript +var isEmail = require( '@stdlib/assert/is-email-address' ); + +var bool; + +bool = isEmail( 'beep@boop.com' ); +// returns true + +bool = isEmail( 'beep' ); +// returns false + +bool = isEmail( 'beep.com' ); +// returns false + +bool = isEmail( null ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-email-address [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-email-address beep@boop.com +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n 'beep@boop.com' | is-email +true +``` + +
+ + + +
+ + + + + + diff --git a/is-email-address/benchmark/benchmark.js b/is-email-address/benchmark/benchmark.js new file mode 100644 index 00000000..f2b2ffd1 --- /dev/null +++ b/is-email-address/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isEmailAddress = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = 'beep@boo'+fromCodePoint( 97 + (i%26) )+'.com'; + bool = isEmailAddress( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-email-address/bin/cli b/is-email-address/bin/cli new file mode 100644 index 00000000..a20af01c --- /dev/null +++ b/is-email-address/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isEmailAddress = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isEmailAddress( args[ 0 ] ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isEmailAddress( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-email-address/docs/repl.txt b/is-email-address/docs/repl.txt new file mode 100644 index 00000000..770bad46 --- /dev/null +++ b/is-email-address/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( value ) + Tests if a value is an email address. + + Validation is not rigorous. *9* RFCs relate to email addresses, and + accounting for all of them is a fool's errand. The function performs the + simplest validation; i.e., requiring at least one `@` symbol. + + For rigorous validation, send a confirmation email. If the email bounces, + consider the email invalid. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an email address. + + Examples + -------- + > var bool = {{alias}}( 'beep@boop.com' ) + true + > bool = {{alias}}( 'beep' ) + false + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-email-address/docs/types/index.d.ts b/is-email-address/docs/types/index.d.ts new file mode 100644 index 00000000..bba4f1d0 --- /dev/null +++ b/is-email-address/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a valid(ish) email address. +* +* @param value - value to test +* @returns boolean indicating whether a value is a valid(ish) email address +* +* @example +* var bool = isEmail( 'beep@boop.com' ); +* // returns true +* +* @example +* var bool = isEmail( 'beep' ); +* // returns false +* +* @example +* var bool = isEmail( 'beep.com' ); +* // returns false +* +* @example +* var bool = isEmail( null ); +* // returns false +*/ +declare function isEmail( value: any ): boolean; + + +// EXPORTS // + +export = isEmail; diff --git a/is-email-address/docs/types/test.ts b/is-email-address/docs/types/test.ts new file mode 100644 index 00000000..968ba4f8 --- /dev/null +++ b/is-email-address/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isEmail = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isEmail( 'beep@boop.com' ); // $ExpectType boolean + isEmail( 'hello world' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isEmail(); // $ExpectError + isEmail( undefined, 123 ); // $ExpectError +} diff --git a/is-email-address/docs/usage.txt b/is-email-address/docs/usage.txt new file mode 100644 index 00000000..f222b97d --- /dev/null +++ b/is-email-address/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-email-address [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-email-address/etc/cli_opts.json b/is-email-address/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-email-address/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-email-address/examples/index.js b/is-email-address/examples/index.js new file mode 100644 index 00000000..aae5a054 --- /dev/null +++ b/is-email-address/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isEmail = require( './../lib' ); + +console.log( isEmail( 'beep@boop.com' ) ); +// => true + +console.log( isEmail( 'beep' ) ); +// => false + +console.log( isEmail( 'beep.com' ) ); +// => false + +console.log( isEmail( null ) ); +// => false diff --git a/is-email-address/lib/index.js b/is-email-address/lib/index.js new file mode 100644 index 00000000..c6045950 --- /dev/null +++ b/is-email-address/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an email address. +* +* @module @stdlib/assert/is-email-address +* +* @example +* var isEmail = require( '@stdlib/assert/is-email-address' ); +* +* var bool = isEmail( 'beep@boop.com' ); +* // returns true +* +* bool = isEmail( 'beep' ); +* // returns false +* +* bool = isEmail( null ); +* // returns false +*/ + +// MODULES // + +var isEmail = require( './main.js' ); + + +// EXPORTS // + +module.exports = isEmail; diff --git a/is-email-address/lib/main.js b/is-email-address/lib/main.js new file mode 100644 index 00000000..89dd2de8 --- /dev/null +++ b/is-email-address/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; + + +// VARIABLES // + +var RE = /@/; + + +// MAIN // + +/** +* Tests if a value is a valid(ish) email address. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is a valid(ish) email address +* +* @example +* var bool = isEmail( 'beep@boop.com' ); +* // returns true +* +* @example +* var bool = isEmail( 'beep' ); +* // returns false +* +* @example +* var bool = isEmail( 'beep.com' ); +* // returns false +* +* @example +* var bool = isEmail( null ); +* // returns false +*/ +function isEmail( value ) { + return isString( value ) && RE.test( value ); +} + + +// EXPORTS // + +module.exports = isEmail; diff --git a/is-email-address/package.json b/is-email-address/package.json new file mode 100644 index 00000000..c7e4bf52 --- /dev/null +++ b/is-email-address/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/assert/is-email-address", + "version": "0.0.0", + "description": "Test if a value is an email address.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-email-address": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utils", + "util", + "utilities", + "utility", + "isemail", + "email address", + "email", + "e-mail", + "address" + ] +} diff --git a/is-email-address/test/fixtures/stdin_error.js.txt b/is-email-address/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-email-address/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-email-address/test/test.cli.js b/is-email-address/test/test.cli.js new file mode 100644 index 00000000..564eeb6a --- /dev/null +++ b/is-email-address/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument is an email address', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'beep@boop.com\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "beep\nbeep@boop.com"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'false\ntrue\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-email-address/test/test.js b/is-email-address/test/test.js new file mode 100644 index 00000000..f47181e0 --- /dev/null +++ b/is-email-address/test/test.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isEmail = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isEmail, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` when provided an email address (equals a string including the `@` character)', function test( t ) { + t.equal( isEmail( 'beep@boop.com' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` when not provided an email address', function test( t ) { + var values; + var i; + + values = [ + '5', + 'beep', + 'beep/boop', + 'beep.com', + 5, + null, + undefined, + NaN, + true, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isEmail( values[ i ] ), false, 'returns false' ); + } + t.end(); +}); diff --git a/is-empty-array/README.md b/is-empty-array/README.md new file mode 100644 index 00000000..c7ee7fe0 --- /dev/null +++ b/is-empty-array/README.md @@ -0,0 +1,78 @@ + + +# isEmptyArray + +> Test if a value is an empty array. + +
+ +## Usage + +```javascript +var isEmptyArray = require( '@stdlib/assert/is-empty-array' ); +``` + +#### isEmptyArray( value ) + +Tests if a value is an empty `array`. + +```javascript +var bool = isEmptyArray( [] ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isEmptyArray = require( '@stdlib/assert/is-empty-array' ); + +var bool = isEmptyArray( [] ); +// returns true + +bool = isEmptyArray( new Array() ); +// returns true + +bool = isEmptyArray( [ 1, 2, 3, 4 ] ); +// returns false + +bool = isEmptyArray( {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-empty-array/benchmark/benchmark.js b/is-empty-array/benchmark/benchmark.js new file mode 100644 index 00000000..4ba9e78e --- /dev/null +++ b/is-empty-array/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isEmptyArray = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEmptyArray( [ i, i+1, i+2 ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-empty-array/docs/repl.txt b/is-empty-array/docs/repl.txt new file mode 100644 index 00000000..425ac3bd --- /dev/null +++ b/is-empty-array/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( value ) + Tests if a value is an empty array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating if a value is an empty array. + + Examples + -------- + > var bool = {{alias}}( [] ) + true + > bool = {{alias}}( [ 1, 2, 3 ] ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-empty-array/docs/types/index.d.ts b/is-empty-array/docs/types/index.d.ts new file mode 100644 index 00000000..a3bb61ff --- /dev/null +++ b/is-empty-array/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an empty array. +* +* @param value - value to test +* @returns boolean indicating whether value is an empty array +* +* @example +* var bool = isEmptyArray( [] ); +* // returns true +* +* @example +* var bool = isEmptyArray( [ 1, 2, 3 ] ); +* // returns false +* +* @example +* var bool = isEmptyArray( {} ); +* // returns false +*/ +declare function isEmptyArray( value: any ): boolean; + + +// EXPORTS // + +export = isEmptyArray; diff --git a/is-empty-array/docs/types/test.ts b/is-empty-array/docs/types/test.ts new file mode 100644 index 00000000..fe27f5c8 --- /dev/null +++ b/is-empty-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isEmptyArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isEmptyArray( [ 1, 2, 3 ] ); // $ExpectType boolean + isEmptyArray( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isEmptyArray(); // $ExpectError + isEmptyArray( [], 123 ); // $ExpectError +} diff --git a/is-empty-array/examples/index.js b/is-empty-array/examples/index.js new file mode 100644 index 00000000..7391810e --- /dev/null +++ b/is-empty-array/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-array-constructor */ + +'use strict'; + +var isEmptyArray = require( './../lib' ); + +console.log( isEmptyArray( [] ) ); +// => true + +console.log( isEmptyArray( new Array() ) ); +// => true + +console.log( isEmptyArray( [ 1, 2, 3 ] ) ); +// => false + +console.log( isEmptyArray( {} ) ); +// => false diff --git a/is-empty-array/lib/index.js b/is-empty-array/lib/index.js new file mode 100644 index 00000000..1cc18842 --- /dev/null +++ b/is-empty-array/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an empty array. +* +* @module @stdlib/assert/is-empty-array +* +* @example +* var isEmptyArray = require( '@stdlib/assert/is-empty-array' ); +* +* var bool = isEmptyArray( [] ); +* // returns true +* +* bool = isEmptyArray( [ 1, 2, 3 ] ); +* // returns false +* +* bool = isEmptyArray( {} ); +* // returns false +*/ + +// MODULES // + +var isEmptyArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isEmptyArray; diff --git a/is-empty-array/lib/main.js b/is-empty-array/lib/main.js new file mode 100644 index 00000000..ed273f13 --- /dev/null +++ b/is-empty-array/lib/main.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isArray = require( '@stdlib/assert/is-array' ); + + +// MAIN // + +/** +* Tests if a value is an empty array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an empty array +* +* @example +* var bool = isEmptyArray( [] ); +* // returns true +* +* @example +* var bool = isEmptyArray( [ 1, 2, 3 ] ); +* // returns false +* +* @example +* var bool = isEmptyArray( {} ); +* // returns false +*/ +function isEmptyArray( value ) { + return ( + isArray( value ) && + value.length === 0 + ); +} + + +// EXPORTS // + +module.exports = isEmptyArray; diff --git a/is-empty-array/package.json b/is-empty-array/package.json new file mode 100644 index 00000000..8c0a74c8 --- /dev/null +++ b/is-empty-array/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-empty-array", + "version": "0.0.0", + "description": "Test if a value is an empty array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "array.isarray", + "array", + "empty", + "is", + "isarray", + "type", + "check", + "validate", + "valid", + "test" + ] +} diff --git a/is-empty-array/test/test.js b/is-empty-array/test/test.js new file mode 100644 index 00000000..da4e38c4 --- /dev/null +++ b/is-empty-array/test/test.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-array-constructor */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isEmptyArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isEmptyArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an empty array', function test( t ) { + t.strictEqual( isEmptyArray( [] ), true, 'returns true' ); + t.strictEqual( isEmptyArray( new Array() ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an empty array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined, + [ 1, 2, 3 ], + new Array( 10 ), + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isEmptyArray( values[i] ), false, 'returns false for when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-empty-object/README.md b/is-empty-object/README.md new file mode 100644 index 00000000..95aaf30a --- /dev/null +++ b/is-empty-object/README.md @@ -0,0 +1,78 @@ + + +# isEmptyObject + +> Test if a value is an empty object. + +
+ +## Usage + +```javascript +var isEmptyObject = require( '@stdlib/assert/is-empty-object' ); +``` + +#### isEmptyObject( value ) + +Tests if a value is an empty `object`. + +```javascript +var bool = isEmptyObject( {} ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isEmptyObject = require( '@stdlib/assert/is-empty-object' ); + +var bool = isEmptyObject( {} ); +// returns true + +bool = isEmptyObject( new Object() ); +// returns true + +bool = isEmptyObject( { 'beep': 'boop' } ); +// returns false + +bool = isEmptyObject( [] ); +// returns false +``` + +
+ + + + + + diff --git a/is-empty-object/benchmark/benchmark.js b/is-empty-object/benchmark/benchmark.js new file mode 100644 index 00000000..e06aa38d --- /dev/null +++ b/is-empty-object/benchmark/benchmark.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-object, object-curly-newline, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isEmptyObject = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + {}, + new Object(), + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + { 'beep': 'boop' }, + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEmptyObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-empty-object/docs/repl.txt b/is-empty-object/docs/repl.txt new file mode 100644 index 00000000..66fa1092 --- /dev/null +++ b/is-empty-object/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( value ) + Tests if a value is an empty object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating if a value is an empty object. + + Examples + -------- + > var bool = {{alias}}( {} ) + true + > bool = {{alias}}( { 'beep': 'boop' } ) + false + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-empty-object/docs/types/index.d.ts b/is-empty-object/docs/types/index.d.ts new file mode 100644 index 00000000..0c2d66a6 --- /dev/null +++ b/is-empty-object/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an empty object. +* +* @param value - value to test +* @returns boolean indicating whether value is an empty object +* +* @example +* var bool = isEmptyObject( {} ); +* // returns true +* +* @example +* var bool = isEmptyObject( { 'beep': 'boop' } ); +* // returns false +* +* @example +* var bool = isEmptyObject( [] ); +* // returns false +*/ +declare function isEmptyObject( value: any ): boolean; + + +// EXPORTS // + +export = isEmptyObject; diff --git a/is-empty-object/docs/types/test.ts b/is-empty-object/docs/types/test.ts new file mode 100644 index 00000000..3f4d7553 --- /dev/null +++ b/is-empty-object/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isEmptyObject = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isEmptyObject( {} ); // $ExpectType boolean + isEmptyObject( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isEmptyObject(); // $ExpectError + isEmptyObject( [], 123 ); // $ExpectError +} diff --git a/is-empty-object/examples/index.js b/is-empty-object/examples/index.js new file mode 100644 index 00000000..ff58d125 --- /dev/null +++ b/is-empty-object/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-object, object-curly-newline */ + +'use strict'; + +var isEmptyObject = require( './../lib' ); + +console.log( isEmptyObject( {} ) ); +// => true + +console.log( isEmptyObject( new Object() ) ); +// => true + +console.log( isEmptyObject( { 'beep': 'boop' } ) ); +// => false + +console.log( isEmptyObject( [] ) ); +// => false diff --git a/is-empty-object/lib/index.js b/is-empty-object/lib/index.js new file mode 100644 index 00000000..648cd737 --- /dev/null +++ b/is-empty-object/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an empty object. +* +* @module @stdlib/assert/is-empty-object +* +* @example +* var isEmptyObject = require( '@stdlib/assert/is-empty-object' ); +* +* var bool = isEmptyObject( {} ); +* // returns true +* +* bool = isEmptyObject( { 'beep': 'boop' } ); +* // returns false +* +* bool = isEmptyObject( [] ); +* // returns false +*/ + +// MODULES // + +var isEmptyObject = require( './main.js' ); + + +// EXPORTS // + +module.exports = isEmptyObject; diff --git a/is-empty-object/lib/main.js b/is-empty-object/lib/main.js new file mode 100644 index 00000000..8b1e2f19 --- /dev/null +++ b/is-empty-object/lib/main.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var objectKeys = require( '@stdlib/utils/keys' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); + + +// VARIABLES // + +var FLG = hasSymbolSupport(); + + +// MAIN // + +/** +* Tests if a value is an empty object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an empty object +* +* @example +* var bool = isEmptyObject( {} ); +* // returns true +* +* @example +* var bool = isEmptyObject( { 'beep': 'boop' } ); +* // returns false +* +* @example +* var bool = isEmptyObject( [] ); +* // returns false +*/ +function isEmptyObject( value ) { + if ( !isPlainObject( value ) ) { + return false; + } + if ( objectKeys( value ).length > 0 ) { + return false; + } + if ( FLG && Object.getOwnPropertySymbols( value ).length > 0 ) { + return false; + } + return true; +} + + +// EXPORTS // + +module.exports = isEmptyObject; diff --git a/is-empty-object/package.json b/is-empty-object/package.json new file mode 100644 index 00000000..038e2c7b --- /dev/null +++ b/is-empty-object/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-empty-object", + "version": "0.0.0", + "description": "Test if a value is an empty object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "empty", + "is", + "isobject", + "isempty", + "obj", + "isobj", + "type", + "check", + "validate", + "valid", + "test" + ] +} diff --git a/is-empty-object/test/test.js b/is-empty-object/test/test.js new file mode 100644 index 00000000..ea90b4fc --- /dev/null +++ b/is-empty-object/test/test.js @@ -0,0 +1,83 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-object, object-curly-newline */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var isEmptyObject = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbolSupport() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isEmptyObject, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an empty object', function test( t ) { + t.strictEqual( isEmptyObject( {} ), true, 'returns true' ); + t.strictEqual( isEmptyObject( new Object() ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an empty object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + { 'beep': 'boop' }, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isEmptyObject( values[i] ), false, 'returns false for when provided ' + values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided an object with symbol property keys', opts, function test( t ) { + var sym = Symbol( 'foo' ); + var obj = {}; + + obj[ sym ] = true; + t.strictEqual( isEmptyObject( obj ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-empty-string/README.md b/is-empty-string/README.md new file mode 100644 index 00000000..5f0f387b --- /dev/null +++ b/is-empty-string/README.md @@ -0,0 +1,126 @@ + + +# isEmptyString + +> Test if a value is an empty string. + +
+ +## Usage + +```javascript +var isEmptyString = require( '@stdlib/assert/is-empty-string' ); +``` + +#### isEmptyString( value ) + +Tests if a value is an empty `string`. + + + +```javascript +var bool = isEmptyString( '' ); +// returns true + +bool = isEmptyString( new String( '' ) ); +// returns true +``` + +#### isEmptyString.isPrimitive( value ) + +Tests if a `value` is an empty primitive `string`. + + + +```javascript +var bool = isEmptyString.isPrimitive( '' ); +// returns true + +bool = isEmptyString.isPrimitive( new String( '' ) ); +// returns false +``` + +#### isEmptyString.isObject( value ) + +Tests if a `value` is an empty `String` object. + + + +```javascript +var bool = isEmptyString.isObject( '' ); +// returns false + +bool = isEmptyString.isObject( new String( '' ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isEmptyString = require( '@stdlib/assert/is-empty-string' ); + +var bool = isEmptyString( '' ); +// returns true + +bool = isEmptyString( new String( '' ) ); +// returns true + +bool = isEmptyString( 'beep' ); +// returns false + +bool = isEmptyString( 0 ); +// returns false + +bool = isEmptyString( null ); +// returns false + +bool = isEmptyString( void 0 ); +// returns false + +bool = isEmptyString( {} ); +// returns false + +bool = isEmptyString( [] ); +// returns false + +bool = isEmptyString( function empty() {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-empty-string/benchmark/benchmark.js b/is-empty-string/benchmark/benchmark.js new file mode 100644 index 00000000..356a5b39 --- /dev/null +++ b/is-empty-string/benchmark/benchmark.js @@ -0,0 +1,205 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isEmptyString = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '', + '5', + 5, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEmptyString( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new String( '' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEmptyString( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '', + '5', + 5, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEmptyString.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new String( '' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEmptyString.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '', + '5', + 5, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEmptyString.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new String( '' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEmptyString.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-empty-string/docs/repl.txt b/is-empty-string/docs/repl.txt new file mode 100644 index 00000000..5b12db3d --- /dev/null +++ b/is-empty-string/docs/repl.txt @@ -0,0 +1,70 @@ + +{{alias}}( value ) + Tests if a value is an empty string. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating if a value is an empty string. + + Examples + -------- + > var bool = {{alias}}( '' ) + true + > bool = {{alias}}( new String( '' ) ) + true + > bool = {{alias}}( 'beep' ) + false + > bool = {{alias}}( [] ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is an empty string primitive. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an empty string primitive. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( '' ) + true + > bool = {{alias}}.isPrimitive( new String( '' ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is an empty `String` object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an empty `String` object. + + Examples + -------- + > var bool = {{alias}}.isObject( new String( '' ) ) + true + > bool = {{alias}}.isObject( '' ) + false + + See Also + -------- + diff --git a/is-empty-string/docs/types/index.d.ts b/is-empty-string/docs/types/index.d.ts new file mode 100644 index 00000000..6078f9cc --- /dev/null +++ b/is-empty-string/docs/types/index.d.ts @@ -0,0 +1,117 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isEmptyString` with methods for testing for primitives and objects, respectively. +*/ +interface IsEmptyString { + /** + * Tests if a value is an empty string. + * + * @param value - value to test + * @returns boolean indicating whether value is an empty string + * + * @example + * var bool = isEmptyString( '' ); + * // returns true + * + * @example + * var bool = isEmptyString( 'beep' ); + * // returns false + * + * @example + * var bool = isEmptyString( [] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an empty string primitive. + * + * @param value - value to test + * @returns boolean indicating whether value is an empty string primitive + * + * @example + * var bool = isEmptyString.isPrimitive( '' ); + * // returns true + * + * @example + * var bool = isEmptyString.isPrimitive( 'beep' ); + * // returns false + * + * @example + * var bool = isEmptyString.isPrimitive( [] ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is an empty string object. + * + * @param value - value to test + * @returns boolean indicating whether value is an empty string object + * + * @example + * var bool = isEmptyString.isObject( '' ); + * // returns false + * + * @example + * var bool = isEmptyString.isObject( new String( '' ) ); + * // returns true + * + * @example + * var bool = isEmptyString.isObject( [] ); + * // returns false + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is an empty string. +* +* @param value - value to test +* @returns boolean indicating whether value is an empty string +* +* @example +* var bool = isEmptyString( '' ); +* // returns true +* +* @example +* var bool = isEmptyString( 'beep' ); +* // returns false +* +* @example +* var bool = isEmptyString( [] ); +* // returns false +* +* @example +* var bool = isEmptyString.isPrimitive( '' ); +* // returns true +* +* @example +* var bool = isEmptyString.isObject( '' ); +* // returns false +*/ +declare var isEmptyString: IsEmptyString; + + +// EXPORTS // + +export = isEmptyString; diff --git a/is-empty-string/docs/types/test.ts b/is-empty-string/docs/types/test.ts new file mode 100644 index 00000000..970373bf --- /dev/null +++ b/is-empty-string/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isEmptyString = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isEmptyString( '' ); // $ExpectType boolean + isEmptyString( 'abc' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isEmptyString(); // $ExpectError + isEmptyString( 'abc', 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isEmptyString.isPrimitive( new String( 'abc' ) ); // $ExpectType boolean + isEmptyString.isPrimitive( '' ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isEmptyString.isPrimitive(); // $ExpectError + isEmptyString.isPrimitive( 'abc', 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isEmptyString.isObject( new String( '' ) ); // $ExpectType boolean + isEmptyString.isObject( '' ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isEmptyString.isObject(); // $ExpectError + isEmptyString.isObject( 'abc', 123 ); // $ExpectError +} diff --git a/is-empty-string/examples/index.js b/is-empty-string/examples/index.js new file mode 100644 index 00000000..eee090e7 --- /dev/null +++ b/is-empty-string/examples/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var isEmptyString = require( './../lib' ); + +console.log( isEmptyString( '' ) ); +// => true + +console.log( isEmptyString( new String( '' ) ) ); +// => true + +console.log( isEmptyString( 'beep' ) ); +// => false + +console.log( isEmptyString( 0 ) ); +// => false + +console.log( isEmptyString( null ) ); +// => false + +console.log( isEmptyString( void 0 ) ); +// => false + +console.log( isEmptyString( {} ) ); +// => false + +console.log( isEmptyString( [] ) ); +// => false + +function empty() { + // empty function... +} + +console.log( isEmptyString( empty ) ); +// => false diff --git a/is-empty-string/lib/index.js b/is-empty-string/lib/index.js new file mode 100644 index 00000000..4bd18d25 --- /dev/null +++ b/is-empty-string/lib/index.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an empty string. +* +* @module @stdlib/assert/is-empty-string +* +* @example +* var isEmptyString = require( '@stdlib/assert/is-empty-string' ); +* +* var bool = isEmptyString( '' ); +* // returns true +* +* bool = isEmptyString( 'beep' ); +* // returns false +* +* bool = isEmptyString( [] ); +* // returns false +* +* @example +* var isEmptyString = require( '@stdlib/assert/is-empty-string' ).isObject; +* +* var bool = isEmptyString( new String( '' ) ); +* // returns true +* +* bool = isEmptyString( '' ); +* // returns false +* +* @example +* var isEmptyString = require( '@stdlib/assert/is-empty-string' ).isPrimitive; +* +* var bool = isEmptyString( '' ); +* // returns true +* +* bool = isEmptyString( new String( '' ) ); +* // returns false +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isEmptyString = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isEmptyString, 'isPrimitive', isPrimitive ); +setReadOnly( isEmptyString, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isEmptyString; diff --git a/is-empty-string/lib/main.js b/is-empty-string/lib/main.js new file mode 100644 index 00000000..a9f24c5a --- /dev/null +++ b/is-empty-string/lib/main.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is an empty string. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an empty string +* +* @example +* var bool = isEmptyString( '' ); +* // returns true +* +* @example +* var bool = isEmptyString( 'beep' ); +* // returns false +* +* @example +* var bool = isEmptyString( [] ); +* // returns false +*/ +function isEmptyString( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isEmptyString; diff --git a/is-empty-string/lib/object.js b/is-empty-string/lib/object.js new file mode 100644 index 00000000..4e61aba5 --- /dev/null +++ b/is-empty-string/lib/object.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isObject; + + +// MAIN // + +/** +* Tests if a value is an empty string object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an empty string object +* +* @example +* var bool = isEmptyString( '' ); +* // returns false +* +* @example +* var bool = isEmptyString( new String( '' ) ); +* // returns true +* +* @example +* var bool = isEmptyString( [] ); +* // returns false +*/ +function isEmptyString( value ) { + return ( + isString( value ) && + value.valueOf() === '' + ); +} + + +// EXPORTS // + +module.exports = isEmptyString; diff --git a/is-empty-string/lib/primitive.js b/is-empty-string/lib/primitive.js new file mode 100644 index 00000000..43c61cf3 --- /dev/null +++ b/is-empty-string/lib/primitive.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests if a value is an empty string primitive. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an empty string primitive +* +* @example +* var bool = isEmptyString( '' ); +* // returns true +* +* @example +* var bool = isEmptyString( 'beep' ); +* // returns false +* +* @example +* var bool = isEmptyString( [] ); +* // returns false +*/ +function isEmptyString( value ) { + return ( value === '' ); +} + + +// EXPORTS // + +module.exports = isEmptyString; diff --git a/is-empty-string/package.json b/is-empty-string/package.json new file mode 100644 index 00000000..a26b6be5 --- /dev/null +++ b/is-empty-string/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-empty-string", + "version": "0.0.0", + "description": "Test if a value is an empty string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "empty", + "is", + "isstring", + "string", + "str", + "isstr", + "type", + "check", + "validate", + "valid", + "test" + ] +} diff --git a/is-empty-string/test/test.js b/is-empty-string/test/test.js new file mode 100644 index 00000000..28809ae3 --- /dev/null +++ b/is-empty-string/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isEmptyString = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isEmptyString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for an empty primitive string', function test( t ) { + t.strictEqual( typeof isEmptyString.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for an empty string object', function test( t ) { + t.strictEqual( typeof isEmptyString.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-empty-string/test/test.main.js b/is-empty-string/test/test.main.js new file mode 100644 index 00000000..3c5a1be8 --- /dev/null +++ b/is-empty-string/test/test.main.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isEmptyString = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isEmptyString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an empty string', function test( t ) { + t.strictEqual( isEmptyString( '' ), true, 'returns true' ); + t.strictEqual( isEmptyString( new String( '' ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an empty string', function test( t ) { + var values; + var i; + + values = [ + '5', + new String( '5' ), + 5, + NaN, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isEmptyString( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-empty-string/test/test.object.js b/is-empty-string/test/test.object.js new file mode 100644 index 00000000..9e94c49c --- /dev/null +++ b/is-empty-string/test/test.object.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); +var isEmptyString = require( './../lib/object.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isEmptyString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an empty string object', function test( t ) { + t.strictEqual( isEmptyString( new String( '' ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive string', function test( t ) { + t.strictEqual( isEmptyString( '' ), false, 'returns false' ); + t.end(); +}); + +tape( 'if `Symbol.toStringTag` is supported, the function guards against objects masquerading as `String` objects', opts, function test( t ) { + var mock = { + 'toString': toString, + 'valueOf': valueOf + }; + if ( hasToStringTag() ) { + mock[ Symbol.toStringTag ] = 'String'; + } + t.strictEqual( isEmptyString( mock ), false, 'returns false' ); + t.end(); + + function toString() { + return ''; + } + function valueOf() { + return ''; + } +}); + +tape( 'the function returns `false` if not provided an empty string', function test( t ) { + var values; + var i; + + values = [ + '5', + new String( '5' ), + 5, + NaN, + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isEmptyString( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-empty-string/test/test.primitive.js b/is-empty-string/test/test.primitive.js new file mode 100644 index 00000000..aa77afd4 --- /dev/null +++ b/is-empty-string/test/test.primitive.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isEmptyString = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isEmptyString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an empty primitive string', function test( t ) { + t.strictEqual( isEmptyString( '' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a string object', function test( t ) { + t.strictEqual( isEmptyString( new String( '' ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an empty string', function test( t ) { + var values; + var i; + + values = [ + '5', + new String( '5' ), + 5, + NaN, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isEmptyString( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-enumerable-property-in/README.md b/is-enumerable-property-in/README.md new file mode 100644 index 00000000..89b6811b --- /dev/null +++ b/is-enumerable-property-in/README.md @@ -0,0 +1,153 @@ + + +# isEnumerablePropertyIn + +> Test if an object's own or inherited property is enumerable. + +
+ +## Usage + +```javascript +var isEnumerablePropertyIn = require( '@stdlib/assert/is-enumerable-property-in' ); +``` + +#### isEnumerablePropertyIn( value, property ) + +Returns a `boolean` indicating if a `value` has an enumerable `property`. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var bool; +var obj; + +function Foo() { + this.foo = 'bar'; + return this; +} + +defineProperty( Foo.prototype, 'beep', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': true +}); + +defineProperty( Foo.prototype, 'boop', { + 'configurable': true, + 'enumerable': false, + 'writable': true, + 'value': true +}); + +obj = new Foo(); + +bool = isEnumerablePropertyIn( obj, 'foo' ); +// returns true + +bool = isEnumerablePropertyIn( obj, 'beep' ); +// returns true + +bool = isEnumerablePropertyIn( obj, 'boop' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isEnumerablePropertyIn( 'beep', 'toString' ); + // returns false + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var obj = { + 'null': 'foo' + }; + + var bool = isEnumerablePropertyIn( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isEnumerablePropertyIn = require( '@stdlib/assert/is-enumerable-property-in' ); + +var bool = isEnumerablePropertyIn( { 'a': 'b' }, 'a' ); +// returns true + +bool = isEnumerablePropertyIn( [ 'a' ], 0 ); +// returns true + +bool = isEnumerablePropertyIn( { 'null': false }, null ); +// returns true + +bool = isEnumerablePropertyIn( { '[object Object]': false }, {} ); +// returns true + +bool = isEnumerablePropertyIn( {}, 'toString' ); +// returns false + +bool = isEnumerablePropertyIn( {}, 'hasOwnProperty' ); +// returns false + +bool = isEnumerablePropertyIn( [ 'a' ], 'length' ); +// returns false + +bool = isEnumerablePropertyIn( null, 'a' ); +// returns false + +bool = isEnumerablePropertyIn( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-enumerable-property-in/benchmark/benchmark.js b/is-enumerable-property-in/benchmark/benchmark.js new file mode 100644 index 00000000..9caf2ce1 --- /dev/null +++ b/is-enumerable-property-in/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isEnumerablePropertyIn = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEnumerablePropertyIn( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-enumerable-property-in/docs/repl.txt b/is-enumerable-property-in/docs/repl.txt new file mode 100644 index 00000000..bb04cd64 --- /dev/null +++ b/is-enumerable-property-in/docs/repl.txt @@ -0,0 +1,35 @@ + +{{alias}}( value, property ) + Tests if an object's own or inherited property is enumerable. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own or inherited property is + enumerable. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = true; + > desc.enumerable = false; + > desc.writable = true; + > desc.value = true; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + true + > bool = {{alias}}( obj, 'beep' ) + false + + See Also + -------- + diff --git a/is-enumerable-property-in/docs/types/index.d.ts b/is-enumerable-property-in/docs/types/index.d.ts new file mode 100644 index 00000000..f1983c41 --- /dev/null +++ b/is-enumerable-property-in/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own or inherited property is enumerable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is enumerable +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': true, +* 'enumerable': false, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isEnumerablePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isEnumerablePropertyIn( obj, 'beep' ); +* // returns false +*/ +declare function isEnumerablePropertyIn( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isEnumerablePropertyIn; diff --git a/is-enumerable-property-in/docs/types/test.ts b/is-enumerable-property-in/docs/types/test.ts new file mode 100644 index 00000000..46e94ee4 --- /dev/null +++ b/is-enumerable-property-in/docs/types/test.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isEnumerablePropertyIn = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + isEnumerablePropertyIn( obj, 'hasOwnProperty' ); // $ExpectType boolean + isEnumerablePropertyIn( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isEnumerablePropertyIn(); // $ExpectError + isEnumerablePropertyIn( {} ); // $ExpectError + isEnumerablePropertyIn( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-enumerable-property-in/examples/index.js b/is-enumerable-property-in/examples/index.js new file mode 100644 index 00000000..2de9a551 --- /dev/null +++ b/is-enumerable-property-in/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isEnumerablePropertyIn = require( './../lib' ); + +var bool = isEnumerablePropertyIn( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = isEnumerablePropertyIn( [ 'a' ], 0 ); +console.log( bool ); +// => true + +bool = isEnumerablePropertyIn( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = isEnumerablePropertyIn( { '[object Object]': false }, {} ); +console.log( bool ); +// => true + +bool = isEnumerablePropertyIn( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isEnumerablePropertyIn( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isEnumerablePropertyIn( [ 'a' ], 'length' ); +console.log( bool ); +// => false + +bool = isEnumerablePropertyIn( null, 'a' ); +console.log( bool ); +// => false + +bool = isEnumerablePropertyIn( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-enumerable-property-in/lib/index.js b/is-enumerable-property-in/lib/index.js new file mode 100644 index 00000000..ed9813a5 --- /dev/null +++ b/is-enumerable-property-in/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own or inherited property is enumerable. +* +* @module @stdlib/assert/is-enumerable-property-in +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isEnumerablePropertyIn = require( '@stdlib/assert/is-enumerable-property-in' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': true, +* 'enumerable': false, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isEnumerablePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isEnumerablePropertyIn( obj, 'beep' ); +* // returns false +*/ + +// MODULES // + +var isEnumerablePropertyIn = require( './main.js' ); + + +// EXPORTS // + +module.exports = isEnumerablePropertyIn; diff --git a/is-enumerable-property-in/lib/main.js b/is-enumerable-property-in/lib/main.js new file mode 100644 index 00000000..90725ea6 --- /dev/null +++ b/is-enumerable-property-in/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptorIn = require( '@stdlib/utils/property-descriptor-in' ); + + +// MAIN // + +/** +* Tests if an object's own or inherited property is enumerable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is enumerable +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': true, +* 'enumerable': false, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isEnumerablePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isEnumerablePropertyIn( obj, 'beep' ); +* // returns false +*/ +function isEnumerablePropertyIn( value, property ) { + var desc = propertyDescriptorIn( value, property ); // Note: works for both property names and symbols, which the `in` operator does not. + return ( desc !== null && desc.enumerable === true ); +} + + +// EXPORTS // + +module.exports = isEnumerablePropertyIn; diff --git a/is-enumerable-property-in/package.json b/is-enumerable-property-in/package.json new file mode 100644 index 00000000..60ab3e5f --- /dev/null +++ b/is-enumerable-property-in/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-enumerable-property-in", + "version": "0.0.0", + "description": "Test if an object's own or inherited property is enumerable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "inherited", + "in", + "prototype", + "proto", + "enumerable", + "for-in", + "descriptor", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-enumerable-property-in/test/test.js b/is-enumerable-property-in/test/test.js new file mode 100644 index 00000000..8dd58270 --- /dev/null +++ b/is-enumerable-property-in/test/test.js @@ -0,0 +1,178 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isEnumerablePropertyIn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isEnumerablePropertyIn, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property is enumerable', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isEnumerablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isEnumerablePropertyIn( [ 1, 2, 3 ], '1' ); + t.equal( bool, true, 'returns true' ); + + bool = isEnumerablePropertyIn( [ 1, 2, 3 ], 1 ); + t.equal( bool, true, 'returns true' ); + + bool = isEnumerablePropertyIn( new Foo(), 'bar' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': 'b' + }); + + bool = isEnumerablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if provided an enumerable inherited property', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + defineProperty( Foo.prototype, 'a', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': true + }); + + obj = new Foo(); + + bool = isEnumerablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isEnumerablePropertyIn( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isEnumerablePropertyIn( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to an enumerable property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + defineProperty( obj, 'd', { + 'configurable': true, + 'enumerable': false, + 'writable': true, + 'value': true + }); + + bool = isEnumerablePropertyIn( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isEnumerablePropertyIn( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + + bool = isEnumerablePropertyIn( [ 'a' ], 'length' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a non-enumerable inherited property', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + defineProperty( Foo.prototype, 'bar', { + 'configurable': true, + 'enumerable': false, + 'writable': true, + 'value': true + }); + + obj = new Foo(); + + bool = isEnumerablePropertyIn( obj, 'bar' ); + t.equal( bool, false, 'returns false' ); + + bool = isEnumerablePropertyIn( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isEnumerablePropertyIn( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isEnumerablePropertyIn( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isEnumerablePropertyIn( 'beep', 'length' ); + t.equal( bool, false, 'returns false' ); + + bool = isEnumerablePropertyIn( 'beep', 'toString' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); diff --git a/is-enumerable-property/README.md b/is-enumerable-property/README.md new file mode 100644 index 00000000..b0da4171 --- /dev/null +++ b/is-enumerable-property/README.md @@ -0,0 +1,143 @@ + + +# isEnumerableProperty + +> Test if an object's own property is enumerable. + +
+ +## Usage + +```javascript +var isEnumerableProperty = require( '@stdlib/assert/is-enumerable-property' ); +``` + +#### isEnumerableProperty( value, property ) + +Returns a `boolean` indicating if a `value` has an enumerable `property`. + +```javascript +var value = { + 'beep': 'boop' +}; + +var bool = isEnumerableProperty( value, 'beep' ); +// returns true + +bool = isEnumerableProperty( value, 'constructor' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- In contrast to the native [Object.prototype.propertyIsEnumerable][mdn-object-property-is-enumerable], this function does **not** throw when provided `null` or `undefined`. Instead, the function returns `false`. + + ```javascript + var bool = isEnumerableProperty( null, 'a' ); + // returns false + + bool = isEnumerableProperty( void 0, 'a' ); + // returns false + ``` + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isEnumerableProperty( 'beep', '1' ); + // returns true + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var value = { + 'null': false + }; + var bool = isEnumerableProperty( value, null ); + // returns true + + value = { + '[object Object]': false + }; + bool = isEnumerableProperty( value, {} ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isEnumerableProperty = require( '@stdlib/assert/is-enumerable-property' ); + +var bool = isEnumerableProperty( { 'a': 'b' }, 'a' ); +// returns true + +bool = isEnumerableProperty( [ 'a' ], 0 ); +// returns true + +bool = isEnumerableProperty( [ 'a' ], 'length' ); +// returns false + +bool = isEnumerableProperty( {}, 'toString' ); +// returns false + +bool = isEnumerableProperty( {}, 'hasOwnProperty' ); +// returns false + +bool = isEnumerableProperty( null, 'a' ); +// returns false + +bool = isEnumerableProperty( void 0, 'a' ); +// returns false + +bool = isEnumerableProperty( { 'null': false }, null ); +// returns true + +bool = isEnumerableProperty( { '[object Object]': false }, {} ); +// returns true +``` + +
+ + + + + + diff --git a/is-enumerable-property/benchmark/benchmark.js b/is-enumerable-property/benchmark/benchmark.js new file mode 100644 index 00000000..bdfd33c5 --- /dev/null +++ b/is-enumerable-property/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isEnumerableProperty = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEnumerableProperty( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-enumerable-property/docs/repl.txt b/is-enumerable-property/docs/repl.txt new file mode 100644 index 00000000..0fdb6048 --- /dev/null +++ b/is-enumerable-property/docs/repl.txt @@ -0,0 +1,28 @@ + +{{alias}}( value, property ) + Tests if an object's own property is enumerable. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own property is enumerable. + + Examples + -------- + > var beep = { 'boop': true }; + > var bool = {{alias}}( beep, 'boop' ) + true + > bool = {{alias}}( beep, 'hasOwnProperty' ) + false + + See Also + -------- + diff --git a/is-enumerable-property/docs/types/index.d.ts b/is-enumerable-property/docs/types/index.d.ts new file mode 100644 index 00000000..4867274b --- /dev/null +++ b/is-enumerable-property/docs/types/index.d.ts @@ -0,0 +1,49 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own property is enumerable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is enumerable +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = isEnumerableProperty( beep, 'boop' ); +* // returns true +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = isEnumerableProperty( beep, 'hasOwnProperty' ); +* // returns false +*/ +declare function isEnumerableProperty( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isEnumerableProperty; diff --git a/is-enumerable-property/docs/types/test.ts b/is-enumerable-property/docs/types/test.ts new file mode 100644 index 00000000..a0782b97 --- /dev/null +++ b/is-enumerable-property/docs/types/test.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isEnumerableProperty = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + isEnumerableProperty( obj, 'hasOwnProperty' ); // $ExpectType boolean + isEnumerableProperty( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isEnumerableProperty(); // $ExpectError + isEnumerableProperty( {} ); // $ExpectError + isEnumerableProperty( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-enumerable-property/examples/index.js b/is-enumerable-property/examples/index.js new file mode 100644 index 00000000..17df4384 --- /dev/null +++ b/is-enumerable-property/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isEnumerableProperty = require( './../lib' ); + +var bool = isEnumerableProperty( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = isEnumerableProperty( [ 'a' ], 0 ); +console.log( bool ); +// => true + +bool = isEnumerableProperty( [ 'a' ], 'length' ); +console.log( bool ); +// => false + +bool = isEnumerableProperty( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isEnumerableProperty( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isEnumerableProperty( null, 'a' ); +console.log( bool ); +// => false + +bool = isEnumerableProperty( void 0, 'a' ); +console.log( bool ); +// => false + +bool = isEnumerableProperty( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = isEnumerableProperty( { '[object Object]': false }, {} ); +console.log( bool ); +// => true diff --git a/is-enumerable-property/lib/has_string_enumerability_bug.js b/is-enumerable-property/lib/has_string_enumerability_bug.js new file mode 100644 index 00000000..3dffeb60 --- /dev/null +++ b/is-enumerable-property/lib/has_string_enumerability_bug.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isEnum = require( './native.js' ); + + +// VARIABLES // + +var bool; + + +// FUNCTIONS // + +/** +* Detects whether an environment has a bug where String indices are not detected as "enumerable" properties. Observed in Node v0.10. +* +* @private +* @returns {boolean} boolean indicating whether an environment has the bug +*/ +function detect() { + return !isEnum.call( 'beep', '0' ); +} + + +// MAIN // + +bool = detect(); + + +// EXPORTS // + +module.exports = bool; diff --git a/is-enumerable-property/lib/index.js b/is-enumerable-property/lib/index.js new file mode 100644 index 00000000..6e38d721 --- /dev/null +++ b/is-enumerable-property/lib/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own property is enumerable. +* +* @module @stdlib/assert/is-enumerable-property +* +* @example +* var isEnumerableProperty = require( '@stdlib/assert/is-enumerable-property' ); +* +* var beep = { +* 'boop': true +* }; +* +* var bool = isEnumerableProperty( beep, 'boop' ); +* // returns true +* +* bool = isEnumerableProperty( beep, 'hasOwnProperty' ); +* // returns false +*/ + +// MODULES // + +var isEnumerableProperty = require( './main.js' ); + + +// EXPORTS // + +module.exports = isEnumerableProperty; diff --git a/is-enumerable-property/lib/main.js b/is-enumerable-property/lib/main.js new file mode 100644 index 00000000..73b380f2 --- /dev/null +++ b/is-enumerable-property/lib/main.js @@ -0,0 +1,80 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ); +var isnan = require( '@stdlib/assert/is-nan' ).isPrimitive; +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var isEnum = require( './native.js' ); +var hasStringEnumBug = require( './has_string_enumerability_bug.js' ); + + +// MAIN // + +/** +* Tests if an object's own property is enumerable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is enumerable +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = isEnumerableProperty( beep, 'boop' ); +* // returns true +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = isEnumerableProperty( beep, 'hasOwnProperty' ); +* // returns false +*/ +function isEnumerableProperty( value, property ) { + var bool; + if ( + value === void 0 || + value === null + ) { + return false; + } + bool = isEnum.call( value, property ); + if ( !bool && hasStringEnumBug && isString( value ) ) { + // Note: we only check for indices, as properties attached to a `String` object are properly detected as enumerable above. + property = +property; + return ( + !isnan( property ) && + isInteger( property ) && + property >= 0 && + property < value.length + ); + } + return bool; +} + + +// EXPORTS // + +module.exports = isEnumerableProperty; diff --git a/is-enumerable-property/lib/native.js b/is-enumerable-property/lib/native.js new file mode 100644 index 00000000..143e1662 --- /dev/null +++ b/is-enumerable-property/lib/native.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests if an object's own property is enumerable. +* +* @private +* @name isEnumerableProperty +* @type {Function} +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is enumerable +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = isEnumerableProperty( beep, 'boop' ); +* // returns true +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = isEnumerableProperty( beep, 'hasOwnProperty' ); +* // returns false +*/ +var isEnumerableProperty = Object.prototype.propertyIsEnumerable; + + +// EXPORTS // + +module.exports = isEnumerableProperty; diff --git a/is-enumerable-property/package.json b/is-enumerable-property/package.json new file mode 100644 index 00000000..796e3804 --- /dev/null +++ b/is-enumerable-property/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-enumerable-property", + "version": "0.0.0", + "description": "Test if an object's own property is enumerable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "enumerable", + "enum", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-enumerable-property/test/test.js b/is-enumerable-property/test/test.js new file mode 100644 index 00000000..6d70f893 --- /dev/null +++ b/is-enumerable-property/test/test.js @@ -0,0 +1,157 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var isEnumerableProperty = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isEnumerableProperty, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument (native throws)', function test( t ) { + var bool; + + bool = isEnumerableProperty( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isEnumerableProperty( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `true` if an object property is enumerable', function test( t ) { + var bool; + + function Foo() { + this.bar = 'beep'; + return this; + } + + bool = isEnumerableProperty( { + 'a': 'b' + }, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isEnumerableProperty( [ 1, 2, 3 ], '1' ); + t.equal( bool, true, 'returns true' ); + + bool = isEnumerableProperty( [ 1, 2, 3 ], 1 ); + t.equal( bool, true, 'returns true' ); + + bool = isEnumerableProperty( new Foo(), 'bar' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object property is not enumerable', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + bool = isEnumerableProperty( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isEnumerableProperty( [ 'a' ], 'length' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an inherited property', function test( t ) { + var bool; + + bool = isEnumerableProperty( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isEnumerableProperty( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isEnumerableProperty( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isEnumerableProperty( 'beep', '1' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'addresses `propertyIsEnumerable` bug in Node v0.10 when provided a `string`', function test( t ) { + var isEnumerableProperty; + var bool; + + isEnumerableProperty = proxyquire( './../lib/main.js', { + './has_string_enumerability_bug.js': true + }); + + bool = isEnumerableProperty( 'beep', '1' ); + t.equal( bool, true, 'returns true' ); + + bool = isEnumerableProperty( 'beep', 1 ); + t.equal( bool, true, 'returns true' ); + + bool = isEnumerableProperty( 'beep', 'boop' ); + t.equal( bool, false, 'returns false' ); + + bool = isEnumerableProperty( 'beep', '10' ); + t.equal( bool, false, 'returns false' ); + + bool = isEnumerableProperty( 'beep', 'length' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'property values are coerced to strings', function test( t ) { + var bool; + var obj; + + obj = { + 'null': false + }; + bool = isEnumerableProperty( obj, null ); + t.equal( bool, true, 'returns true' ); + + obj = { + '[object Object]': false + }; + bool = isEnumerableProperty( obj, {} ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/is-error/README.md b/is-error/README.md new file mode 100644 index 00000000..3e773887 --- /dev/null +++ b/is-error/README.md @@ -0,0 +1,123 @@ + + +# isError + +> Test if a value is an [Error][mdn-error] object. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var isError = require( '@stdlib/assert/is-error' ); +``` + +#### isError( value ) + +Tests if a `value` is an [`Error`][mdn-error] object. + +```javascript +var bool = isError( new Error( 'beep' ) ); +// returns true +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var isError = require( '@stdlib/assert/is-error' ); + +var bool = isError( new Error( 'error' ) ); +// returns true + +bool = isError( new EvalError( 'eval error' ) ); +// returns true + +bool = isError( new RangeError( 'range error' ) ); +// returns true + +bool = isError( new ReferenceError( 'reference error' ) ); +// returns true + +bool = isError( new SyntaxError( 'syntax error' ) ); +// returns true + +bool = isError( new TypeError( 'type error' ) ); +// returns true + +bool = isError( new URIError( 'URI error' ) ); +// returns true + +bool = isError( {} ); +// returns false + +bool = isError( null ); +// returns false +``` + +
+ + + + + +
+ +
+ + + + + + + + diff --git a/is-error/benchmark/benchmark.js b/is-error/benchmark/benchmark.js new file mode 100644 index 00000000..a7f7bf2e --- /dev/null +++ b/is-error/benchmark/benchmark.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isError = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + {}, + [], + null, + false, + true, + new Error( 'beep' ), + new TypeError( 'boop' ), + new SyntaxError( 'foo' ) + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isError( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-error/docs/repl.txt b/is-error/docs/repl.txt new file mode 100644 index 00000000..5add8528 --- /dev/null +++ b/is-error/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is an Error object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an Error object. + + Examples + -------- + > var bool = {{alias}}( new Error( 'beep' ) ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-error/docs/types/index.d.ts b/is-error/docs/types/index.d.ts new file mode 100644 index 00000000..949b67f0 --- /dev/null +++ b/is-error/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an `Error` object. +* +* @param value - value to test +* @returns boolean indicating whether a value is an `Error` object +* +* @example +* var bool = isError( new Error( 'beep' ) ); +* // returns true +* +* @example +* var bool = isError( {} ); +* // returns false +*/ +declare function isError( value: any ): boolean; + + +// EXPORTS // + +export = isError; diff --git a/is-error/docs/types/test.ts b/is-error/docs/types/test.ts new file mode 100644 index 00000000..72329c01 --- /dev/null +++ b/is-error/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isError = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isError( new Error( 'beep' ) ); // $ExpectType boolean + isError( {} ); // $ExpectType boolean +} + +// The function does not compile if provided insufficient arguments... +{ + isError(); // $ExpectError + isError( new Error( 'beep' ), 123 ); // $ExpectError +} diff --git a/is-error/examples/index.js b/is-error/examples/index.js new file mode 100644 index 00000000..0b112e8d --- /dev/null +++ b/is-error/examples/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isError = require( './../lib' ); + +var bool = isError( new Error( 'error' ) ); +console.log( bool ); +// => true + +bool = isError( new EvalError( 'eval error' ) ); +console.log( bool ); +// => true + +bool = isError( new RangeError( 'range error' ) ); +console.log( bool ); +// => true + +bool = isError( new ReferenceError( 'reference error' ) ); +console.log( bool ); +// => true + +bool = isError( new SyntaxError( 'syntax error' ) ); +console.log( bool ); +// => true + +bool = isError( new TypeError( 'type error' ) ); +console.log( bool ); +// => true + +bool = isError( new URIError( 'URI error' ) ); +console.log( bool ); +// => true + +bool = isError( {} ); +console.log( bool ); +// => false + +bool = isError( null ); +console.log( bool ); +// => false diff --git a/is-error/lib/index.js b/is-error/lib/index.js new file mode 100644 index 00000000..7a6f14aa --- /dev/null +++ b/is-error/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an `Error` object. +* +* @module @stdlib/assert/is-error +* +* @example +* var isError = require( '@stdlib/assert/is-error' ); +* +* var bool = isError( new Error( 'beep' ) ); +* // returns true +* +* bool = isError( {} ); +* // returns false +*/ + +// MODULES // + +var isError = require( './main.js' ); + + +// EXPORTS // + +module.exports = isError; diff --git a/is-error/lib/main.js b/is-error/lib/main.js new file mode 100644 index 00000000..cfde6f14 --- /dev/null +++ b/is-error/lib/main.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var getPrototypeOf = require( '@stdlib/utils/get-prototype-of' ); +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// MAIN // + +/** +* Tests if a value is an `Error` object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is an `Error` object +* +* @example +* var bool = isError( new Error( 'beep' ) ); +* // returns true +* +* @example +* var bool = isError( {} ); +* // returns false +*/ +function isError( value ) { + if ( typeof value !== 'object' || value === null ) { + return false; + } + // Check for `Error` objects from the same realm (same Node.js `vm` or same `Window` object)... + if ( value instanceof Error ) { + return true; + } + // Walk the prototype tree until we find an object having the desired native class... + while ( value ) { + if ( nativeClass( value ) === '[object Error]' ) { + return true; + } + value = getPrototypeOf( value ); + } + return false; +} + + +// EXPORTS // + +module.exports = isError; diff --git a/is-error/package.json b/is-error/package.json new file mode 100644 index 00000000..e0ab1995 --- /dev/null +++ b/is-error/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-error", + "version": "0.0.0", + "description": "Test if a value is an Error object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "error", + "err", + "exception", + "is", + "iserr", + "iserror", + "valid", + "isvalid", + "type", + "check", + "validate", + "validation", + "test" + ] +} diff --git a/is-error/test/test.js b/is-error/test/test.js new file mode 100644 index 00000000..626ace2c --- /dev/null +++ b/is-error/test/test.js @@ -0,0 +1,115 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var vm = require( 'vm' ); // TODO: handle in-browser tests +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var inherit = require( '@stdlib/utils/inherit' ); +var isError = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': IS_BROWSER +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isError, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an `Error` object', function test( t ) { + var values; + var i; + + values = [ + new Error( 'error' ), + new EvalError( 'eval error' ), + new RangeError( 'range error' ), + new ReferenceError( 'reference error' ), + new SyntaxError( 'syntax error' ), + new TypeError( 'type error' ), + new URIError( 'URI error' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isError( values[ i ] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an object inheriting from `Error`', function test( t ) { + function CustomError( msg ) { + Error.call( this ); + this.name = 'CustomError'; + this.message = msg; + return this; + } + + inherit( CustomError, Error ); + + t.strictEqual( isError( new CustomError( 'custom error' ) ), true, 'returns true when provided a value which inherits from Error' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an `Error` object from a different realm', opts, function test( t ) { + var error = vm.runInNewContext( 'new Error()' ); + t.strictEqual( isError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an object from a different realm which inherits from an `Error` object', opts, function test( t ) { + var error = vm.runInNewContext( 'function Err() { return this; }; Err.prototype = new Error(); new Err();' ); + t.strictEqual( isError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an `Error` object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined, + [], + {}, + function noop() {}, + function error() {}, + new Date(), + new RegExp( '.*' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isError( values[ i ] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-eval-error/README.md b/is-eval-error/README.md new file mode 100644 index 00000000..a2c0ff8a --- /dev/null +++ b/is-eval-error/README.md @@ -0,0 +1,129 @@ + + +# isEvalError + +> Test if a value is an [EvalError][mdn-eval-error] object. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var isEvalError = require( '@stdlib/assert/is-eval-error' ); +``` + +#### isEvalError( value ) + +Tests if a `value` is an [`EvalError`][mdn-eval-error] object. + +```javascript +var bool = isEvalError( new EvalError( 'beep' ) ); +// returns true +``` + +
+ + + + + +
+ +## Notes + +- This function should **not** be considered robust. While the function should **always** return `true` if provided an [`EvalError`][mdn-eval-error] (or a descendant) object, false positives may occur due to the fact that the [`EvalError`][mdn-eval-error] constructor inherits from [`Error`][mdn-error] and has no internal class of its own. Hence, [`EvalError`][mdn-eval-error] impersonation is possible. + +
+ + + + + +
+ +## Examples + + + +```javascript +var isEvalError = require( '@stdlib/assert/is-eval-error' ); + +var bool = isEvalError( new EvalError( 'eval error' ) ); +// returns true + +bool = isEvalError( new Error( 'error' ) ); +// returns false + +bool = isEvalError( new RangeError( 'range error' ) ); +// returns false + +bool = isEvalError( new ReferenceError( 'reference error' ) ); +// returns false + +bool = isEvalError( new SyntaxError( 'syntax error' ) ); +// returns false + +bool = isEvalError( new TypeError( 'type error' ) ); +// returns false + +bool = isEvalError( new URIError( 'URI error' ) ); +// returns false + +bool = isEvalError( {} ); +// returns false + +bool = isEvalError( null ); +// returns false +``` + +
+ + + + + +
+ +
+ + + + + + + + diff --git a/is-eval-error/benchmark/benchmark.js b/is-eval-error/benchmark/benchmark.js new file mode 100644 index 00000000..92e91711 --- /dev/null +++ b/is-eval-error/benchmark/benchmark.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isEvalError = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + {}, + [], + null, + false, + true, + new Error( 'beep' ), + new EvalError( 'bar' ), + new TypeError( 'boop' ), + new SyntaxError( 'foo' ) + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEvalError( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-eval-error/docs/repl.txt b/is-eval-error/docs/repl.txt new file mode 100644 index 00000000..7f552f81 --- /dev/null +++ b/is-eval-error/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( value ) + Tests if a value is an EvalError object. + + This function should *not* be considered robust. While the function should + always return `true` if provided an EvalError (or a descendant) object, + false positives may occur due to the fact that the EvalError constructor + inherits from Error and has no internal class of its own. Hence, EvalError + impersonation is possible. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an EvalError object. + + Examples + -------- + > var bool = {{alias}}( new EvalError( 'beep' ) ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-eval-error/docs/types/index.d.ts b/is-eval-error/docs/types/index.d.ts new file mode 100644 index 00000000..e4171de5 --- /dev/null +++ b/is-eval-error/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an `EvalError` object. +* +* ## Notes +* +* - This function should **not** be considered robust. While the function should always return `true` if provided an EvalError (or a descendant) object, false positives may occur due to the fact that the EvalError constructor inherits from Error and has no internal class of its own. Hence, EvalError impersonation is possible. +* +* @param value - value to test +* @returns boolean indicating whether a value is an `EvalError` object +* +* @example +* var bool = isEvalError( new EvalError( 'beep' ) ); +* // returns true +* +* @example +* var bool = isEvalError( {} ); +* // returns false +*/ +declare function isEvalError( value: any ): boolean; + + +// EXPORTS // + +export = isEvalError; diff --git a/is-eval-error/docs/types/test.ts b/is-eval-error/docs/types/test.ts new file mode 100644 index 00000000..10966be9 --- /dev/null +++ b/is-eval-error/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isEvalError = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isEvalError( new EvalError( 'beep' ) ); // $ExpectType boolean + isEvalError( {} ); // $ExpectType boolean +} + +// The function does not compile if provided insufficient arguments... +{ + isEvalError(); // $ExpectError + isEvalError( new EvalError( 'beep' ), 123 ); // $ExpectError +} diff --git a/is-eval-error/examples/index.js b/is-eval-error/examples/index.js new file mode 100644 index 00000000..6e1e8518 --- /dev/null +++ b/is-eval-error/examples/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isEvalError = require( './../lib' ); + +var bool = isEvalError( new EvalError( 'eval error' ) ); +console.log( bool ); +// => true + +bool = isEvalError( new Error( 'error' ) ); +console.log( bool ); +// => false + +bool = isEvalError( new RangeError( 'range error' ) ); +console.log( bool ); +// => false + +bool = isEvalError( new ReferenceError( 'reference error' ) ); +console.log( bool ); +// => false + +bool = isEvalError( new SyntaxError( 'syntax error' ) ); +console.log( bool ); +// => false + +bool = isEvalError( new TypeError( 'type error' ) ); +console.log( bool ); +// => false + +bool = isEvalError( new URIError( 'URI error' ) ); +console.log( bool ); +// => false + +bool = isEvalError( {} ); +console.log( bool ); +// => false + +bool = isEvalError( null ); +console.log( bool ); +// => false diff --git a/is-eval-error/lib/index.js b/is-eval-error/lib/index.js new file mode 100644 index 00000000..fd820002 --- /dev/null +++ b/is-eval-error/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an `EvalError` object. +* +* @module @stdlib/assert/is-eval-error +* +* @example +* var isEvalError = require( '@stdlib/assert/is-eval-error' ); +* +* var bool = isEvalError( new EvalError( 'beep' ) ); +* // returns true +* +* bool = isEvalError( {} ); +* // returns false +*/ + +// MODULES // + +var isEvalError = require( './main.js' ); + + +// EXPORTS // + +module.exports = isEvalError; diff --git a/is-eval-error/lib/main.js b/is-eval-error/lib/main.js new file mode 100644 index 00000000..4dd3762d --- /dev/null +++ b/is-eval-error/lib/main.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var getPrototypeOf = require( '@stdlib/utils/get-prototype-of' ); +var typeOf = require( '@stdlib/utils/type-of' ); +var isError = require( '@stdlib/assert/is-error' ); + + +// MAIN // + +/** +* Tests if a value is an `EvalError` object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is an `EvalError` object +* +* @example +* var bool = isEvalError( new EvalError( 'beep' ) ); +* // returns true +* +* @example +* var bool = isEvalError( {} ); +* // returns false +*/ +function isEvalError( value ) { + if ( typeof value !== 'object' || value === null ) { + return false; + } + // Check for `EvalError` objects from the same realm (same Node.js `vm` or same `Window` object)... + if ( value instanceof EvalError ) { + return true; + } + // All `EvalError` objects are `Error` objects... + if ( isError( value ) ) { + // Walk the prototype tree until we find the desired constructor... + while ( value ) { + if ( typeOf( value ) === 'evalerror' ) { + return true; + } + value = getPrototypeOf( value ); + } + } + return false; +} + + +// EXPORTS // + +module.exports = isEvalError; diff --git a/is-eval-error/package.json b/is-eval-error/package.json new file mode 100644 index 00000000..fd71eb7b --- /dev/null +++ b/is-eval-error/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-eval-error", + "version": "0.0.0", + "description": "Test if a value is an EvalError object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "eval", + "error", + "err", + "exception", + "is", + "iserr", + "iserror", + "valid", + "isvalid", + "type", + "check", + "validate", + "validation", + "test" + ] +} diff --git a/is-eval-error/test/test.js b/is-eval-error/test/test.js new file mode 100644 index 00000000..e423bea7 --- /dev/null +++ b/is-eval-error/test/test.js @@ -0,0 +1,115 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var vm = require( 'vm' ); // TODO: handle in-browser tests +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var inherit = require( '@stdlib/utils/inherit' ); +var isEvalError = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': IS_BROWSER +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isEvalError, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an `EvalError` object', function test( t ) { + var values; + var i; + + values = [ + new EvalError( 'eval error' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isEvalError( values[ i ] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an object inheriting from `EvalError`', function test( t ) { + function CustomError( msg ) { + EvalError.call( this ); + this.name = 'CustomError'; + this.message = msg; + return this; + } + + inherit( CustomError, EvalError ); + + t.strictEqual( isEvalError( new CustomError( 'custom error' ) ), true, 'returns true when provided a value which inherits from EvalError' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an `EvalError` object from a different realm', opts, function test( t ) { + var error = vm.runInNewContext( 'new EvalError()' ); + t.strictEqual( isEvalError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an object from a different realm which inherits from an `EvalError` object', opts, function test( t ) { + var error = vm.runInNewContext( 'function Err() { return this; }; Err.prototype = new EvalError(); new Err();' ); + t.strictEqual( isEvalError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an `EvalError` object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined, + [], + {}, + function noop() {}, + function error() {}, + new Date(), + new RegExp( '.*' ), + new Error( 'error' ), + new RangeError( 'range error' ), + new ReferenceError( 'reference error' ), + new SyntaxError( 'syntax error' ), + new TypeError( 'type error' ), + new URIError( 'URI error' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isEvalError( values[ i ] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-even/README.md b/is-even/README.md new file mode 100644 index 00000000..c4aa05da --- /dev/null +++ b/is-even/README.md @@ -0,0 +1,139 @@ + + +# isEven + +> Test if a value is an even number. + +
+ +## Usage + +```javascript +var isEven = require( '@stdlib/assert/is-even' ); +``` + +#### isEven( value ) + +Tests if a `value` is an even `number`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isEven( 6.0 ); +// returns true + +bool = isEven( new Number( 6.0 ) ); +// returns true + +bool = isEven( 3.0 ); +// returns false + +bool = isEven( -3.14 ); +// returns false + +bool = isEven( null ); +// returns false + +bool = isEven( NaN ); +// returns false +``` + +#### isEven.isPrimitive( value ) + +Tests if a `value` is a primitive even `number`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isEven.isPrimitive( -4.0 ); +// returns true + +bool = isEven.isPrimitive( new Number( -4.0 ) ); +// returns false +``` + +#### isEven.isObject( value ) + +Tests if a `value` is a `Number` object having an even number value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isEven.isObject( 4.0 ); +// returns false + +bool = isEven.isObject( new Number( 4.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isEven = require( '@stdlib/assert/is-even' ); + +var bool = isEven( 4.0 ); +// returns true + +bool = isEven( 0.0 ); +// returns true + +bool = isEven( new Number( 4 ) ); +// returns true + +bool = isEven( 5.0 ); +// returns false + +bool = isEven( NaN ); +// returns false + +bool = isEven( '5' ); +// returns false + +bool = isEven( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-even/benchmark/benchmark.js b/is-even/benchmark/benchmark.js new file mode 100644 index 00000000..e1e40af9 --- /dev/null +++ b/is-even/benchmark/benchmark.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isEven = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEven( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEven( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEven.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEven.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEven.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isEven.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-even/docs/repl.txt b/is-even/docs/repl.txt new file mode 100644 index 00000000..eb510f82 --- /dev/null +++ b/is-even/docs/repl.txt @@ -0,0 +1,74 @@ + +{{alias}}( value ) + Tests if a value is an even number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether is an even number. + + Examples + -------- + > var bool = {{alias}}( 4.0 ) + true + > bool = {{alias}}( new Number( 4.0 ) ) + true + > bool = {{alias}}( 3.0 ) + false + > bool = {{alias}}( -3.14 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive that is an even number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive that is an even + number. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( -4.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( -4.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object that is an even number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object that is an even + number. + + Examples + -------- + > var bool = {{alias}}.isObject( 4.0 ) + false + > bool = {{alias}}.isObject( new Number( 4.0 ) ) + true + + See Also + -------- + diff --git a/is-even/docs/types/index.d.ts b/is-even/docs/types/index.d.ts new file mode 100644 index 00000000..6391e309 --- /dev/null +++ b/is-even/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isEven` with methods for testing for primitives and objects, respectively. +*/ +interface IsEven { + /** + * Tests if a value is an even number. + * + * @param value - value to test + * @returns boolean indicating whether value is an even number + * + * @example + * var bool = isEven( 4.0 ); + * // returns true + * + * @example + * var bool = isEven( new Number( 4.0 ) ); + * // returns true + * + * @example + * var bool = isEven( 3.0 ); + * // returns false + * + * @example + * var bool = isEven( new Number( 5.0 ) ); + * // returns false + * + * @example + * var bool = isEven( -3.14 ); + * // returns false + * + * @example + * var bool = isEven( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive that is an even number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive that is an even number + * + * @example + * var bool = isEven.isPrimitive( -4.0 ); + * // returns true + * + * @example + * var bool = isEven.isPrimitive( new Number( -4.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object that is an even number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object that is an even number + * + * @example + * var bool = isEven.isObject( 4.0 ); + * // returns false + * + * @example + * var bool = isEven.isObject( new Number( 4.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is an even number. +* +* @param value - value to test +* @returns boolean indicating whether value is an even number +* +* @example +* var bool = isEven( 4.0 ); +* // returns true +* +* @example +* var bool = isEven( new Number( 4.0 ) ); +* // returns true +* +* @example +* var bool = isEven( 3.0 ); +* // returns false +* +* @example +* var bool = isEven.isPrimitive( new Number( 4.0 ) ); +* // returns false +* +* @example +* var bool = isEven.isObject( 4.0 ); +* // returns false +* +* @example +* var bool = isEven( null ); +* // returns false +*/ +declare var isEven: IsEven; + + +// EXPORTS // + +export = isEven; diff --git a/is-even/docs/types/test.ts b/is-even/docs/types/test.ts new file mode 100644 index 00000000..baea2397 --- /dev/null +++ b/is-even/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isEven = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isEven( 3 ); // $ExpectType boolean + isEven( 4 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isEven(); // $ExpectError + isEven( 4, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isEven.isPrimitive( new Number( 4 ) ); // $ExpectType boolean + isEven.isPrimitive( 4 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isEven.isPrimitive(); // $ExpectError + isEven.isPrimitive( 4, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isEven.isObject( new Number( 4 ) ); // $ExpectType boolean + isEven.isObject( 4 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isEven.isObject(); // $ExpectError + isEven.isObject( 4, 123 ); // $ExpectError +} diff --git a/is-even/examples/index.js b/is-even/examples/index.js new file mode 100644 index 00000000..355c63d2 --- /dev/null +++ b/is-even/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isEven = require( './../lib' ); + +console.log( isEven( 4.0 ) ); +// => true + +console.log( isEven( 0.0 ) ); +// => true + +console.log( isEven( new Number( 4 ) ) ); +// => true + +console.log( isEven( 5.0 ) ); +// => false + +console.log( isEven( NaN ) ); +// => false + +console.log( isEven( '5' ) ); +// => false + +console.log( isEven( null ) ); +// => false diff --git a/is-even/lib/index.js b/is-even/lib/index.js new file mode 100644 index 00000000..f829cd31 --- /dev/null +++ b/is-even/lib/index.js @@ -0,0 +1,84 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an even number. +* +* @module @stdlib/assert/is-even +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ); +* +* var bool = isEven( 4.0 ); +* // returns true +* +* bool = isEven( new Number( 4.0 ) ); +* // returns true +* +* bool = isEven( 3.0 ); +* // returns false +* +* bool = isEven( new Number( 5.0 ) ); +* // returns false +* +* bool = isEven( -3.14 ); +* // returns false +* +* bool = isEven( null ); +* // returns false +* +* @example +* // Use interface to check for even number primitives... +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* +* var bool = isEven( -4.0 ); +* // returns true +* +* bool = isEven( new Number( -4.0 ) ); +* // returns false +* +* @example +* // Use interface to check for even number objects... +* var isEven = require( '@stdlib/assert/is-even' ).isObject; +* +* var bool = isEven( 4.0 ); +* // returns false +* +* bool = isEven( new Number( 4.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isEven = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isEven, 'isPrimitive', isPrimitive ); +setReadOnly( isEven, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isEven; diff --git a/is-even/lib/main.js b/is-even/lib/main.js new file mode 100644 index 00000000..99691091 --- /dev/null +++ b/is-even/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is an even number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an even number. +* +* @example +* var bool = isEven( 4.0 ); +* // returns true +* +* @example +* var bool = isEven( new Number( 4.0 ) ); +* // returns true +* +* @example +* var bool = isEven( 3.0 ); +* // returns false +* +* @example +* var bool = isEven( new Number( 5.0 ) ); +* // returns false +* +* @example +* var bool = isEven( -3.14 ); +* // returns false +* +* @example +* var bool = isEven( null ); +* // returns false +*/ +function isEven( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isEven; diff --git a/is-even/lib/object.js b/is-even/lib/object.js new file mode 100644 index 00000000..6d82af69 --- /dev/null +++ b/is-even/lib/object.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isObject; + + +// MAIN // + +/** +* Tests if a value is a number object that is an even number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object that is an even number +* +* @example +* var bool = isEven( 4.0 ); +* // returns false +* +* @example +* var bool = isEven( new Number( 4.0 ) ); +* // returns true +*/ +function isEven( value ) { + return ( + isInteger( value ) && + value % 2 === 0 + ); +} + + +// EXPORTS // + +module.exports = isEven; diff --git a/is-even/lib/primitive.js b/is-even/lib/primitive.js new file mode 100644 index 00000000..e69b7774 --- /dev/null +++ b/is-even/lib/primitive.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a number primitive that is an even number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive that is an even number +* +* @example +* var bool = isEven( -4.0 ); +* // returns true +* +* @example +* var bool = isEven( new Number( -4.0 ) ); +* // returns false +*/ +function isEven( value ) { + return ( + isInteger( value ) && + value % 2 === 0 + ); +} + + +// EXPORTS // + +module.exports = isEven; diff --git a/is-even/package.json b/is-even/package.json new file mode 100644 index 00000000..8c35d282 --- /dev/null +++ b/is-even/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-even", + "version": "0.0.0", + "description": "Test if a value is an even number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "even", + "is", + "iseven", + "type", + "check", + "primitive", + "object", + "validate", + "valid", + "test" + ] +} diff --git a/is-even/test/test.js b/is-even/test/test.js new file mode 100644 index 00000000..3f01cbf2 --- /dev/null +++ b/is-even/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isEven = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isEven, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive even number', function test( t ) { + t.equal( typeof isEven.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having an even value', function test( t ) { + t.equal( typeof isEven.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-even/test/test.main.js b/is-even/test/test.main.js new file mode 100644 index 00000000..12a1a666 --- /dev/null +++ b/is-even/test/test.main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isEven = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isEven, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an even number', function test( t ) { + t.equal( isEven( 4 ), true, 'returns true' ); + t.equal( isEven( new Number( 4 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided an even number', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.0, + 3.14, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isEven( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-even/test/test.object.js b/is-even/test/test.object.js new file mode 100644 index 00000000..20cea3b4 --- /dev/null +++ b/is-even/test/test.object.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isEven = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isEven, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an even number object', function test( t ) { + t.equal( isEven( new Number( 4 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is even', function test( t ) { + t.equal( isEven( 4 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an even number', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + null, + true, + undefined, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isEven( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-even/test/test.primitive.js b/is-even/test/test.primitive.js new file mode 100644 index 00000000..aab4323b --- /dev/null +++ b/is-even/test/test.primitive.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isEven = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isEven, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive even number', function test( t ) { + t.equal( isEven( -4.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number is even', function test( t ) { + t.equal( isEven( new Number( 4.0 ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an even number', function test( t ) { + var values; + var i; + + values = [ + '4', + 5.0, + null, + true, + undefined, + [], + {}, + function noop() {}, + Number.POSITIVE_INFINITY, + Number.NEGATIVE_INFINITY, + NaN + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isEven( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-falsy-array/README.md b/is-falsy-array/README.md new file mode 100644 index 00000000..3e8c967b --- /dev/null +++ b/is-falsy-array/README.md @@ -0,0 +1,82 @@ + + +# isFalsyArray + +> Test if a value is an array-like object containing only falsy values. + +
+ +## Usage + +```javascript +var isFalsyArray = require( '@stdlib/assert/is-falsy-array' ); +``` + +#### isFalsyArray( value ) + +Tests if a `value` is an array-like `object` containing only falsy values. + +```javascript +var bool = isFalsyArray( [ '', null, void 0, false, 0, NaN ] ); +// returns true + +bool = isFalsyArray( [ [], {} ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isFalsyArray = require( '@stdlib/assert/is-falsy-array' ); + +var bool = isFalsyArray( [ null, '', NaN, 0, void 0, false ] ); +// returns true + +bool = isFalsyArray( [ [], {}, true, 'a', 5, -5 ] ); +// returns false + +bool = isFalsyArray( [] ); +// returns false + +bool = isFalsyArray( false ); +// returns false + +bool = isFalsyArray( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-falsy-array/benchmark/benchmark.js b/is-falsy-array/benchmark/benchmark.js new file mode 100644 index 00000000..167f47fb --- /dev/null +++ b/is-falsy-array/benchmark/benchmark.js @@ -0,0 +1,102 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isFalsyArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( 0 ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i; + bool = isFalsyArray( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/is-falsy-array/docs/repl.txt b/is-falsy-array/docs/repl.txt new file mode 100644 index 00000000..25a18787 --- /dev/null +++ b/is-falsy-array/docs/repl.txt @@ -0,0 +1,27 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only falsy values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an array-like object containing + only falsy values. + + Examples + -------- + > var bool = {{alias}}( [ null, '' ] ) + true + > bool = {{alias}}( [ {}, [] ] ) + false + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-falsy-array/docs/types/index.d.ts b/is-falsy-array/docs/types/index.d.ts new file mode 100644 index 00000000..160d07f6 --- /dev/null +++ b/is-falsy-array/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Test if a value is an array-like object containing only falsy values. +* +* @param value - value to test +* @returns boolean indicating whether a value is an array-like object containing only falsy values +* +* @example +* var bool = isFalsyArray( [ false, null, void 0, '', 0, NaN ] ); +* // returns true +* +* bool = isFalsyArray( [ {}, [] ] ); +* // returns false +* +* bool = isFalsyArray( [] ); +* // returns false +*/ +declare function isFalsyArray( value: any ): boolean; + + +// EXPORTS // + +export = isFalsyArray; diff --git a/is-falsy-array/docs/types/test.ts b/is-falsy-array/docs/types/test.ts new file mode 100644 index 00000000..cebd1743 --- /dev/null +++ b/is-falsy-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFalsyArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFalsyArray( [ null, undefined ] ); // $ExpectType boolean + isFalsyArray( false ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isFalsyArray(); // $ExpectError + isFalsyArray( undefined, 123 ); // $ExpectError +} diff --git a/is-falsy-array/examples/index.js b/is-falsy-array/examples/index.js new file mode 100644 index 00000000..1a2d93cc --- /dev/null +++ b/is-falsy-array/examples/index.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isFalsyArray = require( './../lib' ); + +var bool = isFalsyArray( [ null, '', NaN, 0, void 0, false ] ); +console.log( bool ); +// => true + +bool = isFalsyArray( [ [], {}, true, 'a', 5, -5 ] ); +console.log( bool ); +// => false + +bool = isFalsyArray( [] ); +console.log( bool ); +// => false + +bool = isFalsyArray( false ); +console.log( bool ); +// => false + +bool = isFalsyArray( null ); +console.log( bool ); +// => false diff --git a/is-falsy-array/lib/index.js b/is-falsy-array/lib/index.js new file mode 100644 index 00000000..6b5a97f5 --- /dev/null +++ b/is-falsy-array/lib/index.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only falsy values. +* +* @module @stdlib/assert/is-falsy-array +* +* @example +* var isFalsyArray = require( '@stdlib/assert/is-falsy-array' ); +* +* var bool = isFalsyArray( [ false, null, void 0, '', 0, NaN ] ); +* // returns true +* +* bool = isFalsyArray( [ {}, [] ] ); +* // returns false +* +* bool = isFalsyArray( [] ); +* // returns false +*/ + +// MODULES // + +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); +var isFalsy = require( '@stdlib/assert/is-falsy' ); + + +// MAIN // + +var isFalsyArray = arrayfun( isFalsy ); + + +// EXPORTS // + +module.exports = isFalsyArray; diff --git a/is-falsy-array/package.json b/is-falsy-array/package.json new file mode 100644 index 00000000..118d106c --- /dev/null +++ b/is-falsy-array/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-falsy-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only falsy values.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "falsy", + "falsey", + "truthy", + "array", + "is", + "isfalsy", + "isfalsey", + "isarray", + "array-like", + "typed-array", + "typed array", + "collection", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-falsy-array/test/test.js b/is-falsy-array/test/test.js new file mode 100644 index 00000000..0c54186a --- /dev/null +++ b/is-falsy-array/test/test.js @@ -0,0 +1,95 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isFalsyArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isFalsyArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an array-like object containing only "falsy" values (array)', function test( t ) { + var bool; + var arr; + + arr = [ false, null, void 0, '', 0, NaN ]; + bool = isFalsyArray( arr ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if provided an array-like object containing only "falsy" values (object)', function test( t ) { + var bool; + var arr; + + arr = { + 'length': 6, + '0': false, + '1': null, + '2': void 0, + '3': '', + '4': 0, + '5': NaN + }; + bool = isFalsyArray( arr ); + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an array-like object containing only "falsy" values', function test( t ) { + var bool; + var arr; + + arr = []; + bool = isFalsyArray( arr ); + t.strictEqual( bool, false, 'returns false when provided an empty array' ); + + arr = { + 'length': 0 + }; + bool = isFalsyArray( arr ); + t.strictEqual( bool, false, 'returns false when provided an empty array-like object' ); + + bool = isFalsyArray( null ); + t.strictEqual( bool, false, 'returns false when provided null' ); + + bool = isFalsyArray( false ); + t.strictEqual( bool, false, 'returns false when provided false' ); + + bool = isFalsyArray( '' ); + t.strictEqual( bool, false, 'returns false when provided an empty string' ); + + bool = isFalsyArray( noop ); + t.strictEqual( bool, false, 'returns false when provided an empty function' ); + + t.end(); + + function noop() { + // no-op... + } +}); diff --git a/is-falsy/README.md b/is-falsy/README.md new file mode 100644 index 00000000..d2f671d6 --- /dev/null +++ b/is-falsy/README.md @@ -0,0 +1,120 @@ + + +# isFalsy + +> Test if a value is falsy. + +
+ +## Usage + +```javascript +var isFalsy = require( '@stdlib/assert/is-falsy' ); +``` + +#### isFalsy( value ) + +Tests if a `value` is a value which translates to `false` when evaluated in a boolean context. + +```javascript +var bool = isFalsy( false ); +// returns true + +bool = isFalsy( '' ); +// returns true + +bool = isFalsy( 0 ); +// returns true + +bool = isFalsy( NaN ); +// returns true + +bool = isFalsy( null ); +// returns true + +bool = isFalsy( void 0 ); +// returns true + +bool = isFalsy( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isFalsy = require( '@stdlib/assert/is-falsy' ); + +var bool = isFalsy( false ); +// returns true + +bool = isFalsy( 0 ); +// returns true + +bool = isFalsy( NaN ); +// returns true + +bool = isFalsy( '' ); +// returns true + +bool = isFalsy( void 0 ); +// returns true + +bool = isFalsy( null ); +// returns true + +bool = isFalsy( 'beep' ); +// returns false + +bool = isFalsy( 5 ); +// returns false + +bool = isFalsy( true ); +// returns false + +bool = isFalsy( [] ); +// returns false + +bool = isFalsy( {} ); +// returns false + +bool = isFalsy( function foo() {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-falsy/benchmark/benchmark.js b/is-falsy/benchmark/benchmark.js new file mode 100644 index 00000000..e9ecdf1e --- /dev/null +++ b/is-falsy/benchmark/benchmark.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isFalsy = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '', + '5', + 0, + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + new RegExp( '.*' ), + new Date(), + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFalsy( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-falsy/docs/repl.txt b/is-falsy/docs/repl.txt new file mode 100644 index 00000000..f55e52fa --- /dev/null +++ b/is-falsy/docs/repl.txt @@ -0,0 +1,37 @@ + +{{alias}}( value ) + Tests if a value is a value which translates to `false` when evaluated in a + boolean context. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is falsy. + + Examples + -------- + > var bool = {{alias}}( false ) + true + > bool = {{alias}}( '' ) + true + > bool = {{alias}}( 0 ) + true + > bool = {{alias}}( null ) + true + > bool = {{alias}}( void 0 ) + true + > bool = {{alias}}( NaN ) + true + > bool = {{alias}}( {} ) + false + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-falsy/docs/types/index.d.ts b/is-falsy/docs/types/index.d.ts new file mode 100644 index 00000000..9c76b41f --- /dev/null +++ b/is-falsy/docs/types/index.d.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is falsy. +* +* @param value - value to test +* @returns boolean indicating whether a value is falsy +* +* @example +* var bool = isFalsy( false ); +* // returns true +* +* @example +* var bool = isFalsy( null ); +* // returns true +* +* @example +* var bool = isFalsy( '' ); +* // returns true +* +* @example +* var bool = isFalsy( 0 ); +* // returns true +* +* @example +* var bool = isFalsy( void 0 ); +* // returns true +* +* @example +* var bool = isFalsy( NaN ); +* // returns true +* +* @example +* var bool = isFalsy( [] ); +* // returns false +*/ +declare function isFalsy( value: any ): boolean; + + +// EXPORTS // + +export = isFalsy; diff --git a/is-falsy/docs/types/test.ts b/is-falsy/docs/types/test.ts new file mode 100644 index 00000000..f4e1628e --- /dev/null +++ b/is-falsy/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFalsy = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFalsy( true ); // $ExpectType boolean + isFalsy( false ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isFalsy(); // $ExpectError + isFalsy( undefined, 123 ); // $ExpectError +} diff --git a/is-falsy/examples/index.js b/is-falsy/examples/index.js new file mode 100644 index 00000000..cc1c9e68 --- /dev/null +++ b/is-falsy/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function, no-restricted-syntax */ + +'use strict'; + +var isFalsy = require( './../lib' ); + +console.log( isFalsy( false ) ); +// => true + +console.log( isFalsy( 0 ) ); +// => true + +console.log( isFalsy( NaN ) ); +// => true + +console.log( isFalsy( '' ) ); +// => true + +console.log( isFalsy( void 0 ) ); +// => true + +console.log( isFalsy( null ) ); +// => true + +console.log( isFalsy( 'beep' ) ); +// => false + +console.log( isFalsy( 5 ) ); +// => false + +console.log( isFalsy( true ) ); +// => false + +console.log( isFalsy( [] ) ); +// => false + +console.log( isFalsy( {} ) ); +// => false + +console.log( isFalsy( function foo() {} ) ); +// => false diff --git a/is-falsy/lib/index.js b/is-falsy/lib/index.js new file mode 100644 index 00000000..9603753a --- /dev/null +++ b/is-falsy/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is falsy. +* +* @module @stdlib/assert/is-falsy +* +* @example +* var isFalsy = require( '@stdlib/assert/is-falsy' ); +* +* var bool = isFalsy( false ); +* // returns true +* +* bool = isFalsy( null ); +* // returns true +* +* bool = isFalsy( '' ); +* // returns true +* +* bool = isFalsy( 0 ); +* // returns true +* +* bool = isFalsy( void 0 ); +* // returns true +* +* bool = isFalsy( NaN ); +* // returns true +* +* bool = isFalsy( [] ); +* // returns false +*/ + +// MODULES // + +var isFalsy = require( './main.js' ); + + +// EXPORTS // + +module.exports = isFalsy; diff --git a/is-falsy/lib/main.js b/is-falsy/lib/main.js new file mode 100644 index 00000000..748d7a6d --- /dev/null +++ b/is-falsy/lib/main.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests if a value is falsy. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is falsy +* +* @example +* var bool = isFalsy( false ); +* // returns true +* +* @example +* var bool = isFalsy( null ); +* // returns true +* +* @example +* var bool = isFalsy( '' ); +* // returns true +* +* @example +* var bool = isFalsy( 0 ); +* // returns true +* +* @example +* var bool = isFalsy( void 0 ); +* // returns true +* +* @example +* var bool = isFalsy( NaN ); +* // returns true +* +* @example +* var bool = isFalsy( [] ); +* // returns false +*/ +function isFalsy( value ) { + return ( value ) ? false : true; // eslint-disable-line no-unneeded-ternary +} + + +// EXPORTS // + +module.exports = isFalsy; diff --git a/is-falsy/package.json b/is-falsy/package.json new file mode 100644 index 00000000..c59f9aa5 --- /dev/null +++ b/is-falsy/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-falsy", + "version": "0.0.0", + "description": "Test if a value is falsy.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "falsy", + "falsey", + "truthy", + "is", + "isfalsy", + "isfalsey", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-falsy/test/test.js b/is-falsy/test/test.js new file mode 100644 index 00000000..ac525439 --- /dev/null +++ b/is-falsy/test/test.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isFalsy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isFalsy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a "falsy" value', function test( t ) { + t.strictEqual( isFalsy( false ), true, 'returns true' ); + t.strictEqual( isFalsy( '' ), true, 'returns true' ); + t.strictEqual( isFalsy( 0 ), true, 'returns true' ); + t.strictEqual( isFalsy( NaN ), true, 'returns true' ); + t.strictEqual( isFalsy( null ), true, 'returns true' ); + t.strictEqual( isFalsy( void 0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a "falsy" value', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + true, + [], + {}, + function noop() {}, + new Date(), + /.*/ + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isFalsy( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-finite-array/README.md b/is-finite-array/README.md new file mode 100644 index 00000000..b2a0e8e0 --- /dev/null +++ b/is-finite-array/README.md @@ -0,0 +1,133 @@ + + +# isFiniteArray + +> Test if a value is an array-like object containing only finite numbers. + +
+ +## Usage + +```javascript +var isFiniteArray = require( '@stdlib/assert/is-finite-array' ); +``` + +#### isFiniteArray( value ) + +Tests if a `value` is an array-like object containing **only** finite `numbers`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isFiniteArray( [ -3.0, new Number(3.0) ] ); +// returns true + +bool = isFiniteArray( [ -3.0, 'abc' ] ); +// returns false + +bool = isFiniteArray( [ -3.0, 1.0/0.0 ] ); +// returns false +``` + +#### isFiniteArray.primitives( value ) + +Tests if a `value` is an array-like object containing **only** primitive finite `numbers`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isFiniteArray.primitives( [ -1.0, 0.0, 4.5 ] ); +// returns true + +bool = isFiniteArray.primitives( [ -1.0, 1.0/0.0 ] ); +// returns false + +bool = isFiniteArray.primitives( [ -3.0, new Number(2.0) ] ); +// returns false +``` + +#### isFiniteArray.objects( value ) + +Tests if a `value` is an array-like object containing **only** `Number` objects having finite values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isFiniteArray.objects( [ new Number(-1.0), new Number(2.0) ] ); +// returns true + +bool = isFiniteArray.objects( [ -1.0, 0.0, 1.0 ] ); +// returns false + +bool = isFiniteArray.objects( [ -3.0, new Number(1.0) ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isFiniteArray = require( '@stdlib/assert/is-finite-array' ); + +var bool = isFiniteArray( [ -5.0, 0.0, 2.0, 5.0 ] ); +// returns true + +bool = isFiniteArray( [ -4.0, -3.14, 1.0, 3.14 ] ); +// returns true + +bool = isFiniteArray( [ -1.0, new Number( -6.0 ), 2.0 ] ); +// returns true + +bool = isFiniteArray( [ -1.0, 'abc', 3.0 ] ); +// returns false + +bool = isFiniteArray( [ -2.3, 1.0/0.0, 3.0 ] ); +// returns false + +bool = isFiniteArray( [] ); +// returns false +``` + +
+ + + + + + diff --git a/is-finite-array/benchmark/benchmark.js b/is-finite-array/benchmark/benchmark.js new file mode 100644 index 00000000..428cb0e3 --- /dev/null +++ b/is-finite-array/benchmark/benchmark.js @@ -0,0 +1,120 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isFiniteArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( 1.0 ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( 1.0 ) ); // eslint-disable-line no-new-wrappers + } + } + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = 1.0/0.0; + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x[ len-2 ] = i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isFiniteArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isFiniteArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isFiniteArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-finite-array/docs/repl.txt b/is-finite-array/docs/repl.txt new file mode 100644 index 00000000..420bea9b --- /dev/null +++ b/is-finite-array/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is an array-like object of finite numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an array-like object of finite + numbers. + + Examples + -------- + > var bool = {{alias}}( [ -3.0, new Number(0.0), 2.0 ] ) + true + > bool = {{alias}}( [ -3.0, 1.0/0.0 ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only primitive finite + numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an array-like object containing + only primitive finite numbers. + + Examples + -------- + > var bool = {{alias}}.primitives( [ -1.0, 10.0 ] ) + true + > bool = {{alias}}.primitives( [ -1.0, 0.0, 5.0 ] ) + true + > bool = {{alias}}.primitives( [ -3.0, new Number(-1.0) ] ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only number objects + having finite values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an array-like object containing + only number objects having finite values. + + Examples + -------- + > var bool = {{alias}}.objects( [ new Number(1.0), new Number(3.0) ] ) + true + > bool = {{alias}}.objects( [ -1.0, 0.0, 3.0 ] ) + false + > bool = {{alias}}.objects( [ 3.0, new Number(-1.0) ] ) + false + + See Also + -------- + diff --git a/is-finite-array/docs/types/index.d.ts b/is-finite-array/docs/types/index.d.ts new file mode 100644 index 00000000..ec1e48a8 --- /dev/null +++ b/is-finite-array/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isFiniteArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsFiniteArray { + /** + * Tests if a value is an array-like object of finite numbers. + * + * @param value - value to test + * @returns boolean indicating whether a value is an array-like object of finite numbers + * + * @example + * var bool = isFiniteArray( [ -3.0, new Number(0.0), 2.0 ] ); + * // returns true + * + * @example + * var bool = isFiniteArray( [ -3.0, 1.0/0.0 ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only primitive finite numbers. + * + * @param value - value to test + * @returns boolean indicating whether a value is an array-like object containing only primitive finite numbers + * + * @example + * var bool = isFiniteArray.primitives( [ -1.0, 10.0 ] ); + * // returns true + * + * @example + * var bool = isFiniteArray.primitives( [ -1.5, 0.0, 5.0 ] ); + * // returns true + * + * @example + * var bool = isFiniteArray.primitives( [ -3.0, new Number(-1.0) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects having finite values. + * + * @param value - value to test + * @returns boolean indicating whether a value is an array-like object containing only number objects having finite values + * + * @example + * var bool = isFiniteArray.objects( [ new Number(1.0), new Number(3.0) ] ); + * // returns true + * + * @example + * var bool = isFiniteArray.objects( [ -1.0, 0.0, 3.0 ] ); + * // returns false + * + * @example + * var bool = isFiniteArray.objects( [ 3.0, new Number(-1.0) ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object of finite numbers. +* +* @param value - value to test +* @returns boolean indicating whether a value is an array-like object of finite numbers +* +* @example +* var bool = isFiniteArray( [ -3.0, new Number(0.0), 2.0 ] ); +* // returns true +* +* @example +* var bool = isFiniteArray( [ -3.0, 1.0/0.0 ] ); +* // returns false +* +* @example +* var bool = isFiniteArray.primitives( [ -1.0, 10.0 ] ); +* // returns true +* +* @example +* var bool = isFiniteArray.primitives( [ -1.5, 0.0, 5.0 ] ); +* // returns true +* +* @example +* var bool = isFiniteArray.primitives( [ -3.0, new Number(-1.0) ] ); +* // returns false +* +* @example +* var bool = isFiniteArray.objects( [ new Number(1.0), new Number(3.0) ] ); +* // returns true +* +* @example +* var bool = isFiniteArray.objects( [ -1.0, 0.0, 3.0 ] ); +* // returns false +* +* @example +* var bool = isFiniteArray.objects( [ 3.0, new Number(-1.0) ] ); +* // returns false +*/ +declare var isFiniteArray: IsFiniteArray; + + +// EXPORTS // + +export = isFiniteArray; diff --git a/is-finite-array/docs/types/test.ts b/is-finite-array/docs/types/test.ts new file mode 100644 index 00000000..c28f3cdb --- /dev/null +++ b/is-finite-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFiniteArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFiniteArray( [ 4 ] ); // $ExpectType boolean + isFiniteArray( [ 2.8, 1 / 0 ] ); // $ExpectType boolean + isFiniteArray( [ '2.8' ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isFiniteArray(); // $ExpectError + isFiniteArray( [ -3 ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isFiniteArray.primitives( [ new Number( 3 ) ] ); // $ExpectType boolean + isFiniteArray.primitives( [ 3 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isFiniteArray.primitives(); // $ExpectError + isFiniteArray.primitives( [ 2 ], 123 ); // $ExpectError +} + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isFiniteArray.objects( [ new Number( -2 ) ] ); // $ExpectType boolean + isFiniteArray.objects( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isFiniteArray.objects(); // $ExpectError + isFiniteArray.objects( [ 2 ], 123 ); // $ExpectError +} diff --git a/is-finite-array/examples/index.js b/is-finite-array/examples/index.js new file mode 100644 index 00000000..69d331b2 --- /dev/null +++ b/is-finite-array/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isFiniteArray = require( './../lib' ); + +var bool = isFiniteArray( [ -5.0, 0.0, 2.0, 5.0 ] ); +console.log( bool ); +// => true + +bool = isFiniteArray( [ -4.0, -3.14, 1.0, 3.14 ] ); +console.log( bool ); +// => true + +bool = isFiniteArray( [ -1.0, new Number( -6.0 ), 2.0 ] ); +console.log( bool ); +// => true + +bool = isFiniteArray( [ -1.0, 'abc', 3.0 ] ); +console.log( bool ); +// => false + +bool = isFiniteArray( [ -2.3, 1.0/0.0, 3.0 ] ); +console.log( bool ); +// => false + +bool = isFiniteArray( [] ); +console.log( bool ); +// => false diff --git a/is-finite-array/lib/index.js b/is-finite-array/lib/index.js new file mode 100644 index 00000000..7ff771ad --- /dev/null +++ b/is-finite-array/lib/index.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only finite numbers. +* +* @module @stdlib/assert/is-finite-array +* +* @example +* var isFiniteArray = require( '@stdlib/assert/is-finite-array' ); +* +* var bool = isFiniteArray( [ -3.0, new Number(0.0), 2.0 ] ); +* // returns true +* +* bool = isFiniteArray( [ -3.0, 1.0/0.0 ] ); +* // returns false +* +* @example +* var isFiniteArray = require( '@stdlib/assert/is-finite-array' ).primitives; +* +* var bool = isFiniteArray( [ -1.0, 10.0 ] ); +* // returns true +* +* bool = isFiniteArray( [ -1.5, 0.0, 5.0 ] ); +* // returns true +* +* bool = isFiniteArray( [ -3.0, new Number(-1.0) ] ); +* // returns false +* +* @example +* var isFiniteArray = require( '@stdlib/assert/is-finite-array' ).objects; +* +* var bool = isFiniteArray( [ new Number(1.0), new Number(3.0) ] ); +* // returns true +* +* bool = isFiniteArray( [ -1.0, 0.0, 3.0 ] ); +* // returns false +* +* bool = isFiniteArray( [ 3.0, new Number(-1.0) ] ); +* // returns false +*/ + +// MODULES // + +var isFinite = require( '@stdlib/assert/is-finite' ); // eslint-disable-line stdlib/no-redeclare +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); + + +// MAIN // + +var isFiniteArray = arrayfun( isFinite ); +setReadOnly( isFiniteArray, 'primitives', arrayfun( isFinite.isPrimitive ) ); +setReadOnly( isFiniteArray, 'objects', arrayfun( isFinite.isObject ) ); + + +// EXPORTS // + +module.exports = isFiniteArray; diff --git a/is-finite-array/package.json b/is-finite-array/package.json new file mode 100644 index 00000000..bf526331 --- /dev/null +++ b/is-finite-array/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/assert/is-finite-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only finite numbers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "number", + "numeric", + "isnumber", + "isnumeric", + "finite", + "infinite", + "isfinite", + "type", + "check", + "validate", + "valid", + "test" + ] +} diff --git a/is-finite-array/test/test.js b/is-finite-array/test/test.js new file mode 100644 index 00000000..31c78e3c --- /dev/null +++ b/is-finite-array/test/test.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isFiniteArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isFiniteArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only finite numbers', function test( t ) { + var arr; + + arr = [ 5.0, new Number( 5.0 ), -3.14 ]; + t.strictEqual( isFiniteArray( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': 2.0, + '1': 1.0 + }; + t.strictEqual( isFiniteArray( arr ), true, 'returns true' ); + + arr = [ 5.0, 2.3, 11.1, 1.0/0.0 ]; + t.strictEqual( isFiniteArray( arr ), false, 'returns false' ); + + arr = [ 5.0, '3', null ]; + t.strictEqual( isFiniteArray( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only primitive finite numbers', function test( t ) { + var arr; + + arr = [ 5.0, -0.0, 0.0, 3.14 ]; + t.strictEqual( isFiniteArray.primitives( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': 1.0, + '1': -3.0 + }; + t.strictEqual( isFiniteArray.primitives( arr ), true, 'returns true' ); + + arr = [ 5.0, -0.0, 0.0, 1.0/0.0 ]; + t.strictEqual( isFiniteArray.primitives( arr ), false, 'returns false' ); + + arr = [ new Number( 5.0 ), 1.0, 1.0 ]; + t.strictEqual( isFiniteArray.primitives( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only `Number` objects having finite values', function test( t ) { + var arr; + + arr = [ new Number( 5.0 ), new Number( -0.0 ), new Number( 0.0 ) ]; + t.strictEqual( isFiniteArray.objects( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': new Number( 2.0 ), + '1': new Number( -3.0 ) + }; + t.strictEqual( isFiniteArray.objects( arr ), true, 'returns true' ); + + arr = [ new Number( 5.0 ), new Number( -0.0 ), new Number( 1.0/0.0 ) ]; + t.strictEqual( isFiniteArray.objects( arr ), false, 'returns false' ); + + arr = [ new Number( 5.0 ), 1.0, 1.0 ]; + t.strictEqual( isFiniteArray.objects( arr ), false, 'returns false' ); + + arr = [ -5.0, 1.0, 1.0 ]; + t.strictEqual( isFiniteArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-finite/README.md b/is-finite/README.md new file mode 100644 index 00000000..cac9fb88 --- /dev/null +++ b/is-finite/README.md @@ -0,0 +1,150 @@ + + +# isFinite + +> Test if a value is a finite number. + +
+ +## Usage + + + +```javascript +var isFinite = require( '@stdlib/assert/is-finite' ); +``` + +#### isFinite( value ) + +Tests if a value is a finite `number`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isFinite( 5.0 ); +// returns true + +bool = isFinite( new Number( 5.0 ) ); +// returns true + +bool = isFinite( 1.0/0.0 ); +// returns false + +bool = isFinite( null ); +// returns false +``` + +#### isFinite.isPrimitive( value ) + +Tests if a `value` is a primitive `number` having a finite value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isFinite.isPrimitive( -3.0 ); +// returns true + +bool = isFinite.isPrimitive( new Number( -3.0 ) ); +// returns false +``` + +#### isFinite.isObject( value ) + +Tests if a `value` is a `Number` object having a finite value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isFinite.isObject( 3.0 ); +// returns false + +bool = isFinite.isObject( new Number( 3.0 ) ); +// returns true +``` + +
+ + + +
+ +## Notes + +- In contrast to the built-in [`isFinite`][mdn-is-finite], input values are **not** coerced to numbers. + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isFinite = require( '@stdlib/assert/is-finite' ); + +var bool = isFinite( -5.0 ); +// returns true + +bool = isFinite( 0.0 ); +// returns true + +bool = isFinite( new Number( 5.0 ) ); +// returns true + +bool = isFinite( 5.256 ); +// returns true + +bool = isFinite( 1.0/0.0 ); +// returns false + +bool = isFinite( -1.0/0.0 ); +// returns false + +bool = isFinite( '5' ); +// returns false + +bool = isFinite( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-finite/benchmark/benchmark.js b/is-finite/benchmark/benchmark.js new file mode 100644 index 00000000..e973211e --- /dev/null +++ b/is-finite/benchmark/benchmark.js @@ -0,0 +1,221 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function, stdlib/no-redeclare */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isFinite = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFinite( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFinite( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFinite.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFinite.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFinite.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFinite.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-finite/docs/repl.txt b/is-finite/docs/repl.txt new file mode 100644 index 00000000..8d8ace57 --- /dev/null +++ b/is-finite/docs/repl.txt @@ -0,0 +1,72 @@ + +{{alias}}( value ) + Tests if a value is a finite number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a finite number. + + Examples + -------- + > var bool = {{alias}}( 5.0 ) + true + > bool = {{alias}}( new Number( 5.0 ) ) + true + > bool = {{alias}}( 1.0/0.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive having a finite value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a number primitive having a finite + value. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( -3.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( -3.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a finite value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a number object having a finite + value. + + Examples + -------- + > var bool = {{alias}}.isObject( 3.0 ) + false + > bool = {{alias}}.isObject( new Number( 3.0 ) ) + true + + See Also + -------- + diff --git a/is-finite/docs/types/index.d.ts b/is-finite/docs/types/index.d.ts new file mode 100644 index 00000000..546373aa --- /dev/null +++ b/is-finite/docs/types/index.d.ts @@ -0,0 +1,117 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isFinite` with methods for testing for primitives and objects, respectively. +*/ +interface IsFinite { + /** + * Tests if a value is a finite number. + * + * @param value - value to test + * @returns boolean indicating whether a value is a finite number + * + * @example + * var bool = isFinite( 5.0 ); + * // returns true + * + * @example + * var bool = isFinite( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = isFinite( 1.0/0.0 ); + * // returns false + * + * @example + * var bool = isFinite( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a finite value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a finite value + * + * @example + * var bool = isFinite.isPrimitive( -3.0 ); + * // returns true + * + * @example + * var bool = isFinite.isPrimitive( new Number( -3.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a finite value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a finite value + * + * @example + * var bool = isFinite.isObject( 3.0 ); + * // returns false + * + * @example + * var bool = isFinite.isObject( new Number( 3.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a finite number. +* +* @param value - value to test +* @returns boolean indicating whether a value is a finite number +* +* @example +* var bool = isFinite( 5.0 ); +* // returns true +* +* @example +* var bool = isFinite( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isFinite( 1.0/0.0 ); +* // returns false +* +* @example +* var bool = isFinite( null ); +* // returns false +* +* @example +* var bool = isFinite.isPrimitive( new Number( -3.0 ) ); +* // returns false +* +* @example +* var bool = isFinite.isObject( new Number( 3.0 ) ); +* // returns true +*/ +declare var isFinite: IsFinite; + + +// EXPORTS // + +export = isFinite; diff --git a/is-finite/docs/types/test.ts b/is-finite/docs/types/test.ts new file mode 100644 index 00000000..d2bda035 --- /dev/null +++ b/is-finite/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFinite = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFinite( 3 ); // $ExpectType boolean + isFinite( 1 / 0 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isFinite(); // $ExpectError + isFinite( 4, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isFinite.isPrimitive( new Number( 4 ) ); // $ExpectType boolean + isFinite.isPrimitive( 4 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isFinite.isPrimitive(); // $ExpectError + isFinite.isPrimitive( 4, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isFinite.isObject( new Number( 4 ) ); // $ExpectType boolean + isFinite.isObject( 4 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isFinite.isObject(); // $ExpectError + isFinite.isObject( 4, 123 ); // $ExpectError +} diff --git a/is-finite/examples/index.js b/is-finite/examples/index.js new file mode 100644 index 00000000..bb12012d --- /dev/null +++ b/is-finite/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, stdlib/no-redeclare */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isFinite = require( './../lib' ); + +console.log( isFinite( -5.0 ) ); +// => true + +console.log( isFinite( 0.0 ) ); +// => true + +console.log( isFinite( new Number( 5.0 ) ) ); +// => true + +console.log( isFinite( 5.256 ) ); +// => true + +console.log( isFinite( 1.0/0.0 ) ); +// => false + +console.log( isFinite( -1.0/0.0 ) ); +// => false + +console.log( isFinite( '5' ) ); +// => false + +console.log( isFinite( null ) ); +// => false diff --git a/is-finite/lib/index.js b/is-finite/lib/index.js new file mode 100644 index 00000000..e3622f33 --- /dev/null +++ b/is-finite/lib/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable stdlib/no-redeclare */ + +'use strict'; + +/** +* Test if a value is a finite number. +* +* @module @stdlib/assert/is-finite +* +* @example +* var isFinite = require( '@stdlib/assert/is-finite' ); +* +* var bool = isFinite( 5.0 ); +* // returns true +* +* bool = isFinite( new Number( 5.0 ) ); +* // returns true +* +* bool = isFinite( 1.0/0.0 ); +* // returns false +* +* bool = isFinite( null ); +* // returns false +* +* @example +* var isFinite = require( '@stdlib/assert/is-finite' ).isPrimitive; +* +* var bool = isFinite( -3.0 ); +* // returns true +* +* bool = isFinite( new Number( -3.0 ) ); +* // returns false +* +* @example +* var isFinite = require( '@stdlib/assert/is-finite' ).isObject; +* +* var bool = isFinite( 3.0 ); +* // returns false +* +* bool = isFinite( new Number( 3.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isFinite = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isFinite, 'isPrimitive', isPrimitive ); +setReadOnly( isFinite, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isFinite; diff --git a/is-finite/lib/main.js b/is-finite/lib/main.js new file mode 100644 index 00000000..ca8b2de6 --- /dev/null +++ b/is-finite/lib/main.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable stdlib/no-redeclare */ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a finite number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is a finite number +* +* @example +* var bool = isFinite( 5.0 ); +* // returns true +* +* @example +* var bool = isFinite( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isFinite( 1.0/0.0 ); +* // returns false +* +* @example +* var bool = isFinite( null ); +* // returns false +*/ +function isFinite( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isFinite; diff --git a/is-finite/lib/object.js b/is-finite/lib/object.js new file mode 100644 index 00000000..2ebfaa48 --- /dev/null +++ b/is-finite/lib/object.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable stdlib/no-redeclare */ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; +var isfinite = require( '@stdlib/math/base/assert/is-finite' ); + + +// MAIN // + +/** +* Tests if a value is a number object having a finite value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a finite value +* +* @example +* var bool = isFinite( 3.0 ); +* // returns false +* +* @example +* var bool = isFinite( new Number( 3.0 ) ); +* // returns true +*/ +function isFinite( value ) { + return ( + isNumber( value ) && + isfinite( value.valueOf() ) + ); +} + + +// EXPORTS // + +module.exports = isFinite; diff --git a/is-finite/lib/primitive.js b/is-finite/lib/primitive.js new file mode 100644 index 00000000..a7f0cb21 --- /dev/null +++ b/is-finite/lib/primitive.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable stdlib/no-redeclare */ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isfinite = require( '@stdlib/math/base/assert/is-finite' ); + + +// MAIN // + +/** +* Tests if a value is a number primitive having a finite value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a finite value +* +* @example +* var bool = isFinite( -3.0 ); +* // returns true +* +* @example +* var bool = isFinite( new Number( -3.0 ) ); +* // returns false +*/ +function isFinite( value ) { + return ( + isNumber( value ) && + isfinite( value ) + ); +} + + +// EXPORTS // + +module.exports = isFinite; diff --git a/is-finite/package.json b/is-finite/package.json new file mode 100644 index 00000000..f0227f4a --- /dev/null +++ b/is-finite/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-finite", + "version": "0.0.0", + "description": "Test if a value is a finite number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "finite", + "infinite", + "inf", + "is", + "isfinite", + "type", + "check", + "primitive", + "object", + "validate", + "valid", + "test" + ] +} diff --git a/is-finite/test/test.js b/is-finite/test/test.js new file mode 100644 index 00000000..8207849b --- /dev/null +++ b/is-finite/test/test.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable stdlib/no-redeclare */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isFinite = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isFinite, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number having a finite value', function test( t ) { + t.strictEqual( typeof isFinite.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a finite value', function test( t ) { + t.strictEqual( typeof isFinite.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-finite/test/test.main.js b/is-finite/test/test.main.js new file mode 100644 index 00000000..1269c7f8 --- /dev/null +++ b/is-finite/test/test.main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable stdlib/no-redeclare, no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isFinite = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isFinite, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number having a finite value', function test( t ) { + t.strictEqual( isFinite( 5.0 ), true, 'returns true' ); + t.strictEqual( isFinite( new Number( 5.0 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number having a finite value', function test( t ) { + var values; + var i; + + values = [ + '5', + 1.0/0.0, + -1.0/0.0, + NaN, + null, + true, + false, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isFinite( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-finite/test/test.object.js b/is-finite/test/test.object.js new file mode 100644 index 00000000..26e25595 --- /dev/null +++ b/is-finite/test/test.object.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable stdlib/no-redeclare, no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isFinite = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isFinite, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a finite value', function test( t ) { + t.strictEqual( isFinite( new Number( 5.0 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is a finite value', function test( t ) { + t.strictEqual( isFinite( 3.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a finite number', function test( t ) { + var values; + var i; + + values = [ + '5', + 1.0/0.0, + -1.0/0.0, + NaN, + new Number( 1.0/0.0 ), + new Number( -1.0/0.0 ), + new Number( NaN ), + null, + true, + false, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isFinite( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-finite/test/test.primitive.js b/is-finite/test/test.primitive.js new file mode 100644 index 00000000..902a4d88 --- /dev/null +++ b/is-finite/test/test.primitive.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable stdlib/no-redeclare, no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isFinite = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isFinite, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number having a finite value', function test( t ) { + t.strictEqual( isFinite( -3.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has a finite value', function test( t ) { + t.strictEqual( isFinite( new Number( 5.0 ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a finite number', function test( t ) { + var values; + var i; + + values = [ + '5', + 1.0/0.0, + -1.0/0.0, + NaN, + null, + true, + false, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isFinite( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-float32array/README.md b/is-float32array/README.md new file mode 100644 index 00000000..b43df49d --- /dev/null +++ b/is-float32array/README.md @@ -0,0 +1,116 @@ + + +# isFloat32Array + +> Test if a value is a [Float32Array][mdn-float32array]. + +
+ +## Usage + +```javascript +var isFloat32Array = require( '@stdlib/assert/is-float32array' ); +``` + +#### isFloat32Array( value ) + +Tests if a value is a [`Float32Array`][mdn-float32array]. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); + +var bool = isFloat32Array( new Float32Array( 10 ) ); +// returns true + +bool = isFloat32Array( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat32Array = require( '@stdlib/assert/is-float32array' ); + +var bool = isFloat32Array( new Float32Array( 10 ) ); +// returns true + +bool = isFloat32Array( new Int8Array( 10 ) ); +// returns false + +bool = isFloat32Array( new Uint8Array( 10 ) ); +// returns false + +bool = isFloat32Array( new Uint8ClampedArray( 10 ) ); +// returns false + +bool = isFloat32Array( new Int16Array( 10 ) ); +// returns false + +bool = isFloat32Array( new Uint16Array( 10 ) ); +// returns false + +bool = isFloat32Array( new Int32Array( 10 ) ); +// returns false + +bool = isFloat32Array( new Uint32Array( 10 ) ); +// returns false + +bool = isFloat32Array( new Float64Array( 10 ) ); +// returns false + +bool = isFloat32Array( new Array( 10 ) ); +// returns false + +bool = isFloat32Array( {} ); +// returns false + +bool = isFloat32Array( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-float32array/benchmark/benchmark.js b/is-float32array/benchmark/benchmark.js new file mode 100644 index 00000000..25591281 --- /dev/null +++ b/is-float32array/benchmark/benchmark.js @@ -0,0 +1,126 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isFloat32Array = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFloat32Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float32Array( 10 ), + new Float32Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFloat32Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFloat32Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-float32array/docs/repl.txt b/is-float32array/docs/repl.txt new file mode 100644 index 00000000..af281473 --- /dev/null +++ b/is-float32array/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a Float32Array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a Float32Array. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/float32}}( 10 ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-float32array/docs/types/index.d.ts b/is-float32array/docs/types/index.d.ts new file mode 100644 index 00000000..5fd85c9a --- /dev/null +++ b/is-float32array/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a Float32Array. +* +* @param value - value to test +* @returns boolean indicating whether value is a Float32Array +* +* @example +* var bool = isFloat32Array( new Float32Array( 10 ) ); +* // returns true +* +* @example +* var bool = isFloat32Array( [] ); +* // returns false +*/ +declare function isFloat32Array( value: any ): boolean; + + +// EXPORTS // + +export = isFloat32Array; diff --git a/is-float32array/docs/types/test.ts b/is-float32array/docs/types/test.ts new file mode 100644 index 00000000..32c0bf8c --- /dev/null +++ b/is-float32array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFloat32Array = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFloat32Array( new Float32Array( 10 ) ); // $ExpectType boolean + isFloat32Array( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isFloat32Array(); // $ExpectError + isFloat32Array( new Float32Array( 10 ), 123 ); // $ExpectError +} diff --git a/is-float32array/examples/index.js b/is-float32array/examples/index.js new file mode 100644 index 00000000..753014a8 --- /dev/null +++ b/is-float32array/examples/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat32Array = require( './../lib' ); + +var bool = isFloat32Array( new Float32Array( 10 ) ); +console.log( bool ); +// => true + +bool = isFloat32Array( new Int8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat32Array( new Uint8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat32Array( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat32Array( new Int16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat32Array( new Uint16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat32Array( new Int32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat32Array( new Uint32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat32Array( new Float64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat32Array( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat32Array( {} ); +console.log( bool ); +// => false + +bool = isFloat32Array( null ); +console.log( bool ); +// => false diff --git a/is-float32array/lib/index.js b/is-float32array/lib/index.js new file mode 100644 index 00000000..d912668c --- /dev/null +++ b/is-float32array/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a Float32Array. +* +* @module @stdlib/assert/is-float32array +* +* @example +* var isFloat32Array = require( '@stdlib/assert/is-float32array' ); +* +* var bool = isFloat32Array( new Float32Array( 10 ) ); +* // returns true +* +* bool = isFloat32Array( [] ); +* // returns false +*/ + +// MODULES // + +var isFloat32Array = require( './main.js' ); + + +// EXPORTS // + +module.exports = isFloat32Array; diff --git a/is-float32array/lib/main.js b/is-float32array/lib/main.js new file mode 100644 index 00000000..1c3f6bf0 --- /dev/null +++ b/is-float32array/lib/main.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var hasFloat32Array = ( typeof Float32Array === 'function' );// eslint-disable-line stdlib/require-globals + + +// MAIN // + +/** +* Tests if a value is a Float32Array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a Float32Array +* +* @example +* var bool = isFloat32Array( new Float32Array( 10 ) ); +* // returns true +* +* @example +* var bool = isFloat32Array( [] ); +* // returns false +*/ +function isFloat32Array( value ) { + return ( + ( hasFloat32Array && value instanceof Float32Array ) || // eslint-disable-line stdlib/require-globals + nativeClass( value ) === '[object Float32Array]' + ); +} + + +// EXPORTS // + +module.exports = isFloat32Array; diff --git a/is-float32array/package.json b/is-float32array/package.json new file mode 100644 index 00000000..4d2cc812 --- /dev/null +++ b/is-float32array/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-float32array", + "version": "0.0.0", + "description": "Test if a value is a Float32Array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "float32array", + "float32", + "float", + "typed", + "typed array", + "typed-array", + "array", + "is", + "isarray", + "istypedarray", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-float32array/test/test.js b/is-float32array/test/test.js new file mode 100644 index 00000000..9a4e467d --- /dev/null +++ b/is-float32array/test/test.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat32Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isFloat32Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a Float32Array', function test( t ) { + t.strictEqual( isFloat32Array( new Float32Array( 10 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a Float32Array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + undefined, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float64Array( 10 ), + new Uint32Array( 10 ), + new Int32Array( 10 ), + new Uint16Array( 10 ), + new Int16Array( 10 ), + new Uint8Array( 10 ), + new Int8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isFloat32Array( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-float32matrix-like/README.md b/is-float32matrix-like/README.md new file mode 100644 index 00000000..7857df2f --- /dev/null +++ b/is-float32matrix-like/README.md @@ -0,0 +1,88 @@ + + +# isFloat32MatrixLike + +> Test if a value is a 2-dimensional [ndarray][@stdlib/ndarray/ctor]-like object containing single-precision floating-point numbers. + +
+ +## Usage + +```javascript +var isFloat32MatrixLike = require( '@stdlib/assert/is-float32matrix-like' ); +``` + +#### isFloat32MatrixLike( value ) + +Tests if a value is a 2-dimensional [ndarray][@stdlib/ndarray/ctor]-like object whose underlying data type is `float32`. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +var bool = isFloat32MatrixLike( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float32Array = require( '@stdlib/array/float32' ); +var isFloat32MatrixLike = require( '@stdlib/assert/is-float32matrix-like' ); + +var buffer = new Float32Array( [ 0, 0, 0, 0 ] ); +var arr = ndarray( 'float32', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +var out = isFloat32MatrixLike( arr ); +// returns true + +out = isFloat32MatrixLike( [ 1, 2, 3, 4 ] ); +// returns false + +out = isFloat32MatrixLike( {} ); +// returns false + +out = isFloat32MatrixLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-float32matrix-like/benchmark/benchmark.js b/is-float32matrix-like/benchmark/benchmark.js new file mode 100644 index 00000000..636214e6 --- /dev/null +++ b/is-float32matrix-like/benchmark/benchmark.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float32Array = require( '@stdlib/array/float32' ); +var pkg = require( './../package.json' ).name; +var isFloat32MatrixLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = new Float32Array( [ 0, 0, 0, 0 ] ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'float32', buffer, shape, strides, offset, order ); + + values = [ + arr, + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFloat32MatrixLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-float32matrix-like/docs/repl.txt b/is-float32matrix-like/docs/repl.txt new file mode 100644 index 00000000..f2c4d301 --- /dev/null +++ b/is-float32matrix-like/docs/repl.txt @@ -0,0 +1,42 @@ + +{{alias}}( value ) + Tests if a value is a 2-dimensional ndarray-like object containing single- + precision floating-point numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a 2-dimensional ndarray-like + object containing single-precision floating-point numbers. + + Examples + -------- + > var M = {}; + > M.data = new {{alias:@stdlib/array/float32}}( [ 0, 0, 0, 0 ] ); + > M.ndims = 2; + > M.shape = [ 2, 2 ]; + > M.strides = [ 2, 1 ]; + > M.offset = 0; + > M.order = 'row-major'; + > M.dtype = 'float32'; + > M.length = 4; + > M.flags = {}; + > M.get = function get( i, j ) {}; + > M.set = function set( i, j ) {}; + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-float32matrix-like/docs/types/index.d.ts b/is-float32matrix-like/docs/types/index.d.ts new file mode 100644 index 00000000..0c26b396 --- /dev/null +++ b/is-float32matrix-like/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a 2-dimensional ndarray-like object whose underlying data type is `float32`. +* +* @param v - value to test +* @returns boolean indicating if a value is a 2-dimensional ndarray-like object whose underlying data type is `float32` +* +* @example +* var Float32Array = require( `@stdlib/array/float32` ); +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isFloat32MatrixLike( arr ); +* // returns true +* +* bool = isFloat32MatrixLike( [] ); +* // returns false +*/ +declare function isFloat32MatrixLike( v: any ): boolean; + + +// EXPORTS // + +export = isFloat32MatrixLike; diff --git a/is-float32matrix-like/docs/types/test.ts b/is-float32matrix-like/docs/types/test.ts new file mode 100644 index 00000000..05370779 --- /dev/null +++ b/is-float32matrix-like/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFloat32MatrixLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFloat32MatrixLike( [] ); // $ExpectType boolean +} + +// The function does not compile if provided an unsupported number of arguments... +{ + isFloat32MatrixLike(); // $ExpectError + isFloat32MatrixLike( 'abc', 123 ); // $ExpectError +} diff --git a/is-float32matrix-like/examples/index.js b/is-float32matrix-like/examples/index.js new file mode 100644 index 00000000..f8610061 --- /dev/null +++ b/is-float32matrix-like/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float32Array = require( '@stdlib/array/float32' ); +var isFloat32MatrixLike = require( './../lib' ); + +var buffer = new Float32Array( [ 0, 0, 0, 0 ] ); +var arr = ndarray( 'float32', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +console.log( isFloat32MatrixLike( arr ) ); +// => true + +console.log( isFloat32MatrixLike( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isFloat32MatrixLike( {} ) ); +// => false + +console.log( isFloat32MatrixLike( null ) ); +// => false diff --git a/is-float32matrix-like/lib/index.js b/is-float32matrix-like/lib/index.js new file mode 100644 index 00000000..b0e5d8e7 --- /dev/null +++ b/is-float32matrix-like/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a 2-dimensional ndarray-like object whose underlying data type is `float32`. +* +* @module @stdlib/assert/is-float32matrix-like +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var Float32Array = require( '@stdlib/array/float32' ); +* var isFloat32MatrixLike = require( '@stdlib/assert/is-float32matrix-like' ); +* +* var buffer = new Float32Array( [ 0, 0, 0, 0 ] ); +* +* var arr = ndarray( 'float32', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isFloat32MatrixLike( arr ); +* // returns true +* +* bool = isFloat32MatrixLike( [] ); +* // returns false +*/ + +// MODULES // + +var isFloat32MatrixLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isFloat32MatrixLike; diff --git a/is-float32matrix-like/lib/main.js b/is-float32matrix-like/lib/main.js new file mode 100644 index 00000000..acf03666 --- /dev/null +++ b/is-float32matrix-like/lib/main.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isMatrixLike = require( '@stdlib/assert/is-matrix-like' ); +var isFloat32Array = require( '@stdlib/assert/is-float32array' ); + + +// MAIN // + +/** +* Tests if a value is a 2-dimensional ndarray-like object whose underlying data type is `float32`. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a 2-dimensional ndarray-like object whose underlying data type is `float32` +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var buffer = new Float32Array( [ 0, 0, 0, 0 ] ); +* var arr = ndarray( 'float32', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isFloat32MatrixLike( arr ); +* // returns true +* +* bool = isFloat32MatrixLike( [] ); +* // returns false +*/ +function isFloat32MatrixLike( v ) { + return ( isMatrixLike( v ) && isFloat32Array( v.data ) ); +} + + +// EXPORTS // + +module.exports = isFloat32MatrixLike; diff --git a/is-float32matrix-like/package.json b/is-float32matrix-like/package.json new file mode 100644 index 00000000..081d23e0 --- /dev/null +++ b/is-float32matrix-like/package.json @@ -0,0 +1,79 @@ +{ + "name": "@stdlib/assert/is-float32matrix-like", + "version": "0.0.0", + "description": "Test if a value is a 2-dimensional ndarray-like object containing single-precision floating-point numbers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "matrix", + "ismatrix", + "mat", + "2d", + "float32", + "single", + "float", + "single-precision", + "float32array", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-float32matrix-like/test/test.js b/is-float32matrix-like/test/test.js new file mode 100644 index 00000000..22231a6d --- /dev/null +++ b/is-float32matrix-like/test/test.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var Float32Array = require( '@stdlib/array/float32' ); +var isFloat32MatrixLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isFloat32MatrixLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray containing single-precision floating-point numbers', function test( t ) { + var arr = ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + t.equal( isFloat32MatrixLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray-like object containing single-precision floating-point numbers', function test( t ) { + var arr = { + 'data': new Float32Array( [ 0, 0, 0, 0 ] ), + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'float32', + 'length': 4, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isFloat32MatrixLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a 2-dimensional ndarray-like object containing single-precision floating-point numbers', function test( t ) { + var values; + var arr1; + var arr2; + var i; + + arr1 = ndarray( 'float32', new Float32Array( [ 0, 0, 0 ] ), [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + arr2 = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + values = [ + arr1, + arr2, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isFloat32MatrixLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-float32ndarray-like/README.md b/is-float32ndarray-like/README.md new file mode 100644 index 00000000..57f6c7de --- /dev/null +++ b/is-float32ndarray-like/README.md @@ -0,0 +1,88 @@ + + +# isFloat32ndarrayLike + +> Test if a value is an [ndarray][@stdlib/ndarray/ctor]-like object containing single-precision floating-point numbers. + +
+ +## Usage + +```javascript +var isFloat32ndarrayLike = require( '@stdlib/assert/is-float32ndarray-like' ); +``` + +#### isFloat32ndarrayLike( value ) + +Tests if a value is an [ndarray][@stdlib/ndarray/ctor]-like object whose underlying data type is `float32`. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +var bool = isFloat32ndarrayLike( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float32Array = require( '@stdlib/array/float32' ); +var isFloat32ndarrayLike = require( '@stdlib/assert/is-float32ndarray-like' ); + +var buffer = new Float32Array( [ 0, 0, 0, 0 ] ); +var arr = ndarray( 'float32', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +var out = isFloat32ndarrayLike( arr ); +// returns true + +out = isFloat32ndarrayLike( [ 1, 2, 3, 4 ] ); +// returns false + +out = isFloat32ndarrayLike( {} ); +// returns false + +out = isFloat32ndarrayLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-float32ndarray-like/benchmark/benchmark.js b/is-float32ndarray-like/benchmark/benchmark.js new file mode 100644 index 00000000..9870c022 --- /dev/null +++ b/is-float32ndarray-like/benchmark/benchmark.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float32Array = require( '@stdlib/array/float32' ); +var pkg = require( './../package.json' ).name; +var isFloat32ndarrayLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = new Float32Array( [ 0, 0, 0, 0 ] ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'float32', buffer, shape, strides, offset, order ); + + values = [ + arr, + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFloat32ndarrayLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-float32ndarray-like/docs/repl.txt b/is-float32ndarray-like/docs/repl.txt new file mode 100644 index 00000000..1d06d425 --- /dev/null +++ b/is-float32ndarray-like/docs/repl.txt @@ -0,0 +1,42 @@ + +{{alias}}( value ) + Tests if a value is an ndarray-like object containing single-precision + floating-point numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an ndarray-like object containing + single-precision floating-point numbers. + + Examples + -------- + > var M = {}; + > M.data = new {{alias:@stdlib/array/float32}}( [ 0, 0, 0, 0 ] ); + > M.ndims = 2; + > M.shape = [ 2, 2 ]; + > M.strides = [ 2, 1 ]; + > M.offset = 0; + > M.order = 'row-major'; + > M.dtype = 'float32'; + > M.length = 4; + > M.flags = {}; + > M.get = function get( i, j ) {}; + > M.set = function set( i, j ) {}; + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-float32ndarray-like/docs/types/index.d.ts b/is-float32ndarray-like/docs/types/index.d.ts new file mode 100644 index 00000000..f8ec2b02 --- /dev/null +++ b/is-float32ndarray-like/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an ndarray-like object whose underlying data type is `float32`. +* +* @param v - value to test +* @returns boolean indicating if a value is an ndarray-like object whose underlying data type is `float32` +* +* @example +* var Float32Array = require( `@stdlib/array/float32` ); +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isFloat32ndarrayLike( arr ); +* // returns true +* +* bool = isFloat32ndarrayLike( [] ); +* // returns false +*/ +declare function isFloat32ndarrayLike( v: any ): boolean; + + +// EXPORTS // + +export = isFloat32ndarrayLike; diff --git a/is-float32ndarray-like/docs/types/test.ts b/is-float32ndarray-like/docs/types/test.ts new file mode 100644 index 00000000..9994c12f --- /dev/null +++ b/is-float32ndarray-like/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFloat32ndarrayLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFloat32ndarrayLike( [] ); // $ExpectType boolean +} + +// The function does not compile if provided an unsupported number of arguments... +{ + isFloat32ndarrayLike(); // $ExpectError + isFloat32ndarrayLike( 'abc', 123 ); // $ExpectError +} diff --git a/is-float32ndarray-like/examples/index.js b/is-float32ndarray-like/examples/index.js new file mode 100644 index 00000000..04612839 --- /dev/null +++ b/is-float32ndarray-like/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float32Array = require( '@stdlib/array/float32' ); +var isFloat32ndarrayLike = require( './../lib' ); + +var buffer = new Float32Array( [ 0, 0, 0, 0 ] ); +var arr = ndarray( 'float32', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +console.log( isFloat32ndarrayLike( arr ) ); +// => true + +console.log( isFloat32ndarrayLike( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isFloat32ndarrayLike( {} ) ); +// => false + +console.log( isFloat32ndarrayLike( null ) ); +// => false diff --git a/is-float32ndarray-like/lib/index.js b/is-float32ndarray-like/lib/index.js new file mode 100644 index 00000000..55057d9a --- /dev/null +++ b/is-float32ndarray-like/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an ndarray-like object whose underlying data type is `float32`. +* +* @module @stdlib/assert/is-float32ndarray-like +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var Float32Array = require( '@stdlib/array/float32' ); +* var isFloat32ndarrayLike = require( '@stdlib/assert/is-float32ndarray-like' ); +* +* var buffer = new Float32Array( [ 0, 0, 0, 0 ] ); +* +* var arr = ndarray( 'float32', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isFloat32ndarrayLike( arr ); +* // returns true +* +* bool = isFloat32ndarrayLike( [] ); +* // returns false +*/ + +// MODULES // + +var isFloat32ndarrayLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isFloat32ndarrayLike; diff --git a/is-float32ndarray-like/lib/main.js b/is-float32ndarray-like/lib/main.js new file mode 100644 index 00000000..f51260f6 --- /dev/null +++ b/is-float32ndarray-like/lib/main.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isFloat32Array = require( '@stdlib/assert/is-float32array' ); + + +// MAIN // + +/** +* Tests if a value is an ndarray-like object whose underlying data type is `float32`. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is an ndarray-like object whose underlying data type is `float32` +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var buffer = new Float32Array( [ 0, 0, 0, 0 ] ); +* var arr = ndarray( 'float32', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isFloat32ndarrayLike( arr ); +* // returns true +* +* bool = isFloat32ndarrayLike( [] ); +* // returns false +*/ +function isFloat32ndarrayLike( v ) { + return ( isndarrayLike( v ) && isFloat32Array( v.data ) ); +} + + +// EXPORTS // + +module.exports = isFloat32ndarrayLike; diff --git a/is-float32ndarray-like/package.json b/is-float32ndarray-like/package.json new file mode 100644 index 00000000..507f4cdb --- /dev/null +++ b/is-float32ndarray-like/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-float32ndarray-like", + "version": "0.0.0", + "description": "Test if a value is an ndarray-like object containing single-precision floating-point numbers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "array", + "nd", + "float32", + "single", + "float", + "single-precision", + "float32array", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-float32ndarray-like/test/test.js b/is-float32ndarray-like/test/test.js new file mode 100644 index 00000000..ad0de1df --- /dev/null +++ b/is-float32ndarray-like/test/test.js @@ -0,0 +1,89 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var Float32Array = require( '@stdlib/array/float32' ); +var isFloat32ndarrayLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isFloat32ndarrayLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an ndarray containing single-precision floating-point numbers', function test( t ) { + var arr = ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + t.equal( isFloat32ndarrayLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an ndarray-like object containing single-precision floating-point numbers', function test( t ) { + var arr = { + 'data': new Float32Array( [ 0, 0, 0, 0 ] ), + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'float32', + 'length': 4, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isFloat32ndarrayLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an ndarray-like object containing single-precision floating-point numbers', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + values = [ + arr, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isFloat32ndarrayLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-float32vector-like/README.md b/is-float32vector-like/README.md new file mode 100644 index 00000000..f95a0cd1 --- /dev/null +++ b/is-float32vector-like/README.md @@ -0,0 +1,88 @@ + + +# isFloat32VectorLike + +> Test if a value is a 1-dimensional [ndarray][@stdlib/ndarray/ctor]-like object containing single-precision floating-point numbers. + +
+ +## Usage + +```javascript +var isFloat32VectorLike = require( '@stdlib/assert/is-float32vector-like' ); +``` + +#### isFloat32VectorLike( value ) + +Tests if a value is a 1-dimensional [ndarray][@stdlib/ndarray/ctor]-like object whose underlying data type is `float32`. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + +var bool = isFloat32VectorLike( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float32Array = require( '@stdlib/array/float32' ); +var isFloat32VectorLike = require( '@stdlib/assert/is-float32vector-like' ); + +var buffer = new Float32Array( [ 0, 0, 0, 0 ] ); +var arr = ndarray( 'float32', buffer, [ 4 ], [ 1 ], 0, 'row-major' ); + +var out = isFloat32VectorLike( arr ); +// returns true + +out = isFloat32VectorLike( [ 1, 2, 3, 4 ] ); +// returns false + +out = isFloat32VectorLike( {} ); +// returns false + +out = isFloat32VectorLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-float32vector-like/benchmark/benchmark.js b/is-float32vector-like/benchmark/benchmark.js new file mode 100644 index 00000000..39d2ca8f --- /dev/null +++ b/is-float32vector-like/benchmark/benchmark.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float32Array = require( '@stdlib/array/float32' ); +var pkg = require( './../package.json' ).name; +var isFloat32VectorLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = new Float32Array( [ 0, 0, 0, 0 ] ); + shape = [ 4 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'float32', buffer, shape, strides, offset, order ); + + values = [ + arr, + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFloat32VectorLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-float32vector-like/docs/repl.txt b/is-float32vector-like/docs/repl.txt new file mode 100644 index 00000000..2478fea5 --- /dev/null +++ b/is-float32vector-like/docs/repl.txt @@ -0,0 +1,42 @@ + +{{alias}}( value ) + Tests if a value is a 1-dimensional ndarray-like object containing single- + precision floating-point numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a 1-dimensional ndarray-like + object containing single-precision floating-point numbers. + + Examples + -------- + > var M = {}; + > M.data = new {{alias:@stdlib/array/float32}}( [ 0, 0, 0, 0 ] ); + > M.ndims = 1; + > M.shape = [ 4 ]; + > M.strides = [ 1 ]; + > M.offset = 0; + > M.order = 'row-major'; + > M.dtype = 'float32'; + > M.length = 4; + > M.flags = {}; + > M.get = function get( i, j ) {}; + > M.set = function set( i, j ) {}; + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-float32vector-like/docs/types/index.d.ts b/is-float32vector-like/docs/types/index.d.ts new file mode 100644 index 00000000..c1a6e637 --- /dev/null +++ b/is-float32vector-like/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a 1-dimensional ndarray-like object whose underlying data type is `float32`. +* +* @param v - value to test +* @returns boolean indicating if a value is a 1-dimensional ndarray-like object whose underlying data type is `float32` +* +* @example +* var Float32Array = require( `@stdlib/array/float32` ); +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var bool = isFloat32VectorLike( arr ); +* // returns true +* +* bool = isFloat32VectorLike( [] ); +* // returns false +*/ +declare function isFloat32VectorLike( v: any ): boolean; + + +// EXPORTS // + +export = isFloat32VectorLike; diff --git a/is-float32vector-like/docs/types/test.ts b/is-float32vector-like/docs/types/test.ts new file mode 100644 index 00000000..42b883f0 --- /dev/null +++ b/is-float32vector-like/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFloat32VectorLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFloat32VectorLike( [] ); // $ExpectType boolean +} + +// The function does not compile if provided an unsupported number of arguments... +{ + isFloat32VectorLike(); // $ExpectError + isFloat32VectorLike( 'abc', 123 ); // $ExpectError +} diff --git a/is-float32vector-like/examples/index.js b/is-float32vector-like/examples/index.js new file mode 100644 index 00000000..ce4a881a --- /dev/null +++ b/is-float32vector-like/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float32Array = require( '@stdlib/array/float32' ); +var isFloat32VectorLike = require( './../lib' ); + +var buffer = new Float32Array( [ 0, 0, 0, 0 ] ); +var arr = ndarray( 'float32', buffer, [ 4 ], [ 1 ], 0, 'row-major' ); + +console.log( isFloat32VectorLike( arr ) ); +// => true + +console.log( isFloat32VectorLike( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isFloat32VectorLike( {} ) ); +// => false + +console.log( isFloat32VectorLike( null ) ); +// => false diff --git a/is-float32vector-like/lib/index.js b/is-float32vector-like/lib/index.js new file mode 100644 index 00000000..1d6ce70c --- /dev/null +++ b/is-float32vector-like/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a 1-dimensional ndarray-like object whose underlying data type is `float32`. +* +* @module @stdlib/assert/is-float32vector-like +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var Float32Array = require( '@stdlib/array/float32' ); +* var isFloat32VectorLike = require( '@stdlib/assert/is-float32vector-like' ); +* +* var buffer = new Float32Array( [ 0, 0, 0, 0 ] ); +* +* var arr = ndarray( 'float32', buffer, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var bool = isFloat32VectorLike( arr ); +* // returns true +* +* bool = isFloat32VectorLike( [] ); +* // returns false +*/ + +// MODULES // + +var isFloat32VectorLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isFloat32VectorLike; diff --git a/is-float32vector-like/lib/main.js b/is-float32vector-like/lib/main.js new file mode 100644 index 00000000..67438397 --- /dev/null +++ b/is-float32vector-like/lib/main.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isVectorLike = require( '@stdlib/assert/is-vector-like' ); +var isFloat32Array = require( '@stdlib/assert/is-float32array' ); + + +// MAIN // + +/** +* Tests if a value is a 1-dimensional ndarray-like object whose underlying data type is `float32`. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a 1-dimensional ndarray-like object whose underlying data type is `float32` +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var buffer = new Float32Array( [ 0, 0, 0, 0 ] ); +* var arr = ndarray( 'float32', buffer, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var bool = isFloat32VectorLike( arr ); +* // returns true +* +* bool = isFloat32VectorLike( [] ); +* // returns false +*/ +function isFloat32VectorLike( v ) { + return ( isVectorLike( v ) && isFloat32Array( v.data ) ); +} + + +// EXPORTS // + +module.exports = isFloat32VectorLike; diff --git a/is-float32vector-like/package.json b/is-float32vector-like/package.json new file mode 100644 index 00000000..9a96e28b --- /dev/null +++ b/is-float32vector-like/package.json @@ -0,0 +1,79 @@ +{ + "name": "@stdlib/assert/is-float32vector-like", + "version": "0.0.0", + "description": "Test if a value is a 1-dimensional ndarray-like object containing single-precision floating-point numbers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "vector", + "isvector", + "vec", + "1d", + "float32", + "float", + "single", + "single-precision", + "float32array", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-float32vector-like/test/test.js b/is-float32vector-like/test/test.js new file mode 100644 index 00000000..e0c2b3e0 --- /dev/null +++ b/is-float32vector-like/test/test.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var Float32Array = require( '@stdlib/array/float32' ); +var isFloat32VectorLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isFloat32VectorLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 1-dimensional ndarray containing single-precision floating-point numbers', function test( t ) { + var arr = ndarray( 'float32', new Float32Array( [ 0, 0, 0, 0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + + t.equal( isFloat32VectorLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 1-dimensional ndarray-like object containing single-precision floating-point numbers', function test( t ) { + var arr = { + 'data': new Float32Array( [ 0, 0, 0, 0 ] ), + 'shape': [ 4 ], + 'strides': [ 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 1, + 'dtype': 'float32', + 'length': 4, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isFloat32VectorLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a 1-dimensional ndarray-like object containing single-precision floating-point numbers', function test( t ) { + var values; + var arr1; + var arr2; + var i; + + arr1 = ndarray( 'float32', new Float32Array( [ 0, 0, 0 ] ), [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + arr2 = ndarray( 'generic', [ 0, 0, 0 ], [ 3 ], [ 1 ], 0, 'row-major' ); + + values = [ + arr1, + arr2, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isFloat32VectorLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-float64array/README.md b/is-float64array/README.md new file mode 100644 index 00000000..7959cf71 --- /dev/null +++ b/is-float64array/README.md @@ -0,0 +1,116 @@ + + +# isFloat64Array + +> Test if a value is a [Float64Array][mdn-float64array]. + +
+ +## Usage + +```javascript +var isFloat64Array = require( '@stdlib/assert/is-float64array' ); +``` + +#### isFloat64Array( value ) + +Tests if a value is a [`Float64Array`][mdn-float64array]. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +var bool = isFloat64Array( new Float64Array( 10 ) ); +// returns true + +bool = isFloat64Array( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat64Array = require( '@stdlib/assert/is-float64array' ); + +var bool = isFloat64Array( new Float64Array( 10 ) ); +// returns true + +bool = isFloat64Array( new Int8Array( 10 ) ); +// returns false + +bool = isFloat64Array( new Uint8Array( 10 ) ); +// returns false + +bool = isFloat64Array( new Uint8ClampedArray( 10 ) ); +// returns false + +bool = isFloat64Array( new Int16Array( 10 ) ); +// returns false + +bool = isFloat64Array( new Uint16Array( 10 ) ); +// returns false + +bool = isFloat64Array( new Int32Array( 10 ) ); +// returns false + +bool = isFloat64Array( new Uint32Array( 10 ) ); +// returns false + +bool = isFloat64Array( new Float32Array( 10 ) ); +// returns false + +bool = isFloat64Array( new Array( 10 ) ); +// returns false + +bool = isFloat64Array( {} ); +// returns false + +bool = isFloat64Array( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-float64array/benchmark/benchmark.js b/is-float64array/benchmark/benchmark.js new file mode 100644 index 00000000..480388f3 --- /dev/null +++ b/is-float64array/benchmark/benchmark.js @@ -0,0 +1,126 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isFloat64Array = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFloat64Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float64Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFloat64Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFloat64Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-float64array/docs/repl.txt b/is-float64array/docs/repl.txt new file mode 100644 index 00000000..ecfff90b --- /dev/null +++ b/is-float64array/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a Float64Array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a Float64Array. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/float64}}( 10 ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-float64array/docs/types/index.d.ts b/is-float64array/docs/types/index.d.ts new file mode 100644 index 00000000..ae28f30a --- /dev/null +++ b/is-float64array/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a Float64Array. +* +* @param value - value to test +* @returns boolean indicating whether value is a Float64Array +* +* @example +* var Float64Array = require( `@stdlib/array/float64` ); +* +* var bool = isFloat64Array( new Float64Array( 10 ) ); +* // returns true +* +* @example +* var bool = isFloat64Array( [] ); +* // returns false +*/ +declare function isFloat32Array( value: any ): boolean; + + +// EXPORTS // + +export = isFloat32Array; diff --git a/is-float64array/docs/types/test.ts b/is-float64array/docs/types/test.ts new file mode 100644 index 00000000..af29b0e0 --- /dev/null +++ b/is-float64array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFloat64Array = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFloat64Array( new Float64Array( 10 ) ); // $ExpectType boolean + isFloat64Array( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isFloat64Array(); // $ExpectError + isFloat64Array( new Float64Array( 10 ), 123 ); // $ExpectError +} diff --git a/is-float64array/examples/index.js b/is-float64array/examples/index.js new file mode 100644 index 00000000..8404f5dc --- /dev/null +++ b/is-float64array/examples/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat64Array = require( './../lib' ); + +var bool = isFloat64Array( new Float64Array( 10 ) ); +console.log( bool ); +// => true + +bool = isFloat64Array( new Int8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat64Array( new Uint8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat64Array( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat64Array( new Int16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat64Array( new Uint16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat64Array( new Int32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat64Array( new Uint32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat64Array( new Float32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat64Array( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isFloat64Array( {} ); +console.log( bool ); +// => false + +bool = isFloat64Array( null ); +console.log( bool ); +// => false diff --git a/is-float64array/lib/index.js b/is-float64array/lib/index.js new file mode 100644 index 00000000..8e0626fc --- /dev/null +++ b/is-float64array/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a Float64Array. +* +* @module @stdlib/assert/is-float64array +* +* @example +* var isFloat64Array = require( '@stdlib/assert/is-float64array' ); +* +* var bool = isFloat64Array( new Float64Array( 10 ) ); +* // returns true +* +* bool = isFloat64Array( [] ); +* // returns false +*/ + +// MODULES // + +var isFloat64Array = require( './main.js' ); + + +// EXPORTS // + +module.exports = isFloat64Array; diff --git a/is-float64array/lib/main.js b/is-float64array/lib/main.js new file mode 100644 index 00000000..219818df --- /dev/null +++ b/is-float64array/lib/main.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var hasFloat64Array = ( typeof Float64Array === 'function' ); // eslint-disable-line stdlib/require-globals + + +// MAIN // + +/** +* Tests if a value is a Float64Array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a Float64Array +* +* @example +* var bool = isFloat64Array( new Float64Array( 10 ) ); +* // returns true +* +* @example +* var bool = isFloat64Array( [] ); +* // returns false +*/ +function isFloat64Array( value ) { + return ( + ( hasFloat64Array && value instanceof Float64Array ) || // eslint-disable-line stdlib/require-globals + nativeClass( value ) === '[object Float64Array]' + ); +} + + +// EXPORTS // + +module.exports = isFloat64Array; diff --git a/is-float64array/package.json b/is-float64array/package.json new file mode 100644 index 00000000..fb878814 --- /dev/null +++ b/is-float64array/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-float64array", + "version": "0.0.0", + "description": "Test if a value is a Float64Array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "float64array", + "float64", + "double", + "ieee754", + "typed", + "typed array", + "typed-array", + "array", + "is", + "isarray", + "istypedarray", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-float64array/test/test.js b/is-float64array/test/test.js new file mode 100644 index 00000000..fc68020f --- /dev/null +++ b/is-float64array/test/test.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat64Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isFloat64Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a Float64Array', function test( t ) { + t.strictEqual( isFloat64Array( new Float64Array( 10 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a Float64Array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + undefined, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float32Array( 10 ), + new Uint32Array( 10 ), + new Int32Array( 10 ), + new Uint16Array( 10 ), + new Int16Array( 10 ), + new Uint8Array( 10 ), + new Int8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isFloat64Array( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-float64matrix-like/README.md b/is-float64matrix-like/README.md new file mode 100644 index 00000000..3666290d --- /dev/null +++ b/is-float64matrix-like/README.md @@ -0,0 +1,88 @@ + + +# isFloat64MatrixLike + +> Test if a value is a 2-dimensional [ndarray][@stdlib/ndarray/ctor]-like object containing double-precision floating-point numbers. + +
+ +## Usage + +```javascript +var isFloat64MatrixLike = require( '@stdlib/assert/is-float64matrix-like' ); +``` + +#### isFloat64MatrixLike( value ) + +Tests if a value is a 2-dimensional [ndarray][@stdlib/ndarray/ctor]-like object whose underlying data type is `float64`. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'float64', new Float64Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +var bool = isFloat64MatrixLike( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat64MatrixLike = require( '@stdlib/assert/is-float64matrix-like' ); + +var buffer = new Float64Array( [ 0, 0, 0, 0 ] ); +var arr = ndarray( 'float64', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +var out = isFloat64MatrixLike( arr ); +// returns true + +out = isFloat64MatrixLike( [ 1, 2, 3, 4 ] ); +// returns false + +out = isFloat64MatrixLike( {} ); +// returns false + +out = isFloat64MatrixLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-float64matrix-like/benchmark/benchmark.js b/is-float64matrix-like/benchmark/benchmark.js new file mode 100644 index 00000000..65fcdf02 --- /dev/null +++ b/is-float64matrix-like/benchmark/benchmark.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isFloat64MatrixLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = new Float64Array( [ 0, 0, 0, 0 ] ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'float64', buffer, shape, strides, offset, order ); + + values = [ + arr, + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFloat64MatrixLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-float64matrix-like/docs/repl.txt b/is-float64matrix-like/docs/repl.txt new file mode 100644 index 00000000..c83207b1 --- /dev/null +++ b/is-float64matrix-like/docs/repl.txt @@ -0,0 +1,42 @@ + +{{alias}}( value ) + Tests if a value is a 2-dimensional ndarray-like object containing double- + precision floating-point numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a 2-dimensional ndarray-like + object containing double-precision floating-point numbers. + + Examples + -------- + > var M = {}; + > M.data = new {{alias:@stdlib/array/float64}}( [ 0, 0, 0, 0 ] ); + > M.ndims = 2; + > M.shape = [ 2, 2 ]; + > M.strides = [ 2, 1 ]; + > M.offset = 0; + > M.order = 'row-major'; + > M.dtype = 'float64'; + > M.length = 4; + > M.flags = {}; + > M.get = function get( i, j ) {}; + > M.set = function set( i, j ) {}; + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-float64matrix-like/docs/types/index.d.ts b/is-float64matrix-like/docs/types/index.d.ts new file mode 100644 index 00000000..a2d46833 --- /dev/null +++ b/is-float64matrix-like/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a 2-dimensional ndarray-like object whose underlying data type is `float64`. +* +* @param v - value to test +* @returns boolean indicating if a value is a 2-dimensional ndarray-like object whose underlying data type is `float64` +* +* @example +* var Float64Array = require( `@stdlib/array/float64` ); +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'float64', new Float64Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isFloat64MatrixLike( arr ); +* // returns true +* +* bool = isFloat64MatrixLike( [] ); +* // returns false +*/ +declare function isFloat64MatrixLike( v: any ): boolean; + + +// EXPORTS // + +export = isFloat64MatrixLike; diff --git a/is-float64matrix-like/docs/types/test.ts b/is-float64matrix-like/docs/types/test.ts new file mode 100644 index 00000000..0a8f3a3f --- /dev/null +++ b/is-float64matrix-like/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFloat64MatrixLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFloat64MatrixLike( [] ); // $ExpectType boolean +} + +// The function does not compile if provided an unsupported number of arguments... +{ + isFloat64MatrixLike(); // $ExpectError + isFloat64MatrixLike( 'abc', 123 ); // $ExpectError +} diff --git a/is-float64matrix-like/examples/index.js b/is-float64matrix-like/examples/index.js new file mode 100644 index 00000000..6b0acdfe --- /dev/null +++ b/is-float64matrix-like/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat64MatrixLike = require( './../lib' ); + +var buffer = new Float64Array( [ 0, 0, 0, 0 ] ); +var arr = ndarray( 'float64', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +console.log( isFloat64MatrixLike( arr ) ); +// => true + +console.log( isFloat64MatrixLike( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isFloat64MatrixLike( {} ) ); +// => false + +console.log( isFloat64MatrixLike( null ) ); +// => false diff --git a/is-float64matrix-like/lib/index.js b/is-float64matrix-like/lib/index.js new file mode 100644 index 00000000..2f4275b1 --- /dev/null +++ b/is-float64matrix-like/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a 2-dimensional ndarray-like object whose underlying data type is `float64`. +* +* @module @stdlib/assert/is-float64matrix-like +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var isFloat64MatrixLike = require( '@stdlib/assert/is-float64matrix-like' ); +* +* var buffer = new Float64Array( [ 0, 0, 0, 0 ] ); +* +* var arr = ndarray( 'float64', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isFloat64MatrixLike( arr ); +* // returns true +* +* bool = isFloat64MatrixLike( [] ); +* // returns false +*/ + +// MODULES // + +var isFloat64MatrixLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isFloat64MatrixLike; diff --git a/is-float64matrix-like/lib/main.js b/is-float64matrix-like/lib/main.js new file mode 100644 index 00000000..6f5c7144 --- /dev/null +++ b/is-float64matrix-like/lib/main.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isMatrixLike = require( '@stdlib/assert/is-matrix-like' ); +var isFloat64Array = require( '@stdlib/assert/is-float64array' ); + + +// MAIN // + +/** +* Tests if a value is a 2-dimensional ndarray-like object whose underlying data type is `float64`. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a 2-dimensional ndarray-like object whose underlying data type is `float64` +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var buffer = new Float64Array( [ 0, 0, 0, 0 ] ); +* var arr = ndarray( 'float64', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isFloat64MatrixLike( arr ); +* // returns true +* +* bool = isFloat64MatrixLike( [] ); +* // returns false +*/ +function isFloat64MatrixLike( v ) { + return ( isMatrixLike( v ) && isFloat64Array( v.data ) ); +} + + +// EXPORTS // + +module.exports = isFloat64MatrixLike; diff --git a/is-float64matrix-like/package.json b/is-float64matrix-like/package.json new file mode 100644 index 00000000..66250336 --- /dev/null +++ b/is-float64matrix-like/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-float64matrix-like", + "version": "0.0.0", + "description": "Test if a value is a 2-dimensional ndarray-like object containing double-precision floating-point numbers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "matrix", + "ismatrix", + "mat", + "2d", + "float64", + "double", + "double-precision", + "float64array", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-float64matrix-like/test/test.js b/is-float64matrix-like/test/test.js new file mode 100644 index 00000000..a2aad6e6 --- /dev/null +++ b/is-float64matrix-like/test/test.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat64MatrixLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isFloat64MatrixLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray containing double-precision floating-point numbers', function test( t ) { + var arr = ndarray( 'float64', new Float64Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + t.equal( isFloat64MatrixLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray-like object containing double-precision floating-point numbers', function test( t ) { + var arr = { + 'data': new Float64Array( [ 0, 0, 0, 0 ] ), + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'float64', + 'length': 4, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isFloat64MatrixLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a 2-dimensional ndarray-like object containing double-precision floating-point numbers', function test( t ) { + var values; + var arr1; + var arr2; + var i; + + arr1 = ndarray( 'float64', new Float64Array( [ 0, 0, 0 ] ), [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + arr2 = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + values = [ + arr1, + arr2, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isFloat64MatrixLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-float64ndarray-like/README.md b/is-float64ndarray-like/README.md new file mode 100644 index 00000000..95905c3a --- /dev/null +++ b/is-float64ndarray-like/README.md @@ -0,0 +1,88 @@ + + +# isFloat64ndarrayLike + +> Test if a value is an [ndarray][@stdlib/ndarray/ctor]-like object containing double-precision floating-point numbers. + +
+ +## Usage + +```javascript +var isFloat64ndarrayLike = require( '@stdlib/assert/is-float64ndarray-like' ); +``` + +#### isFloat64ndarrayLike( value ) + +Tests if a value is an [ndarray][@stdlib/ndarray/ctor]-like object whose underlying data type is `float64`. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'float64', new Float64Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +var bool = isFloat64ndarrayLike( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat64ndarrayLike = require( '@stdlib/assert/is-float64ndarray-like' ); + +var buffer = new Float64Array( [ 0, 0, 0, 0 ] ); +var arr = ndarray( 'float64', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +var out = isFloat64ndarrayLike( arr ); +// returns true + +out = isFloat64ndarrayLike( [ 1, 2, 3, 4 ] ); +// returns false + +out = isFloat64ndarrayLike( {} ); +// returns false + +out = isFloat64ndarrayLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-float64ndarray-like/benchmark/benchmark.js b/is-float64ndarray-like/benchmark/benchmark.js new file mode 100644 index 00000000..b3bfd8bf --- /dev/null +++ b/is-float64ndarray-like/benchmark/benchmark.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isFloat64ndarrayLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = new Float64Array( [ 0, 0, 0, 0 ] ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'float64', buffer, shape, strides, offset, order ); + + values = [ + arr, + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFloat64ndarrayLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-float64ndarray-like/docs/repl.txt b/is-float64ndarray-like/docs/repl.txt new file mode 100644 index 00000000..96070989 --- /dev/null +++ b/is-float64ndarray-like/docs/repl.txt @@ -0,0 +1,42 @@ + +{{alias}}( value ) + Tests if a value is an ndarray-like object containing double-precision + floating-point numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an ndarray-like object containing + double-precision floating-point numbers. + + Examples + -------- + > var M = {}; + > M.data = new {{alias:@stdlib/array/float64}}( [ 0, 0, 0, 0 ] ); + > M.ndims = 2; + > M.shape = [ 2, 2 ]; + > M.strides = [ 2, 1 ]; + > M.offset = 0; + > M.order = 'row-major'; + > M.dtype = 'float64'; + > M.length = 4; + > M.flags = {}; + > M.get = function get( i, j ) {}; + > M.set = function set( i, j ) {}; + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-float64ndarray-like/docs/types/index.d.ts b/is-float64ndarray-like/docs/types/index.d.ts new file mode 100644 index 00000000..f65438c6 --- /dev/null +++ b/is-float64ndarray-like/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an ndarray-like object whose underlying data type is `float64`. +* +* @param v - value to test +* @returns boolean indicating if a value is an ndarray-like object whose underlying data type is `float64` +* +* @example +* var Float64Array = require( `@stdlib/array/float64` ); +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'float64', new Float64Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isFloat64ndarrayLike( arr ); +* // returns true +* +* bool = isFloat64ndarrayLike( [] ); +* // returns false +*/ +declare function isFloat64ndarrayLike( v: any ): boolean; + + +// EXPORTS // + +export = isFloat64ndarrayLike; diff --git a/is-float64ndarray-like/docs/types/test.ts b/is-float64ndarray-like/docs/types/test.ts new file mode 100644 index 00000000..bd730e7c --- /dev/null +++ b/is-float64ndarray-like/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFloat64ndarrayLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFloat64ndarrayLike( [] ); // $ExpectType boolean +} + +// The function does not compile if provided an unsupported number of arguments... +{ + isFloat64ndarrayLike(); // $ExpectError + isFloat64ndarrayLike( 'abc', 123 ); // $ExpectError +} diff --git a/is-float64ndarray-like/examples/index.js b/is-float64ndarray-like/examples/index.js new file mode 100644 index 00000000..9308d9c0 --- /dev/null +++ b/is-float64ndarray-like/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat64ndarrayLike = require( './../lib' ); + +var buffer = new Float64Array( [ 0, 0, 0, 0 ] ); +var arr = ndarray( 'float64', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +console.log( isFloat64ndarrayLike( arr ) ); +// => true + +console.log( isFloat64ndarrayLike( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isFloat64ndarrayLike( {} ) ); +// => false + +console.log( isFloat64ndarrayLike( null ) ); +// => false diff --git a/is-float64ndarray-like/lib/index.js b/is-float64ndarray-like/lib/index.js new file mode 100644 index 00000000..25d094ca --- /dev/null +++ b/is-float64ndarray-like/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an ndarray-like object whose underlying data type is `float64`. +* +* @module @stdlib/assert/is-float64ndarray-like +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var isFloat64ndarrayLike = require( '@stdlib/assert/is-float64ndarray-like' ); +* +* var buffer = new Float64Array( [ 0, 0, 0, 0 ] ); +* +* var arr = ndarray( 'float64', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isFloat64ndarrayLike( arr ); +* // returns true +* +* bool = isFloat64ndarrayLike( [] ); +* // returns false +*/ + +// MODULES // + +var isFloat64ndarrayLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isFloat64ndarrayLike; diff --git a/is-float64ndarray-like/lib/main.js b/is-float64ndarray-like/lib/main.js new file mode 100644 index 00000000..a50b5673 --- /dev/null +++ b/is-float64ndarray-like/lib/main.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isFloat64Array = require( '@stdlib/assert/is-float64array' ); + + +// MAIN // + +/** +* Tests if a value is an ndarray-like object whose underlying data type is `float64`. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is an ndarray-like object whose underlying data type is `float64` +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var buffer = new Float64Array( [ 0, 0, 0, 0 ] ); +* var arr = ndarray( 'float64', buffer, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isFloat64ndarrayLike( arr ); +* // returns true +* +* bool = isFloat64ndarrayLike( [] ); +* // returns false +*/ +function isFloat64ndarrayLike( v ) { + return ( isndarrayLike( v ) && isFloat64Array( v.data ) ); +} + + +// EXPORTS // + +module.exports = isFloat64ndarrayLike; diff --git a/is-float64ndarray-like/package.json b/is-float64ndarray-like/package.json new file mode 100644 index 00000000..e4f7cf32 --- /dev/null +++ b/is-float64ndarray-like/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-float64ndarray-like", + "version": "0.0.0", + "description": "Test if a value is an ndarray-like object containing double-precision floating-point numbers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "array", + "nd", + "float64", + "double", + "double-precision", + "float64array", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-float64ndarray-like/test/test.js b/is-float64ndarray-like/test/test.js new file mode 100644 index 00000000..a3a59430 --- /dev/null +++ b/is-float64ndarray-like/test/test.js @@ -0,0 +1,89 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat64ndarrayLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isFloat64ndarrayLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an ndarray containing double-precision floating-point numbers', function test( t ) { + var arr = ndarray( 'float64', new Float64Array( [ 0, 0, 0, 0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + t.equal( isFloat64ndarrayLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an ndarray-like object containing double-precision floating-point numbers', function test( t ) { + var arr = { + 'data': new Float64Array( [ 0, 0, 0, 0 ] ), + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'float64', + 'length': 4, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isFloat64ndarrayLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an ndarray-like object containing double-precision floating-point numbers', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + values = [ + arr, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isFloat64ndarrayLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-float64vector-like/README.md b/is-float64vector-like/README.md new file mode 100644 index 00000000..e673cf83 --- /dev/null +++ b/is-float64vector-like/README.md @@ -0,0 +1,88 @@ + + +# isFloat64VectorLike + +> Test if a value is a 1-dimensional [ndarray][@stdlib/ndarray/ctor]-like object containing double-precision floating-point numbers. + +
+ +## Usage + +```javascript +var isFloat64VectorLike = require( '@stdlib/assert/is-float64vector-like' ); +``` + +#### isFloat64VectorLike( value ) + +Tests if a value is a 1-dimensional [ndarray][@stdlib/ndarray/ctor]-like object whose underlying data type is `float64`. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'float64', new Float64Array( [ 0, 0, 0, 0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + +var bool = isFloat64VectorLike( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat64VectorLike = require( '@stdlib/assert/is-float64vector-like' ); + +var buffer = new Float64Array( [ 0, 0, 0, 0 ] ); +var arr = ndarray( 'float64', buffer, [ 4 ], [ 1 ], 0, 'row-major' ); + +var out = isFloat64VectorLike( arr ); +// returns true + +out = isFloat64VectorLike( [ 1, 2, 3, 4 ] ); +// returns false + +out = isFloat64VectorLike( {} ); +// returns false + +out = isFloat64VectorLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-float64vector-like/benchmark/benchmark.js b/is-float64vector-like/benchmark/benchmark.js new file mode 100644 index 00000000..1245e0b6 --- /dev/null +++ b/is-float64vector-like/benchmark/benchmark.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isFloat64VectorLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = new Float64Array( [ 0, 0, 0, 0 ] ); + shape = [ 4 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'float64', buffer, shape, strides, offset, order ); + + values = [ + arr, + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFloat64VectorLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-float64vector-like/docs/repl.txt b/is-float64vector-like/docs/repl.txt new file mode 100644 index 00000000..f8c90487 --- /dev/null +++ b/is-float64vector-like/docs/repl.txt @@ -0,0 +1,42 @@ + +{{alias}}( value ) + Tests if a value is a 1-dimensional ndarray-like object containing double- + precision floating-point numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a 1-dimensional ndarray-like + object containing double-precision floating-point numbers. + + Examples + -------- + > var M = {}; + > M.data = new {{alias:@stdlib/array/float64}}( [ 0, 0, 0, 0 ] ); + > M.ndims = 1; + > M.shape = [ 4 ]; + > M.strides = [ 1 ]; + > M.offset = 0; + > M.order = 'row-major'; + > M.dtype = 'float64'; + > M.length = 4; + > M.flags = {}; + > M.get = function get( i, j ) {}; + > M.set = function set( i, j ) {}; + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-float64vector-like/docs/types/index.d.ts b/is-float64vector-like/docs/types/index.d.ts new file mode 100644 index 00000000..ab34bdf6 --- /dev/null +++ b/is-float64vector-like/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a 1-dimensional ndarray-like object whose underlying data type is `float64`. +* +* @param v - value to test +* @returns boolean indicating if a value is a 1-dimensional ndarray-like object whose underlying data type is `float64` +* +* @example +* var Float64Array = require( `@stdlib/array/float64` ); +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'float64', new Float64Array( [ 0, 0, 0, 0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var bool = isFloat64VectorLike( arr ); +* // returns true +* +* bool = isFloat64VectorLike( [] ); +* // returns false +*/ +declare function isFloat64VectorLike( v: any ): boolean; + + +// EXPORTS // + +export = isFloat64VectorLike; diff --git a/is-float64vector-like/docs/types/test.ts b/is-float64vector-like/docs/types/test.ts new file mode 100644 index 00000000..32fb8787 --- /dev/null +++ b/is-float64vector-like/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFloat64VectorLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFloat64VectorLike( [] ); // $ExpectType boolean +} + +// The function does not compile if provided an unsupported number of arguments... +{ + isFloat64VectorLike(); // $ExpectError + isFloat64VectorLike( 'abc', 123 ); // $ExpectError +} diff --git a/is-float64vector-like/examples/index.js b/is-float64vector-like/examples/index.js new file mode 100644 index 00000000..9d780c49 --- /dev/null +++ b/is-float64vector-like/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat64VectorLike = require( './../lib' ); + +var buffer = new Float64Array( [ 0, 0, 0, 0 ] ); +var arr = ndarray( 'float64', buffer, [ 4 ], [ 1 ], 0, 'row-major' ); + +console.log( isFloat64VectorLike( arr ) ); +// => true + +console.log( isFloat64VectorLike( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isFloat64VectorLike( {} ) ); +// => false + +console.log( isFloat64VectorLike( null ) ); +// => false diff --git a/is-float64vector-like/lib/index.js b/is-float64vector-like/lib/index.js new file mode 100644 index 00000000..94183dbf --- /dev/null +++ b/is-float64vector-like/lib/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a 1-dimensional ndarray-like object whose underlying data type is `float64`. +* +* @module @stdlib/assert/is-float64vector-like +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var isFloat64VectorLike = require( '@stdlib/assert/is-float64vector-like' ); +* +* var buffer = new Float64Array( [ 0, 0, 0, 0 ] ); +* var arr = ndarray( 'float64', buffer, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var bool = isFloat64VectorLike( arr ); +* // returns true +* +* bool = isFloat64VectorLike( [] ); +* // returns false +*/ + +// MODULES // + +var isFloat64VectorLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isFloat64VectorLike; diff --git a/is-float64vector-like/lib/main.js b/is-float64vector-like/lib/main.js new file mode 100644 index 00000000..d4e3d087 --- /dev/null +++ b/is-float64vector-like/lib/main.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isVectorLike = require( '@stdlib/assert/is-vector-like' ); +var isFloat64Array = require( '@stdlib/assert/is-float64array' ); + + +// MAIN // + +/** +* Tests if a value is a 1-dimensional ndarray-like object whose underlying data type is `float64`. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a 1-dimensional ndarray-like object whose underlying data type is `float64` +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var buffer = new Float64Array( [ 0, 0, 0, 0 ] ); +* var arr = ndarray( 'float64', buffer, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* var bool = isFloat64VectorLike( arr ); +* // returns true +* +* bool = isFloat64VectorLike( [] ); +* // returns false +*/ +function isFloat64VectorLike( v ) { + return ( isVectorLike( v ) && isFloat64Array( v.data ) ); +} + + +// EXPORTS // + +module.exports = isFloat64VectorLike; diff --git a/is-float64vector-like/package.json b/is-float64vector-like/package.json new file mode 100644 index 00000000..d3a85568 --- /dev/null +++ b/is-float64vector-like/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-float64vector-like", + "version": "0.0.0", + "description": "Test if a value is a 1-dimensional ndarray-like object containing double-precision floating-point numbers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "vector", + "isvector", + "vec", + "1d", + "float64", + "double", + "double-precision", + "float64array", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-float64vector-like/test/test.js b/is-float64vector-like/test/test.js new file mode 100644 index 00000000..4b970bab --- /dev/null +++ b/is-float64vector-like/test/test.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isFloat64VectorLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isFloat64VectorLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 1-dimensional ndarray containing double-precision floating-point numbers', function test( t ) { + var arr = ndarray( 'float64', new Float64Array( [ 0, 0, 0, 0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); + + t.equal( isFloat64VectorLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 1-dimensional ndarray-like object containing double-precision floating-point numbers', function test( t ) { + var arr = { + 'data': new Float64Array( [ 0, 0, 0, 0 ] ), + 'shape': [ 4 ], + 'strides': [ 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 1, + 'dtype': 'float64', + 'length': 4, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isFloat64VectorLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a 1-dimensional ndarray-like object containing double-precision floating-point numbers', function test( t ) { + var values; + var arr1; + var arr2; + var i; + + arr1 = ndarray( 'float64', new Float64Array( [ 0, 0, 0 ] ), [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + arr2 = ndarray( 'generic', [ 0, 0, 0 ], [ 3 ], [ 1 ], 0, 'row-major' ); + + values = [ + arr1, + arr2, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isFloat64VectorLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-function-array/README.md b/is-function-array/README.md new file mode 100644 index 00000000..e9154d58 --- /dev/null +++ b/is-function-array/README.md @@ -0,0 +1,101 @@ + + +# isFunctionArray + +> Test if a value is an array-like object containing only functions. + +
+ +## Usage + +```javascript +var isFunctionArray = require( '@stdlib/assert/is-function-array' ); +``` + +#### isFunctionArray( value ) + +Tests if a `value` is an array-like `object` containing only `functions`. + +```javascript +function beep() { + console.log( 'beep' ); +} + +function boop() { + console.log( 'boop' ); +} + +var bool = isFunctionArray( [ beep, boop ] ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isFunctionArray = require( '@stdlib/assert/is-function-array' ); + +function beep() { + console.log( 'beep' ); +} + +function boop() { + console.log( 'boop' ); +} + +var bool = isFunctionArray( [ beep, boop ] ); +// returns true + +bool = isFunctionArray( [ beep, {} ] ); +// returns false + +bool = isFunctionArray( [ [], {} ] ); +// returns false + +bool = isFunctionArray( [ 'a', 'b' ] ); +// returns false + +bool = isFunctionArray( [] ); +// returns false + +bool = isFunctionArray( beep ); +// returns false + +bool = isFunctionArray( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-function-array/benchmark/benchmark.js b/is-function-array/benchmark/benchmark.js new file mode 100644 index 00000000..8f21dbe3 --- /dev/null +++ b/is-function-array/benchmark/benchmark.js @@ -0,0 +1,104 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var noop = require( '@stdlib/utils/noop' ); +var pkg = require( './../package.json' ).name; +var isFunctionArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( noop ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i; + bool = isFunctionArray( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/is-function-array/docs/repl.txt b/is-function-array/docs/repl.txt new file mode 100644 index 00000000..4c5392dc --- /dev/null +++ b/is-function-array/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only functions. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + functions. + + Examples + -------- + > function beep() {}; + > function boop() {}; + > var bool = {{alias}}( [ beep, boop ] ) + true + > bool = {{alias}}( [ {}, beep ] ) + false + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-function-array/docs/types/index.d.ts b/is-function-array/docs/types/index.d.ts new file mode 100644 index 00000000..785be2b3 --- /dev/null +++ b/is-function-array/docs/types/index.d.ts @@ -0,0 +1,46 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Test if a value is an array-like object containing only functions. +* +* @param value - value to test +* @returns boolean indicating whether an input value is an array-like object containing only functions +* +* @example +* function beep() {} +* +* function boop() {} +* +* var bool = isFunctionArray( [ beep, boop ] ); +* // returns true +* +* bool = isFunctionArray( [ {}, beep ] ); +* // returns false +* +* bool = isFunctionArray( [] ); +* // returns false +*/ +declare function isFunctionArray( value: any ): boolean; + + +// EXPORTS // + +export = isFunctionArray; diff --git a/is-function-array/docs/types/test.ts b/is-function-array/docs/types/test.ts new file mode 100644 index 00000000..38e703b4 --- /dev/null +++ b/is-function-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFunctionArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFunctionArray( [ 'abc', 'def' ] ); // $ExpectType boolean + isFunctionArray( [ ( x: number ) => x ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isFunctionArray(); // $ExpectError + isFunctionArray( [], 123 ); // $ExpectError +} diff --git a/is-function-array/examples/index.js b/is-function-array/examples/index.js new file mode 100644 index 00000000..f130dc2a --- /dev/null +++ b/is-function-array/examples/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isFunctionArray = require( './../lib' ); + +function beep() { + console.log( 'beep' ); +} + +function boop() { + console.log( 'boop' ); +} + +var bool = isFunctionArray( [ beep, boop ] ); +console.log( bool ); +// => true + +bool = isFunctionArray( [ beep, {} ] ); +console.log( bool ); +// => false + +bool = isFunctionArray( [ [], {} ] ); +console.log( bool ); +// => false + +bool = isFunctionArray( [ 'a', 'b' ] ); +console.log( bool ); +// => false + +bool = isFunctionArray( [] ); +console.log( bool ); +// => false + +bool = isFunctionArray( beep ); +console.log( bool ); +// => false + +bool = isFunctionArray( null ); +console.log( bool ); +// => false diff --git a/is-function-array/lib/index.js b/is-function-array/lib/index.js new file mode 100644 index 00000000..111e0355 --- /dev/null +++ b/is-function-array/lib/index.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only functions. +* +* @module @stdlib/assert/is-function-array +* +* @example +* var isFunctionArray = require( '@stdlib/assert/is-function-array' ); +* +* function beep() {} +* +* function boop() {} +* +* var bool = isFunctionArray( [ beep, boop ] ); +* // returns true +* +* bool = isFunctionArray( [ {}, beep ] ); +* // returns false +* +* bool = isFunctionArray( [] ); +* // returns false +*/ + +// MODULES // + +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); +var isFunction = require( '@stdlib/assert/is-function' ); + + +// MAIN // + +var isFunctionArray = arrayfun( isFunction ); + + +// EXPORTS // + +module.exports = isFunctionArray; diff --git a/is-function-array/package.json b/is-function-array/package.json new file mode 100644 index 00000000..a0ee41b7 --- /dev/null +++ b/is-function-array/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-function-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only functions.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "function", + "array", + "is", + "isfunction", + "isarray", + "array-like", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-function-array/test/test.js b/is-function-array/test/test.js new file mode 100644 index 00000000..3ef79df8 --- /dev/null +++ b/is-function-array/test/test.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isFunctionArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isFunctionArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests if a value is an array of functions', function test( t ) { + var bool; + var arr; + + function beep() {} + function boop() {} + + arr = [ beep, boop ]; + bool = isFunctionArray( arr ); + t.strictEqual( bool, true, 'returns true' ); + + arr = [ beep, null ]; + bool = isFunctionArray( arr ); + t.strictEqual( bool, false, 'returns false' ); + + arr = []; + bool = isFunctionArray( arr ); + t.strictEqual( bool, false, 'returns false when provided an empty array' ); + + bool = isFunctionArray( null ); + t.strictEqual( bool, false, 'returns false when provided a null' ); + + bool = isFunctionArray( beep ); + t.strictEqual( bool, false, 'returns false when provided a function' ); + + t.end(); +}); diff --git a/is-function/README.md b/is-function/README.md new file mode 100644 index 00000000..f53875aa --- /dev/null +++ b/is-function/README.md @@ -0,0 +1,91 @@ + + +# isFunction + +> Test if a value is a function. + +
+ +## Usage + +```javascript +var isFunction = require( '@stdlib/assert/is-function' ); +``` + +#### isFunction( value ) + +Tests if a `value` is a `function`. + +```javascript +function beep() { + console.log( 'beep' ); +} + +var bool = isFunction( beep ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isFunction = require( '@stdlib/assert/is-function' ); + +var bool = isFunction( function foo() {} ); +// returns true + +bool = isFunction( 'beep' ); +// returns false + +bool = isFunction( 5 ); +// returns false + +bool = isFunction( true ); +// returns false + +bool = isFunction( null ); +// returns false + +bool = isFunction( [] ); +// returns false + +bool = isFunction( {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-function/benchmark/benchmark.js b/is-function/benchmark/benchmark.js new file mode 100644 index 00000000..65984ff6 --- /dev/null +++ b/is-function/benchmark/benchmark.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isFunction = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + new RegExp( '.*' ), + new Date(), + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isFunction( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-function/docs/repl.txt b/is-function/docs/repl.txt new file mode 100644 index 00000000..61e7871a --- /dev/null +++ b/is-function/docs/repl.txt @@ -0,0 +1,25 @@ + +{{alias}}( value ) + Tests if a value is a function. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a function. + + Examples + -------- + > function beep() {}; + > var bool = {{alias}}( beep ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-function/docs/types/index.d.ts b/is-function/docs/types/index.d.ts new file mode 100644 index 00000000..2053c1e6 --- /dev/null +++ b/is-function/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a function. +* +* @param value - value to test +* @returns boolean indicating whether value is a function +* +* @example +* function beep() { +* return 'beep'; +* } +* +* var bool = isFunction( beep ); +* // returns true +*/ +declare function isFunction( value: any ): boolean; + + +// EXPORTS // + +export = isFunction; diff --git a/is-function/docs/types/test.ts b/is-function/docs/types/test.ts new file mode 100644 index 00000000..41de7d4c --- /dev/null +++ b/is-function/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isFunction = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isFunction( ( x: number ) => x ); // $ExpectType boolean + isFunction( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isFunction(); // $ExpectError + isFunction( ( x: number ) => x, 123 ); // $ExpectError +} diff --git a/is-function/examples/index.js b/is-function/examples/index.js new file mode 100644 index 00000000..34c0b3d9 --- /dev/null +++ b/is-function/examples/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function, no-restricted-syntax */ + +'use strict'; + +var isFunction = require( './../lib' ); + +console.log( isFunction( function foo() {} ) ); +// => true + +console.log( isFunction( 'beep' ) ); +// => false + +console.log( isFunction( 5 ) ); +// => false + +console.log( isFunction( true ) ); +// => false + +console.log( isFunction( null ) ); +// => false + +console.log( isFunction( [] ) ); +// => false + +console.log( isFunction( {} ) ); +// => false diff --git a/is-function/lib/index.js b/is-function/lib/index.js new file mode 100644 index 00000000..e70e3eba --- /dev/null +++ b/is-function/lib/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a function. +* +* @module @stdlib/assert/is-function +* +* @example +* var isFunction = require( '@stdlib/assert/is-function' ); +* +* function beep() { +* return 'beep'; +* } +* +* var bool = isFunction( beep ); +* // returns true +*/ + +// MODULES // + +var isFunction = require( './main.js' ); + + +// EXPORTS // + +module.exports = isFunction; diff --git a/is-function/lib/main.js b/is-function/lib/main.js new file mode 100644 index 00000000..ed9b48dd --- /dev/null +++ b/is-function/lib/main.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var typeOf = require( '@stdlib/utils/type-of' ); + + +// MAIN // + +/** +* Tests if a value is a function. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a function +* +* @example +* function beep() { +* return 'beep'; +* } +* +* var bool = isFunction( beep ); +* // returns true +*/ +function isFunction( value ) { + // Note: cannot use `typeof` directly, as various browser engines incorrectly return `'function'` when operating on non-function objects, such as regular expressions and NodeLists. + return ( typeOf( value ) === 'function' ); +} + + +// EXPORTS // + +module.exports = isFunction; diff --git a/is-function/package.json b/is-function/package.json new file mode 100644 index 00000000..ebd72897 --- /dev/null +++ b/is-function/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/assert/is-function", + "version": "0.0.0", + "description": "Test if a value is a function.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "function", + "is", + "isfunction", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-function/test/test.js b/is-function/test/test.js new file mode 100644 index 00000000..e1ebf254 --- /dev/null +++ b/is-function/test/test.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isFunction = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isFunction, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a function', function test( t ) { + function beep() { + return 'boop'; + } + t.strictEqual( isFunction( beep ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + undefined, + true, + false, + null, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isFunction( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-generator-object-like/README.md b/is-generator-object-like/README.md new file mode 100644 index 00000000..b8a06ee6 --- /dev/null +++ b/is-generator-object-like/README.md @@ -0,0 +1,94 @@ + + +# isGeneratorObjectLike + +> Test if a value is [`generator`][mdn-generator-object] object-like. + +
+ +## Usage + +```javascript +var isGeneratorObjectLike = require( '@stdlib/assert/is-generator-object-like' ); +``` + +#### isGeneratorObjectLike( value ) + +Tests if a `value` is [`generator`][mdn-generator-object] object-like. + + + +```javascript +var obj = { + 'next': function noop() {}, + 'return': function noop() {}, + 'throw': function noop() {} +}; +var bool = isGeneratorObjectLike( obj ); +// returns true + +bool = isGeneratorObjectLike( {} ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var noop = require( '@stdlib/utils/noop' ); +var isGeneratorObjectLike = require( '@stdlib/assert/is-generator-object-like' ); + +var obj = { + 'next': noop, + 'return': noop, + 'throw': noop +}; +var bool = isGeneratorObjectLike( obj ); +// returns true + +bool = isGeneratorObjectLike( {} ); +// returns false + +bool = isGeneratorObjectLike( [] ); +// returns false + +bool = isGeneratorObjectLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-generator-object-like/benchmark/benchmark.js b/is-generator-object-like/benchmark/benchmark.js new file mode 100644 index 00000000..0681c680 --- /dev/null +++ b/is-generator-object-like/benchmark/benchmark.js @@ -0,0 +1,92 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var evil = require( '@stdlib/utils/eval' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var hasGeneratorsSupport = require( '@stdlib/assert/has-generator-support' ); +var pkg = require( './../package.json' ).name; +var isGeneratorObjectLike = require( './../lib' ); + + +// FUNCTIONS // + +function createGenerator() { + var str = '(function () {'; + str += 'function* generator() {'; + str += ' while ( true ) {'; + str += ' yield 1.0;'; + str += ' }'; + str += '}'; + str += 'return generator;'; + str += '})()'; + return evil( str ); +} + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var generator; + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + { + 'next': function noop() {}, + 'return': function noop() {}, + 'throw': function noop() {} + } + ]; + + if ( hasGeneratorsSupport() ) { + generator = createGenerator(); + values.push( generator() ); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isGeneratorObjectLike( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-generator-object-like/docs/repl.txt b/is-generator-object-like/docs/repl.txt new file mode 100644 index 00000000..f07f842d --- /dev/null +++ b/is-generator-object-like/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( value ) + Tests if a value is generator object-like. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is generator object-like. + + Examples + -------- + > var obj = { + ... 'next': function noop() {}, + ... 'return': function noop() {}, + ... 'throw': function noop() {} + ... }; + > var bool = {{alias}}( obj ) + true + > bool = {{alias}}( {} ) + false + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-generator-object-like/docs/types/index.d.ts b/is-generator-object-like/docs/types/index.d.ts new file mode 100644 index 00000000..4375037b --- /dev/null +++ b/is-generator-object-like/docs/types/index.d.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is generator object-like. +* +* @param value - value to test +* @returns boolean indicating whether value is generator object-like +* +* @example +* var gen = { +* 'next': function noop() {}, +* 'return': function noop() {}, +* 'throw': function noop() {} +* }; +* var bool = isGeneratorObjectLike( gen ); +* // returns true +* +* @example +* function* generateID() { +* var idx = 0; +* while ( idx < idx+1 ) { +* yield idx; +* idx += 1; +* } +* } +* var bool = isGeneratorObjectLike( generateID() ); +* // returns true +* +* @example +* var bool = isGeneratorObjectLike( {} ); +* // returns false +* +* @example +* var bool = isGeneratorObjectLike( null ); +* // returns false +*/ +declare function isGeneratorObjectLike( value: any ): boolean; + + +// EXPORTS // + +export = isGeneratorObjectLike; diff --git a/is-generator-object-like/docs/types/test.ts b/is-generator-object-like/docs/types/test.ts new file mode 100644 index 00000000..cf15256d --- /dev/null +++ b/is-generator-object-like/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isGeneratorObjectLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isGeneratorObjectLike( {} ); // $ExpectType boolean + isGeneratorObjectLike( null ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isGeneratorObjectLike(); // $ExpectError + isGeneratorObjectLike( {}, 123 ); // $ExpectError +} diff --git a/is-generator-object-like/examples/index.js b/is-generator-object-like/examples/index.js new file mode 100644 index 00000000..44c6a9c7 --- /dev/null +++ b/is-generator-object-like/examples/index.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var noop = require( '@stdlib/utils/noop' ); +var isGeneratorObjectLike = require( './../lib' ); + +var obj = { + 'next': noop, + 'return': noop, + 'throw': noop +}; +console.log( isGeneratorObjectLike( obj ) ); +// => true + +console.log( isGeneratorObjectLike( {} ) ); +// => false + +console.log( isGeneratorObjectLike( [] ) ); +// => false + +console.log( isGeneratorObjectLike( null ) ); +// => false diff --git a/is-generator-object-like/lib/index.js b/is-generator-object-like/lib/index.js new file mode 100644 index 00000000..03967d3b --- /dev/null +++ b/is-generator-object-like/lib/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is generator object-like. +* +* @module @stdlib/assert/is-generator-object-like +* +* @example +* var isGeneratorObjectLike = require( '@stdlib/assert/is-generator-object-like' ); +* +* var gen = { +* 'next': function noop() {}, +* 'return': function noop() {}, +* 'throw': function noop() {} +* }; +* var bool = isGeneratorObjectLike( gen ); +* // returns true +* +* bool = isGeneratorObjectLike( {} ); +* // returns false +*/ + +// MODULES // + +var isGeneratorObject = require( './main.js' ); + + +// EXPORTS // + +module.exports = isGeneratorObject; diff --git a/is-generator-object-like/lib/main.js b/is-generator-object-like/lib/main.js new file mode 100644 index 00000000..9e9e893f --- /dev/null +++ b/is-generator-object-like/lib/main.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests if a value is generator object-like. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is generator object-like +* +* @example +* var gen = { +* 'next': function noop() {}, +* 'return': function noop() {}, +* 'throw': function noop() {} +* }; +* var bool = isGeneratorObjectLike( gen ); +* // returns true +* +* @example +* function* generateID() { +* var idx = 0; +* while ( idx < idx+1 ) { +* yield idx; +* idx += 1; +* } +* } +* var bool = isGeneratorObjectLike( generateID() ); +* // returns true +* +* @example +* var bool = isGeneratorObjectLike( {} ); +* // returns false +* +* @example +* var bool = isGeneratorObjectLike( null ); +* // returns false +*/ +function isGeneratorObjectLike( value ) { + return ( + value !== null && + typeof value === 'object' && + typeof value.next === 'function' && + typeof value.return === 'function' && + typeof value.throw === 'function' + ); +} + + +// EXPORTS // + +module.exports = isGeneratorObjectLike; diff --git a/is-generator-object-like/package.json b/is-generator-object-like/package.json new file mode 100644 index 00000000..e33f6300 --- /dev/null +++ b/is-generator-object-like/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-generator-object-like", + "version": "0.0.0", + "description": "Test if a value is generator object-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "object", + "obj", + "is", + "isobject", + "isgenerator", + "generator", + "generator object", + "iterator", + "yield", + "es2015", + "es6", + "type", + "check", + "validate", + "test" + ] +} diff --git a/is-generator-object-like/test/es2015-generator/index.js b/is-generator-object-like/test/es2015-generator/index.js new file mode 100644 index 00000000..d312b1d3 --- /dev/null +++ b/is-generator-object-like/test/es2015-generator/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/* eslint-disable no-restricted-syntax */ + +// MODULES // + +var tape = require( 'tape' ); +var isGeneratorObject = require( './../../lib' ); + + +// FUNCTIONS // + +var GeneratorFunction = Object.getPrototypeOf( function* gen() {} ).constructor; + + +// FUNCTIONS // + +function* generateID() { + var idx = 0; + while ( idx < idx+1 ) { + yield idx; + idx += 1; + } +} + + +// TESTS // + +tape( 'the function returns `true` if provided a generator object', function test( t ) { + var gen = generateID(); + t.equal( isGeneratorObject( gen ), true, 'returns true' ); + + gen = new GeneratorFunction( 'idx', 'yield idx' )(); + t.equal( isGeneratorObject( gen ), true, 'returns true' ); + t.end(); +}); diff --git a/is-generator-object-like/test/test.js b/is-generator-object-like/test/test.js new file mode 100644 index 00000000..958bbe9b --- /dev/null +++ b/is-generator-object-like/test/test.js @@ -0,0 +1,77 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var detectGeneratorSupport = require( '@stdlib/assert/has-generator-support' ); +var noop = require( '@stdlib/utils/noop' ); +var isGeneratorObject = require( './../lib' ); + + +// VARIABLES // + +var hasGeneratorsSupport = detectGeneratorSupport(); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isGeneratorObject, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a generator object-like value', function test( t ) { + var values; + var i; + values = [ + '5', + 5, + true, + void 0, + null, + NaN, + [], + function noop() {}, + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isGeneratorObject( values[i] ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided a generator object-like value', function test( t ) { + var gen = { + 'next': noop, + 'return': noop, + 'throw': noop + }; + t.equal( isGeneratorObject( gen ), true, 'returns true' ); + t.end(); +}); + +// Run generator function tests if environment supports `function*()`... + +if ( hasGeneratorsSupport ) { + require( './es2015-generator' ); // eslint-disable-line stdlib/no-unassigned-require +} diff --git a/is-generator-object/README.md b/is-generator-object/README.md new file mode 100644 index 00000000..6b3e2504 --- /dev/null +++ b/is-generator-object/README.md @@ -0,0 +1,102 @@ + + +# isGeneratorObject + +> Test if a value is a [`generator`][mdn-generator-object] object. + +
+ +## Usage + +```javascript +var isGeneratorObject = require( '@stdlib/assert/is-generator-object' ); +``` + +#### isGeneratorObject( value ) + +Tests if a `value` is a [`generator`][mdn-generator-object] object. + + + +```javascript +function* generateID() { + var idx = 0; + while ( idx < idx+1 ) { + yield idx; + idx += 1; + } +} + +var bool = isGeneratorObject( generateID() ); +// returns true + +bool = isGeneratorObject( generateID ); +// returns false + +bool = isGeneratorObject( {} ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isGeneratorObject = require( '@stdlib/assert/is-generator-object' ); + +function* generator() { + while ( true ) { + yield 1.0; + } +} + +var bool = isGeneratorObject( generator() ); +// returns true + +bool = isGeneratorObject( {} ); +// returns false + +bool = isGeneratorObject( [] ); +// returns false + +bool = isGeneratorObject( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-generator-object/benchmark/benchmark.js b/is-generator-object/benchmark/benchmark.js new file mode 100644 index 00000000..53b4fab6 --- /dev/null +++ b/is-generator-object/benchmark/benchmark.js @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var evil = require( '@stdlib/utils/eval' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var hasGeneratorsSupport = require( '@stdlib/assert/has-generator-support' ); +var pkg = require( './../package.json' ).name; +var isGeneratorObject = require( './../lib' ); + + +// FUNCTIONS // + +function createGenerator() { + var str = '(function () {'; + str += 'function* generator() {'; + str += ' while ( true ) {'; + str += ' yield 1.0;'; + str += ' }'; + str += '}'; + str += 'return generator;'; + str += '})()'; + return evil( str ); +} + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var generator; + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + if ( hasGeneratorsSupport() ) { + generator = createGenerator(); + values.push( generator() ); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isGeneratorObject( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-generator-object/docs/repl.txt b/is-generator-object/docs/repl.txt new file mode 100644 index 00000000..5505e787 --- /dev/null +++ b/is-generator-object/docs/repl.txt @@ -0,0 +1,35 @@ + +{{alias}}( value ) + Tests if a value is a generator object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a generator object. + + Examples + -------- + > function* generateID() { + ... var idx = 0; + ... while ( idx < idx+1 ) { + ... yield idx; + ... idx += 1; + ... } + ... }; + > var bool = {{alias}}( generateID() ) + true + > bool = {{alias}}( generateID ) + false + > bool = {{alias}}( {} ) + false + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-generator-object/docs/types/index.d.ts b/is-generator-object/docs/types/index.d.ts new file mode 100644 index 00000000..f2a66578 --- /dev/null +++ b/is-generator-object/docs/types/index.d.ts @@ -0,0 +1,51 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a generator object. +* +* @param value - value to test +* @returns boolean indicating whether value is a generator object +* +* @example +* function* generateID() { +* var idx = 0; +* while ( idx < idx+1 ) { +* yield idx; +* idx += 1; +* } +* } +* var bool = isGeneratorObject( generateID() ); +* // returns true +* +* @example +* var bool = isGeneratorObject( {} ); +* // returns false +* +* @example +* var bool = isGeneratorObject( null ); +* // returns false +*/ +declare function isGeneratorObject( value: any ): boolean; + + +// EXPORTS // + +export = isGeneratorObject; diff --git a/is-generator-object/docs/types/test.ts b/is-generator-object/docs/types/test.ts new file mode 100644 index 00000000..875d518e --- /dev/null +++ b/is-generator-object/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isGeneratorObject = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isGeneratorObject( {} ); // $ExpectType boolean + isGeneratorObject( null ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isGeneratorObject(); // $ExpectError + isGeneratorObject( {}, 123 ); // $ExpectError +} diff --git a/is-generator-object/examples/index.js b/is-generator-object/examples/index.js new file mode 100644 index 00000000..3dca9acc --- /dev/null +++ b/is-generator-object/examples/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasGeneratorsSupport = require( '@stdlib/assert/has-generator-support' ); +var evil = require( '@stdlib/utils/eval' ); +var isGeneratorObject = require( './../lib' ); + +function createGenerator() { + var str = '(function () {'; + str += 'function* generator() {'; + str += ' while ( true ) {'; + str += ' yield 1.0;'; + str += ' }'; + str += '}'; + str += 'return generator;'; + str += '})()'; + return evil( str ); +} + +var generator; +if ( hasGeneratorsSupport() ) { + generator = createGenerator(); + console.log( isGeneratorObject( generator() ) ); + // => true +} + +console.log( isGeneratorObject( {} ) ); +// => false + +console.log( isGeneratorObject( [] ) ); +// => false + +console.log( isGeneratorObject( null ) ); +// => false diff --git a/is-generator-object/lib/index.js b/is-generator-object/lib/index.js new file mode 100644 index 00000000..51d4cadc --- /dev/null +++ b/is-generator-object/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a generator object. +* +* @module @stdlib/assert/is-generator-object +* +* @example +* var isGeneratorObject = require( '@stdlib/assert/is-generator-object' ); +* +* function* generateID() { +* var idx = 0; +* while ( idx < idx+1 ) { +* yield idx; +* idx += 1; +* } +* } +* +* var bool = isGeneratorObject( generateID() ); +* // returns true +* +* bool = isGeneratorObject( generateID ); +* // returns false +* +* bool = isGeneratorObject( {} ); +* // returns false +*/ + +// MODULES // + +var isGeneratorObject = require( './main.js' ); + + +// EXPORTS // + +module.exports = isGeneratorObject; diff --git a/is-generator-object/lib/main.js b/is-generator-object/lib/main.js new file mode 100644 index 00000000..1dec2458 --- /dev/null +++ b/is-generator-object/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasGeneratorSupport = require( '@stdlib/assert/has-generator-support' ); +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var hasGenerators = hasGeneratorSupport(); + + +// MAIN // + +/** +* Tests if a value is a generator object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a generator object +* +* @example +* function* generateID() { +* var idx = 0; +* while ( idx < idx+1 ) { +* yield idx; +* idx += 1; +* } +* } +* var bool = isGeneratorObject( generateID() ); +* // returns true +* +* @example +* var bool = isGeneratorObject( {} ); +* // returns false +* +* @example +* var bool = isGeneratorObject( null ); +* // returns false +*/ +function isGeneratorObject( value ) { + return ( hasGenerators && nativeClass( value ) === '[object Generator]' ); +} + + +// EXPORTS // + +module.exports = isGeneratorObject; diff --git a/is-generator-object/package.json b/is-generator-object/package.json new file mode 100644 index 00000000..918f0739 --- /dev/null +++ b/is-generator-object/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-generator-object", + "version": "0.0.0", + "description": "Test if a value is a generator object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "object", + "obj", + "is", + "isobject", + "isgenerator", + "generator", + "generator object", + "iterator", + "yield", + "es2015", + "es6", + "type", + "check", + "validate", + "test" + ] +} diff --git a/is-generator-object/test/es2015-generator/index.js b/is-generator-object/test/es2015-generator/index.js new file mode 100644 index 00000000..d312b1d3 --- /dev/null +++ b/is-generator-object/test/es2015-generator/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/* eslint-disable no-restricted-syntax */ + +// MODULES // + +var tape = require( 'tape' ); +var isGeneratorObject = require( './../../lib' ); + + +// FUNCTIONS // + +var GeneratorFunction = Object.getPrototypeOf( function* gen() {} ).constructor; + + +// FUNCTIONS // + +function* generateID() { + var idx = 0; + while ( idx < idx+1 ) { + yield idx; + idx += 1; + } +} + + +// TESTS // + +tape( 'the function returns `true` if provided a generator object', function test( t ) { + var gen = generateID(); + t.equal( isGeneratorObject( gen ), true, 'returns true' ); + + gen = new GeneratorFunction( 'idx', 'yield idx' )(); + t.equal( isGeneratorObject( gen ), true, 'returns true' ); + t.end(); +}); diff --git a/is-generator-object/test/test.js b/is-generator-object/test/test.js new file mode 100644 index 00000000..496d2e6c --- /dev/null +++ b/is-generator-object/test/test.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var detectGeneratorSupport = require( '@stdlib/assert/has-generator-support' ); +var isGeneratorObject = require( './../lib' ); + + +// VARIABLES // + +var hasGeneratorsSupport = detectGeneratorSupport(); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isGeneratorObject, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a generator object', function test( t ) { + var values; + var i; + values = [ + '5', + 5, + true, + void 0, + null, + NaN, + [], + function noop() {}, + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isGeneratorObject( values[i] ), false, 'returns false' ); + } + t.end(); +}); + +// Run generator function tests if environment supports `function*()`... + +if ( hasGeneratorsSupport ) { + require( './es2015-generator' ); // eslint-disable-line stdlib/no-unassigned-require +} diff --git a/is-gzip-buffer/README.md b/is-gzip-buffer/README.md new file mode 100644 index 00000000..1fe40f50 --- /dev/null +++ b/is-gzip-buffer/README.md @@ -0,0 +1,144 @@ + + +# isgzipBuffer + +> Test if a value is a [gzip][gzip-rfc-1952] buffer. + +
+ +## Usage + +```javascript +var isgzipBuffer = require( '@stdlib/assert/is-gzip-buffer' ); +``` + +#### isgzipBuffer( value ) + +Tests if a value is a [`gzip`][gzip-rfc-1952] buffer. + +```javascript +var Uint8Array = require( '@stdlib/array/uint8' ); + +var buf = new Uint8Array( 20 ); +buf[ 0 ] = 31; // 0x1f => magic number +buf[ 1 ] = 139; // 0x8b +buf[ 2 ] = 8; // 0x08 => compression method + +var bool = isgzipBuffer( buf ); +// returns true + +bool = isgzipBuffer( [] ); +// returns false +``` + +
+ + + +
+ +## Notes + +- A [gzip][gzip-rfc-1952] buffer is defined as either a Node.js [`Buffer`][@stdlib/buffer/ctor] or [`Uint8Array`][@stdlib/array/uint8] which contains a 10-byte header, a body containing the compressed payload, and an 8-byte footer containing a CRC-32 checksum and the length of the original uncompressed data, modulo `2^32`. +- This function only examines the 10-byte header to ensure the header includes the expected magic number and compression method. The function does not perform an integrity check. + +
+ + + +
+ +## Examples + + + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var isgzipBuffer = require( '@stdlib/assert/is-gzip-buffer' ); + +var buf = new Uint8Array( 20 ); +buf[ 0 ] = 31; // 0x1f => magic number +buf[ 1 ] = 139; // 0x8b +buf[ 2 ] = 8; // 0x08 => compression method + +var bool = isgzipBuffer( buf ); +// returns true + +bool = isgzipBuffer( new Float32Array( 20 ) ); +// returns false + +bool = isgzipBuffer( new Int8Array( 20 ) ); +// returns false + +bool = isgzipBuffer( new Uint8Array( 20 ) ); +// returns false + +bool = isgzipBuffer( new Uint8ClampedArray( 20 ) ); +// returns false + +bool = isgzipBuffer( new Int16Array( 20 ) ); +// returns false + +bool = isgzipBuffer( new Uint16Array( 20 ) ); +// returns false + +bool = isgzipBuffer( new Int32Array( 20 ) ); +// returns false + +bool = isgzipBuffer( new Uint32Array( 20 ) ); +// returns false + +bool = isgzipBuffer( new Float64Array( 20 ) ); +// returns false + +bool = isgzipBuffer( new Array( 20 ) ); +// returns false + +bool = isgzipBuffer( {} ); +// returns false + +bool = isgzipBuffer( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-gzip-buffer/benchmark/benchmark.js b/is-gzip-buffer/benchmark/benchmark.js new file mode 100644 index 00000000..7f225770 --- /dev/null +++ b/is-gzip-buffer/benchmark/benchmark.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var string2buffer = require( '@stdlib/buffer/from-string' ); +var pkg = require( './../package.json' ).name; +var isgzipBuffer = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 20 ), + new Float32Array( 20 ), + new Int32Array( 20 ), + new Uint32Array( 20 ), + new Int16Array( 20 ), + new Uint16Array( 20 ), + new Int8Array( 20 ), + new Uint8Array( 20 ), + new Uint8ClampedArray( 20 ), + string2buffer( 'beep boop bop bip foo bar' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isgzipBuffer( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-gzip-buffer/docs/repl.txt b/is-gzip-buffer/docs/repl.txt new file mode 100644 index 00000000..9c6791a9 --- /dev/null +++ b/is-gzip-buffer/docs/repl.txt @@ -0,0 +1,37 @@ + +{{alias}}( value ) + Tests if a value is a gzip buffer. + + A gzip buffer is defined as either a Node.js Buffer or Uint8Array which + contains a 10-byte header, a body containing the compressed payload, and an + 8-byte footer containing a CRC-32 checksum and the length of the original + uncompressed data, modulo 2^32. + + This function only examines the 10-byte header to ensure the header includes + the expected magic number and compression method. The function does not + perform an integrity check. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a gzip buffer. + + Examples + -------- + > var buf = new {{alias:@stdlib/array/uint8}}( 20 ); + > buf[ 0 ] = 31; // 0x1f => magic number + > buf[ 1 ] = 139; // 0x8b + > buf[ 2 ] = 8; // 0x08 => compression method + > var bool = {{alias}}( buf ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-gzip-buffer/docs/types/index.d.ts b/is-gzip-buffer/docs/types/index.d.ts new file mode 100644 index 00000000..8bc65b7b --- /dev/null +++ b/is-gzip-buffer/docs/types/index.d.ts @@ -0,0 +1,52 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a gzip buffer (or Uint8Array). +* +* ## Notes +* +* - A gzip buffer is defined as either a Node.js Buffer or Uint8Array which contains a 10-byte header, a body containing the compressed payload, and an 8-byte footer containing a CRC-32 checksum and the length of the original uncompressed data, modulo 2^32. +* - This function only examines the 10-byte header to ensure the header includes the expected magic number and compression method. The function does not perform an integrity check. +* +* @param value - value to test +* @returns boolean indicating whether a value is a gzip buffer +* +* @example +* var Uint8Array = require( `@stdlib/array/uint8` ); +* +* var buf = new Uint8Array( 20 ); +* buf[ 0 ] = 31; // 0x1f => magic number +* buf[ 1 ] = 139; // 0x8b +* buf[ 2 ] = 8; // 0x08 => compression method +* +* var bool = isgzipBuffer( buf ); +* // returns true +* +* @example +* var bool = isgzipBuffer( [] ); +* // returns false +*/ +declare function isgzipBuffer( value: any ): boolean; + + +// EXPORTS // + +export = isgzipBuffer; diff --git a/is-gzip-buffer/docs/types/test.ts b/is-gzip-buffer/docs/types/test.ts new file mode 100644 index 00000000..af25619f --- /dev/null +++ b/is-gzip-buffer/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isgzipBuffer = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isgzipBuffer( new Uint8Array( 20 ) ); // $ExpectType boolean + isgzipBuffer( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isgzipBuffer(); // $ExpectError + isgzipBuffer( [], 123 ); // $ExpectError +} diff --git a/is-gzip-buffer/examples/index.js b/is-gzip-buffer/examples/index.js new file mode 100644 index 00000000..17adccb9 --- /dev/null +++ b/is-gzip-buffer/examples/index.js @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var isgzipBuffer = require( './../lib' ); + +var buf = new Uint8Array( 20 ); +buf[ 0 ] = 31; // 0x1f => magic number +buf[ 1 ] = 139; // 0x8b +buf[ 2 ] = 8; // 0x08 => compression method + +var bool = isgzipBuffer( buf ); +console.log( bool ); +// => true + +bool = isgzipBuffer( new Float32Array( 20 ) ); +console.log( bool ); +// => false + +bool = isgzipBuffer( new Int8Array( 20 ) ); +console.log( bool ); +// => false + +bool = isgzipBuffer( new Uint8Array( 20 ) ); +console.log( bool ); +// => false + +bool = isgzipBuffer( new Uint8ClampedArray( 20 ) ); +console.log( bool ); +// => false + +bool = isgzipBuffer( new Int16Array( 20 ) ); +console.log( bool ); +// => false + +bool = isgzipBuffer( new Uint16Array( 20 ) ); +console.log( bool ); +// => false + +bool = isgzipBuffer( new Int32Array( 20 ) ); +console.log( bool ); +// => false + +bool = isgzipBuffer( new Uint32Array( 20 ) ); +console.log( bool ); +// => false + +bool = isgzipBuffer( new Float64Array( 20 ) ); +console.log( bool ); +// => false + +bool = isgzipBuffer( new Array( 20 ) ); +console.log( bool ); +// => false + +bool = isgzipBuffer( {} ); +console.log( bool ); +// => false + +bool = isgzipBuffer( null ); +console.log( bool ); +// => false diff --git a/is-gzip-buffer/lib/index.js b/is-gzip-buffer/lib/index.js new file mode 100644 index 00000000..193bdf67 --- /dev/null +++ b/is-gzip-buffer/lib/index.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a gzip buffer (or Uint8Array). +* +* @module @stdlib/assert/is-gzip-buffer +* +* @example +* var Uint8Array = require( '@stdlib/array/uint8' ); +* var isgzipBuffer = require( '@stdlib/assert/is-gzip-buffer' ); +* +* var buf = new Uint8Array( 20 ); +* buf[ 0 ] = 31; // 0x1f => magic number +* buf[ 1 ] = 139; // 0x8b +* buf[ 2 ] = 8; // 0x08 => compression method +* +* var bool = isgzipBuffer( buf ); +* // returns true +* +* bool = isgzipBuffer( new Uint8Array( 20 ) ); +* // returns false +* +* bool = isgzipBuffer( [] ); +* // returns false +*/ + +// MODULES // + +var isgzipBuffer = require( './main.js' ); + + +// EXPORTS // + +module.exports = isgzipBuffer; diff --git a/is-gzip-buffer/lib/main.js b/is-gzip-buffer/lib/main.js new file mode 100644 index 00000000..849bc618 --- /dev/null +++ b/is-gzip-buffer/lib/main.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isUint8Array = require( '@stdlib/assert/is-uint8array' ); +var isBuffer = require( '@stdlib/assert/is-buffer' ); + + +// MAIN // + +/** +* Tests if a value is a gzip buffer (or Uint8Array). +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is a gzip buffer +* +* @example +* var Uint8Array = require( '@stdlib/array/uint8' ); +* +* var buf = new Uint8Array( 20 ); +* buf[ 0 ] = 31; // 0x1f => magic number +* buf[ 1 ] = 139; // 0x8b +* buf[ 2 ] = 8; // 0x08 => compression method +* +* var bool = isgzipBuffer( buf ); +* // returns true +* +* @example +* var Uint8Array = require( '@stdlib/array/uint8' ); +* +* var bool = isgzipBuffer( new Uint8Array( 20 ) ); +* // returns false +* +* @example +* var bool = isgzipBuffer( [] ); +* // returns false +*/ +function isgzipBuffer( value ) { + if ( !isUint8Array( value ) && !isBuffer( value ) ) { + return false; + } + if ( value.length < 19 ) { // 10-byte header + 8-byte footer + payload + return false; + } + return ( + // Check for expected magic number: + value[ 0 ] === 0x1F && + value[ 1 ] === 0x8B && + + // Check for expected compression method: + value[ 2 ] === 0x08 + ); +} + + +// EXPORTS // + +module.exports = isgzipBuffer; diff --git a/is-gzip-buffer/package.json b/is-gzip-buffer/package.json new file mode 100644 index 00000000..b67020cd --- /dev/null +++ b/is-gzip-buffer/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-gzip-buffer", + "version": "0.0.0", + "description": "Test if a value is a gzip buffer.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "buffer", + "is", + "isbuffer", + "type", + "check", + "validate", + "valid", + "isvalid", + "test", + "gzip", + "compress", + "deflate" + ] +} diff --git a/is-gzip-buffer/test/fixtures/data.json b/is-gzip-buffer/test/fixtures/data.json new file mode 100644 index 00000000..eea3460a --- /dev/null +++ b/is-gzip-buffer/test/fixtures/data.json @@ -0,0 +1 @@ +[31,139,8,8,60,71,53,95,0,3,112,103,110,45,100,97,116,97,46,112,103,110,0,93,148,77,107,27,49,16,134,239,250,21,34,208,75,74,197,206,232,99,181,186,20,150,246,20,112,176,123,232,193,248,96,239,71,93,98,210,67,147,178,80,242,223,59,26,109,99,141,141,15,235,125,253,204,104,94,189,210,254,235,159,233,249,69,223,125,190,59,168,253,183,159,47,211,250,248,229,200,143,244,49,252,205,239,118,191,94,159,199,85,255,126,190,254,183,191,28,135,167,245,121,55,253,126,189,80,189,123,250,161,192,232,201,233,191,251,15,195,229,73,55,9,93,130,198,116,135,55,13,198,144,228,43,169,75,14,13,146,132,70,111,6,91,41,54,249,96,226,225,77,97,134,54,67,16,20,122,99,137,178,70,207,78,64,174,225,114,54,67,179,236,212,248,188,8,229,168,218,44,59,97,99,90,130,28,67,139,88,123,76,208,154,64,162,39,236,42,41,230,192,27,199,82,30,107,153,111,56,52,64,98,48,122,148,75,164,117,32,11,68,141,190,42,24,83,211,112,175,246,214,12,76,190,204,213,178,25,115,109,70,155,108,199,203,136,70,247,19,86,245,144,68,246,61,102,106,59,181,130,2,224,138,157,209,143,159,30,69,47,232,120,21,29,83,99,80,87,205,103,127,115,47,218,80,189,57,213,254,66,114,142,93,36,137,185,186,155,79,141,53,62,139,96,84,47,157,130,100,221,127,141,192,126,12,34,58,235,78,67,14,200,36,27,18,64,156,130,146,144,69,168,46,53,101,62,160,140,140,203,13,217,4,158,16,56,38,253,224,63,138,109,69,158,48,231,68,236,92,147,60,148,181,112,80,54,34,39,152,172,45,206,80,80,222,247,142,234,65,76,174,43,239,9,58,5,193,144,43,92,144,66,210,215,110,18,100,125,17,178,151,203,169,142,9,36,239,74,43,202,201,195,25,4,70,59,14,69,42,32,138,185,215,160,3,69,101,59,219,235,198,66,155,92,40,75,225,172,244,167,86,12,110,215,170,93,142,88,21,76,8,201,249,34,24,163,40,71,117,146,128,14,119,100,142,218,82,191,119,151,11,104,145,179,137,28,151,135,83,172,44,235,146,45,23,6,210,85,210,255,168,39,164,107,161,164,29,161,76,88,159,147,108,91,44,32,114,62,85,13,186,182,128,28,151,237,177,182,155,207,80,78,4,82,90,142,78,244,179,235,5,149,179,162,142,65,204,128,217,50,164,164,244,203,80,215,195,124,24,98,209,56,155,66,165,83,228,115,55,69,217,214,83,16,156,45,231,28,57,45,187,243,28,5,71,187,147,71,184,255,7,107,209,132,198,191,5,0,0] diff --git a/is-gzip-buffer/test/test.js b/is-gzip-buffer/test/test.js new file mode 100644 index 00000000..ff310bd0 --- /dev/null +++ b/is-gzip-buffer/test/test.js @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Buffer = require( '@stdlib/buffer/ctor' ); +var isgzipBuffer = require( './../lib' ); + + +// FIXTURES // + +var DATA = require( './fixtures/data.json' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isgzipBuffer, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a gzip buffer', function test( t ) { + t.strictEqual( isgzipBuffer( new Uint8Array( DATA ) ), true, 'returns expected value' ); + t.strictEqual( isgzipBuffer( new Buffer( DATA ) ), true, 'returns expected value' ); // eslint-disable-line no-buffer-constructor + t.end(); +}); + +tape( 'the function returns `false` if not provided a gzip buffer', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + void 0, + [], + {}, + function noop() {}, + new Float64Array( 20 ), + new Float32Array( 20 ), + new Uint32Array( 20 ), + new Int32Array( 20 ), + new Uint16Array( 20 ), + new Int16Array( 20 ), + new Uint8Array( 20 ), + new Int8Array( 20 ), + new Uint8ClampedArray( 20 ), + new Buffer( 20 ), // eslint-disable-line no-buffer-constructor + DATA + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isgzipBuffer( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-hex-string/README.md b/is-hex-string/README.md new file mode 100644 index 00000000..c3d58ed6 --- /dev/null +++ b/is-hex-string/README.md @@ -0,0 +1,136 @@ + + +# isHexString + +> Test whether a string contains only hexadecimal digits. + +
+ +## Usage + +```javascript +var isHexString = require( '@stdlib/assert/is-hex-string' ); +``` + +#### isHexString( value ) + +Tests whether a string contains only hexadecimal digits. + +```javascript +var bool = isHexString( '0123456789abcdefABCDEF' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- For non-string values, the function returns `false`. +- The function does **not** recognize `x` (as in the standard `0x` prefix). + +
+ + + +
+ +## Examples + + + +```javascript +var isHexString = require( '@stdlib/assert/is-hex-string' ); + +var out = isHexString( '0123456789abcdefABCDEF' ); +// returns true + +out = isHexString( '' ); +// returns false + +out = isHexString( '0xffffff' ); +// returns false + +out = isHexString( 123 ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-hex-string [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-hex-string 0123456789abcdefABCDEF +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n '0123456789abcdefABCDEF' | is-hex-string +true +``` + +
+ + + +
+ + + + + + diff --git a/is-hex-string/benchmark/benchmark.js b/is-hex-string/benchmark/benchmark.js new file mode 100644 index 00000000..b196d976 --- /dev/null +++ b/is-hex-string/benchmark/benchmark.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isHexString = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::single-character', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = fromCodePoint( (i%20)+48 ); + bool = isHexString( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::string', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = '0123456789abcdefABCDEF' + fromCodePoint( (i%20)+48 ); + bool = isHexString( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-hex-string/bin/cli b/is-hex-string/bin/cli new file mode 100644 index 00000000..13e860e8 --- /dev/null +++ b/is-hex-string/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isHexString = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isHexString( args[ 0 ] ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isHexString( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-hex-string/docs/repl.txt b/is-hex-string/docs/repl.txt new file mode 100644 index 00000000..71a8b4c4 --- /dev/null +++ b/is-hex-string/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( str ) + Tests whether a string contains only hexadecimal digits. + + The function does not recognize `x` (as in the standard `0x` prefix). + + Parameters + ---------- + str: string + Input string. + + Returns + ------- + bool: boolean + Boolean indicating whether a string contains only hexadecimal digits. + + Examples + -------- + > var bool = {{alias}}( '0123456789abcdefABCDEF' ) + true + > bool = {{alias}}( '0xffffff' ) + false + > bool = {{alias}}( 'x' ) + false + > bool = {{alias}}( '' ) + false + + See Also + -------- + diff --git a/is-hex-string/docs/types/index.d.ts b/is-hex-string/docs/types/index.d.ts new file mode 100644 index 00000000..545161cd --- /dev/null +++ b/is-hex-string/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests whether a string contains only hexadecimal digits. +* +* @param x - value to test +* @returns boolean indicating if a string contains only hexadecimal digits +* +* @example +* var out = isHexString( '0123456789abcdefABCDEF' ); +* // returns true +* +* @example +* var out = isHexString( '0xffffff' ); +* // returns false +* +* @example +* var out = isHexString( '' ); +* // returns false +* +* @example +* var out = isHexString( 123 ); +* // returns false +*/ +declare function isHexString( value: any ): boolean; + + +// EXPORTS // + +export = isHexString; diff --git a/is-hex-string/docs/types/test.ts b/is-hex-string/docs/types/test.ts new file mode 100644 index 00000000..33345742 --- /dev/null +++ b/is-hex-string/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isHexString = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isHexString( '0xffffff' ); // $ExpectType boolean + isHexString( 'World' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isHexString(); // $ExpectError + isHexString( '0xffffff', 123 ); // $ExpectError +} diff --git a/is-hex-string/docs/usage.txt b/is-hex-string/docs/usage.txt new file mode 100644 index 00000000..36eba20e --- /dev/null +++ b/is-hex-string/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-hex-string [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-hex-string/etc/cli_opts.json b/is-hex-string/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-hex-string/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-hex-string/examples/index.js b/is-hex-string/examples/index.js new file mode 100644 index 00000000..366164c1 --- /dev/null +++ b/is-hex-string/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isHexString = require( './../lib' ); + +console.log( isHexString( '0123456789abcdefABCDEF' ) ); +// => true + +console.log( isHexString( '' ) ); +// => false + +console.log( isHexString( '0xffffff' ) ); +// => false + +console.log( isHexString( 123 ) ); +// => false diff --git a/is-hex-string/lib/index.js b/is-hex-string/lib/index.js new file mode 100644 index 00000000..39eaf34d --- /dev/null +++ b/is-hex-string/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether a string contains only hexadecimal digits. +* +* @module @stdlib/assert/is-hex-string +* +* @example +* var isHexString = require( '@stdlib/assert/is-hex-string' ); +* +* var out = isHexString( '0123456789abcdefABCDEF' ); +* // returns true +* +* out = isHexString( '0xffffff' ); +* // returns false +* +* out = isHexString( '' ); +* // returns false +*/ + +// MODULES // + +var isHexString = require( './main.js' ); + + +// EXPORTS // + +module.exports = isHexString; diff --git a/is-hex-string/lib/main.js b/is-hex-string/lib/main.js new file mode 100644 index 00000000..b2cf23a3 --- /dev/null +++ b/is-hex-string/lib/main.js @@ -0,0 +1,89 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; + + +// VARIABLES // + +// Character codes: +var ZERO = 48; +var NINE = 57; +var A = 65; +var F = 70; +var a = 97; +var f = 102; + + +// MAIN // + +/** +* Tests whether a string contains only hexadecimal digits. +* +* @param {*} x - value to test +* @returns {boolean} boolean indicating if a string contains only hexadecimal digits +* +* @example +* var out = isHexString( '0123456789abcdefABCDEF' ); +* // returns true +* +* @example +* var out = isHexString( '0xffffff' ); +* // returns false +* +* @example +* var out = isHexString( '' ); +* // returns false +* +* @example +* var out = isHexString( 123 ); +* // returns false +*/ +function isHexString( x ) { + var len; + var ch; + var i; + if ( !isString( x ) ) { + return false; + } + len = x.length; + if ( !len ) { + return false; + } + for ( i = 0; i < len; i++ ) { + ch = x.charCodeAt( i ); + if ( + ch < ZERO || + ( ch > NINE && ch < A ) || + ( ch > F && ch < a ) || + ch > f + ) { + return false; + } + } + return true; +} + + +// EXPORTS // + +module.exports = isHexString; diff --git a/is-hex-string/package.json b/is-hex-string/package.json new file mode 100644 index 00000000..09961f10 --- /dev/null +++ b/is-hex-string/package.json @@ -0,0 +1,79 @@ +{ + "name": "@stdlib/assert/is-hex-string", + "version": "0.0.0", + "description": "Test whether a string contains only hexadecimal digits.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-hex-string": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "string", + "str", + "hex", + "hexadecimal", + "digit", + "is", + "isstring", + "ishex", + "ishexadecimal", + "validate", + "valid", + "isvalid", + "test", + "check" + ] +} diff --git a/is-hex-string/test/fixtures/stdin_error.js.txt b/is-hex-string/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-hex-string/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-hex-string/test/test.cli.js b/is-hex-string/test/test.cli.js new file mode 100644 index 00000000..23b035ae --- /dev/null +++ b/is-hex-string/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument contains only hexadecimal digits', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'0123456789abcdefABCDEF\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "beep\n0123456789abcdefABCDEF"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'false\ntrue\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-hex-string/test/test.js b/is-hex-string/test/test.js new file mode 100644 index 00000000..518735f6 --- /dev/null +++ b/is-hex-string/test/test.js @@ -0,0 +1,102 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isHexString = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isHexString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a string containing all hexadecimal digits', function test( t ) { + t.strictEqual( isHexString( '0123456789abcdefABCDEF' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a string containing all hexadecimal digits', function test( t ) { + var values; + var i; + + values = [ + '', + 'È', + 'g', + 'h', + 'i', + 'j', + 'k', + 'l', + 'm', + 'n', + 'o', + 'p', + 'q', + 'r', + 's', + 't', + 'u', + 'v', + 'w', + 'x', + 'y', + 'z', + 'G', + 'H', + 'I', + 'J', + 'K', + 'L', + 'M', + 'N', + 'O', + 'P', + 'Q', + 'R', + 'S', + 'T', + 'U', + 'V', + 'W', + 'X', + 'Y', + 'Z', + new String( 'abcdef' ), // eslint-disable-line no-new-wrappers + 5, + null, + NaN, + undefined, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isHexString( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-infinite/README.md b/is-infinite/README.md new file mode 100644 index 00000000..56311f96 --- /dev/null +++ b/is-infinite/README.md @@ -0,0 +1,142 @@ + + +# isInfinite + +> Test if a value is an infinite number. + +
+ +## Usage + +```javascript +var isInfinite = require( '@stdlib/assert/is-infinite' ); +``` + +#### isInfinite( value ) + +Tests if a value is an infinite `number`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isInfinite( 1.0/0.0 ); +// returns true + +bool = isInfinite( new Number( 1.0/0.0 ) ); +// returns true + +bool = isInfinite( 1.0 ); +// returns false + +bool = isInfinite( null ); +// returns false +``` + +#### isInfinite.isPrimitive( value ) + +Tests if a `value` is a primitive `number` having an infinite value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isInfinite.isPrimitive( -1.0/0.0 ); +// returns true + +bool = isInfinite.isPrimitive( new Number( -1.0/0.0 ) ); +// returns false +``` + +#### isInfinite.isObject( value ) + +Tests if a `value` is a `Number` object having an infinite value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isInfinite.isObject( 1.0/0.0 ); +// returns false + +bool = isInfinite.isObject( new Number( 1.0/0.0 ) ); +// returns true +``` + +
+ + + +
+ +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isInfinite = require( '@stdlib/assert/is-infinite' ); + +var bool = isInfinite( 1.0/0.0 ); +// returns true + +bool = isInfinite( -1.0/0.0 ); +// returns true + +bool = isInfinite( new Number( 1.0/0.0 ) ); +// returns true + +bool = isInfinite( -5.0 ); +// returns false + +bool = isInfinite( 0.0 ); +// returns false + +bool = isInfinite( 5.256 ); +// returns false + +bool = isInfinite( '1.0/0.0' ); +// returns false + +bool = isInfinite( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-infinite/benchmark/benchmark.js b/is-infinite/benchmark/benchmark.js new file mode 100644 index 00000000..e6796878 --- /dev/null +++ b/is-infinite/benchmark/benchmark.js @@ -0,0 +1,233 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isInfinite = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + 1.0/0.0, + -1.0/0.0, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInfinite( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ), + new Number( 1.0/0.0 ), + new Number( -1.0/0.0 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInfinite( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + 1.0/0.0, + -1.0/0.0, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInfinite.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ), + new Number( 1.0/0.0 ), + new Number( -1.0/0.0 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInfinite.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + 1.0/0.0, + -1.0/0.0, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInfinite.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ), + new Number( 1.0/0.0 ), + new Number( -1.0/0.0 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInfinite.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-infinite/docs/repl.txt b/is-infinite/docs/repl.txt new file mode 100644 index 00000000..6e95e4b5 --- /dev/null +++ b/is-infinite/docs/repl.txt @@ -0,0 +1,72 @@ + +{{alias}}( value ) + Tests if a value is an infinite number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an infinite number. + + Examples + -------- + > var bool = {{alias}}( 1.0/0.0 ) + true + > bool = {{alias}}( new Number( -1.0/0.0 ) ) + true + > bool = {{alias}}( 5.0 ) + false + > bool = {{alias}}( '1.0/0.0' ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive having an infinite value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a number primitive having an + infinite value. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( -1.0/0.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( -1.0/0.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having an infinite value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a number object having an infinite + value. + + Examples + -------- + > var bool = {{alias}}.isObject( 1.0/0.0 ) + false + > bool = {{alias}}.isObject( new Number( 1.0/0.0 ) ) + true + + See Also + -------- + diff --git a/is-infinite/docs/types/index.d.ts b/is-infinite/docs/types/index.d.ts new file mode 100644 index 00000000..d33f0c81 --- /dev/null +++ b/is-infinite/docs/types/index.d.ts @@ -0,0 +1,109 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isInfinite` with methods for testing for primitives and objects, respectively. +*/ +interface IsInfinite { + /** + * Tests if a value is an infinite number. + * + * @param value - value to test + * @returns boolean indicating whether a value is an infinite number + * + * @example + * var bool = isInfinite( 1.0/0.0 ); + * // returns true + * + * @example + * var bool = isInfinite( new Number( 1.0/0.0 ) ); + * // returns true + * + * @example + * var bool = isInfinite( 5.0 ); + * // returns false + * + * @example + * var bool = isInfinite( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having an infinite value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having an infinite value + * + * @example + * var bool = isInfinite.isPrimitive( -1.0/0.0 ); + * // returns true + * + * @example + * var bool = isInfinite.isPrimitive( new Number( -1.0/0.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having an infinite value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having an infinite value + * + * @example + * var bool = isInfinite.isObject( 1.0/0.0 ); + * // returns false + * + * @example + * var bool = isInfinite.isObject( new Number( 1.0/0.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is an infinite number. +* +* @param value - value to test +* @returns boolean indicating whether a value is an infinite number +* +* @example +* var bool = isInfinite( 1.0/0.0 ); +* // returns true +* +* @example +* var bool = isInfinite( new Number( 1.0/0.0 ) ); +* // returns true +* +* @example +* var bool = isInfinite( 5.0 ); +* // returns false +* +* @example +* var bool = isInfinite( null ); +* // returns false +*/ +declare var isInfinite: IsInfinite; + + +// EXPORTS // + +export = isInfinite; diff --git a/is-infinite/docs/types/test.ts b/is-infinite/docs/types/test.ts new file mode 100644 index 00000000..0406377e --- /dev/null +++ b/is-infinite/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isInfinite = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isInfinite( 3 ); // $ExpectType boolean + isInfinite( 1 / 0 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isInfinite(); // $ExpectError + isInfinite( 1 / 0, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isInfinite.isPrimitive( new Number( 1 / 0 ) ); // $ExpectType boolean + isInfinite.isPrimitive( 1 / 0 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isInfinite.isPrimitive(); // $ExpectError + isInfinite.isPrimitive( 1 / 0, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isInfinite.isObject( new Number( 1 / 0 ) ); // $ExpectType boolean + isInfinite.isObject( 1 / 0 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isInfinite.isObject(); // $ExpectError + isInfinite.isObject( 1 / 0, 123 ); // $ExpectError +} diff --git a/is-infinite/examples/index.js b/is-infinite/examples/index.js new file mode 100644 index 00000000..3d76d761 --- /dev/null +++ b/is-infinite/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isInfinite = require( './../lib' ); + +console.log( isInfinite( 1.0/0.0 ) ); +// => true + +console.log( isInfinite( -1.0/0.0 ) ); +// => true + +console.log( isInfinite( new Number( 1.0/0.0 ) ) ); +// => true + +console.log( isInfinite( -5.0 ) ); +// => false + +console.log( isInfinite( 0.0 ) ); +// => false + +console.log( isInfinite( 5.256 ) ); +// => false + +console.log( isInfinite( '1.0/0.0' ) ); +// => false + +console.log( isInfinite( null ) ); +// => false diff --git a/is-infinite/lib/index.js b/is-infinite/lib/index.js new file mode 100644 index 00000000..1897357d --- /dev/null +++ b/is-infinite/lib/index.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an infinite number. +* +* @module @stdlib/assert/is-infinite +* +* @example +* var isInfinite = require( '@stdlib/assert/is-infinite' ); +* +* var bool = isInfinite( 1.0/0.0 ); +* // returns true +* +* bool = isInfinite( new Number( 1.0/0.0 ) ); +* // returns true +* +* bool = isInfinite( 5.0 ); +* // returns false +* +* bool = isInfinite( null ); +* // returns false +* +* @example +* var isInfinite = require( '@stdlib/assert/is-infinite' ).isPrimitive; +* +* var bool = isInfinite( -1.0/0.0 ); +* // returns true +* +* bool = isInfinite( new Number( -1.0/0.0 ) ); +* // returns false +* +* @example +* var isInfinite = require( '@stdlib/assert/is-infinite' ).isObject; +* +* var bool = isInfinite( 1.0/0.0 ); +* // returns false +* +* bool = isInfinite( new Number( 1.0/0.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isInfinite = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isInfinite, 'isPrimitive', isPrimitive ); +setReadOnly( isInfinite, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isInfinite; diff --git a/is-infinite/lib/main.js b/is-infinite/lib/main.js new file mode 100644 index 00000000..a5a5d5e9 --- /dev/null +++ b/is-infinite/lib/main.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is an infinite number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is an infinite number +* +* @example +* var bool = isInfinite( 1.0/0.0 ); +* // returns true +* +* @example +* var bool = isInfinite( new Number( 1.0/0.0 ) ); +* // returns true +* +* @example +* var bool = isInfinite( 5.0 ); +* // returns false +* +* @example +* var bool = isInfinite( null ); +* // returns false +*/ +function isInfinite( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isInfinite; diff --git a/is-infinite/lib/object.js b/is-infinite/lib/object.js new file mode 100644 index 00000000..599205ea --- /dev/null +++ b/is-infinite/lib/object.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; +var isinf = require( '@stdlib/math/base/assert/is-infinite' ); + + +// MAIN // + +/** +* Tests if a value is a number object having an infinite value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having an infinite value +* +* @example +* var bool = isInfinite( 1.0/0.0 ); +* // returns false +* +* @example +* var bool = isInfinite( new Number( 1.0/0.0 ) ); +* // returns true +*/ +function isInfinite( value ) { + return ( + isNumber( value ) && + isinf( value.valueOf() ) + ); +} + + +// EXPORTS // + +module.exports = isInfinite; diff --git a/is-infinite/lib/primitive.js b/is-infinite/lib/primitive.js new file mode 100644 index 00000000..ffcb7106 --- /dev/null +++ b/is-infinite/lib/primitive.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isinf = require( '@stdlib/math/base/assert/is-infinite' ); + + +// MAIN // + +/** +* Tests if a value is a number primitive having an infinite value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having an infinite value +* +* @example +* var bool = isInfinite( -1.0/0.0 ); +* // returns true +* +* @example +* var bool = isInfinite( new Number( -1.0/0.0 ) ); +* // returns false +*/ +function isInfinite( value ) { + return ( + isNumber( value ) && + isinf( value ) + ); +} + + +// EXPORTS // + +module.exports = isInfinite; diff --git a/is-infinite/package.json b/is-infinite/package.json new file mode 100644 index 00000000..7bc8d6c1 --- /dev/null +++ b/is-infinite/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-infinite", + "version": "0.0.0", + "description": "Test if a value is an infinite number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "finite", + "infinite", + "inf", + "is", + "isinfinite", + "isinf", + "type", + "check", + "primitive", + "object", + "validate", + "valid", + "test" + ] +} diff --git a/is-infinite/test/test.js b/is-infinite/test/test.js new file mode 100644 index 00000000..ad9fdef4 --- /dev/null +++ b/is-infinite/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isInfinite = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isInfinite, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number having an infinite value', function test( t ) { + t.strictEqual( typeof isInfinite.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having an infinite value', function test( t ) { + t.strictEqual( typeof isInfinite.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-infinite/test/test.main.js b/is-infinite/test/test.main.js new file mode 100644 index 00000000..7fd21058 --- /dev/null +++ b/is-infinite/test/test.main.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isInfinite = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isInfinite, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number having an infinite value', function test( t ) { + t.strictEqual( isInfinite( 1.0/0.0 ), true, 'returns true' ); + t.strictEqual( isInfinite( new Number( 1.0/0.0 ) ), true, 'returns true' ); + t.strictEqual( isInfinite( -1.0/0.0 ), true, 'returns true' ); + t.strictEqual( isInfinite( new Number( -1.0/0.0 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number having an infinite value', function test( t ) { + var values; + var i; + + values = [ + '5', + 1.0, + -1.0, + NaN, + null, + true, + false, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isInfinite( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-infinite/test/test.object.js b/is-infinite/test/test.object.js new file mode 100644 index 00000000..8913d0d4 --- /dev/null +++ b/is-infinite/test/test.object.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isInfinite = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isInfinite, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having an infinite value', function test( t ) { + t.strictEqual( isInfinite( new Number( 1.0/0.0 ) ), true, 'returns true' ); + t.strictEqual( isInfinite( new Number( -1.0/0.0 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is an infinite value', function test( t ) { + t.strictEqual( isInfinite( 1.0/0.0 ), false, 'returns false' ); + t.strictEqual( isInfinite( -1.0/0.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an infinite number', function test( t ) { + var values; + var i; + + values = [ + '5', + 1.0, + -1.0, + NaN, + new Number( 1.0 ), + new Number( -1.0 ), + new Number( NaN ), + null, + true, + false, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isInfinite( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-infinite/test/test.primitive.js b/is-infinite/test/test.primitive.js new file mode 100644 index 00000000..e3c28ae5 --- /dev/null +++ b/is-infinite/test/test.primitive.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isInfinite = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isInfinite, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number having an infinite value', function test( t ) { + t.strictEqual( isInfinite( -1.0/0.0 ), true, 'returns true' ); + t.strictEqual( isInfinite( 1.0/0.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has an infinite value', function test( t ) { + t.strictEqual( isInfinite( new Number( 1.0/0.0 ) ), false, 'returns false' ); + t.strictEqual( isInfinite( new Number( -1.0/0.0 ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an infinite number', function test( t ) { + var values; + var i; + + values = [ + '5', + 1.0, + -1.0, + NaN, + null, + true, + false, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isInfinite( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-inherited-property/README.md b/is-inherited-property/README.md new file mode 100644 index 00000000..4ebb2722 --- /dev/null +++ b/is-inherited-property/README.md @@ -0,0 +1,144 @@ + + +# isInheritedProperty + +> Test if an object has an inherited property. + +
+ +## Usage + +```javascript +var isInheritedProperty = require( '@stdlib/assert/is-inherited-property' ); +``` + +#### isInheritedProperty( value, property ) + +Returns a `boolean` indicating if a `value` has an inherited `property`. + +```javascript +var obj = { + 'beep': 'boop' +}; + +var bool = isInheritedProperty( obj, 'beep' ); +// returns false + +bool = isInheritedProperty( obj, 'hasOwnProperty' ); +// returns true + +bool = isInheritedProperty( obj, 'bap' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- The function does **not** throw when provided `null` or `undefined`. Instead, the function returns `false`. + + ```javascript + var bool = isInheritedProperty( null, 'a' ); + // returns false + + bool = isInheritedProperty( void 0, 'a' ); + // returns false + ``` + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isInheritedProperty( 'beep', 'toString' ); + // returns true + ``` + +- Non-symbol property arguments are coerced to `strings`. + + ```javascript + function Foo() { + return this; + } + + Foo.prototype.null = true; + Foo.prototype[ '[object Object]' ] = true; + + var obj = new Foo(); + + var bool = isInheritedProperty( obj, null ); + // returns true + + bool = isInheritedProperty( obj, {} ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isInheritedProperty = require( '@stdlib/assert/is-inherited-property' ); + +var bool = isInheritedProperty( {}, 'hasOwnProperty' ); +// returns true + +bool = isInheritedProperty( { 'a': 'b' }, 'a' ); +// returns false + +bool = isInheritedProperty( { 'a': 'b' }, 'c' ); +// returns false + +bool = isInheritedProperty( { 'a': 'b' }, null ); +// returns false + +bool = isInheritedProperty( null, 'a' ); +// returns false + +bool = isInheritedProperty( void 0, 'a' ); +// returns false + +bool = isInheritedProperty( { 'null': false }, null ); +// returns false + +bool = isInheritedProperty( { '[object Object]': false }, {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-inherited-property/benchmark/benchmark.js b/is-inherited-property/benchmark/benchmark.js new file mode 100644 index 00000000..6a97de2d --- /dev/null +++ b/is-inherited-property/benchmark/benchmark.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isInheritedProperty = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var obj; + var key; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + key = fromCodePoint( 97+(i%26) ); + obj = {}; + obj[ key ] = i; + bool = isInheritedProperty( obj, key ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-inherited-property/docs/repl.txt b/is-inherited-property/docs/repl.txt new file mode 100644 index 00000000..d2f67f3b --- /dev/null +++ b/is-inherited-property/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, property ) + Tests if an object has an inherited property. + + Value arguments other than `null` or `undefined` are coerced to objects. + + Non-symbol property arguments are coerced to strings. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object has an inherited property. + + Examples + -------- + > var beep = { 'boop': true }; + > var bool = {{alias}}( beep, 'boop' ) + false + > bool = {{alias}}( beep, 'toString' ) + true + > bool = {{alias}}( beep, 'bop' ) + false + + See Also + -------- + diff --git a/is-inherited-property/docs/types/index.d.ts b/is-inherited-property/docs/types/index.d.ts new file mode 100644 index 00000000..5c3a1f24 --- /dev/null +++ b/is-inherited-property/docs/types/index.d.ts @@ -0,0 +1,57 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object has an inherited property. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object has an inherited property +* +* @example +* var obj = { +* 'boop': true +* }; +* +* var bool = isInheritedProperty( obj, 'toString' ); +* // returns true +* +* @example +* var obj = { +* 'boop': true +* }; +* +* var bool = isInheritedProperty( obj, 'boop' ); +* // returns false +* +* @example +* var obj = { +* 'boop': true +* }; +* +* var bool = isInheritedProperty( obj, 'bap' ); +* // returns false +*/ +declare function isInheritedProperty( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isInheritedProperty; diff --git a/is-inherited-property/docs/types/test.ts b/is-inherited-property/docs/types/test.ts new file mode 100644 index 00000000..8cfa2bf2 --- /dev/null +++ b/is-inherited-property/docs/types/test.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isInheritedProperty = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + isInheritedProperty( obj, 'toString' ); // $ExpectType boolean + isInheritedProperty( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isInheritedProperty(); // $ExpectError + isInheritedProperty( {} ); // $ExpectError + isInheritedProperty( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-inherited-property/examples/index.js b/is-inherited-property/examples/index.js new file mode 100644 index 00000000..c886a7a1 --- /dev/null +++ b/is-inherited-property/examples/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isInheritedProperty = require( './../lib' ); + +var bool = isInheritedProperty( {}, 'hasOwnProperty' ); +console.log( bool ); +// => true + +bool = isInheritedProperty( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => false + +bool = isInheritedProperty( { 'a': 'b' }, 'c' ); +console.log( bool ); +// => false + +bool = isInheritedProperty( { 'a': 'b' }, null ); +console.log( bool ); +// => false + +bool = isInheritedProperty( null, 'a' ); +console.log( bool ); +// => false + +bool = isInheritedProperty( void 0, 'a' ); +console.log( bool ); +// => false + +bool = isInheritedProperty( { 'null': false }, null ); +console.log( bool ); +// => false + +bool = isInheritedProperty( { '[object Object]': false }, {} ); +console.log( bool ); +// => false diff --git a/is-inherited-property/lib/index.js b/is-inherited-property/lib/index.js new file mode 100644 index 00000000..c8d4be2d --- /dev/null +++ b/is-inherited-property/lib/index.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object has an inherited property. +* +* @module @stdlib/assert/is-inherited-property +* +* @example +* var isInheritedProperty = require( '@stdlib/assert/is-inherited-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* var bool = isInheritedProperty( obj, 'toString' ); +* // returns true +* +* bool = isInheritedProperty( obj, 'boop' ); +* // returns false +* +* bool = isInheritedProperty( obj, 'bop' ); +* // returns false +*/ + +// MODULES // + +var isInheritedProperty = require( './main.js' ); + + +// EXPORTS // + +module.exports = isInheritedProperty; diff --git a/is-inherited-property/lib/main.js b/is-inherited-property/lib/main.js new file mode 100644 index 00000000..8f11e077 --- /dev/null +++ b/is-inherited-property/lib/main.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); + + +// MAIN // + +/** +* Tests if an object has an inherited property. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object has an inherited property +* +* @example +* var obj = { +* 'boop': true +* }; +* +* var bool = isInheritedProperty( obj, 'toString' ); +* // returns true +* +* @example +* var obj = { +* 'boop': true +* }; +* +* var bool = isInheritedProperty( obj, 'boop' ); +* // returns false +* +* @example +* var obj = { +* 'boop': true +* }; +* +* var bool = isInheritedProperty( obj, 'bap' ); +* // returns false +*/ +function isInheritedProperty( value, property ) { + if ( value === void 0 || value === null || hasOwnProp( value, property ) ) { + return false; + } + if ( typeof property === 'symbol' ) { + return property in Object( value ); + } + return ( String( property ) in Object( value ) ); +} + + +// EXPORTS // + +module.exports = isInheritedProperty; diff --git a/is-inherited-property/package.json b/is-inherited-property/package.json new file mode 100644 index 00000000..b726f5f7 --- /dev/null +++ b/is-inherited-property/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-inherited-property", + "version": "0.0.0", + "description": "Test if an object has an inherited property.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "has", + "inherited", + "in", + "prototype", + "proto", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-inherited-property/test/test.js b/is-inherited-property/test/test.js new file mode 100644 index 00000000..d2e572a8 --- /dev/null +++ b/is-inherited-property/test/test.js @@ -0,0 +1,155 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var isInheritedProperty = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isInheritedProperty, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isInheritedProperty( null, 'beep' ); + t.strictEqual( bool, false, 'returns false when provided null' ); + + bool = isInheritedProperty( void 0, 'beep' ); + t.strictEqual( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `true` if an object has an inherited property', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + Foo.prototype.bar = true; + + obj = new Foo(); + + bool = isInheritedProperty( obj, 'bar' ); + t.strictEqual( bool, true, 'returns true' ); + + bool = isInheritedProperty( {}, 'hasOwnProperty' ); + t.strictEqual( bool, true, 'returns true' ); + + bool = isInheritedProperty( {}, 'toString' ); + t.strictEqual( bool, true, 'returns true' ); + + bool = isInheritedProperty( {}, 'constructor' ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object has an own property', function test( t ) { + var bool; + + bool = isInheritedProperty( { 'a': 'b' }, 'a' ); + t.strictEqual( bool, false, 'returns false' ); + + bool = isInheritedProperty( [ 1, 2, 3 ], '1' ); + t.strictEqual( bool, false, 'returns false' ); + + bool = isInheritedProperty( [ 1, 2, 3 ], 1 ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object does not have a specified property', function test( t ) { + var bool; + + bool = isInheritedProperty( { 'a': 'b' }, 'c' ); + t.strictEqual( bool, false, 'returns false' ); + + bool = isInheritedProperty( { 'a': 'b' }, null ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isInheritedProperty( 'beep', 'toString' ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'non-symbol property values are coerced to strings', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + Foo.prototype.null = true; + Foo.prototype[ '[object Object]' ] = true; + + obj = new Foo(); + + bool = isInheritedProperty( obj, null ); + t.strictEqual( bool, true, 'returns true' ); + + bool = isInheritedProperty( obj, {} ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function supports symbols', function test( t ) { + var bool; + var obj; + var s; + + function Foo() { + return this; + } + + if ( hasSymbolSupport() ) { + s = Symbol( 'foo' ); + Foo.prototype[ s ] = true; + obj = new Foo(); + bool = isInheritedProperty( obj, s ); + t.strictEqual( bool, true, 'returns true' ); + } else { + t.pass( 'environment does not support symbols' ); + } + t.end(); +}); diff --git a/is-int16array/README.md b/is-int16array/README.md new file mode 100644 index 00000000..d8b51136 --- /dev/null +++ b/is-int16array/README.md @@ -0,0 +1,116 @@ + + +# isInt16Array + +> Test if a value is an [Int16Array][mdn-int16array]. + +
+ +## Usage + +```javascript +var isInt16Array = require( '@stdlib/assert/is-int16array' ); +``` + +#### isInt16Array( value ) + +Tests if a value is an [`Int16Array`][mdn-int16array]. + +```javascript +var Int16Array = require( '@stdlib/array/int16' ); + +var bool = isInt16Array( new Int16Array( 10 ) ); +// returns true + +bool = isInt16Array( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isInt16Array = require( '@stdlib/assert/is-int16array' ); + +var bool = isInt16Array( new Int16Array( 10 ) ); +// returns true + +bool = isInt16Array( new Int8Array( 10 ) ); +// returns false + +bool = isInt16Array( new Uint8Array( 10 ) ); +// returns false + +bool = isInt16Array( new Uint8ClampedArray( 10 ) ); +// returns false + +bool = isInt16Array( new Uint16Array( 10 ) ); +// returns false + +bool = isInt16Array( new Int32Array( 10 ) ); +// returns false + +bool = isInt16Array( new Uint32Array( 10 ) ); +// returns false + +bool = isInt16Array( new Float32Array( 10 ) ); +// returns false + +bool = isInt16Array( new Float64Array( 10 ) ); +// returns false + +bool = isInt16Array( new Array( 10 ) ); +// returns false + +bool = isInt16Array( {} ); +// returns false + +bool = isInt16Array( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-int16array/benchmark/benchmark.js b/is-int16array/benchmark/benchmark.js new file mode 100644 index 00000000..ce39411f --- /dev/null +++ b/is-int16array/benchmark/benchmark.js @@ -0,0 +1,126 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isInt16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInt16Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Int16Array( 10 ), + new Int16Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInt16Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInt16Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-int16array/docs/repl.txt b/is-int16array/docs/repl.txt new file mode 100644 index 00000000..c9858f58 --- /dev/null +++ b/is-int16array/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is an Int16Array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an Int16Array. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/int16}}( 10 ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-int16array/docs/types/index.d.ts b/is-int16array/docs/types/index.d.ts new file mode 100644 index 00000000..49be8a49 --- /dev/null +++ b/is-int16array/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an Int16Array. +* +* @param value - value to test +* @returns boolean indicating whether value is an Int16Array +* +* @example +* var bool = isInt16Array( new Int16Array( 10 ) ); +* // returns true +* +* @example +* var bool = isInt16Array( [] ); +* // returns false +*/ +declare function isInt16Array( value: any ): boolean; + + +// EXPORTS // + +export = isInt16Array; diff --git a/is-int16array/docs/types/test.ts b/is-int16array/docs/types/test.ts new file mode 100644 index 00000000..ffce4f27 --- /dev/null +++ b/is-int16array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isInt16Array = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isInt16Array( new Int16Array( 4 ) ); // $ExpectType boolean + isInt16Array( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isInt16Array(); // $ExpectError + isInt16Array( new Int16Array( 4 ), 123 ); // $ExpectError +} diff --git a/is-int16array/examples/index.js b/is-int16array/examples/index.js new file mode 100644 index 00000000..3ebb4732 --- /dev/null +++ b/is-int16array/examples/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isInt16Array = require( './../lib' ); + +var bool = isInt16Array( new Int16Array( 10 ) ); +console.log( bool ); +// => true + +bool = isInt16Array( new Int8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt16Array( new Uint8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt16Array( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => false + +bool = isInt16Array( new Uint16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt16Array( new Int32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt16Array( new Uint32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt16Array( new Float32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt16Array( new Float64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt16Array( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt16Array( {} ); +console.log( bool ); +// => false + +bool = isInt16Array( null ); +console.log( bool ); +// => false diff --git a/is-int16array/lib/index.js b/is-int16array/lib/index.js new file mode 100644 index 00000000..d089e80c --- /dev/null +++ b/is-int16array/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an Int16Array. +* +* @module @stdlib/assert/is-int16array +* +* @example +* var isInt16Array = require( '@stdlib/assert/is-int16array' ); +* +* var bool = isInt16Array( new Int16Array( 10 ) ); +* // returns true +* +* bool = isInt16Array( [] ); +* // returns false +*/ + +// MODULES // + +var isInt16Array = require( './main.js' ); + + +// EXPORTS // + +module.exports = isInt16Array; diff --git a/is-int16array/lib/main.js b/is-int16array/lib/main.js new file mode 100644 index 00000000..0a2e04c0 --- /dev/null +++ b/is-int16array/lib/main.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var hasInt16Array = ( typeof Int16Array === 'function' ); // eslint-disable-line stdlib/require-globals + + +// MAIN // + +/** +* Tests if a value is an Int16Array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an Int16Array +* +* @example +* var bool = isInt16Array( new Int16Array( 10 ) ); +* // returns true +* +* @example +* var bool = isInt16Array( [] ); +* // returns false +*/ +function isInt16Array( value ) { + return ( + ( hasInt16Array && value instanceof Int16Array ) || // eslint-disable-line stdlib/require-globals + nativeClass( value ) === '[object Int16Array]' + ); +} + + +// EXPORTS // + +module.exports = isInt16Array; diff --git a/is-int16array/package.json b/is-int16array/package.json new file mode 100644 index 00000000..356683ea --- /dev/null +++ b/is-int16array/package.json @@ -0,0 +1,82 @@ +{ + "name": "@stdlib/assert/is-int16array", + "version": "0.0.0", + "description": "Test if a value is an Int16Array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "int16array", + "int16", + "signed", + "integer", + "int", + "short", + "word", + "halfword", + "typed", + "typed array", + "typed-array", + "array", + "is", + "isarray", + "istypedarray", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-int16array/test/test.js b/is-int16array/test/test.js new file mode 100644 index 00000000..ecaf55e6 --- /dev/null +++ b/is-int16array/test/test.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isInt16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isInt16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an Int16Array', function test( t ) { + t.strictEqual( isInt16Array( new Int16Array( 10 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an Int16Array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + undefined, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Uint32Array( 10 ), + new Int32Array( 10 ), + new Uint16Array( 10 ), + new Uint8Array( 10 ), + new Int8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isInt16Array( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-int32array/README.md b/is-int32array/README.md new file mode 100644 index 00000000..cb9557d9 --- /dev/null +++ b/is-int32array/README.md @@ -0,0 +1,116 @@ + + +# isInt32Array + +> Test if a value is an [Int32Array][mdn-int32array]. + +
+ +## Usage + +```javascript +var isInt32Array = require( '@stdlib/assert/is-int32array' ); +``` + +#### isInt32Array( value ) + +Tests if a value is an [`Int32Array`][mdn-int32array]. + +```javascript +var Int32Array = require( '@stdlib/array/int32' ); + +var bool = isInt32Array( new Int32Array( 10 ) ); +// returns true + +bool = isInt32Array( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isInt32Array = require( '@stdlib/assert/is-int32array' ); + +var bool = isInt32Array( new Int32Array( 10 ) ); +// returns true + +bool = isInt32Array( new Int8Array( 10 ) ); +// returns false + +bool = isInt32Array( new Uint8Array( 10 ) ); +// returns false + +bool = isInt32Array( new Uint8ClampedArray( 10 ) ); +// returns false + +bool = isInt32Array( new Uint16Array( 10 ) ); +// returns false + +bool = isInt32Array( new Int16Array( 10 ) ); +// returns false + +bool = isInt32Array( new Uint32Array( 10 ) ); +// returns false + +bool = isInt32Array( new Float32Array( 10 ) ); +// returns false + +bool = isInt32Array( new Float64Array( 10 ) ); +// returns false + +bool = isInt32Array( new Array( 10 ) ); +// returns false + +bool = isInt32Array( {} ); +// returns false + +bool = isInt32Array( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-int32array/benchmark/benchmark.js b/is-int32array/benchmark/benchmark.js new file mode 100644 index 00000000..0c52e31a --- /dev/null +++ b/is-int32array/benchmark/benchmark.js @@ -0,0 +1,126 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isInt32Array = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInt32Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Int32Array( 10 ), + new Int32Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInt32Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInt32Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-int32array/docs/repl.txt b/is-int32array/docs/repl.txt new file mode 100644 index 00000000..c6da54db --- /dev/null +++ b/is-int32array/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is an Int32Array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an Int32Array. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/int32}}( 10 ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-int32array/docs/types/index.d.ts b/is-int32array/docs/types/index.d.ts new file mode 100644 index 00000000..98e97b86 --- /dev/null +++ b/is-int32array/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an Int32Array. +* +* @param value - value to test +* @returns boolean indicating whether value is an Int32Array +* +* @example +* var bool = isInt32Array( new Int32Array( 10 ) ); +* // returns true +* +* @example +* var bool = isInt32Array( [] ); +* // returns false +*/ +declare function isInt32Array( value: any ): boolean; + + +// EXPORTS // + +export = isInt32Array; diff --git a/is-int32array/docs/types/test.ts b/is-int32array/docs/types/test.ts new file mode 100644 index 00000000..c648a888 --- /dev/null +++ b/is-int32array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isInt32Array = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isInt32Array( new Int32Array( 4 ) ); // $ExpectType boolean + isInt32Array( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isInt32Array(); // $ExpectError + isInt32Array( new Int32Array( 4 ), 123 ); // $ExpectError +} diff --git a/is-int32array/examples/index.js b/is-int32array/examples/index.js new file mode 100644 index 00000000..433dfd7e --- /dev/null +++ b/is-int32array/examples/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isInt32Array = require( './../lib' ); + +var bool = isInt32Array( new Int32Array( 10 ) ); +console.log( bool ); +// => true + +bool = isInt32Array( new Int8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt32Array( new Uint8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt32Array( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => false + +bool = isInt32Array( new Uint16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt32Array( new Int16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt32Array( new Uint32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt32Array( new Float32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt32Array( new Float64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt32Array( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt32Array( {} ); +console.log( bool ); +// => false + +bool = isInt32Array( null ); +console.log( bool ); +// => false diff --git a/is-int32array/lib/index.js b/is-int32array/lib/index.js new file mode 100644 index 00000000..b71c651f --- /dev/null +++ b/is-int32array/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an Int32Array. +* +* @module @stdlib/assert/is-int32array +* +* @example +* var isInt32Array = require( '@stdlib/assert/is-int32array' ); +* +* var bool = isInt32Array( new Int32Array( 10 ) ); +* // returns true +* +* bool = isInt32Array( [] ); +* // returns false +*/ + +// MODULES // + +var isInt32Array = require( './main.js' ); + + +// EXPORTS // + +module.exports = isInt32Array; diff --git a/is-int32array/lib/main.js b/is-int32array/lib/main.js new file mode 100644 index 00000000..05cc094e --- /dev/null +++ b/is-int32array/lib/main.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var hasInt32Array = ( typeof Int32Array === 'function' ); // eslint-disable-line stdlib/require-globals + + +// MAIN // + +/** +* Tests if a value is an Int32Array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an Int32Array +* +* @example +* var bool = isInt32Array( new Int32Array( 10 ) ); +* // returns true +* +* @example +* var bool = isInt32Array( [] ); +* // returns false +*/ +function isInt32Array( value ) { + return ( + ( hasInt32Array && value instanceof Int32Array ) || // eslint-disable-line stdlib/require-globals + nativeClass( value ) === '[object Int32Array]' + ); +} + + +// EXPORTS // + +module.exports = isInt32Array; diff --git a/is-int32array/package.json b/is-int32array/package.json new file mode 100644 index 00000000..e80aadee --- /dev/null +++ b/is-int32array/package.json @@ -0,0 +1,83 @@ +{ + "name": "@stdlib/assert/is-int32array", + "version": "0.0.0", + "description": "Test if a value is an Int32Array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "int32array", + "int32", + "signed", + "integer", + "int", + "long", + "word", + "longword", + "doubleword", + "typed", + "typed array", + "typed-array", + "array", + "is", + "isarray", + "istypedarray", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-int32array/test/test.js b/is-int32array/test/test.js new file mode 100644 index 00000000..27ffbc78 --- /dev/null +++ b/is-int32array/test/test.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isInt32Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isInt32Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an Int32Array', function test( t ) { + t.strictEqual( isInt32Array( new Int32Array( 10 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an Int32Array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + undefined, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Uint8Array( 10 ), + new Int8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isInt32Array( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-int8array/README.md b/is-int8array/README.md new file mode 100644 index 00000000..5fb82bb7 --- /dev/null +++ b/is-int8array/README.md @@ -0,0 +1,116 @@ + + +# isInt8Array + +> Test if a value is an [Int8Array][mdn-int8array]. + +
+ +## Usage + +```javascript +var isInt8Array = require( '@stdlib/assert/is-int8array' ); +``` + +#### isInt8Array( value ) + +Tests if a value is an [`Int8Array`][mdn-int8array]. + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); + +var bool = isInt8Array( new Int8Array( 10 ) ); +// returns true + +bool = isInt8Array( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isInt8Array = require( '@stdlib/assert/is-int8array' ); + +var bool = isInt8Array( new Int8Array( 10 ) ); +// returns true + +bool = isInt8Array( new Uint8Array( 10 ) ); +// returns false + +bool = isInt8Array( new Uint8ClampedArray( 10 ) ); +// returns false + +bool = isInt8Array( new Int16Array( 10 ) ); +// returns false + +bool = isInt8Array( new Uint16Array( 10 ) ); +// returns false + +bool = isInt8Array( new Int32Array( 10 ) ); +// returns false + +bool = isInt8Array( new Uint32Array( 10 ) ); +// returns false + +bool = isInt8Array( new Float32Array( 10 ) ); +// returns false + +bool = isInt8Array( new Float64Array( 10 ) ); +// returns false + +bool = isInt8Array( new Array( 10 ) ); +// returns false + +bool = isInt8Array( {} ); +// returns false + +bool = isInt8Array( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-int8array/benchmark/benchmark.js b/is-int8array/benchmark/benchmark.js new file mode 100644 index 00000000..5d2bf173 --- /dev/null +++ b/is-int8array/benchmark/benchmark.js @@ -0,0 +1,126 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isInt8Array = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInt8Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Int8Array( 10 ), + new Int8Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInt8Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInt8Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-int8array/docs/repl.txt b/is-int8array/docs/repl.txt new file mode 100644 index 00000000..c07fe89b --- /dev/null +++ b/is-int8array/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is an Int8Array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an Int8Array. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/int8}}( 10 ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-int8array/docs/types/index.d.ts b/is-int8array/docs/types/index.d.ts new file mode 100644 index 00000000..4e5d99bb --- /dev/null +++ b/is-int8array/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an Int8Array. +* +* @param value - value to test +* @returns boolean indicating whether value is an Int8Array +* +* @example +* var bool = isInt8Array( new Int8Array( 10 ) ); +* // returns true +* +* @example +* var bool = isInt8Array( [] ); +* // returns false +*/ +declare function isInt8Array( value: any ): boolean; + + +// EXPORTS // + +export = isInt8Array; diff --git a/is-int8array/docs/types/test.ts b/is-int8array/docs/types/test.ts new file mode 100644 index 00000000..e962a2bc --- /dev/null +++ b/is-int8array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isInt8Array = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isInt8Array( new Int8Array( 4 ) ); // $ExpectType boolean + isInt8Array( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isInt8Array(); // $ExpectError + isInt8Array( new Int8Array( 4 ), 123 ); // $ExpectError +} diff --git a/is-int8array/examples/index.js b/is-int8array/examples/index.js new file mode 100644 index 00000000..b3688990 --- /dev/null +++ b/is-int8array/examples/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isInt8Array = require( './../lib' ); + +var bool = isInt8Array( new Int8Array( 10 ) ); +console.log( bool ); +// => true + +bool = isInt8Array( new Uint8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt8Array( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => false + +bool = isInt8Array( new Int16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt8Array( new Uint16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt8Array( new Int32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt8Array( new Uint32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt8Array( new Float32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt8Array( new Float64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt8Array( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isInt8Array( {} ); +console.log( bool ); +// => false + +bool = isInt8Array( null ); +console.log( bool ); +// => false diff --git a/is-int8array/lib/index.js b/is-int8array/lib/index.js new file mode 100644 index 00000000..8f7ec87e --- /dev/null +++ b/is-int8array/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an Int8Array. +* +* @module @stdlib/assert/is-int8array +* +* @example +* var isInt8Array = require( '@stdlib/assert/is-int8array' ); +* +* var bool = isInt8Array( new Int8Array( 10 ) ); +* // returns true +* +* bool = isInt8Array( [] ); +* // returns false +*/ + +// MODULES // + +var isInt8Array = require( './main.js' ); + + +// EXPORTS // + +module.exports = isInt8Array; diff --git a/is-int8array/lib/main.js b/is-int8array/lib/main.js new file mode 100644 index 00000000..1d9a453f --- /dev/null +++ b/is-int8array/lib/main.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var hasInt8Array = ( typeof Int8Array === 'function' ); // eslint-disable-line stdlib/require-globals + + +// MAIN // + +/** +* Tests if a value is an Int8Array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an Int8Array +* +* @example +* var bool = isInt8Array( new Int8Array( 10 ) ); +* // returns true +* +* @example +* var bool = isInt8Array( [] ); +* // returns false +*/ +function isInt8Array( value ) { + return ( + ( hasInt8Array && value instanceof Int8Array ) || // eslint-disable-line stdlib/require-globals + nativeClass( value ) === '[object Int8Array]' + ); +} + + +// EXPORTS // + +module.exports = isInt8Array; diff --git a/is-int8array/package.json b/is-int8array/package.json new file mode 100644 index 00000000..ccbaa586 --- /dev/null +++ b/is-int8array/package.json @@ -0,0 +1,81 @@ +{ + "name": "@stdlib/assert/is-int8array", + "version": "0.0.0", + "description": "Test if a value is an Int8Array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "int8array", + "int8", + "signed", + "integer", + "int", + "byte", + "octet", + "typed", + "typed array", + "typed-array", + "array", + "is", + "isarray", + "istypedarray", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-int8array/test/test.js b/is-int8array/test/test.js new file mode 100644 index 00000000..56159d8e --- /dev/null +++ b/is-int8array/test/test.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isInt8Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isInt8Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an Int8Array', function test( t ) { + t.strictEqual( isInt8Array( new Int8Array( 10 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an Int8Array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + undefined, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Uint32Array( 10 ), + new Int32Array( 10 ), + new Uint16Array( 10 ), + new Int16Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isInt8Array( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-integer-array/README.md b/is-integer-array/README.md new file mode 100644 index 00000000..39182617 --- /dev/null +++ b/is-integer-array/README.md @@ -0,0 +1,130 @@ + + +# isIntegerArray + +> Test if a value is an array-like object containing only integers. + +
+ +## Usage + +```javascript +var isIntegerArray = require( '@stdlib/assert/is-integer-array' ); +``` + +#### isIntegerArray( value ) + +Tests if a `value` is an array-like object containing **only** `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isIntegerArray( [ -3, new Number(3) ] ); +// returns true + +bool = isIntegerArray( [ -3, 'abc' ] ); +// returns false +``` + +#### isIntegerArray.primitives( value ) + +Tests if a `value` is an array-like object containing **only** primitive `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isIntegerArray.primitives( [ -1.0, 0.0, 4.0 ] ); +// returns true + +bool = isIntegerArray.primitives( [ -1.0, 2.2 ] ); +// returns false + +bool = isIntegerArray.primitives( [ -3.0, new Number(2.0) ] ); +// returns false +``` + +#### isIntegerArray.objects( value ) + +Tests if a `value` is an array-like object containing **only** `Number` objects having `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isIntegerArray.objects( [ new Number(-1.0), new Number(2.0) ] ); +// returns true + +bool = isIntegerArray.objects( [ -1.0, 0.0, 1.0 ] ); +// returns false + +bool = isIntegerArray.objects( [ -3.0, new Number(1.0) ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isIntegerArray = require( '@stdlib/assert/is-integer-array' ); + +var bool = isIntegerArray( [ -5, 0, 2, 5 ] ); +// returns true + +bool = isIntegerArray( [ -4, -3, 1, 3 ] ); +// returns true + +bool = isIntegerArray( [ -1, new Number( -6 ), 2 ] ); +// returns true + +bool = isIntegerArray( [ -1, 'abc', 3 ] ); +// returns false + +bool = isIntegerArray( [ -2.3, 0, 3 ] ); +// returns false + +bool = isIntegerArray( [] ); +// returns false +``` + +
+ + + + + + diff --git a/is-integer-array/benchmark/benchmark.js b/is-integer-array/benchmark/benchmark.js new file mode 100644 index 00000000..2d997d93 --- /dev/null +++ b/is-integer-array/benchmark/benchmark.js @@ -0,0 +1,118 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isIntegerArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( 1.0 ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( 1.0 ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isIntegerArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isIntegerArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isIntegerArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-integer-array/docs/repl.txt b/is-integer-array/docs/repl.txt new file mode 100644 index 00000000..520df6c6 --- /dev/null +++ b/is-integer-array/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is an array-like object of integer values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an array-like object of integer + values. + + Examples + -------- + > var bool = {{alias}}( [ -3.0, new Number(0.0), 2.0 ] ) + true + > bool = {{alias}}( [ -3.0, '3.0' ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only primitive integer + values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an array-like object containing + only primitive integer values. + + Examples + -------- + > var bool = {{alias}}.primitives( [ -1.0, 10.0 ] ) + true + > bool = {{alias}}.primitives( [ -1.0, 0.0, 5.0 ] ) + true + > bool = {{alias}}.primitives( [ -3.0, new Number(-1.0) ] ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only number objects + having integer values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an array-like object containing + only number objects having integer values. + + Examples + -------- + > var bool = {{alias}}.objects( [ new Number(1.0), new Number(3.0) ] ) + true + > bool = {{alias}}.objects( [ -1.0, 0.0, 3.0 ] ) + false + > bool = {{alias}}.objects( [ 3.0, new Number(-1.0) ] ) + false + + See Also + -------- + diff --git a/is-integer-array/docs/types/index.d.ts b/is-integer-array/docs/types/index.d.ts new file mode 100644 index 00000000..3aa953b7 --- /dev/null +++ b/is-integer-array/docs/types/index.d.ts @@ -0,0 +1,108 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isIntegerArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsIntegerArray { + /** + * Tests if a value is an array-like object containing only integers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only integers + * + * @example + * var bool = isIntegerArray( [ -3.0, new Number(0.0), 2.0 ] ); + * // returns true + * + * @example + * var bool = isIntegerArray( [ -3.0, '3.0' ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only primitive integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only primitive integer values + * + * @example + * var bool = isIntegerArray.primitives( [ -1.0, 10.0 ] ); + * // returns true + * + * @example + * var bool = isIntegerArray.primitives( [ -1.0, 0.0, 5.0 ] ); + * // returns true + * + * @example + * var bool = isIntegerArray.primitives( [ -3.0, new Number(-1.0) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects having integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number objects having integer values + * + * @example + * var bool = isIntegerArray.objects( [ new Number(1.0), new Number(3.0) ] ); + * // returns true + * + * @example + * var bool = isIntegerArray.objects( [ -1.0, 0.0, 3.0 ] ); + * // returns false + * + * @example + * var bool = isIntegerArray.objects( [ 3.0, new Number(-1.0) ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object containing only integers. +* +* @param value - value to test +* @returns boolean indicating whether value is an array-like object containing only integers +* +* @example +* var bool = isIntegerArray( [ -3.0, new Number(0.0), 2.0 ] ); +* // returns true +* +* @example +* var bool = isIntegerArray( [ -3.0, '3.0' ] ); +* // returns false +* @example +* var bool = isIntegerArray.primitives( [ -1.0, 0.0, 5.0 ] ); +* // returns true +* +* @example +* var bool = isIntegerArray.objects( [ new Number(1.0), new Number(3.0) ] ); +* // returns true +*/ +declare var isIntegerArray: IsIntegerArray; + + +// EXPORTS // + +export = isIntegerArray; diff --git a/is-integer-array/docs/types/test.ts b/is-integer-array/docs/types/test.ts new file mode 100644 index 00000000..493518a7 --- /dev/null +++ b/is-integer-array/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isIntegerArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isIntegerArray( [ 4 ] ); // $ExpectType boolean + isIntegerArray( [ 2.8 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isIntegerArray(); // $ExpectError + isIntegerArray( [ -3 ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isIntegerArray.primitives( [ new Number( 3 ) ] ); // $ExpectType boolean + isIntegerArray.primitives( [ 3 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isIntegerArray.primitives(); // $ExpectError + isIntegerArray.primitives( [ 2 ], 123 ); // $ExpectError +} + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isIntegerArray.objects( [ new Number( -2 ) ] ); // $ExpectType boolean + isIntegerArray.objects( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isIntegerArray.objects(); // $ExpectError + isIntegerArray.objects( [ 2 ], 123 ); // $ExpectError +} diff --git a/is-integer-array/examples/index.js b/is-integer-array/examples/index.js new file mode 100644 index 00000000..b7a20a29 --- /dev/null +++ b/is-integer-array/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isIntegerArray = require( './../lib' ); + +var bool = isIntegerArray( [ -5, 0, 2, 5 ] ); +console.log( bool ); +// => true + +bool = isIntegerArray( [ -4, -3, 1, 3 ] ); +console.log( bool ); +// => true + +bool = isIntegerArray( [ -1, new Number( -6 ), 2 ] ); +console.log( bool ); +// => true + +bool = isIntegerArray( [ -1, 'abc', 3 ] ); +console.log( bool ); +// => false + +bool = isIntegerArray( [ -2.3, 0, 3 ] ); +console.log( bool ); +// => false + +bool = isIntegerArray( [] ); +console.log( bool ); +// => false diff --git a/is-integer-array/lib/index.js b/is-integer-array/lib/index.js new file mode 100644 index 00000000..a0210960 --- /dev/null +++ b/is-integer-array/lib/index.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only integers. +* +* @module @stdlib/assert/is-integer-array +* +* @example +* var isIntegerArray = require( '@stdlib/assert/is-integer-array' ); +* +* var bool = isIntegerArray( [ -3.0, new Number(0.0), 2.0 ] ); +* // returns true +* +* bool = isIntegerArray( [ -3.0, '3.0' ] ); +* // returns false +* +* @example +* var isIntegerArray = require( '@stdlib/assert/is-integer-array' ).primitives; +* +* var bool = isIntegerArray( [ -1.0, 10.0 ] ); +* // returns true +* +* bool = isIntegerArray( [ -1.0, 0.0, 5.0 ] ); +* // returns true +* +* bool = isIntegerArray( [ -3.0, new Number(-1.0) ] ); +* // returns false +* +* @example +* var isIntegerArray = require( '@stdlib/assert/is-integer-array' ).objects; +* +* var bool = isIntegerArray( [ new Number(1.0), new Number(3.0) ] ); +* // returns true +* +* bool = isIntegerArray( [ -1.0, 0.0, 3.0 ] ); +* // returns false +* +* bool = isIntegerArray( [ 3.0, new Number(-1.0) ] ); +* // returns false +*/ + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); + + +// MAIN // + +var isIntegerArray = arrayfun( isInteger ); +setReadOnly( isIntegerArray, 'primitives', arrayfun( isInteger.isPrimitive ) ); +setReadOnly( isIntegerArray, 'objects', arrayfun( isInteger.isObject ) ); + + +// EXPORTS // + +module.exports = isIntegerArray; diff --git a/is-integer-array/package.json b/is-integer-array/package.json new file mode 100644 index 00000000..d3c34576 --- /dev/null +++ b/is-integer-array/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/assert/is-integer-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only integers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "number", + "numeric", + "isnumber", + "isnumeric", + "integer", + "int", + "isinteger", + "type", + "check", + "validate", + "valid", + "test" + ] +} diff --git a/is-integer-array/test/test.js b/is-integer-array/test/test.js new file mode 100644 index 00000000..5db0601a --- /dev/null +++ b/is-integer-array/test/test.js @@ -0,0 +1,97 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isIntegerArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isIntegerArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only integer values', function test( t ) { + var arr; + + arr = [ 5, new Number( 5 ), -3 ]; + t.equal( isIntegerArray( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': 2, + '1': 1 + }; + t.equal( isIntegerArray( arr ), true, 'returns true' ); + + arr = [ 5.0, 2.3, 11.1 ]; + t.equal( isIntegerArray( arr ), false, 'returns false' ); + + arr = [ 5.0, '3', null ]; + t.equal( isIntegerArray( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only primitive integer values', function test( t ) { + var arr; + + arr = [ 5, -0, 0 ]; + t.equal( isIntegerArray.primitives( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': 1, + '1': -3 + }; + t.equal( isIntegerArray.primitives( arr ), true, 'returns true' ); + + arr = [ new Number( 5 ), 1, 1 ]; + t.equal( isIntegerArray.primitives( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only `Number` objects having integer values', function test( t ) { + var arr; + + arr = [ new Number( 5 ), new Number( -0 ), new Number( 0 ) ]; + t.equal( isIntegerArray.objects( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': new Number( 2 ), + '1': new Number( -3 ) + }; + t.equal( isIntegerArray.objects( arr ), true, 'returns true' ); + + arr = [ new Number( 5 ), 1, 1 ]; + t.equal( isIntegerArray.objects( arr ), false, 'returns false' ); + + arr = [ -5, 1, 1 ]; + t.equal( isIntegerArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-integer/README.md b/is-integer/README.md new file mode 100644 index 00000000..9e04ff78 --- /dev/null +++ b/is-integer/README.md @@ -0,0 +1,136 @@ + + +# isInteger + +> Test if a value is a number having an integer value. + +
+ +## Usage + +```javascript +var isInteger = require( '@stdlib/assert/is-integer' ); +``` + +#### isInteger( value ) + +Tests if a value is a `number` having an `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isInteger( 5.0 ); +// returns true + +bool = isInteger( new Number( 5.0 ) ); +// returns true + +bool = isInteger( -3.14 ); +// returns false + +bool = isInteger( null ); +// returns false +``` + +#### isInteger.isPrimitive( value ) + +Tests if a `value` is a primitive `number` having an `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isInteger.isPrimitive( -3.0 ); +// returns true + +bool = isInteger.isPrimitive( new Number( -3.0 ) ); +// returns false +``` + +#### isInteger.isObject( value ) + +Tests if a `value` is a `Number` object having an `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isInteger.isObject( 3.0 ); +// returns false + +bool = isInteger.isObject( new Number( 3.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isInteger = require( '@stdlib/assert/is-integer' ); + +var bool = isInteger( -5.0 ); +// returns true + +bool = isInteger( 0.0 ); +// returns true + +bool = isInteger( new Number( 5.0 ) ); +// returns true + +bool = isInteger( 5.256 ); +// returns false + +bool = isInteger( 1.0/0.0 ); +// returns false + +bool = isInteger( -1.0/0.0 ); +// returns false + +bool = isInteger( '5' ); +// returns false + +bool = isInteger( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-integer/benchmark/benchmark.js b/is-integer/benchmark/benchmark.js new file mode 100644 index 00000000..020c38c1 --- /dev/null +++ b/is-integer/benchmark/benchmark.js @@ -0,0 +1,222 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isInteger = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInteger( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInteger( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInteger.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInteger.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInteger.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isInteger.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-integer/docs/repl.txt b/is-integer/docs/repl.txt new file mode 100644 index 00000000..fec3a1c2 --- /dev/null +++ b/is-integer/docs/repl.txt @@ -0,0 +1,72 @@ + +{{alias}}( value ) + Tests if a value is an integer. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an integer. + + Examples + -------- + > var bool = {{alias}}( 5.0 ) + true + > bool = {{alias}}( new Number( 5.0 ) ) + true + > bool = {{alias}}( -3.14 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive having an integer value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive having an integer + value. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( -3.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( -3.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having an integer value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having an integer + value. + + Examples + -------- + > var bool = {{alias}}.isObject( 3.0 ) + false + > bool = {{alias}}.isObject( new Number( 3.0 ) ) + true + + See Also + -------- + diff --git a/is-integer/docs/types/index.d.ts b/is-integer/docs/types/index.d.ts new file mode 100644 index 00000000..707513bc --- /dev/null +++ b/is-integer/docs/types/index.d.ts @@ -0,0 +1,117 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isInteger` with methods for testing for primitives and objects, respectively. +*/ +interface IsInteger { + /** + * Tests if a value is an integer. + * + * @param value - value to test + * @returns boolean indicating whether value is an integer + * + * @example + * var bool = isInteger( 5.0 ); + * // returns true + * + * @example + * var bool = isInteger( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = isInteger( -3.14 ); + * // returns false + * + * @example + * var bool = isInteger( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having an integer value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having an integer value + * + * @example + * var bool = isInteger.isPrimitive( -3.0 ); + * // returns true + * + * @example + * var bool = isInteger.isPrimitive( new Number( -3.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having an integer value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having an integer value + * + * @example + * var bool = isInteger.isObject( 3.0 ); + * // returns false + * + * @example + * var bool = isInteger.isObject( new Number( 3.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is an integer. +* +* @param value - value to test +* @returns boolean indicating whether value is an integer +* +* @example +* var bool = isInteger( 5.0 ); +* // returns true +* +* @example +* var bool = isInteger( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isInteger( -3.14 ); +* // returns false +* +* @example +* var bool = isInteger( null ); +* // returns false +* +* @example +* var bool = isInteger.isPrimitive( -3.0 ); +* // returns true +* +* @example +* var bool = isInteger.isObject( new Number( 3.0 ) ); +* // returns true +*/ +declare var isInteger: IsInteger; + + +// EXPORTS // + +export = isInteger; diff --git a/is-integer/docs/types/test.ts b/is-integer/docs/types/test.ts new file mode 100644 index 00000000..977650d5 --- /dev/null +++ b/is-integer/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isInteger = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isInteger( 1.2 ); // $ExpectType boolean + isInteger( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isInteger(); // $ExpectError + isInteger( 2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isInteger.isPrimitive( new Number( 2 ) ); // $ExpectType boolean + isInteger.isPrimitive( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isInteger.isPrimitive(); // $ExpectError + isInteger.isPrimitive( 2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isInteger.isObject( new Number( 2 ) ); // $ExpectType boolean + isInteger.isObject( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isInteger.isObject(); // $ExpectError + isInteger.isObject( 2, 123 ); // $ExpectError +} diff --git a/is-integer/examples/index.js b/is-integer/examples/index.js new file mode 100644 index 00000000..fb32d82b --- /dev/null +++ b/is-integer/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isInteger = require( './../lib' ); + +console.log( isInteger( -5.0 ) ); +// => true + +console.log( isInteger( 0.0 ) ); +// => true + +console.log( isInteger( new Number( 5.0 ) ) ); +// => true + +console.log( isInteger( 5.256 ) ); +// => false + +console.log( isInteger( 1.0/0.0 ) ); +// => false + +console.log( isInteger( -1.0/0.0 ) ); +// => false + +console.log( isInteger( '5' ) ); +// => false + +console.log( isInteger( null ) ); +// => false diff --git a/is-integer/lib/index.js b/is-integer/lib/index.js new file mode 100644 index 00000000..d79282ed --- /dev/null +++ b/is-integer/lib/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an integer. +* +* @module @stdlib/assert/is-integer +* +* @example +* var isInteger = require( '@stdlib/assert/is-integer' ); +* +* var bool = isInteger( 5.0 ); +* // returns true +* +* bool = isInteger( new Number( 5.0 ) ); +* // returns true +* +* bool = isInteger( -3.14 ); +* // returns false +* +* bool = isInteger( null ); +* // returns false +* +* @example +* // Use interface to check for integer primitives... +* var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +* +* var bool = isInteger( -3.0 ); +* // returns true +* +* bool = isInteger( new Number( -3.0 ) ); +* // returns false +* +* @example +* // Use interface to check for integer objects... +* var isInteger = require( '@stdlib/assert/is-integer' ).isObject; +* +* var bool = isInteger( 3.0 ); +* // returns false +* +* bool = isInteger( new Number( 3.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isInteger = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isInteger, 'isPrimitive', isPrimitive ); +setReadOnly( isInteger, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isInteger; diff --git a/is-integer/lib/integer.js b/is-integer/lib/integer.js new file mode 100644 index 00000000..648bdf27 --- /dev/null +++ b/is-integer/lib/integer.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var PINF = require( '@stdlib/constants/math/float64-pinf' ); +var NINF = require( '@stdlib/constants/math/float64-ninf' ); +var isInt = require( '@stdlib/math/base/assert/is-integer' ); + + +// MAIN // + +/** +* Tests if a number primitive is an integer value. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a number primitive is an integer value +*/ +function isInteger( value ) { + return ( + value < PINF && + value > NINF && + isInt( value ) + ); +} + + +// EXPORTS // + +module.exports = isInteger; diff --git a/is-integer/lib/main.js b/is-integer/lib/main.js new file mode 100644 index 00000000..ededdb02 --- /dev/null +++ b/is-integer/lib/main.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is an integer. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an integer +* +* @example +* var bool = isInteger( 5.0 ); +* // returns true +* +* @example +* var bool = isInteger( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isInteger( -3.14 ); +* // returns false +* +* @example +* var bool = isInteger( null ); +* // returns false +*/ +function isInteger( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isInteger; diff --git a/is-integer/lib/object.js b/is-integer/lib/object.js new file mode 100644 index 00000000..076d0c92 --- /dev/null +++ b/is-integer/lib/object.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; +var isInt = require( './integer.js' ); + + +// MAIN // + +/** +* Tests if a value is a number object having an integer value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having an integer value +* +* @example +* var bool = isInteger( 3.0 ); +* // returns false +* +* @example +* var bool = isInteger( new Number( 3.0 ) ); +* // returns true +*/ +function isInteger( value ) { + return ( + isNumber( value ) && + isInt( value.valueOf() ) + ); +} + + +// EXPORTS // + +module.exports = isInteger; diff --git a/is-integer/lib/primitive.js b/is-integer/lib/primitive.js new file mode 100644 index 00000000..df10869b --- /dev/null +++ b/is-integer/lib/primitive.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isInt = require( './integer.js' ); + + +// MAIN // + +/** +* Tests if a value is a number primitive having an integer value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having an integer value +* +* @example +* var bool = isInteger( -3.0 ); +* // returns true +* +* @example +* var bool = isInteger( new Number( -3.0 ) ); +* // returns false +*/ +function isInteger( value ) { + return ( + isNumber( value ) && + isInt( value ) + ); +} + + +// EXPORTS // + +module.exports = isInteger; diff --git a/is-integer/package.json b/is-integer/package.json new file mode 100644 index 00000000..3341c950 --- /dev/null +++ b/is-integer/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-integer", + "version": "0.0.0", + "description": "Test if a value is a number having an integer value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "integer", + "int", + "is", + "isinteger", + "type", + "check", + "primitive", + "object" + ] +} diff --git a/is-integer/test/test.js b/is-integer/test/test.js new file mode 100644 index 00000000..f677ed1b --- /dev/null +++ b/is-integer/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isInteger = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number having an integer value', function test( t ) { + t.equal( typeof isInteger.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having an integer value', function test( t ) { + t.equal( typeof isInteger.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-integer/test/test.main.js b/is-integer/test/test.main.js new file mode 100644 index 00000000..5ddd5f15 --- /dev/null +++ b/is-integer/test/test.main.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isInteger = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number having an integer value', function test( t ) { + t.equal( isInteger( 5 ), true, 'returns true' ); + t.equal( isInteger( new Number( 5 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number having an integer value', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + null, + true, + undefined, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-integer/test/test.object.js b/is-integer/test/test.object.js new file mode 100644 index 00000000..f26c0d6c --- /dev/null +++ b/is-integer/test/test.object.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isInteger = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having an integer value', function test( t ) { + t.equal( isInteger( new Number( 5 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is an integer value', function test( t ) { + t.equal( isInteger( 3.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + undefined, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-integer/test/test.primitive.js b/is-integer/test/test.primitive.js new file mode 100644 index 00000000..69eb4e41 --- /dev/null +++ b/is-integer/test/test.primitive.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isInteger = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number having an integer value', function test( t ) { + t.equal( isInteger( -3.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has an integer value', function test( t ) { + t.equal( isInteger( new Number( 5.0 ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + undefined, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-iterable-like/README.md b/is-iterable-like/README.md new file mode 100644 index 00000000..1917da81 --- /dev/null +++ b/is-iterable-like/README.md @@ -0,0 +1,93 @@ + + +# isIterableLike + +> Test if a value is [`iterable`][mdn-iterable-protocol]-like. + +
+ +## Usage + +```javascript +var isIterableLike = require( '@stdlib/assert/is-iterable-like' ); +``` + +#### isIterableLike( value ) + +Tests if a `value` is [`iterable`][mdn-iterator-protocol]-like. + +```javascript +var bool = isIterableLike( [] ); +// returns + +bool = isIterableLike( {} ); +// returns false +``` + +
+ + + +
+ +## Notes + +- In order to be [iterable][mdn-iterable-protocol], an `object` must implement the `@@iterator` method, which, when called, returns an [iterator protocol-compliant object][mdn-iterator-protocol]. +- An [iterator protocol-compliant object][mdn-iterator-protocol] is an `object` having a `next` method following the [iterator protocol][mdn-iterator-protocol]. +- As full [iterator][mdn-iterator-protocol] compliance is **impossible** to achieve without evaluating an [iterator][mdn-iterator-protocol], this function checks **only** for interface compliance. +- In environments lacking `Symbol.iterator` support, this function always returns `false`. + +
+ + + +
+ +## Examples + + + +```javascript +var isIterableLike = require( '@stdlib/assert/is-iterable-like' ); + +var bool = isIterableLike( [] ); +// returns + +bool = isIterableLike( {} ); +// returns false + +bool = isIterableLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-iterable-like/benchmark/benchmark.js b/is-iterable-like/benchmark/benchmark.js new file mode 100644 index 00000000..2e5db9a3 --- /dev/null +++ b/is-iterable-like/benchmark/benchmark.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isIterableLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + { + 'next': function noop() {}, + 'return': function noop() {} + } + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isIterableLike( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-iterable-like/docs/repl.txt b/is-iterable-like/docs/repl.txt new file mode 100644 index 00000000..1d4aabd9 --- /dev/null +++ b/is-iterable-like/docs/repl.txt @@ -0,0 +1,38 @@ + +{{alias}}( value ) + Tests if a value is iterable-like. + + In order to be iterable, an object must implement the @@iterator method, + which, when called, returns an iterator protocol-compliant object. + + An iterator protocol-compliant object is an object having a `next` method + which adheres to the iterator protocol. + + As full iterator compliance is impossible to achieve without evaluating an + iterator, this function checks *only* for interface compliance. + + In environments lacking Symbol.iterator support, this function always + returns `false`. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is iterable-like. + + Examples + -------- + > var bool = {{alias}}( [ 1, 2, 3 ] ) + + > bool = {{alias}}( {} ) + false + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-iterable-like/docs/types/index.d.ts b/is-iterable-like/docs/types/index.d.ts new file mode 100644 index 00000000..fa02629c --- /dev/null +++ b/is-iterable-like/docs/types/index.d.ts @@ -0,0 +1,51 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is iterable-like. +* +* ## Notes +* +* - In order to be iterable, an object must implement the @@iterator method, which, when called, returns an iterator protocol-compliant object. +* - An iterator protocol-compliant object is an object having a `next` method which adheres to the iterator protocol. +* - As full iterator compliance is impossible to achieve without evaluating an iterator, this function checks *only* for interface compliance. +* - In environments lacking Symbol.iterator support, this function always returns `false`. +* +* @param value - value to test +* @returns boolean indicating whether a value is iterable-like +* +* @example +* var bool = isIterableLike( [] ); +* // returns +* +* @example +* var bool = isIterableLike( {} ); +* // returns false +* +* @example +* var bool = isIterableLike( null ); +* // returns false +*/ +declare function isIterableLike( value: any ): boolean; + + +// EXPORTS // + +export = isIterableLike; diff --git a/is-iterable-like/docs/types/test.ts b/is-iterable-like/docs/types/test.ts new file mode 100644 index 00000000..8722cb4c --- /dev/null +++ b/is-iterable-like/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isIterableLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isIterableLike( [ 1, 2, 3 ] ); // $ExpectType boolean + isIterableLike( {} ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isIterableLike(); // $ExpectError + isIterableLike( [ 1, 2, 3 ], 'abc' ); // $ExpectError +} diff --git a/is-iterable-like/examples/index.js b/is-iterable-like/examples/index.js new file mode 100644 index 00000000..e8685d97 --- /dev/null +++ b/is-iterable-like/examples/index.js @@ -0,0 +1,30 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isIterableLike = require( './../lib' ); + +console.log( isIterableLike( [] ) ); +// => + +console.log( isIterableLike( {} ) ); +// => false + +console.log( isIterableLike( null ) ); +// => false diff --git a/is-iterable-like/lib/index.js b/is-iterable-like/lib/index.js new file mode 100644 index 00000000..fbd72d14 --- /dev/null +++ b/is-iterable-like/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is iterable-like. +* +* @module @stdlib/assert/is-iterable-like +* +* @example +* var isIterableLike = require( '@stdlib/assert/is-iterable-like' ); +* +* var bool = isIterableLike( [] ); +* // returns +* +* bool = isIterableLike( {} ); +* // returns false +*/ + +// MODULES // + +var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' ); +var constantFunction = require( '@stdlib/utils/constant-function' ); +var main = require( './main.js' ); + + +// MAIN // + +var isIterable; +if ( hasIteratorSymbolSupport() ) { + isIterable = main; +} else { + isIterable = constantFunction( false ); +} + + +// EXPORTS // + +module.exports = isIterable; diff --git a/is-iterable-like/lib/main.js b/is-iterable-like/lib/main.js new file mode 100644 index 00000000..7b821790 --- /dev/null +++ b/is-iterable-like/lib/main.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( '@stdlib/assert/is-primitive' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var ITERATOR = require( '@stdlib/symbol/iterator' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); + + +// MAIN // + +/** +* Tests if a value is iterable-like. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is iterable-like +* +* @example +* var bool = isIterableLike( [] ); +* // returns +* +* @example +* var bool = isIterableLike( {} ); +* // returns false +* +* @example +* var bool = isIterableLike( null ); +* // returns false +*/ +function isIterableLike( value ) { + var f; + if ( isPrimitive( value ) ) { + return false; + } + f = value[ ITERATOR ]; + return ( + isFunction( f ) && + f.length === 0 && + isIteratorLike( value[ ITERATOR ]() ) // Why this form? In order to preserve `this`. + ); +} + + +// EXPORTS // + +module.exports = isIterableLike; diff --git a/is-iterable-like/package.json b/is-iterable-like/package.json new file mode 100644 index 00000000..bbbb1c54 --- /dev/null +++ b/is-iterable-like/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-iterable-like", + "version": "0.0.0", + "description": "Test if a value is iterable-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "object", + "obj", + "is", + "isobject", + "isiterable", + "iterator", + "iterable", + "es2015", + "es6", + "type", + "check", + "validate", + "test" + ] +} diff --git a/is-iterable-like/test/test.js b/is-iterable-like/test/test.js new file mode 100644 index 00000000..5886fb2b --- /dev/null +++ b/is-iterable-like/test/test.js @@ -0,0 +1,96 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' ); +var isIterableLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isIterableLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an iterable-like value', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + void 0, + null, + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isIterableLike( values[i] ), false, 'returns expected value when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an iterable-like value (ES2015+/ES6+)', function test( t ) { + if ( hasIteratorSymbolSupport() ) { + t.equal( isIterableLike( [] ), true, 'returns expected value' ); + } else { + t.ok( true, 'environment lacks iterator symbol support' ); + } + t.end(); +}); + +tape( 'in non-ES2015+/ES6+ environments, the function always returns `false`', function test( t ) { + var isIterableLike; + var values; + var i; + + isIterableLike = proxyquire( './../lib/index.js', { + '@stdlib/assert/has-iterator-symbol-support': hasSupport + }); + + values = [ + '5', + 5, + NaN, + true, + false, + void 0, + null, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isIterableLike( values[i] ), false, 'returns expected value when provided '+values[i] ); + } + t.end(); + + function hasSupport() { + return false; + } +}); diff --git a/is-iterator-like/README.md b/is-iterator-like/README.md new file mode 100644 index 00000000..8ab61a22 --- /dev/null +++ b/is-iterator-like/README.md @@ -0,0 +1,101 @@ + + +# isIteratorLike + +> Test if a value is [`iterator`][mdn-iterator-protocol]-like. + +
+ +## Usage + +```javascript +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +``` + +#### isIteratorLike( value ) + +Tests if a `value` is [`iterator`][mdn-iterator-protocol]-like. + + + +```javascript +var obj = { + 'next': function noop() {} +}; +var bool = isIteratorLike( obj ); +// returns true + +bool = isIteratorLike( {} ); +// returns false +``` + +
+ + + +
+ +## Notes + +- An [iterator protocol-compliant object][mdn-iterator-protocol] is an `object` having a `next` method following the [iterator protocol][mdn-iterator-protocol]. +- As full [iterator][mdn-iterator-protocol] compliance is **impossible** to achieve without evaluating an [iterator][mdn-iterator-protocol], this function checks **only** for interface compliance. + +
+ + + +
+ +## Examples + + + +```javascript +var noop = require( '@stdlib/utils/noop' ); +var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); + +var obj = { + 'next': noop +}; +var bool = isIteratorLike( obj ); +// returns true + +bool = isIteratorLike( {} ); +// returns false + +bool = isIteratorLike( [] ); +// returns false + +bool = isIteratorLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-iterator-like/benchmark/benchmark.js b/is-iterator-like/benchmark/benchmark.js new file mode 100644 index 00000000..e99ceca0 --- /dev/null +++ b/is-iterator-like/benchmark/benchmark.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isIteratorLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + { + 'next': function noop() {}, + 'return': function noop() {} + } + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isIteratorLike( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-iterator-like/docs/repl.txt b/is-iterator-like/docs/repl.txt new file mode 100644 index 00000000..a9d806f7 --- /dev/null +++ b/is-iterator-like/docs/repl.txt @@ -0,0 +1,35 @@ + +{{alias}}( value ) + Tests if a value is iterator-like. + + An iterator protocol-compliant object is an object having a `next` method + which adheres to the iterator protocol. + + As full iterator compliance is impossible to achieve without evaluating an + iterator, this function checks *only* for interface compliance. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is iterator-like. + + Examples + -------- + > var obj = { + ... 'next': function noop() {} + ... }; + > var bool = {{alias}}( obj ) + true + > bool = {{alias}}( {} ) + false + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-iterator-like/docs/types/index.d.ts b/is-iterator-like/docs/types/index.d.ts new file mode 100644 index 00000000..c575da8a --- /dev/null +++ b/is-iterator-like/docs/types/index.d.ts @@ -0,0 +1,68 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +interface Iterator { + /** + * Returns an iterator protocol-compliant object containing the next iterated value (if one exists) and a boolean flag indicating whether the iterator is finished. + * + * @returns iterator protocol-compliant object + */ + next(): IteratorResult; +} + +interface IteratorResult { + /** + * Iterated value (if one exists). + */ + value?: any; + + /** + * Boolean flag indicating whether the iterator is finished. + */ + done: boolean; +} + +/** +* Tests if a value is iterator-like. +* +* @param value - value to test +* @returns boolean indicating whether value is iterator-like +* +* @example +* var it = { +* 'next': function noop() {} +* }; +* var bool = isIteratorLike( it ); +* // returns true +* +* @example +* var bool = isIteratorLike( {} ); +* // returns false +* +* @example +* var bool = isIteratorLike( null ); +* // returns false +*/ +declare function isIteratorLike( value: any ): boolean; + + +// EXPORTS // + +export = isIteratorLike; diff --git a/is-iterator-like/docs/types/test.ts b/is-iterator-like/docs/types/test.ts new file mode 100644 index 00000000..ad750bd0 --- /dev/null +++ b/is-iterator-like/docs/types/test.ts @@ -0,0 +1,64 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isIteratorLike = require( './index' ); + +/** +* Returns an iterator protocol-compliant object. +* +* @returns iterator protocol-compliant object +*/ +function iterator() { + return { + 'next': next + }; + + /** + * Implements the iterator protocol `next` method. + * + * @returns iterator protocol-compliant object + */ + function next() { + return { + 'value': true, + 'done': false + }; + } +} + + +// TESTS // + +// The function returns a boolean... +{ + isIteratorLike( iterator() ); // $ExpectType boolean + isIteratorLike( '5' ); // $ExpectType boolean + isIteratorLike( 5 ); // $ExpectType boolean + isIteratorLike( true ); // $ExpectType boolean + isIteratorLike( false ); // $ExpectType boolean + isIteratorLike( null ); // $ExpectType boolean + isIteratorLike( undefined ); // $ExpectType boolean + isIteratorLike( [] ); // $ExpectType boolean + isIteratorLike( {} ); // $ExpectType boolean + isIteratorLike( ( x: number ): number => x ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + isIteratorLike(); // $ExpectError +} diff --git a/is-iterator-like/examples/index.js b/is-iterator-like/examples/index.js new file mode 100644 index 00000000..6ac6d3e7 --- /dev/null +++ b/is-iterator-like/examples/index.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var noop = require( '@stdlib/utils/noop' ); +var isIteratorLike = require( './../lib' ); + +var obj = { + 'next': noop +}; +console.log( isIteratorLike( obj ) ); +// => true + +console.log( isIteratorLike( {} ) ); +// => false + +console.log( isIteratorLike( [] ) ); +// => false + +console.log( isIteratorLike( null ) ); +// => false diff --git a/is-iterator-like/lib/index.js b/is-iterator-like/lib/index.js new file mode 100644 index 00000000..a3b39f63 --- /dev/null +++ b/is-iterator-like/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is iterator-like. +* +* @module @stdlib/assert/is-iterator-like +* +* @example +* var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); +* +* var it = { +* 'next': function noop() {} +* }; +* var bool = isIteratorLike( it ); +* // returns true +* +* bool = isIteratorLike( {} ); +* // returns false +*/ + +// MODULES // + +var isIterator = require( './main.js' ); + + +// EXPORTS // + +module.exports = isIterator; diff --git a/is-iterator-like/lib/main.js b/is-iterator-like/lib/main.js new file mode 100644 index 00000000..a378e972 --- /dev/null +++ b/is-iterator-like/lib/main.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); + + +// MAIN // + +/** +* Tests if a value is iterator-like. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is iterator-like +* +* @example +* var it = { +* 'next': function noop() {} +* }; +* var bool = isIteratorLike( it ); +* // returns true +* +* @example +* var bool = isIteratorLike( {} ); +* // returns false +* +* @example +* var bool = isIteratorLike( null ); +* // returns false +*/ +function isIteratorLike( value ) { + return ( + value !== null && + typeof value === 'object' && + isFunction( value.next ) && + value.next.length === 0 + ); +} + + +// EXPORTS // + +module.exports = isIteratorLike; diff --git a/is-iterator-like/package.json b/is-iterator-like/package.json new file mode 100644 index 00000000..6a684026 --- /dev/null +++ b/is-iterator-like/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-iterator-like", + "version": "0.0.0", + "description": "Test if a value is iterator-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "object", + "obj", + "is", + "isobject", + "isiterator", + "iterator", + "es2015", + "es6", + "type", + "check", + "validate", + "test" + ] +} diff --git a/is-iterator-like/test/test.js b/is-iterator-like/test/test.js new file mode 100644 index 00000000..163c2b3e --- /dev/null +++ b/is-iterator-like/test/test.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var noop = require( '@stdlib/utils/noop' ); +var isIteratorLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isIteratorLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an iterator-like value', function test( t ) { + var values; + var i; + values = [ + '5', + 5, + true, + false, + void 0, + null, + NaN, + [], + function noop() {}, + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isIteratorLike( values[i] ), false, 'returns expected value when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an iterator-like value', function test( t ) { + var it = { + 'next': noop + }; + t.equal( isIteratorLike( it ), true, 'returns expected value' ); + t.end(); +}); diff --git a/is-json/README.md b/is-json/README.md new file mode 100644 index 00000000..01f6bf9f --- /dev/null +++ b/is-json/README.md @@ -0,0 +1,90 @@ + + +# isJSON + +> Test if a value is a parseable JSON string. + +
+ +## Usage + +```javascript +var isJSON = require( '@stdlib/assert/is-json' ); +``` + +#### isJSON( value ) + +Tests if a `value` is a parseable JSON `string`. + +```javascript +var value = '{"a":5}'; + +var bool = isJSON( value ); +// returns true +``` + +
+ + + +
+ +## Notes + +- The implementation validates that the input `value` is a `string` literal. For all other inputs, the method returns `false`. + +- The implementation validates that a `string` begins with either `[` or `{` and ends with a corresponding `]` or `}`, respectively. Hence, the method will return `false` for the following `strings`, despite `JSON.parse` accepting their input: + + - `''` (e.g., `'5'`) + - `''` (e.g., `'true'`) + - `'null'` + +- The implementation wraps `JSON.parse` inside a `try/catch`. Hence, this function cannot be optimized by the compiler during runtime. Nevertheless, using this function is better than embedding a `try/catch` within a larger `function` which could be optimized in the absence of a `try/catch`. + +
+ + + +
+ +## Examples + + + +```javascript +var isJSON = require( '@stdlib/assert/is-json' ); + +var bool = isJSON( '{"a":5}' ); +// returns true + +bool = isJSON( '{a":5}' ); +// returns false +``` + +
+ + + + + + diff --git a/is-json/benchmark/benchmark.js b/is-json/benchmark/benchmark.js new file mode 100644 index 00000000..90f35643 --- /dev/null +++ b/is-json/benchmark/benchmark.js @@ -0,0 +1,121 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isJSON = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::invalid-arguments', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isJSON( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::invalid-json', function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isJSON( '{a":"'+fromCodePoint( 97 + (i%26) )+'"}' ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::valid-json', function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isJSON( '{"a":"'+fromCodePoint( 97 + (i%26) )+'"}' ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( 'JSON.parse', function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = JSON.parse( '{"a":"'+fromCodePoint( 97 + (i%26) )+'"}' ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-json/docs/repl.txt b/is-json/docs/repl.txt new file mode 100644 index 00000000..8abcb9e8 --- /dev/null +++ b/is-json/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a parseable JSON string. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a parseable JSON string. + + Examples + -------- + > var bool = {{alias}}( '{"a":5}' ) + true + > bool = {{alias}}( '{a":5}' ) + false + + See Also + -------- + diff --git a/is-json/docs/types/index.d.ts b/is-json/docs/types/index.d.ts new file mode 100644 index 00000000..a6a26299 --- /dev/null +++ b/is-json/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a parseable JSON string. +* +* @param value - value to test +* @returns boolean indicating if a value is a parseable JSON string +* +* @example +* var v = isJSON( '{"a":5}' ); +* // returns true +* +* @example +* var v = isJSON( '{a":5}' ); +* // returns false +*/ +declare function isJSON( value: any ): boolean; + + +// EXPORTS // + +export = isJSON; diff --git a/is-json/docs/types/test.ts b/is-json/docs/types/test.ts new file mode 100644 index 00000000..63d0349e --- /dev/null +++ b/is-json/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isJSON = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isJSON( '{"a":5}' ); // $ExpectType boolean + isJSON( '{a":5}' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isJSON(); // $ExpectError + isJSON( undefined, 123 ); // $ExpectError +} diff --git a/is-json/examples/index.js b/is-json/examples/index.js new file mode 100644 index 00000000..d9182ec1 --- /dev/null +++ b/is-json/examples/index.js @@ -0,0 +1,27 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isJSON = require( './../lib' ); + +console.log( isJSON( '{"a":5}' ) ); +// => true + +console.log( isJSON( '{a":5}' ) ); +// => false diff --git a/is-json/lib/index.js b/is-json/lib/index.js new file mode 100644 index 00000000..7d0de77c --- /dev/null +++ b/is-json/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a parseable JSON string. +* +* @module @stdlib/assert/is-json +* +* @example +* var isJSON = require( '@stdlib/assert/is-json' ); +* +* var v = isJSON( '{"a":5}' ); +* // returns true +* +* v = isJSON( '{a":5}' ); +* // returns false +*/ + +// MODULES // + +var isJSON = require( './main.js' ); + + +// EXPORTS // + +module.exports = isJSON; diff --git a/is-json/lib/main.js b/is-json/lib/main.js new file mode 100644 index 00000000..11ccfd60 --- /dev/null +++ b/is-json/lib/main.js @@ -0,0 +1,106 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// VARIABLES // + +/** +* Detects a JSON string. +* +* Regular expression: `/^\{[\s\S]*\}$|^\[[\s\S]*\]$/` +* +* - `^\{` +* +* - match a `{` literal which is the first character +* +* - `[\s\S]*` +* +* - match any whitespace and non-whitespace characters which occur `0` or more times +* +* - `\}$` +* +* - match a `}` literal which is the last character +* +* - `|` +* +* - alternatively +* +* - `^\[` +* +* - match a `[` literal which is the first character +* +* - `[\s\S]*` +* +* - match any whitespace and non-whitespace characters which occur `0` or more times +* +* - `\]$` +* +* - match a `]` literal which is the last character +* +* +* Example matching strings: +* +* - `'{}'` +* - `'[]'` +* - `'{adjlkfaj3743.,><\n\t\rdf}'` +* - `'[adjlkfaj3743.,><\n\t\rdf]'` +* - `'{"a":5}'` +* +* @constant +* @type {RegExp} +* @default /^\{[\s\S]*\}$|^\[[\s\S]*\]$/ +*/ +var re = /^\{[\s\S]*\}$|^\[[\s\S]*\]$/; + + +// MAIN // + +/** +* Tests if a value is a parseable JSON string. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a parseable JSON string +* +* @example +* var v = isJSON( '{"a":5}' ); +* // returns true +* +* @example +* var v = isJSON( '{a":5}' ); +* // returns false +*/ +function isJSON( value ) { + if ( typeof value !== 'string' ) { + return false; + } + if ( !re.test( value ) ) { + return false; + } + try { + JSON.parse( value ); + } catch ( err ) { // eslint-disable-line no-unused-vars + return false; + } + return true; +} + + +// EXPORTS // + +module.exports = isJSON; diff --git a/is-json/package.json b/is-json/package.json new file mode 100644 index 00000000..181ac914 --- /dev/null +++ b/is-json/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-json", + "version": "0.0.0", + "description": "Test if a value is a parseable JSON string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "json", + "string", + "parseable", + "is", + "isjson", + "test", + "check", + "validate", + "isvalid", + "valid" + ] +} diff --git a/is-json/test/test.js b/is-json/test/test.js new file mode 100644 index 00000000..31164fe5 --- /dev/null +++ b/is-json/test/test.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isJSON = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isJSON, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a parseable JSON string', function test( t ) { + var bool; + + bool = isJSON( '{"a":5}' ); + t.ok( bool ); + + bool = isJSON( '{}' ); + t.ok( bool ); + + bool = isJSON( '[]' ); + t.ok( bool ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a parseable JSON string', function test( t ) { + var values; + var i; + + values = [ + 5, + '5', + 'true', + 'null', + 'NaN', + '[', + '{', + ']', + '}', + '[{', + ']}', + '{[', + '}]', + null, + undefined, + true, + NaN, + function noop() {}, + [], + {}, + '{a":5}', + new String( '{"a":5}' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isJSON( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-leap-year/README.md b/is-leap-year/README.md new file mode 100644 index 00000000..b5bc3e0a --- /dev/null +++ b/is-leap-year/README.md @@ -0,0 +1,161 @@ + + +# isLeapYear + +> Test if a value corresponds to a [leap year][leap-year] in the [Gregorian calendar][gregorian-calendar]. + +A [leap year][leap-year] is defined as any year which is exactly divisible by `4`, except for years which are exactly divisible by `100` and not by `400`. In this definition, `100` corresponds to years marking a new century, and `400` corresponds to the length of the _leap cycle_. + +
+ +## Usage + +```javascript +var isLeapYear = require( '@stdlib/assert/is-leap-year' ); +``` + +#### isLeapYear( \[value] ) + +Tests if a `value` corresponds to a [leap year][leap-year] in the [Gregorian calendar][gregorian-calendar]. + +```javascript +var bool = isLeapYear(); +// returns + +bool = isLeapYear( new Date() ); +// returns + +bool = isLeapYear( 2000 ); +// returns true + +bool = isLeapYear( 2017 ); +// returns false +``` + +
+ + + +
+ +## Notes + +- If not provided any `arguments`, the function returns a `boolean` indicating if the current year (according to local time) is a [leap year][leap-year]. + +- If provided a `value` which is neither an integer value nor a [`Date`][date-object], the function **always** returns `false`. + + ```javascript + var bool = isLeapYear( '2016' ); + // returns false + + bool = isLeapYear( null ); + // returns false + ``` + +
+ + + +
+ +## Examples + + + +```javascript +var isLeapYear = require( '@stdlib/assert/is-leap-year' ); + +var bool; +var i; + +for ( i = 0; i < 2021; i++ ) { + bool = isLeapYear( i ); + console.log( 'The year %d %s a leap year.', i, ( bool ) ? 'is' : 'is not' ); +} +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-leap-year [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-leap-year + + +$ is-leap-year 1993 +false +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n 1992 | is-leap-year +true +``` + +
+ + + +
+ + + + + + diff --git a/is-leap-year/benchmark/benchmark.js b/is-leap-year/benchmark/benchmark.js new file mode 100644 index 00000000..c32d30c8 --- /dev/null +++ b/is-leap-year/benchmark/benchmark.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isLeapYear = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::invalid-arguments', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isLeapYear( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::year', function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isLeapYear( i%2017 ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::date-object', function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isLeapYear( new Date() ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-leap-year/bin/cli b/is-leap-year/bin/cli new file mode 100644 index 00000000..46788d30 --- /dev/null +++ b/is-leap-year/bin/cli @@ -0,0 +1,96 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isLeapYear = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + if ( args.length ) { + console.log( isLeapYear( args[ 0 ] ) ); // eslint-disable-line no-console + } else { + console.log( isLeapYear() ); // eslint-disable-line no-console + } + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isLeapYear( parseFloat( lines[ i ] ) ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-leap-year/docs/repl.txt b/is-leap-year/docs/repl.txt new file mode 100644 index 00000000..79ff2312 --- /dev/null +++ b/is-leap-year/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value ) + Tests whether a value corresponds to a leap year in the Gregorian calendar. + + A leap year is defined as any year which is exactly divisible by 4, except + for years which are exactly divisible by 100 and not by 400. In this + definition, 100 corresponds to years marking a new century, and 400 + corresponds to the length of the *leap cycle*. + + If not provided any arguments, the function returns a boolean indicating + if the current year (according to local time) is a leap year. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value corresponds to a leap year. + + Examples + -------- + > var bool = {{alias}}( new Date() ) + + > bool = {{alias}}( 1996 ) + true + > bool = {{alias}}( 2001 ) + false + + See Also + -------- + diff --git a/is-leap-year/docs/types/index.d.ts b/is-leap-year/docs/types/index.d.ts new file mode 100644 index 00000000..18f7226c --- /dev/null +++ b/is-leap-year/docs/types/index.d.ts @@ -0,0 +1,52 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests whether a value corresponds to a leap year in the Gregorian calendar. +* +* ## Notes +* +* - According to the Gregorian calendar, every year that is exactly divisible by `4` is a leap year, except those years which are also divisible by `100` and not by `400` (e.g., `1900`). +* +* @param value - input value +* @returns boolean whether a value corresponds to a leap year +* +* @example +* var bool = isLeapYear(); +* // returns +* +* @example +* var bool = isLeapYear( new Date() ); +* // returns +* +* @example +* var bool = isLeapYear( 1996 ); +* // returns true +* +* @example +* var bool = isLeapYear( 2001 ); +* // returns false +*/ +declare function isLeapYear( value?: any ): boolean; + + +// EXPORTS // + +export = isLeapYear; diff --git a/is-leap-year/docs/types/test.ts b/is-leap-year/docs/types/test.ts new file mode 100644 index 00000000..5749d1c8 --- /dev/null +++ b/is-leap-year/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isLeapYear = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isLeapYear(); // $ExpectType boolean + isLeapYear( 1996 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isLeapYear( undefined, 123 ); // $ExpectError +} diff --git a/is-leap-year/docs/usage.txt b/is-leap-year/docs/usage.txt new file mode 100644 index 00000000..733ed5b5 --- /dev/null +++ b/is-leap-year/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-leap-year [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-leap-year/etc/cli_opts.json b/is-leap-year/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-leap-year/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-leap-year/examples/index.js b/is-leap-year/examples/index.js new file mode 100644 index 00000000..24dcdf00 --- /dev/null +++ b/is-leap-year/examples/index.js @@ -0,0 +1,29 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isLeapYear = require( './../lib' ); + +var bool; +var i; + +for ( i = 0; i < 2021; i++ ) { + bool = isLeapYear( i ); + console.log( 'The year %d %s a leap year.', i, ( bool ) ? 'is' : 'is not' ); +} diff --git a/is-leap-year/lib/index.js b/is-leap-year/lib/index.js new file mode 100644 index 00000000..c48c6898 --- /dev/null +++ b/is-leap-year/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether a value corresponds to a leap year in the Gregorian calendar. +* +* @module @stdlib/assert/is-leap-year +* +* @example +* var isLeapYear = require( '@stdlib/assert/is-leap-year' ); +* +* var bool = isLeapYear(); +* // returns +* +* bool = isLeapYear( new Date() ); +* // returns +* +* bool = isLeapYear( 1996 ); +* // returns true +* +* bool = isLeapYear( 2001 ); +* // returns false +*/ + +// MODULES // + +var isLeapYear = require( './main.js' ); + + +// EXPORTS // + +module.exports = isLeapYear; diff --git a/is-leap-year/lib/main.js b/is-leap-year/lib/main.js new file mode 100644 index 00000000..848bfd56 --- /dev/null +++ b/is-leap-year/lib/main.js @@ -0,0 +1,81 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isDateObject = require( '@stdlib/assert/is-date-object' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; + + +// MAIN // + +/** +* Tests whether a value corresponds to a leap year in the Gregorian calendar. +* +* ## Notes +* +* - According to the Gregorian calendar, every year that is exactly divisible by `4` is a leap year, except those years which are also divisible by `100` and not by `400` (e.g., `1900`). +* +* @param {*} [value] - input value +* @returns {boolean} boolean whether a value corresponds to a leap year +* +* @example +* var bool = isLeapYear(); +* // returns +* +* @example +* var bool = isLeapYear( new Date() ); +* // returns +* +* @example +* var bool = isLeapYear( 1996 ); +* // returns true +* +* @example +* var bool = isLeapYear( 2001 ); +* // returns false +*/ +function isLeapYear( value ) { + var yr; + if ( arguments.length ) { + if ( isDateObject( value ) ) { + yr = value.getFullYear(); + } else if ( isInteger( value ) ) { + yr = value; + } else { + return false; + } + } else { + // Note: cannot cache, as possible for application to cross into a new year: + yr = ( new Date() ).getFullYear(); + } + // Special case if year is a new century... + if ( (yr % 100) === 0 ) { + // Centuries are only leap years at the end of "leap cycles" which happen every `400` years: + return ( (yr % 400) === 0 ); + } + // All other years which are exactly divisible by `4` are leap years: + return ( (yr % 4) === 0 ); +} + + +// EXPORTS // + +module.exports = isLeapYear; diff --git a/is-leap-year/package.json b/is-leap-year/package.json new file mode 100644 index 00000000..ceea0d62 --- /dev/null +++ b/is-leap-year/package.json @@ -0,0 +1,82 @@ +{ + "name": "@stdlib/assert/is-leap-year", + "version": "0.0.0", + "description": "Test if a value corresponds to a leap year in the Gregorian calendar.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-leap-year": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "date", + "year", + "yr", + "leap", + "leap-year", + "gregorian", + "calendar", + "time", + "is", + "isleap", + "isleapyear", + "type", + "check", + "valid", + "validate", + "test", + "isvalid" + ] +} diff --git a/is-leap-year/test/fixtures/stdin_error.js.txt b/is-leap-year/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-leap-year/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-leap-year/test/test.cli.js b/is-leap-year/test/test.cli.js new file mode 100644 index 00000000..46aaf539 --- /dev/null +++ b/is-leap-year/test/test.cli.js @@ -0,0 +1,247 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints out whether the current year corresponds to a leap year in the Gregorian calendar', opts, function test( t ) { + var cmd; + var str; + + cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if an input argument corresponds to a leap year in the Gregorian calendar', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'1992\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var expected; + var cmd; + + cmd = [ + 'printf "1992\n1993\n1994\n1995\n1996"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + expected = [ true, false, false, false, true ].join( '\n' )+'\n'; + t.strictEqual( stdout.toString(), expected, 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-leap-year/test/test.js b/is-leap-year/test/test.js new file mode 100644 index 00000000..92bace98 --- /dev/null +++ b/is-leap-year/test/test.js @@ -0,0 +1,137 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var leftPad = require( '@stdlib/string/left-pad' ); +var isLeapYear = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isLeapYear, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a value which is neither an integer nor a `Date` object', function test( t ) { + var values; + var bool; + var i; + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + undefined, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + bool = isLeapYear( values[ i ] ); + t.strictEqual( bool, false, 'returns false if provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns a `boolean`', function test( t ) { + var bool = isLeapYear(); + t.strictEqual( typeof bool, 'boolean', 'returns a boolean' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a leap year (integer)', function test( t ) { + var bool; + var i; + + for ( i = -2000; i < 2101; i += 4 ) { + bool = isLeapYear( i ); + if ( (i % 100) === 0 && (i % 400) !== 0 ) { + continue; + } + t.strictEqual( bool, true, 'returns true for '+i ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided leap year (integer)', function test( t ) { + var bool; + var i; + + for ( i = -2000; i < 2101; i++ ) { + bool = isLeapYear( i ); + if ( (i % 100) === 0 && (i % 400) === 0 ) { + continue; + } + if ( (i % 4) === 0 ) { + continue; + } + t.strictEqual( bool, false, 'returns false for '+i ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided a leap year (`Date`)', function test( t ) { + var bool; + var yr; + var d; + var i; + + for ( i = 0; i < 2101; i += 4 ) { + yr = leftPad( i.toString(), 4, '0' ); + d = new Date( yr+'-01-11' ); + bool = isLeapYear( d ); + yr = d.getFullYear(); + if ( (yr % 100) === 0 && (yr % 400) !== 0 ) { + continue; + } + t.strictEqual( bool, true, 'returns true for '+yr ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided leap year (`Date`)', function test( t ) { + var bool; + var yr; + var d; + var i; + + for ( i = 0; i < 2101; i++ ) { + yr = leftPad( i.toString(), 4, '0' ); + d = new Date( yr+'-01-11' ); + bool = isLeapYear( d ); + yr = d.getFullYear(); + if ( (yr % 100) === 0 && (yr % 400) === 0 ) { + continue; + } + if ( (yr % 4) === 0 ) { + continue; + } + t.strictEqual( bool, false, 'returns false for '+yr ); + } + t.end(); +}); diff --git a/is-little-endian/README.md b/is-little-endian/README.md new file mode 100644 index 00000000..1ad2ee3d --- /dev/null +++ b/is-little-endian/README.md @@ -0,0 +1,108 @@ + + +# Little Endian + +> Check if an environment is [little endian][endianness]. + +
+ +## Usage + +```javascript +var IS_LITTLE_ENDIAN = require( '@stdlib/assert/is-little-endian' ); +``` + +#### IS_LITTLE_ENDIAN + +`Boolean` indicating if an environment is [little endian][endianness]. + +```javascript +var bool = IS_LITTLE_ENDIAN; +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var IS_LITTLE_ENDIAN = require( '@stdlib/assert/is-little-endian' ); + +console.log( IS_LITTLE_ENDIAN ); +// => +``` + +
+ + + +* * * + +
+## CLI + +
+ +### Usage + +```text +Usage: is-little-endian [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-little-endian + +``` + +
+ + + +
+ + + + + + diff --git a/is-little-endian/bin/cli b/is-little-endian/bin/cli new file mode 100644 index 00000000..2e8e7ac7 --- /dev/null +++ b/is-little-endian/bin/cli @@ -0,0 +1,60 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var isLittleEndian = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var flags; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + console.log( isLittleEndian ); // eslint-disable-line no-console +} + +main(); diff --git a/is-little-endian/docs/repl.txt b/is-little-endian/docs/repl.txt new file mode 100644 index 00000000..740a1386 --- /dev/null +++ b/is-little-endian/docs/repl.txt @@ -0,0 +1,12 @@ + +{{alias}} + Boolean indicating if the environment is little endian. + + Examples + -------- + > {{alias}} + + + See Also + -------- + diff --git a/is-little-endian/docs/types/index.d.ts b/is-little-endian/docs/types/index.d.ts new file mode 100644 index 00000000..0db1ca6e --- /dev/null +++ b/is-little-endian/docs/types/index.d.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Return a boolean indicating if an environment is little endian. +* +* @example +* var bool = IS_LITTLE_ENDIAN; +* // returns +*/ +declare const IS_LITTLE_ENDIAN: boolean; + + +// EXPORTS // + +export = IS_LITTLE_ENDIAN; diff --git a/is-little-endian/docs/types/test.ts b/is-little-endian/docs/types/test.ts new file mode 100644 index 00000000..4074d69d --- /dev/null +++ b/is-little-endian/docs/types/test.ts @@ -0,0 +1,28 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import IS_LITTLE_ENDIAN = require( './index' ); + + +// TESTS // + +// The variable is a boolean... +{ + // tslint:disable-next-line:no-unused-expression + IS_LITTLE_ENDIAN; // $ExpectType boolean +} diff --git a/is-little-endian/docs/usage.txt b/is-little-endian/docs/usage.txt new file mode 100644 index 00000000..e0fb62dc --- /dev/null +++ b/is-little-endian/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-little-endian [options] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-little-endian/etc/cli_opts.json b/is-little-endian/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-little-endian/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-little-endian/examples/index.js b/is-little-endian/examples/index.js new file mode 100644 index 00000000..3ce5e6c2 --- /dev/null +++ b/is-little-endian/examples/index.js @@ -0,0 +1,23 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var IS_LITTLE_ENDIAN = require( './../lib' ); + +console.log( IS_LITTLE_ENDIAN ); diff --git a/is-little-endian/lib/ctors.js b/is-little-endian/lib/ctors.js new file mode 100644 index 00000000..5d7a0e8c --- /dev/null +++ b/is-little-endian/lib/ctors.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint16Array = require( '@stdlib/array/uint16' ); + + +// MAIN // + +var ctors = { + 'uint16': Uint16Array, + 'uint8': Uint8Array +}; + + +// EXPORTS // + +module.exports = ctors; diff --git a/is-little-endian/lib/index.js b/is-little-endian/lib/index.js new file mode 100644 index 00000000..39ea203a --- /dev/null +++ b/is-little-endian/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return a boolean indicating if an environment is little endian. +* +* @module @stdlib/assert/is-little-endian +* +* @example +* var IS_LITTLE_ENDIAN = require( '@stdlib/assert/is-little-endian' ); +* +* var bool = IS_LITTLE_ENDIAN; +* // returns +*/ + +// MODULES // + +var IS_LITTLE_ENDIAN = require( './main.js' ); + + +// EXPORTS // + +module.exports = IS_LITTLE_ENDIAN; diff --git a/is-little-endian/lib/main.js b/is-little-endian/lib/main.js new file mode 100644 index 00000000..190df3de --- /dev/null +++ b/is-little-endian/lib/main.js @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var ctors = require( './ctors.js' ); + + +// VARIABLES // + +var bool; + + +// FUNCTIONS // + +/** +* Returns a boolean indicating if an environment is little endian. +* +* @private +* @returns {boolean} boolean indicating if an environment is little endian +* +* @example +* var bool = isLittleEndian(); +* // returns +*/ +function isLittleEndian() { + var uint16view; + var uint8view; + + uint16view = new ctors[ 'uint16' ]( 1 ); + + /* + * Set the uint16 view to a value having distinguishable lower and higher order words. + * + * 4660 => 0x1234 => 0x12 0x34 => '00010010 00110100' => (0x12,0x34) == (18,52) + */ + uint16view[ 0 ] = 0x1234; + + // Create a uint8 view on top of the uint16 buffer: + uint8view = new ctors[ 'uint8' ]( uint16view.buffer ); + + // If little endian, the least significant byte will be first... + return ( uint8view[ 0 ] === 0x34 ); +} + + +// MAIN // + +bool = isLittleEndian(); + + +// EXPORTS // + +module.exports = bool; diff --git a/is-little-endian/package.json b/is-little-endian/package.json new file mode 100644 index 00000000..4d534a46 --- /dev/null +++ b/is-little-endian/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-little-endian", + "version": "0.0.0", + "description": "Check if an environment is little endian.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-little-endian": "./bin/cli" + }, + "main": "./lib", + "directories": { + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "le", + "be", + "little", + "endian", + "big", + "is", + "cli", + "byte order", + "check" + ] +} diff --git a/is-little-endian/test/test.cli.js b/is-little-endian/test/test.cli.js new file mode 100644 index 00000000..02335591 --- /dev/null +++ b/is-little-endian/test/test.cli.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints either `true` or `false` to `stdout` indicating whether an environment is little endian', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'true\n' || str === 'false\n', true, 'prints either `true` or `false` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); diff --git a/is-little-endian/test/test.js b/is-little-endian/test/test.js new file mode 100644 index 00000000..307ceffe --- /dev/null +++ b/is-little-endian/test/test.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var IS_LITTLE_ENDIAN = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_LITTLE_ENDIAN, 'boolean', 'main export is a boolean' ); + t.end(); +}); + +tape( 'the boolean is `true` if an environment is little endian', function test( t ) { + var IS_LITTLE_ENDIAN = proxyquire( './../lib/main.js', { + './ctors.js': { + 'uint8': Foo + } + }); + + t.equal( IS_LITTLE_ENDIAN, true, 'is little endian' ); + + t.end(); + + // Mock little endian byte order, where least significant bits come first... + function Foo() { + return new Uint8Array( [ 52, 18 ] ); + } +}); + +tape( 'the boolean is `false` if an environment is not little endian (e.g., big endian)', function test( t ) { + var IS_LITTLE_ENDIAN = proxyquire( './../lib/main.js', { + './ctors.js': { + 'uint8': Foo + } + }); + + t.equal( IS_LITTLE_ENDIAN, false, 'is not little endian' ); + t.end(); + + // Mock big endian byte order, where most significant bits are first... + function Foo() { + return new Uint8Array( [ 18, 52 ] ); + } +}); diff --git a/is-lowercase/README.md b/is-lowercase/README.md new file mode 100644 index 00000000..841c665f --- /dev/null +++ b/is-lowercase/README.md @@ -0,0 +1,141 @@ + + +# isLowercase + +> Test if a value is a lowercase string. + +
+ +## Usage + +```javascript +var isLowercase = require( '@stdlib/assert/is-lowercase' ); +``` + +#### isLowercase( value ) + +Tests if a `value` is a lowercase `string`. + +```javascript +var bool = isLowercase( 'salt and light' ); +// returns true + +bool = isLowercase( 'HELLO' ); +// returns false + +bool = isLowercase( 'World' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- This function validates that a `value` is a `string`. For all other types, the function returns `false`. + +
+ + + +
+ +## Examples + + + +```javascript +var isLowercase = require( '@stdlib/assert/is-lowercase' ); + +var bool = isLowercase( 'hello' ); +// returns true + +bool = isLowercase( '' ); +// returns false + +bool = isLowercase( 'Hello' ); +// returns false + +bool = isLowercase( 'HELLO' ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-lowercase [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-lowercase BEEP +false +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n 'boop' | is-lowercase +true +``` + +
+ + + +
+ + + + + + diff --git a/is-lowercase/benchmark/benchmark.js b/is-lowercase/benchmark/benchmark.js new file mode 100644 index 00000000..91292418 --- /dev/null +++ b/is-lowercase/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isLowercase = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isLowercase( fromCodePoint( i%126 ) ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-lowercase/bin/cli b/is-lowercase/bin/cli new file mode 100644 index 00000000..abd78754 --- /dev/null +++ b/is-lowercase/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isLowercase = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isLowercase( args[ 0 ] ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isLowercase( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-lowercase/docs/repl.txt b/is-lowercase/docs/repl.txt new file mode 100644 index 00000000..161273ad --- /dev/null +++ b/is-lowercase/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a lowercase string. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a lowercase string. + + Examples + -------- + > var bool = {{alias}}( 'hello' ) + true + > bool = {{alias}}( 'World' ) + false + + See Also + -------- + diff --git a/is-lowercase/docs/types/index.d.ts b/is-lowercase/docs/types/index.d.ts new file mode 100644 index 00000000..6b3ef695 --- /dev/null +++ b/is-lowercase/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a lowercase string. +* +* @param value - value to test +* @returns boolean indicating whether value is a lowercase string +* +* @example +* var bool = isLowercase( 'salt and light' ); +* // returns true +* +* @example +* var bool = isLowercase( 'HELLO' ); +* // returns false +* +* @example +* var bool = isLowercase( 'World' ); +* // returns false +* +* @example +* var bool = isLowercase( '!' ); +* // returns false +*/ +declare function isLowercase( value: any ): boolean; + + +// EXPORTS // + +export = isLowercase; diff --git a/is-lowercase/docs/types/test.ts b/is-lowercase/docs/types/test.ts new file mode 100644 index 00000000..a66abb75 --- /dev/null +++ b/is-lowercase/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isLowercase = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isLowercase( 'salt and light' ); // $ExpectType boolean + isLowercase( 'World' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isLowercase(); // $ExpectError + isLowercase( 'salt and light', 123 ); // $ExpectError +} diff --git a/is-lowercase/docs/usage.txt b/is-lowercase/docs/usage.txt new file mode 100644 index 00000000..99694ac6 --- /dev/null +++ b/is-lowercase/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-lowercase [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-lowercase/etc/cli_opts.json b/is-lowercase/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-lowercase/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-lowercase/examples/index.js b/is-lowercase/examples/index.js new file mode 100644 index 00000000..945f71e7 --- /dev/null +++ b/is-lowercase/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isLowercase = require( './../lib' ); + +console.log( isLowercase( 'hello' ) ); +// => true + +console.log( isLowercase( '' ) ); +// => false + +console.log( isLowercase( 'Hello' ) ); +// => false + +console.log( isLowercase( 'HELLO' ) ); +// => false diff --git a/is-lowercase/lib/index.js b/is-lowercase/lib/index.js new file mode 100644 index 00000000..f541b1cc --- /dev/null +++ b/is-lowercase/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a lowercase string. +* +* @module @stdlib/assert/is-lowercase +* +* @example +* var isLowercase = require( '@stdlib/assert/is-lowercase' ); +* +* var bool = isLowercase( 'hello' ); +* // returns true +* +* bool = isLowercase( 'World' ); +* // returns false +*/ + +// MODULES // + +var isLowercase = require( './main.js' ); + + +// EXPORTS // + +module.exports = isLowercase; diff --git a/is-lowercase/lib/main.js b/is-lowercase/lib/main.js new file mode 100644 index 00000000..1899c015 --- /dev/null +++ b/is-lowercase/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var lowercase = require( '@stdlib/string/lowercase' ); +var uppercase = require( '@stdlib/string/uppercase' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a lowercase string. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a lowercase string +* +* @example +* var bool = isLowercase( 'salt and light' ); +* // returns true +* +* @example +* var bool = isLowercase( 'HELLO' ); +* // returns false +* +* @example +* var bool = isLowercase( 'World' ); +* // returns false +* +* @example +* var bool = isLowercase( '!' ); +* // returns false +*/ +function isLowercase( value ) { + return ( + isString( value ) && + value === lowercase( value ) && + value !== uppercase( value ) + ); +} + + +// EXPORTS // + +module.exports = isLowercase; diff --git a/is-lowercase/package.json b/is-lowercase/package.json new file mode 100644 index 00000000..9335a99e --- /dev/null +++ b/is-lowercase/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-lowercase", + "version": "0.0.0", + "description": "Test if a value is a lowercase string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-lowercase": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "islowercase", + "lowercase", + "string", + "valid", + "validate", + "test" + ] +} diff --git a/is-lowercase/test/fixtures/stdin_error.js.txt b/is-lowercase/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-lowercase/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-lowercase/test/test.cli.js b/is-lowercase/test/test.cli.js new file mode 100644 index 00000000..761fe649 --- /dev/null +++ b/is-lowercase/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument contains only lowercase letters', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'Hello\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'false\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "BEEP\nboop"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'false\ntrue\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-lowercase/test/test.js b/is-lowercase/test/test.js new file mode 100644 index 00000000..bf9a5e7f --- /dev/null +++ b/is-lowercase/test/test.js @@ -0,0 +1,77 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isLowercase = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isLowercase, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a lowercase string', function test( t ) { + var values; + var bool; + var i; + + values = [ + 'hello world!', + 'above all summits it is calm' + ]; + + for ( i = 0; i < values.length; i++ ) { + bool = isLowercase( values[ i ] ); + t.strictEqual( bool, true, 'returns true when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a lowercase string', function test( t ) { + var values; + var i; + + values = [ + 'In all the tree-tops you feel scarcely a breath', + 'HELLO WORLD', + '', + '1139094843', + '!', + void 0, + 0, + NaN, + null, + false, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isLowercase( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-matrix-like/README.md b/is-matrix-like/README.md new file mode 100644 index 00000000..9bb236d6 --- /dev/null +++ b/is-matrix-like/README.md @@ -0,0 +1,83 @@ + + +# isMatrixLike + +> Test if a value is 2-dimensional [ndarray][@stdlib/ndarray/ctor]-like object. + +
+ +## Usage + +```javascript +var isMatrixLike = require( '@stdlib/assert/is-matrix-like' ); +``` + +#### isMatrixLike( value ) + +Tests if a value is a 2-dimensional [ndarray][@stdlib/ndarray/ctor]-like object. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var bool = isMatrixLike( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isMatrixLike = require( '@stdlib/assert/is-matrix-like' ); + +var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var out = isMatrixLike( arr ); +// returns true + +out = isMatrixLike( [ 1, 2, 3, 4 ] ); +// returns false + +out = isMatrixLike( {} ); +// returns false + +out = isMatrixLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-matrix-like/benchmark/benchmark.js b/is-matrix-like/benchmark/benchmark.js new file mode 100644 index 00000000..736e370b --- /dev/null +++ b/is-matrix-like/benchmark/benchmark.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var isMatrixLike = require( './../lib' ); + + +// MAIN // + +bench( pkg + '::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isMatrixLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg + '::true', function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr1; + var arr2; + var i; + + buffer = [ 0, 0, 0, 0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr1 = ndarray( 'generic', buffer, shape, strides, offset, order ); + arr2 = ndarray( 'generic', buffer, shape, strides, offset, order ); + + values = [ + arr1, + arr2 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isMatrixLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-matrix-like/docs/repl.txt b/is-matrix-like/docs/repl.txt new file mode 100644 index 00000000..b2c6f6e8 --- /dev/null +++ b/is-matrix-like/docs/repl.txt @@ -0,0 +1,41 @@ + +{{alias}}( value ) + Tests if a value is a 2-dimensional ndarray-like object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a 2-dimensional ndarray-like + object. + + Examples + -------- + > var M = {}; + > M.data = [ 0, 0, 0, 0 ]; + > M.ndims = 2; + > M.shape = [ 2, 2 ]; + > M.strides = [ 2, 1 ]; + > M.offset = 0; + > M.order = 'row-major'; + > M.dtype = 'generic'; + > M.length = 4; + > M.flags = {}; + > M.get = function get( i, j ) {}; + > M.set = function set( i, j ) {}; + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-matrix-like/docs/types/index.d.ts b/is-matrix-like/docs/types/index.d.ts new file mode 100644 index 00000000..2ff8f330 --- /dev/null +++ b/is-matrix-like/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a 2-dimensional ndarray-like object. +* +* @param v - value to test +* @returns boolean indicating if a value is a 2-dimensional ndarray-like object +* +* @example +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isMatrixLike( arr ); +* // returns true +* +* bool = isMatrixLike( [] ); +* // returns false +*/ +declare function isMatrixLike( v: any ): boolean; + + +// EXPORTS // + +export = isMatrixLike; diff --git a/is-matrix-like/docs/types/test.ts b/is-matrix-like/docs/types/test.ts new file mode 100644 index 00000000..a4eb253d --- /dev/null +++ b/is-matrix-like/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* tslint:disable:no-invalid-this */ + +import isMatrixLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const matrix = { + 'data': [ 2, 1, 1, 2 ], + 'ndims': 2, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': function get( i: number, j: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + return this.data[ idx ]; + }, + 'set': function set( i: number, j: number, v: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + this.data[ idx ] = v; + return v; + } + }; + isMatrixLike( matrix ); // $ExpectType boolean + isMatrixLike( [] ); // $ExpectType boolean + isMatrixLike( false ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isMatrixLike(); // $ExpectError +} diff --git a/is-matrix-like/examples/index.js b/is-matrix-like/examples/index.js new file mode 100644 index 00000000..fd30caa4 --- /dev/null +++ b/is-matrix-like/examples/index.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isMatrixLike = require( './../lib' ); + +var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +console.log( isMatrixLike( arr ) ); +// => true + +console.log( isMatrixLike( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isMatrixLike( {} ) ); +// => false + +console.log( isMatrixLike( null ) ); +// => false diff --git a/is-matrix-like/lib/index.js b/is-matrix-like/lib/index.js new file mode 100644 index 00000000..56f19c2b --- /dev/null +++ b/is-matrix-like/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a 2-dimensional ndarray-like object. +* +* @module @stdlib/assert/is-matrix-like +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isMatrixLike = require( '@stdlib/assert/is-matrix-like' ); +* +* var arr = ndarray( 'float64', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isMatrixLike( arr ); +* // returns true +* +* bool = isMatrixLike( [] ); +* // returns false +*/ + +// MODULES // + +var isMatrixLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isMatrixLike; diff --git a/is-matrix-like/lib/main.js b/is-matrix-like/lib/main.js new file mode 100644 index 00000000..b203b4ee --- /dev/null +++ b/is-matrix-like/lib/main.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); + + +// MAIN // + +/** +* Tests if a value is a 2-dimensional ndarray-like object. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a 2-dimensional ndarray-like object +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isMatrixLike( arr ); +* // returns true +* +* bool = isMatrixLike( [] ); +* // returns false +*/ +function isMatrixLike( v ) { + return ( + isndarrayLike( v ) && + v.ndims === 2 && + v.shape.length === 2 && + v.strides.length === 2 + ); +} + + +// EXPORTS // + +module.exports = isMatrixLike; diff --git a/is-matrix-like/package.json b/is-matrix-like/package.json new file mode 100644 index 00000000..849410fc --- /dev/null +++ b/is-matrix-like/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-matrix-like", + "version": "0.0.0", + "description": "Test if a value is a 2-dimensional ndarray-like object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "matrix", + "ismatrix", + "2d", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-matrix-like/test/test.js b/is-matrix-like/test/test.js new file mode 100644 index 00000000..3767f13d --- /dev/null +++ b/is-matrix-like/test/test.js @@ -0,0 +1,89 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var isMatrixLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isMatrixLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray', function test( t ) { + var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isMatrixLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray-like object', function test( t ) { + var arr; + + arr = { + 'data': [ 0, 0, 0, 0 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isMatrixLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a 2-dimensional ndarray-like object', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 0, 0, 0 ], [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + values = [ + arr, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isMatrixLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-method-in/README.md b/is-method-in/README.md new file mode 100644 index 00000000..09670094 --- /dev/null +++ b/is-method-in/README.md @@ -0,0 +1,145 @@ + + +# isMethodIn + +> Test if an object has a specified method name, either own or inherited. + +
+ +## Usage + +```javascript +var isMethodIn = require( '@stdlib/assert/is-method-in' ); +``` + +#### isMethodIn( value, property ) + +Returns a `boolean` indicating if a `value` has a specified method name, either own or inherited. + +```javascript +var value = { + 'beep': 'boop' +}; + +var bool = isMethodIn( value, 'toString' ); +// returns true + +bool = isMethodIn( value, 'beep' ); +// returns false + +bool = isMethodIn( value, 'bap' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- The function does **not** throw when provided `null` or `undefined`. Instead, the function returns `false`. + + ```javascript + var bool = isMethodIn( null, 'toString' ); + // returns false + + bool = isMethodIn( void 0, 'toString' ); + // returns false + ``` + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isMethodIn( 'beep', 'toString' ); + // returns true + ``` + +- Non-symbol property arguments are coerced to `strings`. + + ```javascript + function noop() { + // Example function... + } + + var value = { + 'null': noop + }; + var bool = isMethodIn( value, null ); + // returns true + + value = { + '[object Object]': noop + }; + bool = isMethodIn( value, {} ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isMethodIn = require( '@stdlib/assert/is-method-in' ); + +var bool = isMethodIn( {}, 'toString' ); +// returns true + +bool = isMethodIn( { 'a': 'b' }, 'a' ); +// returns false + +bool = isMethodIn( { 'a': 'b' }, 'c' ); +// returns false + +bool = isMethodIn( { 'a': 'b' }, null ); +// returns false + +bool = isMethodIn( null, 'a' ); +// returns false + +bool = isMethodIn( void 0, 'a' ); +// returns false + +bool = isMethodIn( { 'null': isMethodIn }, null ); +// returns true + +bool = isMethodIn( { '[object Object]': isMethodIn }, {} ); +// returns true +``` + +
+ + + + + + diff --git a/is-method-in/benchmark/benchmark.js b/is-method-in/benchmark/benchmark.js new file mode 100644 index 00000000..8f4fadc7 --- /dev/null +++ b/is-method-in/benchmark/benchmark.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isMethodIn = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var obj; + var key; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + key = fromCodePoint( 97+(i%26) ); + obj = {}; + obj[ key ] = i; + bool = isMethodIn( obj, key ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::built-in', function benchmark( b ) { + var bool; + var obj; + var key; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + key = fromCodePoint( 97+(i%26) ); + obj = {}; + obj[ key ] = i; + bool = ( key in obj ) && typeof obj[ key ] === 'function'; + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-method-in/docs/repl.txt b/is-method-in/docs/repl.txt new file mode 100644 index 00000000..6b543f4e --- /dev/null +++ b/is-method-in/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, property ) + Tests if an object has a specified method name, either own or inherited. + + Value arguments other than `null` or `undefined` are coerced to objects. + + Non-symbol property arguments are coerced to strings. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object has a specified method name. + + Examples + -------- + > var beep = { 'boop': true }; + > var bool = {{alias}}( beep, 'toString' ) + true + > bool = {{alias}}( beep, 'boop' ) + false + > bool = {{alias}}( beep, 'bop' ) + false + + See Also + -------- + diff --git a/is-method-in/docs/types/index.d.ts b/is-method-in/docs/types/index.d.ts new file mode 100644 index 00000000..8aad7863 --- /dev/null +++ b/is-method-in/docs/types/index.d.ts @@ -0,0 +1,49 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object has a specified method name, either own or inherited. +* +* ## Notes +* +* - Value arguments other than `null` or `undefined` are coerced to objects. +* - Non-symbol property arguments are coerced to strings. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object has a specified method name +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = isMethodIn( beep, 'toString' ); +* // returns true +* +* bool = isMethodIn( beep, 'boop' ); +* // returns false +*/ +declare function isMethodIn( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isMethodIn; diff --git a/is-method-in/docs/types/test.ts b/is-method-in/docs/types/test.ts new file mode 100644 index 00000000..5cb8983c --- /dev/null +++ b/is-method-in/docs/types/test.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isMethodIn = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const beep = { + 'boop': true + }; + isMethodIn( beep, 'boop' ); // $ExpectType boolean + isMethodIn( beep, 'toString' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isMethodIn(); // $ExpectError + isMethodIn( {} ); // $ExpectError + isMethodIn( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-method-in/examples/index.js b/is-method-in/examples/index.js new file mode 100644 index 00000000..90ce87ea --- /dev/null +++ b/is-method-in/examples/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isMethodIn = require( './../lib' ); + +var bool = isMethodIn( {}, 'toString' ); +console.log( bool ); +// => true + +bool = isMethodIn( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => false + +bool = isMethodIn( { 'a': 'b' }, 'c' ); +console.log( bool ); +// => false + +bool = isMethodIn( { 'a': 'b' }, null ); +console.log( bool ); +// => false + +bool = isMethodIn( null, 'a' ); +console.log( bool ); +// => false + +bool = isMethodIn( void 0, 'a' ); +console.log( bool ); +// => false + +bool = isMethodIn( { 'null': isMethodIn }, null ); +console.log( bool ); +// => true + +bool = isMethodIn( { '[object Object]': isMethodIn }, {} ); +console.log( bool ); +// => true diff --git a/is-method-in/lib/index.js b/is-method-in/lib/index.js new file mode 100644 index 00000000..456d88ad --- /dev/null +++ b/is-method-in/lib/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object has a specified method name, either own or inherited. +* +* @module @stdlib/assert/is-method-in +* +* @example +* var isMethodIn = require( '@stdlib/assert/is-method-in' ); +* +* var beep = { +* 'boop': true +* }; +* +* var bool = isMethodIn( beep, 'toString' ); +* // returns true +* +* bool = isMethodIn( beep, 'boop' ); +* // returns false +*/ + +// MODULES // + +var isMethodIn = require( './main.js' ); + + +// EXPORTS // + +module.exports = isMethodIn; diff --git a/is-method-in/lib/main.js b/is-method-in/lib/main.js new file mode 100644 index 00000000..923a6405 --- /dev/null +++ b/is-method-in/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); + + +// MAIN // + +/** +* Tests if an object has a specified method name, either own or inherited. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object has a specified method name +* +* @example +* var beep = { +* 'boop': true +* }; +* +* var bool = isMethodIn( beep, 'toString' ); +* // returns true +* +* bool = isMethodIn( beep, 'boop' ); +* // returns false +*/ +function isMethodIn( value, property ) { + if ( value === void 0 || value === null ) { + return false; + } + value = Object( value ); + if ( typeof property !== 'symbol' ) { + property = String( property ); + } + return ( + ( property in value ) && + isFunction( value[ property ] ) + ); +} + + +// EXPORTS // + +module.exports = isMethodIn; diff --git a/is-method-in/package.json b/is-method-in/package.json new file mode 100644 index 00000000..1355b3dd --- /dev/null +++ b/is-method-in/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-method-in", + "version": "0.0.0", + "description": "Test if an object has a specified method name, either own or inherited.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "has", + "hasown", + "hasownproperty", + "hasprop", + "hasproperty", + "method", + "inherited", + "in", + "prototype", + "proto", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-method-in/test/test.js b/is-method-in/test/test.js new file mode 100644 index 00000000..43c58803 --- /dev/null +++ b/is-method-in/test/test.js @@ -0,0 +1,118 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var isMethodIn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isMethodIn, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isMethodIn( null, 'toString' ); + t.strictEqual( bool, false, 'returns false when provided null' ); + + bool = isMethodIn( void 0, 'toString' ); + t.strictEqual( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `true` if an object has a specified method name (either own or inherited)', function test( t ) { + var bool; + + bool = isMethodIn( { 'a': noop }, 'a' ); + t.strictEqual( bool, true, 'returns true' ); + + bool = isMethodIn( [ 1, 2, 3 ], 'toString' ); + t.strictEqual( bool, true, 'returns true' ); + + bool = isMethodIn( {}, 'toString' ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object does not have a specified method name (either own or inherited)', function test( t ) { + var bool; + + bool = isMethodIn( { 'a': 'b' }, 'a' ); + t.strictEqual( bool, false, 'returns false' ); + + bool = isMethodIn( { 'a': 'b' }, 'c' ); + t.strictEqual( bool, false, 'returns false' ); + + bool = isMethodIn( { 'a': 'b' }, null ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isMethodIn( 'beep', 'toString' ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'non-symbol property values are coerced to strings', function test( t ) { + var bool; + + bool = isMethodIn( { 'null': noop }, null ); + t.strictEqual( bool, true, 'returns true' ); + + bool = isMethodIn( { '[object Object]': noop }, {} ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function supports symbols', function test( t ) { + var bool; + var obj; + var s; + + if ( hasSymbolSupport() ) { + obj = {}; + s = Symbol( 'foo' ); + obj[ s ] = noop; + bool = isMethodIn( obj, s ); + t.strictEqual( bool, true, 'returns true' ); + } else { + t.pass( 'environment does not support symbols' ); + } + t.end(); +}); diff --git a/is-method/README.md b/is-method/README.md new file mode 100644 index 00000000..853dabbd --- /dev/null +++ b/is-method/README.md @@ -0,0 +1,140 @@ + + +# isMethod + +> Test if an object has a specified method name. + +
+ +## Usage + +```javascript +var isMethod = require( '@stdlib/assert/is-method' ); +``` + +#### isMethod( value, property ) + +Returns a `boolean` indicating if a `value` has a specified [**own**][@stdlib/assert/has-own-property] method name. + +```javascript +function noop() { + // Example function... +} + +var value = { + 'beep': noop +}; + +var bool = isMethod( value, 'beep' ); +// returns true + +bool = isMethod( value, 'toString' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isMethod( 'beep', 'toString' ); + // returns false + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + function noop() { + // Example function... + } + + var value = { + 'null': noop + }; + var bool = isMethod( value, null ); + // returns true + + value = { + '[object Object]': noop + }; + bool = isMethod( value, {} ); + // returns true + ``` + +- The function searches only [**own**][@stdlib/assert/has-own-property] properties. + +
+ + + +
+ +## Examples + + + + + +```javascript +var isMethod = require( '@stdlib/assert/is-method' ); + +var bool = isMethod( { 'a': isMethod }, 'a' ); +// returns true + +bool = isMethod( { 'a': 'b' }, 'a' ); +// returns false + +bool = isMethod( { 'a': 'b' }, null ); +// returns false + +bool = isMethod( {}, 'toString' ); +// returns false + +bool = isMethod( null, 'a' ); +// returns false + +bool = isMethod( void 0, 'a' ); +// returns false + +bool = isMethod( { 'null': isMethod }, null ); +// returns true + +bool = isMethod( { '[object Object]': isMethod }, {} ); +// returns true +``` + +
+ + + + + + diff --git a/is-method/benchmark/benchmark.js b/is-method/benchmark/benchmark.js new file mode 100644 index 00000000..59e44818 --- /dev/null +++ b/is-method/benchmark/benchmark.js @@ -0,0 +1,80 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-prototype-builtins */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isMethod = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var obj; + var key; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + key = fromCodePoint( 97+(i%26) ); + obj = {}; + obj[ key ] = i; + bool = isMethod( obj, key ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::built-in', function benchmark( b ) { + var bool; + var obj; + var key; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + key = fromCodePoint( 97+(i%26) ); + obj = {}; + obj[ key ] = i; + bool = obj.hasOwnProperty( key ) && typeof obj[ key ] === 'function'; + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-method/docs/repl.txt b/is-method/docs/repl.txt new file mode 100644 index 00000000..ffb1eba9 --- /dev/null +++ b/is-method/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, property ) + Tests if an object has a specified method name. + + Value arguments other than `null` or `undefined` are coerced to objects. + + Property arguments are coerced to strings. + + The function only searches own properties. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object has a specified method name. + + Examples + -------- + > var beep = { 'boop': function beep() { return 'beep'; } }; + > var bool = {{alias}}( beep, 'boop' ) + true + > bool = {{alias}}( beep, 'toString' ) + false + + See Also + -------- + diff --git a/is-method/docs/types/index.d.ts b/is-method/docs/types/index.d.ts new file mode 100644 index 00000000..b8b63cc0 --- /dev/null +++ b/is-method/docs/types/index.d.ts @@ -0,0 +1,50 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object has a specified method name. +* +* ## Notes +* +* - Value arguments other than `null` or `undefined` are coerced to objects. +* - Property arguments are coerced to strings. +* - The function only searches own properties. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object has a specified method name +* +* @example +* var beep = { +* 'boop': isMethod +* }; +* +* var bool = isMethod( beep, 'boop' ); +* // returns true +* +* var bool = isMethod( beep, 'toString' ); +* // returns false +*/ +declare function isMethod( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isMethod; diff --git a/is-method/docs/types/test.ts b/is-method/docs/types/test.ts new file mode 100644 index 00000000..1b29424c --- /dev/null +++ b/is-method/docs/types/test.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isMethod = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const beep = { + 'boop': isMethod + }; + isMethod( beep, 'boop' ); // $ExpectType boolean + isMethod( beep, 'toString' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isMethod(); // $ExpectError + isMethod( {} ); // $ExpectError + isMethod( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-method/examples/index.js b/is-method/examples/index.js new file mode 100644 index 00000000..d551b295 --- /dev/null +++ b/is-method/examples/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isMethod = require( './../lib' ); + +var bool = isMethod( { 'a': isMethod }, 'a' ); +console.log( bool ); +// => true + +bool = isMethod( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => false + +bool = isMethod( { 'a': 'b' }, null ); +console.log( bool ); +// => false + +bool = isMethod( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isMethod( null, 'a' ); +console.log( bool ); +// => false + +bool = isMethod( void 0, 'a' ); +console.log( bool ); +// => false + +bool = isMethod( { 'null': isMethod }, null ); +console.log( bool ); +// => true + +bool = isMethod( { '[object Object]': isMethod }, {} ); +console.log( bool ); +// => true diff --git a/is-method/lib/index.js b/is-method/lib/index.js new file mode 100644 index 00000000..c46946c4 --- /dev/null +++ b/is-method/lib/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object has a specified method name. +* +* @module @stdlib/assert/is-method +* +* @example +* var isMethod = require( '@stdlib/assert/is-method' ); +* +* var beep = { +* 'boop': isMethod +* }; +* +* var bool = isMethod( beep, 'boop' ); +* // returns true +* +* bool = isMethod( beep, 'toString' ); +* // returns false +*/ + +// MODULES // + +var isMethod = require( './main.js' ); + + +// EXPORTS // + +module.exports = isMethod; diff --git a/is-method/lib/main.js b/is-method/lib/main.js new file mode 100644 index 00000000..aca8a2e9 --- /dev/null +++ b/is-method/lib/main.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isFunction = require( '@stdlib/assert/is-function' ); + + +// MAIN // + +/** +* Tests if an object has a specified method name. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object has a specified method name +* +* @example +* var beep = { +* 'boop': isMethod +* }; +* +* var bool = isMethod( beep, 'boop' ); +* // returns true +* +* var bool = isMethod( beep, 'toString' ); +* // returns false +*/ +function isMethod( value, property ) { + return ( + hasOwnProp( value, property ) && + isFunction( value[ property ] ) + ); +} + + +// EXPORTS // + +module.exports = isMethod; diff --git a/is-method/package.json b/is-method/package.json new file mode 100644 index 00000000..f1467e4d --- /dev/null +++ b/is-method/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-method", + "version": "0.0.0", + "description": "Test if an object has a specified method name.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "has", + "hasown", + "hasownproperty", + "property", + "method", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-method/test/test.js b/is-method/test/test.js new file mode 100644 index 00000000..48407305 --- /dev/null +++ b/is-method/test/test.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var noop = require( '@stdlib/utils/noop' ); +var isMethod = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isMethod, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument (native throws)', function test( t ) { + var bool; + + bool = isMethod( null, 'toString' ); + t.strictEqual( bool, false, 'returns false when provided null' ); + + bool = isMethod( void 0, 'toString' ); + t.strictEqual( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `true` if an object has a specified method name', function test( t ) { + var bool; + + bool = isMethod( { 'a': noop }, 'a' ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object does not have a specified own method name', function test( t ) { + var bool; + + bool = isMethod( { 'a': 'b' }, 'a' ); + t.strictEqual( bool, false, 'returns false' ); + + bool = isMethod( { 'a': 'b' }, null ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a prototype property', function test( t ) { + var bool; + + bool = isMethod( {}, 'toString' ); + t.strictEqual( bool, false, 'returns false when provided a prototype property' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isMethod( 'beep', 'toString' ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'property values are coerced to strings', function test( t ) { + var bool; + + bool = isMethod( { 'null': noop }, null ); + t.strictEqual( bool, true, 'returns true' ); + + bool = isMethod( { '[object Object]': noop }, {} ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/is-named-typed-tuple-like/README.md b/is-named-typed-tuple-like/README.md new file mode 100644 index 00000000..1ad4a5e7 --- /dev/null +++ b/is-named-typed-tuple-like/README.md @@ -0,0 +1,87 @@ + + +# isNamedTypedTupleLike + +> Test if a value is [named typed tuple][@stdlib/utils/named-typed-tuple]-like. + +
+ +## Usage + +```javascript +var isNamedTypedTupleLike = require( '@stdlib/assert/is-named-typed-tuple-like' ); +``` + +#### isNamedTypedTupleLike( value ) + +Tests if a value is [named typed tuple][@stdlib/utils/named-typed-tuple]-like. + +```javascript +var namedtypedtuple = require( '@stdlib/utils/named-typed-tuple' ); + +var Point = namedtypedtuple( [ 'x', 'y' ] ); +var p = new Point(); + +var bool = isNamedTypedTupleLike( p ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var namedtypedtuple = require( '@stdlib/utils/named-typed-tuple' ); +var isNamedTypedTupleLike = require( '@stdlib/assert/is-named-typed-tuple-like' ); + +var Point = namedtypedtuple( [ 'x', 'y' ] ); +var p = new Point(); + +var bool = isNamedTypedTupleLike( p ); +// returns true + +bool = isNamedTypedTupleLike( [ 1, 2, 3, 4 ] ); +// returns false + +bool = isNamedTypedTupleLike( {} ); +// returns false + +bool = isNamedTypedTupleLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-named-typed-tuple-like/benchmark/benchmark.js b/is-named-typed-tuple-like/benchmark/benchmark.js new file mode 100644 index 00000000..12af8689 --- /dev/null +++ b/is-named-typed-tuple-like/benchmark/benchmark.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var namedtypedtuple = require( '@stdlib/utils/named-typed-tuple' ); +var pkg = require( './../package.json' ).name; +var isNamedTypedTupleLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var Point; + var bool; + var p; + var i; + + Point = namedtypedtuple( [ 'x', 'y' ] ); + p = new Point(); + + values = [ + p, + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNamedTypedTupleLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-named-typed-tuple-like/docs/repl.txt b/is-named-typed-tuple-like/docs/repl.txt new file mode 100644 index 00000000..5886b9e0 --- /dev/null +++ b/is-named-typed-tuple-like/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( value ) + Tests if a value is named typed tuple-like. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is named typed tuple-like. + + Examples + -------- + > var Point = {{alias:@stdlib/utils/named-typed-tuple}}( [ 'x', 'y' ] ); + > var p = new Point(); + > var bool = {{alias}}( p ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-named-typed-tuple-like/docs/types/index.d.ts b/is-named-typed-tuple-like/docs/types/index.d.ts new file mode 100644 index 00000000..47dd4cda --- /dev/null +++ b/is-named-typed-tuple-like/docs/types/index.d.ts @@ -0,0 +1,45 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is named typed tuple-like. +* +* @param v - value to test +* @returns boolean indicating if a value is named typed tuple-like +* +* @example +* var namedtypedtuple = require( `@stdlib/utils/named-typed-tuple` ); +* +* var Point = namedtypedtuple( [ 'x', 'y' ] ); +* +* var p = new Point(); +* +* var bool = isNamedTypedTupleLike( p ); +* // returns true +* +* bool = isNamedTypedTupleLike( [] ); +* // returns false +*/ +declare function isNamedTypedTupleLike( v: any ): boolean; + + +// EXPORTS // + +export = isNamedTypedTupleLike; diff --git a/is-named-typed-tuple-like/docs/types/test.ts b/is-named-typed-tuple-like/docs/types/test.ts new file mode 100644 index 00000000..db436a6b --- /dev/null +++ b/is-named-typed-tuple-like/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNamedTypedTupleLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNamedTypedTupleLike( {} ); // $ExpectType boolean + isNamedTypedTupleLike( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNamedTypedTupleLike(); // $ExpectError + isNamedTypedTupleLike( {}, 123 ); // $ExpectError +} diff --git a/is-named-typed-tuple-like/examples/index.js b/is-named-typed-tuple-like/examples/index.js new file mode 100644 index 00000000..0de14254 --- /dev/null +++ b/is-named-typed-tuple-like/examples/index.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var namedtypedtuple = require( '@stdlib/utils/named-typed-tuple' ); +var isNamedTypedTupleLike = require( './../lib' ); + +var Point = namedtypedtuple( [ 'x', 'y' ] ); +var p = new Point(); + +console.log( isNamedTypedTupleLike( p ) ); +// => true + +console.log( isNamedTypedTupleLike( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isNamedTypedTupleLike( {} ) ); +// => false + +console.log( isNamedTypedTupleLike( null ) ); +// => false diff --git a/is-named-typed-tuple-like/lib/index.js b/is-named-typed-tuple-like/lib/index.js new file mode 100644 index 00000000..f2023556 --- /dev/null +++ b/is-named-typed-tuple-like/lib/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is named typed tuple-like. +* +* @module @stdlib/assert/is-named-typed-tuple-like +* +* @example +* var namedtypedtuple = require( '@stdlib/utils/named-typed-tuple' ); +* var isNamedTypedTupleLike = require( '@stdlib/assert/is-named-typed-tuple-like' ); +* +* var Point = namedtypedtuple( [ 'x', 'y' ] ); +* +* var p = new Point(); +* +* var bool = isNamedTypedTupleLike( p ); +* // returns true +* +* bool = isNamedTypedTupleLike( [] ); +* // returns false +*/ + +// MODULES // + +var isNamedTypedTupleLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNamedTypedTupleLike; diff --git a/is-named-typed-tuple-like/lib/main.js b/is-named-typed-tuple-like/lib/main.js new file mode 100644 index 00000000..ee27dcc4 --- /dev/null +++ b/is-named-typed-tuple-like/lib/main.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isTypedArray = require( '@stdlib/assert/is-typed-array' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); + + +// MAIN // + +/** +* Tests if a value is named typed tuple-like. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is named typed tuple-like +* +* @example +* var namedtypedtuple = require( '@stdlib/utils/named-typed-tuple' ); +* +* var Point = namedtypedtuple( [ 'x', 'y' ] ); +* +* var p = new Point(); +* +* var bool = isNamedTypedTupleLike( p ); +* // returns true +* +* bool = isNamedTypedTupleLike( [] ); +* // returns false +*/ +function isNamedTypedTupleLike( v ) { + return ( + isTypedArray( v ) && + typeof v.fieldOf === 'function' && + typeof v.findField === 'function' && + typeof v.ind2key === 'function' && + typeof v.key2ind === 'function' && + typeof v.lastFieldOf === 'function' && + typeof v.subtuple === 'function' && + hasOwnProp( v, 'fields' ) && + hasOwnProp( v, 'orderedFields' ) + ); +} + + +// EXPORTS // + +module.exports = isNamedTypedTupleLike; diff --git a/is-named-typed-tuple-like/package.json b/is-named-typed-tuple-like/package.json new file mode 100644 index 00000000..6b64441d --- /dev/null +++ b/is-named-typed-tuple-like/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-named-typed-tuple-like", + "version": "0.0.0", + "description": "Test if a value is named typed tuple-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "istuple", + "istuplelike", + "typed", + "tuple", + "tuple-like", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-named-typed-tuple-like/test/test.js b/is-named-typed-tuple-like/test/test.js new file mode 100644 index 00000000..69e0a790 --- /dev/null +++ b/is-named-typed-tuple-like/test/test.js @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var namedtypedtuple = require( '@stdlib/utils/named-typed-tuple' ); +var Float64Array = require( '@stdlib/array/float64' ); +var noop = require( '@stdlib/utils/noop' ); +var isNamedTypedTuple = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNamedTypedTuple, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a named typed tuple', function test( t ) { + var Point; + var p; + + Point = namedtypedtuple( [ 'x', 'y' ] ); + p = new Point(); + + t.equal( isNamedTypedTuple( p ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a named typed tuple-like object', function test( t ) { + var tuple; + + tuple = new Float64Array( [ 1.0, 2.0 ] ); + tuple.fieldOf = noop; + tuple.findField = noop; + tuple.ind2key = noop; + tuple.key2ind = noop; + tuple.lastFieldOf = noop; + tuple.subtuple = noop; + tuple.fields = [ 'x', 'y' ]; + tuple.orderedFields = [ 'x', 'y' ]; + + t.equal( isNamedTypedTuple( tuple ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a named typed tuple-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNamedTypedTuple( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nan-array/README.md b/is-nan-array/README.md new file mode 100644 index 00000000..bf4a1f23 --- /dev/null +++ b/is-nan-array/README.md @@ -0,0 +1,127 @@ + + +# isNaNArray + +> Test if a value is an array-like object containing only NaN values. + +
+ +## Usage + +```javascript +var isNaNArray = require( '@stdlib/assert/is-nan-array' ); +``` + +#### isNaNArray( value ) + +Tests if a `value` is an array-like object containing **only** `NaN` values. + +```javascript +var bool = isNaNArray( [ NaN, NaN, NaN ] ); +// returns true + +bool = isNaNArray( [ NaN, 2 ] ); +// returns false +``` + +#### isNaNArray.primitives( value ) + +Tests if a `value` is an array-like object containing **only** primitive `NaN` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNaNArray.primitives( [ NaN, NaN, NaN ] ); +// returns true + +bool = isNaNArray.primitives( [ NaN, new Number( NaN ) ] ); +// returns false +``` + +#### isNaNArray.objects( value ) + +Tests if a `value` is an array-like object containing **only** object `NaN` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNaNArray.objects( [ new Number( NaN ), new Number( NaN ) ] ); +// returns true + +bool = isNaNArray.objects( [ NaN, new Number( NaN ) ] ); +// returns false + +bool = isNaNArray.objects( [ NaN, NaN, NaN ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isNaNArray = require( '@stdlib/assert/is-nan-array' ); + +var bool = isNaNArray( [ NaN ] ); +// returns true + +bool = isNaNArray( [ NaN, NaN, NaN ] ); +// returns true + +bool = isNaNArray( [ new Number( NaN ), NaN, NaN ] ); +// returns true + +bool = isNaNArray( new Float64Array( [ NaN, NaN ] ) ); +// returns true + +bool = isNaNArray( NaN ); +// returns false + +bool = isNaNArray( [ 'a', 'b', 'c' ] ); +// returns false + +bool = isNaNArray( [ 'a', NaN ] ); +// returns false +``` + +
+ + + + + + diff --git a/is-nan-array/benchmark/benchmark.js b/is-nan-array/benchmark/benchmark.js new file mode 100644 index 00000000..819abf89 --- /dev/null +++ b/is-nan-array/benchmark/benchmark.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isNaNArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( NaN ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( NaN ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isNaNArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isNaNArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isNaNArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-nan-array/docs/repl.txt b/is-nan-array/docs/repl.txt new file mode 100644 index 00000000..be2bd39f --- /dev/null +++ b/is-nan-array/docs/repl.txt @@ -0,0 +1,73 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only NaN values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + NaN values. + + Examples + -------- + > var bool = {{alias}}( [ NaN, NaN, NaN ] ) + true + > bool = {{alias}}( [ NaN, 2 ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only primitive NaN + values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + primitive NaN values. + + Examples + -------- + > var bool = {{alias}}.primitives( [ NaN, new Number( NaN ) ] ) + false + > bool = {{alias}}.primitives( [ NaN, NaN, NaN ] ) + true + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only number objects + having NaN values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + number objects having NaN values. + + Examples + -------- + > var bool = {{alias}}.objects( [ new Number( NaN ), new Number( NaN ) ] ) + true + > bool = {{alias}}.objects( [ NaN, new Number( NaN ), new Number( NaN ) ] ) + false + > bool = {{alias}}.objects( [ NaN, NaN, NaN ] ) + false + + See Also + -------- + diff --git a/is-nan-array/docs/types/index.d.ts b/is-nan-array/docs/types/index.d.ts new file mode 100644 index 00000000..8f1c88d7 --- /dev/null +++ b/is-nan-array/docs/types/index.d.ts @@ -0,0 +1,104 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNaNArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsNaNArray { + /** + * Tests if a value is an array-like object containing only NaN values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only NaN values + * + * @example + * var bool = isNaNArray( [NaN,NaN,NaN] ); + * // returns true + * + * @example + * var bool = isNaNArray( [NaN,2] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only primitive NaN values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only primitive NaN values + * + * @example + * var bool = isNaNArray.primitives( [NaN,new Number( NaN )] ); + * // returns false + * + * @example + * var bool = isNaNArray.primitives( [NaN,NaN,NaN] ); + * // returns true + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects having NaN values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number objects having NaN values + * + * @example + * var bool = isNaNArray.objects( [new Number( NaN ),new Number( NaN )] ); + * // returns true + * + * @example + * var bool = isNaNArray.objects( [NaN,new Number( NaN ),new Number( NaN )] ); + * // returns false + * + * @example + * var bool = isNaNArray.objects( [NaN,NaN,NaN] ); + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object containing only NaN values. +* +* @param value - value to test +* @returns boolean indicating whether value is an array-like object containing only NaN values +* +* @example +* var bool = isNaNArray( [NaN,NaN,NaN] ); +* // returns true +* +* @example +* var bool = isNaNArray( [NaN,2] ); +* // returns false +* +* @example +* var bool = isNaNArray.primitives( [NaN,NaN,NaN] ); +* // returns true +* +* @example +* var bool = isNaNArray.objects( [new Number( NaN ),new Number( NaN )] ); +* // returns true +*/ +declare var isNumberArray: IsNaNArray; + + +// EXPORTS // + +export = isNumberArray; diff --git a/is-nan-array/docs/types/test.ts b/is-nan-array/docs/types/test.ts new file mode 100644 index 00000000..5586a671 --- /dev/null +++ b/is-nan-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNaNArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNaNArray( [ NaN, NaN, NaN ] ); // $ExpectType boolean + isNaNArray( [ NaN, null, NaN ] ); // $ExpectType boolean + isNaNArray( [ '2.8', NaN, NaN ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNaNArray(); // $ExpectError + isNaNArray( [ -3 ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNaNArray.primitives( [ new Number( NaN ) ] ); // $ExpectType boolean + isNaNArray.primitives( [ NaN ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isNaNArray.primitives(); // $ExpectError + isNaNArray.primitives( [ NaN, NaN ], NaN ); // $ExpectError +} + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNaNArray.objects( [ new Number( NaN ) ] ); // $ExpectType boolean + isNaNArray.objects( [ NaN ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isNaNArray.objects(); // $ExpectError + isNaNArray.objects( [ NaN ], 123 ); // $ExpectError +} diff --git a/is-nan-array/examples/index.js b/is-nan-array/examples/index.js new file mode 100644 index 00000000..a8fa8c61 --- /dev/null +++ b/is-nan-array/examples/index.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Float64Array = require( '@stdlib/array/float64' ); +var Number = require( '@stdlib/number/ctor' ); +var isNaNArray = require( './../lib' ); + +var bool = isNaNArray( [ NaN ] ); +console.log( bool ); +// => true + +bool = isNaNArray( [ NaN, NaN, NaN ] ); +console.log( bool ); +// => true + +bool = isNaNArray( [ new Number( NaN ), NaN, NaN ] ); +console.log( bool ); +// => true + +bool = isNaNArray( new Float64Array( [ NaN, NaN ] ) ); +console.log( bool ); +// => true + +bool = isNaNArray( NaN ); +console.log( bool ); +// => false + +bool = isNaNArray( [ 'a', 'b', 'c' ] ); +console.log( bool ); +// => false + +bool = isNaNArray( [ 'a', NaN ] ); +console.log( bool ); +// => false diff --git a/is-nan-array/lib/index.js b/is-nan-array/lib/index.js new file mode 100644 index 00000000..45e40b7f --- /dev/null +++ b/is-nan-array/lib/index.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only `NaN` values. +* +* @module @stdlib/assert/is-nan-array +* +* @example +* var isNaNArray = require( '@stdlib/assert/is-nan-array' ); +* +* var bool = isNaNArray( [NaN,NaN,NaN] ); +* // returns true +* +* bool = isNaNArray( [NaN,2] ); +* // returns false +* +* bool = isNaNArray.primitives( [NaN,new Number( NaN )] ); +* // returns false +* +* bool = isNaNArray.primitives( [NaN,NaN,NaN] ); +* // returns true +* +* bool = isNaNArray.objects( [new Number( NaN ),new Number( NaN )] ); +* // returns true +* +* bool = isNaNArray.objects( [NaN,new Number( NaN ),new Number( NaN )] ); +* // returns false +* +* bool = isNaNArray.objects( [NaN,NaN,NaN] ); +* // returns false +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); +var isnan = require( '@stdlib/assert/is-nan' ); + + +// MAIN // + +var isNaNArray = arrayfun( isnan ); +setReadOnly( isNaNArray, 'primitives', arrayfun( isnan.isPrimitive ) ); +setReadOnly( isNaNArray, 'objects', arrayfun( isnan.isObject ) ); + + +// EXPORTS // + +module.exports = isNaNArray; diff --git a/is-nan-array/package.json b/is-nan-array/package.json new file mode 100644 index 00000000..c2c621a9 --- /dev/null +++ b/is-nan-array/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-nan-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only NaN values.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "non-numeric", + "nan", + "isnan", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-nan-array/test/test.js b/is-nan-array/test/test.js new file mode 100644 index 00000000..7adbe789 --- /dev/null +++ b/is-nan-array/test/test.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Number = require( '@stdlib/number/ctor' ); +var isNaNArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNaNArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only `NaN` values', function test( t ) { + var arr; + + arr = [ NaN, new Number( NaN ), NaN ]; + t.equal( isNaNArray( arr ), true, 'returns true' ); + + arr = new Float64Array( [ NaN, NaN ] ); + t.equal( isNaNArray( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': NaN, + '1': NaN + }; + t.equal( isNaNArray( arr ), true, 'returns true' ); + + arr = [ NaN, 3, NaN ]; + t.equal( isNaNArray( arr ), false, 'returns false' ); + + arr = [ NaN, null, NaN ]; + t.equal( isNaNArray( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'attached to the main export is a method to test for an array-like object containing only primitive `NaN` values', function test( t ) { + var arr; + + arr = [ NaN, NaN, NaN ]; + t.equal( isNaNArray.primitives( arr ), true, 'returns true' ); + + arr = new Float64Array( [ NaN, NaN ] ); + t.equal( isNaNArray.primitives( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': NaN, + '1': NaN + }; + t.equal( isNaNArray.primitives( arr ), true, 'returns true' ); + + arr = [ new Number( NaN ), NaN, NaN ]; + t.equal( isNaNArray.primitives( arr ), false, 'returns false' ); + + arr = new Float64Array( [ 2.3, NaN ] ); + t.equal( isNaNArray.primitives( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'attached to the main export is a method to test for an array-like object containing only object `NaN` values', function test( t ) { + var arr; + + arr = [ NaN, NaN, NaN ]; + t.equal( isNaNArray.objects( arr ), false, 'returns false' ); + + arr = [ new Number( NaN ), NaN, NaN ]; + t.equal( isNaNArray.objects( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': new Number( NaN ), + '1': new Number( NaN ) + }; + t.equal( isNaNArray.objects( arr ), true, 'returns true' ); + + arr = [ new Number( NaN ), new Number( NaN ), new Number( NaN ) ]; + t.equal( isNaNArray.objects( arr ), true, 'returns true' ); + + arr = new Float64Array( [ NaN, NaN, NaN ] ); + t.equal( isNaNArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-nan/README.md b/is-nan/README.md new file mode 100644 index 00000000..a852f9c2 --- /dev/null +++ b/is-nan/README.md @@ -0,0 +1,134 @@ + + +# isNaN + +> Test if a value is NaN. + +
+ +## Usage + +```javascript +var isnan = require( '@stdlib/assert/is-nan' ); +``` + +#### isnan( value ) + +Tests if a `value` is `NaN`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isnan( NaN ); +// returns true + +bool = isnan( new Number( NaN ) ); +// returns true + +bool = isnan( 3.14 ); +// returns false + +bool = isnan( null ); +// returns false +``` + +#### isnan.isPrimitive( value ) + +Tests if a `value` is a `NaN` primitive `number`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isnan.isPrimitive( NaN ); +// returns true + +bool = isnan.isPrimitive( 3.14 ); +// returns false + +bool = isnan.isPrimitive( new Number( NaN ) ); +// returns false +``` + +#### isnan.isObject( value ) + +Tests if a `value` is a `Number` object having a value of `NaN`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isnan.isObject( NaN ); +// returns false + +bool = isnan.isObject( new Number( NaN ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var isnan = require( '@stdlib/assert/is-nan' ); + +var bool = isnan( NaN ); +// returns true + +bool = isnan( new Number( NaN ) ); +// returns true + +bool = isnan( 5 ); +// returns false + +bool = isnan( '5' ); +// returns false + +bool = isnan( null ); +// returns false + +bool = isnan( Symbol( 'NaN' ) ); +// returns false +``` + +
+ + + + + + diff --git a/is-nan/benchmark/benchmark.js b/is-nan/benchmark/benchmark.js new file mode 100644 index 00000000..3b5c40d9 --- /dev/null +++ b/is-nan/benchmark/benchmark.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isnan = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isnan( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isnan( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isnan.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isnan.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isnan.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isnan.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-nan/docs/repl.txt b/is-nan/docs/repl.txt new file mode 100644 index 00000000..ba801ba5 --- /dev/null +++ b/is-nan/docs/repl.txt @@ -0,0 +1,73 @@ + +{{alias}}( value ) + Tests if a value is NaN. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is NaN. + + Examples + -------- + > var bool = {{alias}}( NaN ) + true + > bool = {{alias}}( new Number( NaN ) ) + true + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a NaN number primitive. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a NaN number primitive. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( NaN ) + true + > bool = {{alias}}.isPrimitive( 3.14 ) + false + > bool = {{alias}}.isPrimitive( new Number( NaN ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a value of NaN. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a value of + NaN. + + Examples + -------- + > var bool = {{alias}}.isObject( NaN ) + false + > bool = {{alias}}.isObject( new Number( NaN ) ) + true + + See Also + -------- + diff --git a/is-nan/docs/types/index.d.ts b/is-nan/docs/types/index.d.ts new file mode 100644 index 00000000..541f8ca3 --- /dev/null +++ b/is-nan/docs/types/index.d.ts @@ -0,0 +1,121 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isnan` with methods for testing for primitives and objects, respectively. +*/ +interface IsNaN { + /** + * Tests if a value is `NaN`. + * + * @param value - value to test + * @returns boolean indicating whether value is `NaN` + * + * @example + * var bool = isnan( NaN ); + * // returns true + * + * @example + * var bool = isnan( new Number( NaN ) ); + * // returns true + * + * @example + * var bool = isnan( 3.14 ); + * // returns false + * + * @example + * var bool = isnan( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a `NaN` number primitive. + * + * @param value - value to test + * @returns boolean indicating if a value is a `NaN` number primitive + * + * @example + * var bool = isnan.isPrimitive( NaN ); + * // returns true + * + * @example + * var bool = isnan.isPrimitive( 3.14 ); + * // returns false + * + * @example + * var bool = isnan.isPrimitive( new Number( NaN ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a value of `NaN`. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a value of `NaN` + * + * @example + * var bool = isnan.isObject( NaN ); + * // returns false + * + * @example + * var bool = isnan.isObject( new Number( NaN ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is `NaN`. +* +* @param value - value to test +* @returns boolean indicating whether value is `NaN` +* +* @example +* var bool = isnan( NaN ); +* // returns true +* +* @example +* var bool = isnan( new Number( NaN ) ); +* // returns true +* +* @example +* var bool = isnan( 3.14 ); +* // returns false +* +* @example +* var bool = isnan( null ); +* // returns false +* +* @example +* var bool = isnan.isPrimitive( NaN ); +* // returns true +* +* @example +* var bool = isnan.isObject( new Number( NaN ) ); +* // returns true +*/ +declare var isnan: IsNaN; + + +// EXPORTS // + +export = isnan; diff --git a/is-nan/docs/types/test.ts b/is-nan/docs/types/test.ts new file mode 100644 index 00000000..3b2b6bb5 --- /dev/null +++ b/is-nan/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isnan = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isnan( 3 ); // $ExpectType boolean + isnan( NaN ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isnan(); // $ExpectError + isnan( NaN, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isnan.isPrimitive( new Number( NaN ) ); // $ExpectType boolean + isnan.isPrimitive( NaN ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isnan.isPrimitive(); // $ExpectError + isnan.isPrimitive( NaN, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isnan.isObject( new Number( NaN ) ); // $ExpectType boolean + isnan.isObject( NaN ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isnan.isObject(); // $ExpectError + isnan.isObject( NaN, 123 ); // $ExpectError +} diff --git a/is-nan/examples/index.js b/is-nan/examples/index.js new file mode 100644 index 00000000..f667b925 --- /dev/null +++ b/is-nan/examples/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var Number = require( '@stdlib/number/ctor' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var isnan = require( './../lib' ); + +console.log( isnan( NaN ) ); +// => true + +console.log( isnan( new Number( NaN ) ) ); +// => true + +console.log( isnan( 5 ) ); +// => false + +console.log( isnan( '5' ) ); +// => false + +console.log( isnan( null ) ); +// => false + +if ( hasSymbolSupport() ) { + console.log( isnan( Symbol( 'NaN' ) ) ); + // => false +} diff --git a/is-nan/lib/index.js b/is-nan/lib/index.js new file mode 100644 index 00000000..204d926c --- /dev/null +++ b/is-nan/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is `NaN`. +* +* @module @stdlib/assert/is-nan +* +* @example +* var isnan = require( '@stdlib/assert/is-nan' ); +* +* var bool = isnan( NaN ); +* // returns true +* +* bool = isnan( new Number( NaN ) ); +* // returns true +* +* bool = isnan( 3.14 ); +* // returns false +* +* bool = isnan( null ); +* // returns false +* +* @example +* var isnan = require( '@stdlib/assert/is-nan' ).isPrimitive; +* +* var bool = isnan( NaN ); +* // returns true +* +* bool = isnan( 3.14 ); +* // returns false +* +* bool = isnan( new Number( NaN ) ); +* // returns false +* +* @example +* var isnan = require( '@stdlib/assert/is-nan' ).isObject; +* +* var bool = isnan( NaN ); +* // returns false +* +* bool = isnan( new Number( NaN ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isnan = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isnan, 'isPrimitive', isPrimitive ); +setReadOnly( isnan, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isnan; diff --git a/is-nan/lib/main.js b/is-nan/lib/main.js new file mode 100644 index 00000000..66331038 --- /dev/null +++ b/is-nan/lib/main.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is `NaN`. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is `NaN` +* +* @example +* var bool = isnan( NaN ); +* // returns true +* +* @example +* var bool = isnan( new Number( NaN ) ); +* // returns true +* +* @example +* var bool = isnan( 3.14 ); +* // returns false +* +* @example +* var bool = isnan( null ); +* // returns false +*/ +function isnan( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isnan; diff --git a/is-nan/lib/object.js b/is-nan/lib/object.js new file mode 100644 index 00000000..4a1dd5e3 --- /dev/null +++ b/is-nan/lib/object.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; +var isNan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Tests if a value is a number object having a value of `NaN`. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a value of `NaN` +* +* @example +* var bool = isnan( NaN ); +* // returns false +* +* @example +* var bool = isnan( new Number( NaN ) ); +* // returns true +*/ +function isnan( value ) { + return ( + isNumber( value ) && + isNan( value.valueOf() ) + ); +} + + +// EXPORTS // + +module.exports = isnan; diff --git a/is-nan/lib/primitive.js b/is-nan/lib/primitive.js new file mode 100644 index 00000000..e2505bec --- /dev/null +++ b/is-nan/lib/primitive.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isNan = require( '@stdlib/math/base/assert/is-nan' ); + + +// MAIN // + +/** +* Tests if a value is a `NaN` number primitive. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a `NaN` number primitive +* +* @example +* var bool = isnan( NaN ); +* // returns true +* +* @example +* var bool = isnan( 3.14 ); +* // returns false +* +* @example +* var bool = isnan( new Number( NaN ) ); +* // returns false +*/ +function isnan( value ) { + return ( + isNumber( value ) && + isNan( value ) + ); +} + + +// EXPORTS // + +module.exports = isnan; diff --git a/is-nan/package.json b/is-nan/package.json new file mode 100644 index 00000000..f58a0106 --- /dev/null +++ b/is-nan/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-nan", + "version": "0.0.0", + "description": "Test if a value is NaN.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "non-numeric", + "nan", + "is", + "isnan", + "type", + "check", + "validate", + "valid", + "test" + ] +} diff --git a/is-nan/test/test.js b/is-nan/test/test.js new file mode 100644 index 00000000..83a025ea --- /dev/null +++ b/is-nan/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isnan = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isnan, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a `NaN` primitive number', function test( t ) { + t.equal( typeof isnan.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a value of `NaN`', function test( t ) { + t.equal( typeof isnan.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-nan/test/test.main.js b/is-nan/test/test.main.js new file mode 100644 index 00000000..42b1b5cc --- /dev/null +++ b/is-nan/test/test.main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isnan = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isnan, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided `NaN`', function test( t ) { + t.equal( isnan( NaN ), true, 'returns true' ); + t.equal( isnan( new Number( NaN ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided `NaN`', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + 3.14, + -1.0, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isnan( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nan/test/test.object.js b/is-nan/test/test.object.js new file mode 100644 index 00000000..75a707fe --- /dev/null +++ b/is-nan/test/test.object.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var Number = require( '@stdlib/number/ctor' ); +var isnan = require( './../lib/object.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isnan, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a value of `NaN`', function test( t ) { + t.equal( isnan( new Number( NaN ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number', function test( t ) { + t.equal( isnan( 3.14 ), false, 'returns false' ); + t.equal( isnan( NaN ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided `NaN`', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isnan( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` when provided a symbol (symbols are unique)', opts, function test( t ) { + var s = Symbol( 'foo' ); + t.equal( isnan( s ), false, 'returns false' ); + t.end(); +}); diff --git a/is-nan/test/test.primitive.js b/is-nan/test/test.primitive.js new file mode 100644 index 00000000..4a496da9 --- /dev/null +++ b/is-nan/test/test.primitive.js @@ -0,0 +1,83 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var Number = require( '@stdlib/number/ctor' ); +var isnan = require( './../lib/primitive.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isnan, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a `NaN` primitive number', function test( t ) { + t.equal( isnan( NaN ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the value of the object is `NaN`', function test( t ) { + t.equal( isnan( new Number( 5 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.equal( isnan( new Number( NaN ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided `NaN`', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + 3.14, + -1.0, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isnan( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` when provided a symbol (symbols are unique)', opts, function test( t ) { + var s = Symbol( 'foo' ); + t.equal( isnan( s ), false, 'returns false' ); + t.end(); +}); diff --git a/is-native-function/README.md b/is-native-function/README.md new file mode 100644 index 00000000..aad10a00 --- /dev/null +++ b/is-native-function/README.md @@ -0,0 +1,117 @@ + + +# isNativeFunction + +> Test if a value is a native function. + +
+ +Native functions execute native code that is typically not written in JavaScript, but a lower-level language like C++. This includes the JavaScript [built-in functions][mdn-builtins], functions implemented using [Node.js C/C++ addons][node-js-add-ons], and code compiled via [WebAssembly][webassembly]. + +
+ + + +
+ +## Usage + +```javascript +var isNativeFunction = require( '@stdlib/assert/is-native-function' ); +``` + +#### isNativeFunction( value ) + +Tests if a `value` is a native `function`. + +```javascript +var bool = isNativeFunction( Date ); +// returns true + +function beep() { + console.log( 'beep' ); +} + +bool = isNativeFunction( beep ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isNativeFunction = require( '@stdlib/assert/is-native-function' ); + +var bool = isNativeFunction( Math.sqrt ); +// returns true + +bool = isNativeFunction( Date ); +// returns true + +bool = isNativeFunction( RegExp ); +// returns true + +bool = isNativeFunction( function foo() {} ); +// returns false + +bool = isNativeFunction( 'beep' ); +// returns false + +bool = isNativeFunction( 5 ); +// returns false + +bool = isNativeFunction( true ); +// returns false + +bool = isNativeFunction( null ); +// returns false + +bool = isNativeFunction( [] ); +// returns false + +bool = isNativeFunction( {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-native-function/benchmark/benchmark.js b/is-native-function/benchmark/benchmark.js new file mode 100644 index 00000000..85508049 --- /dev/null +++ b/is-native-function/benchmark/benchmark.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNativeFunction = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + new RegExp( '.*' ), + new Date(), + function noop() {}, + Math.sqrt, // eslint-disable-line stdlib/no-builtin-math + Date, + RegExp + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNativeFunction( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-native-function/docs/repl.txt b/is-native-function/docs/repl.txt new file mode 100644 index 00000000..3941494f --- /dev/null +++ b/is-native-function/docs/repl.txt @@ -0,0 +1,27 @@ + +{{alias}}( value ) + Tests if a value is a native function. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a native function. + + Examples + -------- + > var bool = {{alias}}( Date ) + true + > function beep() {}; + > bool = {{alias}}( beep ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-native-function/docs/types/index.d.ts b/is-native-function/docs/types/index.d.ts new file mode 100644 index 00000000..081b565e --- /dev/null +++ b/is-native-function/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a native function. +* +* @param value - value to test +* @returns boolean indicating whether value is a native function +* +* @example +* function beep() { +* console.log( 'boop' ); +* } +* +* var bool = isNativeFunction( beep ); +* // returns false +* +* bool = isNativeFunction( Date ); +* // returns true +*/ +declare function isNativeFunction( value: any ): boolean; + + +// EXPORTS // + +export = isNativeFunction; diff --git a/is-native-function/docs/types/test.ts b/is-native-function/docs/types/test.ts new file mode 100644 index 00000000..1a260560 --- /dev/null +++ b/is-native-function/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNativeFunction = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNativeFunction( ( x: number ) => x ); // $ExpectType boolean + isNativeFunction( Date ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNativeFunction(); // $ExpectError + isNativeFunction( ( x: number ) => x, 123 ); // $ExpectError +} diff --git a/is-native-function/examples/index.js b/is-native-function/examples/index.js new file mode 100644 index 00000000..fc0b3df4 --- /dev/null +++ b/is-native-function/examples/index.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function, no-restricted-syntax */ + +'use strict'; + +var isNativeFunction = require( './../lib' ); + +console.log( isNativeFunction( Math.sqrt ) ); // eslint-disable-line stdlib/no-builtin-math +// => true + +console.log( isNativeFunction( Date ) ); +// => true + +console.log( isNativeFunction( RegExp ) ); +// => true + +console.log( isNativeFunction( function foo() {} ) ); +// => false + +console.log( isNativeFunction( 'beep' ) ); +// => false + +console.log( isNativeFunction( 5 ) ); +// => false + +console.log( isNativeFunction( true ) ); +// => false + +console.log( isNativeFunction( null ) ); +// => false + +console.log( isNativeFunction( [] ) ); +// => false + +console.log( isNativeFunction( {} ) ); +// => false diff --git a/is-native-function/lib/index.js b/is-native-function/lib/index.js new file mode 100644 index 00000000..b2d53417 --- /dev/null +++ b/is-native-function/lib/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a native function. +* +* @module @stdlib/assert/is-native-function +* +* @example +* var isNativeFunction = require( '@stdlib/assert/is-native-function' ); +* +* function beep() { +* console.log( 'boop' ); +* } +* +* var bool = isNativeFunction( beep ); +* // returns false +* +* bool = isNativeFunction( Date ); +* // returns true +*/ + +// MODULES // + +var isNativeFunction = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNativeFunction; diff --git a/is-native-function/lib/main.js b/is-native-function/lib/main.js new file mode 100644 index 00000000..66d2e946 --- /dev/null +++ b/is-native-function/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); +var reNativeFunction = require( '@stdlib/regexp/native-function' ); + + +// VARIABLES // + +// Using `Function#toString` bypasses a value's own `toString` method to provide an extra, but not security proof, precaution to prevent a provided function from impersonating a native function: +var fcn2str = Function.prototype.toString; + + +// MAIN // + +/** +* Tests if a value is a native function. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a native function +* +* @example +* function beep() { +* console.log( 'boop' ); +* } +* +* var bool = isNativeFunction( beep ); +* // returns false +* +* bool = isNativeFunction( Date ); +* // returns true +*/ +function isNativeFunction( value ) { + return ( + isFunction( value ) && + reNativeFunction.REGEXP.test( fcn2str.call( value ) ) + ); +} + + +// EXPORTS // + +module.exports = isNativeFunction; diff --git a/is-native-function/package.json b/is-native-function/package.json new file mode 100644 index 00000000..910c91aa --- /dev/null +++ b/is-native-function/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-native-function", + "version": "0.0.0", + "description": "Test if a value is a native function.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "function", + "fcn", + "is", + "isfunction", + "native", + "isnative", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-native-function/test/test.js b/is-native-function/test/test.js new file mode 100644 index 00000000..84d3487e --- /dev/null +++ b/is-native-function/test/test.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNativeFunction = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNativeFunction, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a native function', function test( t ) { + t.strictEqual( isNativeFunction( Date ), true, 'returns true' ); + t.strictEqual( isNativeFunction( Math.sqrt ), true, 'returns true' ); // eslint-disable-line stdlib/no-builtin-math + t.strictEqual( isNativeFunction( RegExp ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a non-native function', function test( t ) { + function beep() { + return 'boop'; + } + t.strictEqual( isNativeFunction( beep ), false, 'returns false' ); + t.strictEqual( isNativeFunction( test ), false, 'returns false' ); + t.strictEqual( isNativeFunction( tape ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + undefined, + true, + false, + null, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNativeFunction( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-ndarray-like/README.md b/is-ndarray-like/README.md new file mode 100644 index 00000000..9415818e --- /dev/null +++ b/is-ndarray-like/README.md @@ -0,0 +1,97 @@ + + +# isndarrayLike + +> Test if a value is [ndarray][@stdlib/ndarray/ctor]-like. + +
+ +## Usage + +```javascript +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +``` + +#### isndarrayLike( value ) + +Tests if a value is [ndarray][@stdlib/ndarray/ctor]-like. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var bool = isndarrayLike( arr ); +// returns true +``` + +A value is [ndarray][@stdlib/ndarray/ctor]-like if a value is an `object` with the following properties: + +- **dtype**: `string` specifying a data type. +- **data**: `object` pointing to an underlying data buffer. +- **shape**: array-like `object` containing dimensions. +- **strides**: array-like `object` containing stride lengths. +- **offset**: `number` specifying the index offset. +- **order**: `string` describing the memory layout. +- **ndims**: `number` specifying the number of dimensions. +- **length**: `number` specifying the total number of elements. +- **flags**: `object` containing meta data. +- **get**: `function` for retrieving elements. +- **set**: `function` for setting elements. + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); + +var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var bool = isndarrayLike( arr ); +// returns true + +bool = isndarrayLike( [ 1, 2, 3, 4 ] ); +// returns false + +bool = isndarrayLike( {} ); +// returns false + +bool = isndarrayLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-ndarray-like/benchmark/benchmark.js b/is-ndarray-like/benchmark/benchmark.js new file mode 100644 index 00000000..3e239461 --- /dev/null +++ b/is-ndarray-like/benchmark/benchmark.js @@ -0,0 +1,146 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var pkg = require( './../package.json' ).name; +var isndarrayLike = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::true,ndarray', function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = [ 0, 0, 0, 0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + + values = [ + arr, + arr, + arr, + arr, + arr + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isndarrayLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true,ndarray_like', function benchmark( b ) { + var values; + var bool; + var arr; + var i; + + arr = { + 'data': [ 0, 0, 0, 0 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + values = [ + arr, + arr, + arr, + arr, + arr + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isndarrayLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + 5, + 'beep', + true, + false, + null, + [ 1, 2, 3 ], + {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isndarrayLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-ndarray-like/docs/repl.txt b/is-ndarray-like/docs/repl.txt new file mode 100644 index 00000000..7409dbe0 --- /dev/null +++ b/is-ndarray-like/docs/repl.txt @@ -0,0 +1,40 @@ + +{{alias}}( value ) + Tests if a value is ndarray-like. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is ndarray-like. + + Examples + -------- + > var M = {}; + > M.data = [ 0, 0, 0, 0 ]; + > M.ndims = 2; + > M.shape = [ 2, 2 ]; + > M.strides = [ 2, 1 ]; + > M.offset = 0; + > M.order = 'row-major'; + > M.dtype = 'generic'; + > M.length = 4; + > M.flags = {}; + > M.get = function get( i, j ) {}; + > M.set = function set( i, j ) {}; + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-ndarray-like/docs/types/index.d.ts b/is-ndarray-like/docs/types/index.d.ts new file mode 100644 index 00000000..156ae0e2 --- /dev/null +++ b/is-ndarray-like/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is ndarray-like. +* +* @param v - value to test +* @returns boolean indicating if a value is ndarray-like +* +* @example +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isndarrayLike( arr ); +* // returns true +* +* bool = isndarrayLike( [] ); +* // returns false +*/ +declare function isndarrayLike( v: any ): boolean; + + +// EXPORTS // + +export = isndarrayLike; diff --git a/is-ndarray-like/docs/types/test.ts b/is-ndarray-like/docs/types/test.ts new file mode 100644 index 00000000..cf7067ff --- /dev/null +++ b/is-ndarray-like/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* tslint:disable:no-invalid-this */ + +import isndarrayLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const ndarray = { + 'data': [ 2, 1, 1, 2 ], + 'ndims': 2, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': function get( i: number, j: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + return this.data[ idx ]; + }, + 'set': function set( i: number, j: number, v: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + this.data[ idx ] = v; + return v; + } + }; + isndarrayLike( ndarray ); // $ExpectType boolean + isndarrayLike( [] ); // $ExpectType boolean + isndarrayLike( false ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isndarrayLike(); // $ExpectError +} diff --git a/is-ndarray-like/examples/index.js b/is-ndarray-like/examples/index.js new file mode 100644 index 00000000..f57e0a55 --- /dev/null +++ b/is-ndarray-like/examples/index.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isndarrayLike = require( './../lib' ); + +var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +console.log( isndarrayLike( arr ) ); +// => true + +console.log( isndarrayLike( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isndarrayLike( {} ) ); +// => false + +console.log( isndarrayLike( null ) ); +// => false diff --git a/is-ndarray-like/lib/index.js b/is-ndarray-like/lib/index.js new file mode 100644 index 00000000..1571eccf --- /dev/null +++ b/is-ndarray-like/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is ndarray-like. +* +* @module @stdlib/assert/is-ndarray-like +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +* +* var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isndarrayLike( arr ); +* // returns true +* +* bool = isndarrayLike( [] ); +* // returns false +*/ + +// MODULES // + +var isndarrayLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isndarrayLike; diff --git a/is-ndarray-like/lib/main.js b/is-ndarray-like/lib/main.js new file mode 100644 index 00000000..b222c7b7 --- /dev/null +++ b/is-ndarray-like/lib/main.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + + +// MAIN // + +/** +* Tests if a value is ndarray-like. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is ndarray-like +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* +* var bool = isndarrayLike( arr ); +* // returns true +* +* bool = isndarrayLike( [] ); +* // returns false +*/ +function isndarrayLike( v ) { + return ( + v instanceof ndarray || + ( + v !== null && + typeof v === 'object' && + typeof v.data === 'object' && + typeof v.shape === 'object' && + typeof v.strides === 'object' && + typeof v.offset === 'number' && + typeof v.order === 'string' && + typeof v.ndims === 'number' && + typeof v.dtype === 'string' && + typeof v.length === 'number' && + typeof v.flags === 'object' && + typeof v.get === 'function' && + typeof v.set === 'function' + ) + ); +} + + +// EXPORTS // + +module.exports = isndarrayLike; diff --git a/is-ndarray-like/package.json b/is-ndarray-like/package.json new file mode 100644 index 00000000..2b48a733 --- /dev/null +++ b/is-ndarray-like/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/assert/is-ndarray-like", + "version": "0.0.0", + "description": "Test if a value is ndarray-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-ndarray-like/test/test.js b/is-ndarray-like/test/test.js new file mode 100644 index 00000000..ce0301a7 --- /dev/null +++ b/is-ndarray-like/test/test.js @@ -0,0 +1,85 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var isndarrayLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isndarrayLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an ndarray', function test( t ) { + var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isndarrayLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an ndarray-like object', function test( t ) { + var arr; + + arr = { + 'data': [ 0, 0, 0, 0 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isndarrayLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isndarrayLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-negative-integer-array/README.md b/is-negative-integer-array/README.md new file mode 100644 index 00000000..a5da80ff --- /dev/null +++ b/is-negative-integer-array/README.md @@ -0,0 +1,133 @@ + + +# isNegativeIntegerArray + +> Test if a value is an array-like object containing only negative integers. + +
+ +## Usage + +```javascript +var isNegativeIntegerArray = require( '@stdlib/assert/is-negative-integer-array' ); +``` + +#### isNegativeIntegerArray( value ) + +Tests if a `value` is an array-like object containing **only** negative `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeIntegerArray( [ -3, new Number(-3) ] ); +// returns true + +bool = isNegativeIntegerArray( [ -3, '3.0' ] ); +// returns false +``` + +#### isNegativeIntegerArray.primitives( value ) + +Tests if a `value` is an array-like object containing **only** negative primitive `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeIntegerArray.primitives( [ -1.0, -10.0 ] ); +// returns true + +bool = isNegativeIntegerArray.primitives( [ -1.0, 0.0, -10.0 ] ); +// returns false + +bool = isNegativeIntegerArray.primitives( [ -3.0, new Number(-1.0) ] ); +// returns false +``` + +#### isNegativeIntegerArray.objects( value ) + +Tests if a `value` is an array-like object containing **only** `Number` objects holding negative `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeIntegerArray.objects( [ new Number(-1.0), new Number(-10.0) ] ); +// returns true + +bool = isNegativeIntegerArray.objects( [ -1.0, 0.0, -10.0 ] ); +// returns false + +bool = isNegativeIntegerArray.objects( [ -3.0, new Number(-1.0) ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNegativeIntegerArray = require( '@stdlib/assert/is-negative-integer-array' ); + +var bool = isNegativeIntegerArray( [ -5, -2, -3 ] ); +// returns true + +bool = isNegativeIntegerArray( [ -4, -3, -2, -1 ] ); +// returns true + +bool = isNegativeIntegerArray( [ -1, new Number( -6 ), -3 ] ); +// returns true + +bool = isNegativeIntegerArray( [ -3, -2, -1, 0 ] ); +// returns false + +bool = isNegativeIntegerArray( [ -1, 'abc', -3 ] ); +// returns false + +bool = isNegativeIntegerArray( [ -2.3, -1, -3 ] ); +// returns false + +bool = isNegativeIntegerArray( [] ); +// returns false +``` + +
+ + + + + + diff --git a/is-negative-integer-array/benchmark/benchmark.js b/is-negative-integer-array/benchmark/benchmark.js new file mode 100644 index 00000000..8b79c1f8 --- /dev/null +++ b/is-negative-integer-array/benchmark/benchmark.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isNegativeIntegerArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( -1.0 ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( -1.0 ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isNegativeIntegerArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isNegativeIntegerArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isNegativeIntegerArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-negative-integer-array/docs/repl.txt b/is-negative-integer-array/docs/repl.txt new file mode 100644 index 00000000..fbeb2e83 --- /dev/null +++ b/is-negative-integer-array/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only negative integers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + negative integers. + + Examples + -------- + > var bool = {{alias}}( [ -3.0, new Number(-3.0) ] ) + true + > bool = {{alias}}( [ -3.0, '-3.0' ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only negative primitive + integer values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + negative primitive integer values. + + Examples + -------- + > var bool = {{alias}}.primitives( [ -1.0, -10.0 ] ) + true + > bool = {{alias}}.primitives( [ -1.0, 0.0, -10.0 ] ) + false + > bool = {{alias}}.primitives( [ -3.0, new Number(-1.0) ] ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only number objects + having negative integer values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + number objects having negative integer values. + + Examples + -------- + > var bool = {{alias}}.objects( [ new Number(-1.0), new Number(-10.0) ] ) + true + > bool = {{alias}}.objects( [ -1.0, 0.0, -10.0 ] ) + false + > bool = {{alias}}.objects( [ -3.0, new Number(-1.0) ] ) + false + + See Also + -------- + diff --git a/is-negative-integer-array/docs/types/index.d.ts b/is-negative-integer-array/docs/types/index.d.ts new file mode 100644 index 00000000..77d45039 --- /dev/null +++ b/is-negative-integer-array/docs/types/index.d.ts @@ -0,0 +1,109 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNegativeIntegerArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsNegativeIntegerArray { + /** + * Tests if a value is an array-like object containing only negative integers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only negative integers + * + * @example + * var bool = isNegativeIntegerArray( [ -3.0, new Number(-3.0) ] ); + * // returns true + * + * @example + * var bool = isNegativeIntegerArray( [ -3.0, '-3.0' ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only negative primitive integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only negative primitive integer values + * + * @example + * var bool = isNegativeIntegerArray.primitives( [ -1.0, -10.0 ] ); + * // returns true + * + * @example + * var bool = isNegativeIntegerArray.primitives( [ -1.0, 0.0, -10.0 ] ); + * // returns false + * + * @example + * var bool = isNegativeIntegerArray.primitives( [ -3.0, new Number(-1.0) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects having negative integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number objects having negative integer values + * + * @example + * var bool = isNegativeIntegerArray.objects( [ new Number(-1.0), new Number(-10.0) ] ); + * // returns true + * + * @example + * var bool = isNegativeIntegerArray.objects( [ -1.0, 0.0, -10.0 ] ); + * // returns false + * + * @example + * var bool = isNegativeIntegerArray.objects( [ -3.0, new Number(-1.0) ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object containing only negative integers. +* +* @param value - value to test +* @returns boolean indicating whether value is an array-like object containing only negative integers +* +* @example +* var bool = isNegativeIntegerArray( [ -3.0, new Number(-3.0) ] ); +* // returns true +* +* @example +* var bool = isNegativeIntegerArray( [ -3.0, '-3.0' ] ); +* // returns false +* +* @example +* var bool = isNegativeIntegerArray.primitives( [ -1.0, -10.0 ] ); +* // returns true +* +* @example +* var bool = isNegativeIntegerArray.objects( [ new Number(-1.0), new Number(-10.0) ] ); +* // returns true +*/ +declare var isNegativeIntegerArray: IsNegativeIntegerArray; + + +// EXPORTS // + +export = isNegativeIntegerArray; diff --git a/is-negative-integer-array/docs/types/test.ts b/is-negative-integer-array/docs/types/test.ts new file mode 100644 index 00000000..ae49ea3f --- /dev/null +++ b/is-negative-integer-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNegativeIntegerArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNegativeIntegerArray( [ 4 ] ); // $ExpectType boolean + isNegativeIntegerArray( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNegativeIntegerArray(); // $ExpectError + isNegativeIntegerArray( [ -3 ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNegativeIntegerArray.primitives( [ new Number( -3 ) ] ); // $ExpectType boolean + isNegativeIntegerArray.primitives( [ -3 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isNegativeIntegerArray.primitives(); // $ExpectError + isNegativeIntegerArray.primitives( [ -2 ], 123 ); // $ExpectError +} + + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNegativeIntegerArray.objects( [ new Number( -2 ) ] ); // $ExpectType boolean + isNegativeIntegerArray.objects( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isNegativeIntegerArray.objects(); // $ExpectError + isNegativeIntegerArray.objects( [ -2 ], 123 ); // $ExpectError +} diff --git a/is-negative-integer-array/examples/index.js b/is-negative-integer-array/examples/index.js new file mode 100644 index 00000000..953c42fd --- /dev/null +++ b/is-negative-integer-array/examples/index.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNegativeIntegerArray = require( './../lib' ); + +var bool = isNegativeIntegerArray( [ -5, -2, -3 ] ); +console.log( bool ); +// => true + +bool = isNegativeIntegerArray( [ -4, -3, -2, -1 ] ); +console.log( bool ); +// => true + +bool = isNegativeIntegerArray( [ -1, new Number( -6 ), -3 ] ); +console.log( bool ); +// => true + +bool = isNegativeIntegerArray( [ -3, -2, -1, 0 ] ); +console.log( bool ); +// => false + +bool = isNegativeIntegerArray( [ -1, 'abc', -3 ] ); +console.log( bool ); +// => false + +bool = isNegativeIntegerArray( [ -2.3, -1, -3 ] ); +console.log( bool ); +// => false + +bool = isNegativeIntegerArray( [] ); +console.log( bool ); +// => false diff --git a/is-negative-integer-array/lib/index.js b/is-negative-integer-array/lib/index.js new file mode 100644 index 00000000..3b112202 --- /dev/null +++ b/is-negative-integer-array/lib/index.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only negative integers. +* +* @module @stdlib/assert/is-negative-integer-array +* +* @example +* var isNegativeIntegerArray = require( '@stdlib/assert/is-negative-integer-array' ); +* +* var bool = isNegativeIntegerArray( [ -3.0, new Number(-3.0) ] ); +* // returns true +* +* bool = isNegativeIntegerArray( [ -3.0, '-3.0' ] ); +* // returns false +* +* @example +* var isNegativeIntegerArray = require( '@stdlib/assert/is-negative-integer-array' ).primitives; +* +* var bool = isNegativeIntegerArray( [ -1.0, -10.0 ] ); +* // returns true +* +* bool = isNegativeIntegerArray( [ -1.0, 0.0, -10.0 ] ); +* // returns false +* +* bool = isNegativeIntegerArray( [ -3.0, new Number(-1.0) ] ); +* // returns false +* +* @example +* var isNegativeIntegerArray = require( '@stdlib/assert/is-negative-integer-array' ).objects; +* +* var bool = isNegativeIntegerArray( [ new Number(-1.0), new Number(-10.0) ] ); +* // returns true +* +* bool = isNegativeIntegerArray( [ -1.0, 0.0, -10.0 ] ); +* // returns false +* +* bool = isNegativeIntegerArray( [ -3.0, new Number(-1.0) ] ); +* // returns false +*/ + +// MODULES // + +var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); + + +// MAIN // + +var isNegativeIntegerArray = arrayfun( isNegativeInteger ); +setReadOnly( isNegativeIntegerArray, 'primitives', arrayfun( isNegativeInteger.isPrimitive ) ); +setReadOnly( isNegativeIntegerArray, 'objects', arrayfun( isNegativeInteger.isObject ) ); + + +// EXPORTS // + +module.exports = isNegativeIntegerArray; diff --git a/is-negative-integer-array/package.json b/is-negative-integer-array/package.json new file mode 100644 index 00000000..800d2414 --- /dev/null +++ b/is-negative-integer-array/package.json @@ -0,0 +1,81 @@ +{ + "name": "@stdlib/assert/is-negative-integer-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only negative integers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "number", + "numeric", + "negative", + "isnumber", + "isnumeric", + "integer", + "int", + "isinteger", + "type", + "check", + "validate", + "valid", + "test" + ] +} diff --git a/is-negative-integer-array/test/test.js b/is-negative-integer-array/test/test.js new file mode 100644 index 00000000..443eb8ca --- /dev/null +++ b/is-negative-integer-array/test/test.js @@ -0,0 +1,101 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Number = require( '@stdlib/number/ctor' ); +var isNegativeIntegerArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNegativeIntegerArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only negative integer values', function test( t ) { + var arr; + + arr = [ -5.0, new Number( -5 ), -1.0 ]; + t.equal( isNegativeIntegerArray( arr ), true, 'returns true' ); + + arr = new Int32Array( [ -2, -9, -4 ] ); + t.equal( isNegativeIntegerArray( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': new Number( -5 ), + '1': new Number( -2 ) + }; + t.equal( isNegativeIntegerArray( arr ), true, 'returns true' ); + + arr = [ -5.0, '3', null ]; + t.equal( isNegativeIntegerArray( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only negative primitive integers', function test( t ) { + var arr; + + arr = [ -5.0, -1.0 ]; + t.equal( isNegativeIntegerArray.primitives( arr ), true, 'returns true' ); + + arr = new Int32Array( [ -2, -9, -4 ] ); + t.equal( isNegativeIntegerArray.primitives( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': new Number( -5 ), + '1': new Number( -2 ) + }; + t.equal( isNegativeIntegerArray.primitives( arr ), false, 'returns false' ); + + arr = [ new Number( -5 ), -1.0, -1.0 ]; + t.equal( isNegativeIntegerArray.primitives( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only Number objects having negative integer values', function test( t ) { + var arr; + + arr = [ new Number( -5 ), new Number( -5 ) ]; + t.equal( isNegativeIntegerArray.objects( arr ), true, 'returns true' ); + + arr = [ -5, -3 ]; + t.equal( isNegativeIntegerArray.objects( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': new Number( -5 ), + '1': new Number( -2 ) + }; + t.equal( isNegativeIntegerArray.objects( arr ), true, 'returns true' ); + + arr = new Int32Array( [ -2, -9, -4 ] ); + t.equal( isNegativeIntegerArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-negative-integer/README.md b/is-negative-integer/README.md new file mode 100644 index 00000000..709db334 --- /dev/null +++ b/is-negative-integer/README.md @@ -0,0 +1,136 @@ + + +# isNegativeInteger + +> Test if a value is a number having a negative integer value. + +
+ +## Usage + +```javascript +var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ); +``` + +#### isNegativeInteger( value ) + +Tests if a `value` is a `number` having a negative `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeInteger( -5.0 ); +// returns true + +bool = isNegativeInteger( new Number( -5.0 ) ); +// returns true + +bool = isNegativeInteger( 5.0 ); +// returns false + +bool = isNegativeInteger( -3.14 ); +// returns false + +bool = isNegativeInteger( null ); +// returns false +``` + +#### isNegativeInteger.isPrimitive( value ) + +Tests if a `value` is a primitive `number` having a negative `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeInteger.isPrimitive( -3.0 ); +// returns true + +bool = isNegativeInteger.isPrimitive( new Number( -3.0 ) ); +// returns false +``` + +#### isNegativeInteger.isObject( value ) + +Tests if a `value` is a `Number` object having a negative `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeInteger.isObject( -3.0 ); +// returns false + +bool = isNegativeInteger.isObject( new Number( -3.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ); + +var bool = isNegativeInteger( -5.0 ); +// returns true + +bool = isNegativeInteger( new Number( -5.0 ) ); +// returns true + +bool = isNegativeInteger( 0.0 ); +// returns false + +bool = isNegativeInteger( -3.14 ); +// returns false + +bool = isNegativeInteger( 5.0 ); +// returns false + +bool = isNegativeInteger( '-5' ); +// returns false + +bool = isNegativeInteger( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-negative-integer/benchmark/benchmark.js b/is-negative-integer/benchmark/benchmark.js new file mode 100644 index 00000000..67098cf1 --- /dev/null +++ b/is-negative-integer/benchmark/benchmark.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isNegativeInteger = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeInteger( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeInteger( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeInteger.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeInteger.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeInteger.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeInteger.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-negative-integer/docs/repl.txt b/is-negative-integer/docs/repl.txt new file mode 100644 index 00000000..0e61e999 --- /dev/null +++ b/is-negative-integer/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is a negative integer. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a negative integer. + + Examples + -------- + > var bool = {{alias}}( -5.0 ) + true + > bool = {{alias}}( new Number( -5.0 ) ) + true + > bool = {{alias}}( 5.0 ) + false + > bool = {{alias}}( -3.14 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive having a negative integer value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive having a negative + integer value. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( -3.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( -3.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a negative integer value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a negative + integer value. + + Examples + -------- + > var bool = {{alias}}.isObject( -3.0 ) + false + > bool = {{alias}}.isObject( new Number( -3.0 ) ) + true + + + See Also + -------- + diff --git a/is-negative-integer/docs/types/index.d.ts b/is-negative-integer/docs/types/index.d.ts new file mode 100644 index 00000000..54cadb99 --- /dev/null +++ b/is-negative-integer/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNegativeInteger` with methods for testing for primitives and objects, respectively. +*/ +interface IsNegativeInteger { + /** + * Tests if a value is a negative integer. + * + * @param value - value to test + * @returns boolean indicating whether value is a negative integer + * + * @example + * var bool = isNegativeInteger( -5.0 ); + * // returns true + * + * @example + * var bool = isNegativeInteger( new Number( -5.0 ) ); + * // returns true + * + * @example + * var bool = isNegativeInteger( 5.0 ); + * // returns false + * + * @example + * var bool = isNegativeInteger( -3.14 ); + * // returns false + * + * @example + * var bool = isNegativeInteger( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a negative integer value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a negative integer value + * + * @example + * var bool = isNegativeInteger.isPrimitive( -3.0 ); + * // returns true + * + * @example + * var bool = isNegativeInteger.isPrimitive( new Number( -3.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a negative integer value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a negative integer value + * + * @example + * var bool = isNegativeInteger.isObject( -3.0 ); + * // returns false + * + * @example + * var bool = isNegativeInteger.isObject( new Number( -3.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a negative integer. +* +* @param value - value to test +* @returns boolean indicating whether value is a negative integer +* +* @example +* var bool = isNegativeInteger( -5.0 ); +* // returns true +* +* @example +* var bool = isNegativeInteger( new Number( -5.0 ) ); +* // returns true +* +* @example +* var bool = isNegativeInteger( 5.0 ); +* // returns false +* +* @example +* var bool = isNegativeInteger( -3.14 ); +* // returns false +* +* @example +* var bool = isNegativeInteger( null ); +* // returns false +* +* @example +* var bool = isNegativeInteger.isPrimitive( -3.0 ); +* // returns true +* +* @example +* var bool = isNegativeInteger.isObject( new Number( -3.0 ) ); +* // returns true +*/ +declare var isNegativeInteger: IsNegativeInteger; + + +// EXPORTS // + +export = isNegativeInteger; diff --git a/is-negative-integer/docs/types/test.ts b/is-negative-integer/docs/types/test.ts new file mode 100644 index 00000000..855de7c4 --- /dev/null +++ b/is-negative-integer/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNegativeInteger = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNegativeInteger( 3 ); // $ExpectType boolean + isNegativeInteger( -2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNegativeInteger(); // $ExpectError + isNegativeInteger( -2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNegativeInteger.isPrimitive( new Number( -2 ) ); // $ExpectType boolean + isNegativeInteger.isPrimitive( -2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isNegativeInteger.isPrimitive(); // $ExpectError + isNegativeInteger.isPrimitive( -2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNegativeInteger.isObject( new Number( -2 ) ); // $ExpectType boolean + isNegativeInteger.isObject( -2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isNegativeInteger.isObject(); // $ExpectError + isNegativeInteger.isObject( -2, 123 ); // $ExpectError +} diff --git a/is-negative-integer/examples/index.js b/is-negative-integer/examples/index.js new file mode 100644 index 00000000..bfbbab46 --- /dev/null +++ b/is-negative-integer/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNegativeInteger = require( './../lib' ); + +console.log( isNegativeInteger( -5.0 ) ); +// => true + +console.log( isNegativeInteger( new Number( -5.0 ) ) ); +// => true + +console.log( isNegativeInteger( 0.0 ) ); +// => false + +console.log( isNegativeInteger( -3.14 ) ); +// => false + +console.log( isNegativeInteger( 5.0 ) ); +// => false + +console.log( isNegativeInteger( '-5' ) ); +// => false + +console.log( isNegativeInteger( null ) ); +// => false diff --git a/is-negative-integer/lib/index.js b/is-negative-integer/lib/index.js new file mode 100644 index 00000000..24fa6f74 --- /dev/null +++ b/is-negative-integer/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a negative integer. +* +* @module @stdlib/assert/is-negative-integer +* +* @example +* var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ); +* +* var bool = isNegativeInteger( -5.0 ); +* // returns true +* +* bool = isNegativeInteger( new Number( -5.0 ) ); +* // returns true +* +* bool = isNegativeInteger( 5.0 ); +* // returns false +* +* bool = isNegativeInteger( -3.14 ); +* // returns false +* +* bool = isNegativeInteger( null ); +* // returns false +* +* @example +* var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive; +* +* var bool = isNegativeInteger( -3.0 ); +* // returns true +* +* bool = isNegativeInteger( new Number( -3.0 ) ); +* // returns false +* +* @example +* var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isObject; +* +* var bool = isNegativeInteger( -3.0 ); +* // returns false +* +* bool = isNegativeInteger( new Number( -3.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isNegativeInteger = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isNegativeInteger, 'isPrimitive', isPrimitive ); +setReadOnly( isNegativeInteger, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isNegativeInteger; diff --git a/is-negative-integer/lib/main.js b/is-negative-integer/lib/main.js new file mode 100644 index 00000000..c6a39ac5 --- /dev/null +++ b/is-negative-integer/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a negative integer. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a negative integer +* +* @example +* var bool = isNegativeInteger( -5.0 ); +* // returns true +* +* @example +* var bool = isNegativeInteger( new Number( -5.0 ) ); +* // returns true +* +* @example +* var bool = isNegativeInteger( 5.0 ); +* // returns false +* +* @example +* var bool = isNegativeInteger( -3.14 ); +* // returns false +* +* @example +* var bool = isNegativeInteger( null ); +* // returns false +*/ +function isNegativeInteger( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isNegativeInteger; diff --git a/is-negative-integer/lib/object.js b/is-negative-integer/lib/object.js new file mode 100644 index 00000000..6f28c107 --- /dev/null +++ b/is-negative-integer/lib/object.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isObject; + + +// MAIN // + +/** +* Tests if a value is a number object having a negative integer value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a negative integer value +* +* @example +* var bool = isNegativeInteger( -3.0 ); +* // returns false +* +* @example +* var bool = isNegativeInteger( new Number( -3.0 ) ); +* // returns true +*/ +function isNegativeInteger( value ) { + return ( + isInteger( value ) && + value.valueOf() < 0.0 + ); +} + + +// EXPORTS // + +module.exports = isNegativeInteger; diff --git a/is-negative-integer/lib/primitive.js b/is-negative-integer/lib/primitive.js new file mode 100644 index 00000000..e91cea80 --- /dev/null +++ b/is-negative-integer/lib/primitive.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a number primitive having a negative integer value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a negative integer value +* +* @example +* var bool = isNegativeInteger( -3.0 ); +* // returns true +* +* @example +* var bool = isNegativeInteger( new Number( -3.0 ) ); +* // returns false +*/ +function isNegativeInteger( value ) { + return ( + isInteger( value ) && + value < 0.0 + ); +} + + +// EXPORTS // + +module.exports = isNegativeInteger; diff --git a/is-negative-integer/package.json b/is-negative-integer/package.json new file mode 100644 index 00000000..99908c0a --- /dev/null +++ b/is-negative-integer/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-negative-integer", + "version": "0.0.0", + "description": "Test if a value is a number having a negative integer value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "integer", + "int", + "negative", + "is", + "isinteger", + "type", + "check", + "primitive", + "object", + "test", + "validate", + "valid" + ] +} diff --git a/is-negative-integer/test/test.js b/is-negative-integer/test/test.js new file mode 100644 index 00000000..a27614eb --- /dev/null +++ b/is-negative-integer/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNegativeInteger = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNegativeInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number having a negative integer value', function test( t ) { + t.equal( typeof isNegativeInteger.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a negative integer value', function test( t ) { + t.equal( typeof isNegativeInteger.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-negative-integer/test/test.main.js b/is-negative-integer/test/test.main.js new file mode 100644 index 00000000..bf63506e --- /dev/null +++ b/is-negative-integer/test/test.main.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNegativeInteger = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNegativeInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number having a negative integer value', function test( t ) { + t.equal( isNegativeInteger( -5.0 ), true, 'returns true' ); + t.equal( isNegativeInteger( new Number( -5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number having a negative integer value', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + 1.0, + 0.0, + new Number( 3.0 ), // eslint-disable-line no-new-wrappers + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNegativeInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-negative-integer/test/test.object.js b/is-negative-integer/test/test.object.js new file mode 100644 index 00000000..4f349368 --- /dev/null +++ b/is-negative-integer/test/test.object.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNegativeInteger = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNegativeInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a negative integer value', function test( t ) { + t.equal( isNegativeInteger( new Number( -5.0 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is a negative integer value', function test( t ) { + t.equal( isNegativeInteger( -3.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + NaN, + new Number( NaN ), + null, + true, + undefined, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNegativeInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-negative-integer/test/test.primitive.js b/is-negative-integer/test/test.primitive.js new file mode 100644 index 00000000..4dd3ec3d --- /dev/null +++ b/is-negative-integer/test/test.primitive.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNegativeInteger = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNegativeInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number having a negative integer value', function test( t ) { + t.equal( isNegativeInteger( -3.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has a negative integer value', function test( t ) { + t.equal( isNegativeInteger( new Number( -5.0 ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + NaN, + null, + true, + undefined, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNegativeInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-negative-number-array/README.md b/is-negative-number-array/README.md new file mode 100644 index 00000000..ecc7feae --- /dev/null +++ b/is-negative-number-array/README.md @@ -0,0 +1,124 @@ + + +# isNegativeNumberArray + +> Test if a value is an array-like object containing only negative numbers. + +
+ +## Usage + +```javascript +var isNegativeNumberArray = require( '@stdlib/assert/is-negative-number-array' ); +``` + +#### isNegativeNumberArray( value ) + +Tests if a `value` is an array-like object containing **only** negative numbers. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeNumberArray( [ -3.0, new Number(-3.0) ] ); +// returns true + +bool = isNegativeNumberArray( [ -3.0, '-3.0' ] ); +// returns false +``` + +#### isNegativeNumberArray.primitives( value ) + +Tests if a `value` is an array-like object `array` containing **only** primitive negative numbers. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeNumberArray.primitives( [ -1.0, -5.0, -10.0 ] ); +// returns true + +bool = isNegativeNumberArray.primitives( [ -3.0, new Number(-1.0) ] ); +// returns false +``` + +#### isNegativeNumberArray.objects( value ) + +Tests if a `value` is an array-like object `array` containing **only** object negative numbers. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeNumberArray.objects( [ new Number(-1.0), new Number(-1.0) ] ); +// returns true + +bool = isNegativeNumberArray.objects( [ -1.0, -5.0, -10.0 ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNegativeNumberArray = require( '@stdlib/assert/is-negative-number-array' ); + +var bool = isNegativeNumberArray( [ -5.0, -0.2, -3.9 ] ); +// returns true + +bool = isNegativeNumberArray( [ -1, -2, -3 ] ); +// returns true + +bool = isNegativeNumberArray( [ -1, new Number( -6 ), -3 ] ); +// returns true + +bool = isNegativeNumberArray( [ 0, -2, -4 ] ); +// returns false + +bool = isNegativeNumberArray( [ -1, 'abc', -3 ] ); +// returns false + +bool = isNegativeNumberArray( -78.0 ); +// returns false +``` + +
+ + + + + + diff --git a/is-negative-number-array/benchmark/benchmark.js b/is-negative-number-array/benchmark/benchmark.js new file mode 100644 index 00000000..be752630 --- /dev/null +++ b/is-negative-number-array/benchmark/benchmark.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isNegativeNumberArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( -1.0 ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( -1.0 ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isNegativeNumberArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isNegativeNumberArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isNegativeNumberArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-negative-number-array/docs/repl.txt b/is-negative-number-array/docs/repl.txt new file mode 100644 index 00000000..c8dad1d6 --- /dev/null +++ b/is-negative-number-array/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only negative numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + negative numbers. + + Examples + -------- + > var bool = {{alias}}( [ -3.0, new Number(-3.0) ] ) + true + > bool = {{alias}}( [ -3.0, '-3.0' ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only primitive negative + numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + primitive negative numbers. + + Examples + -------- + > var bool = {{alias}}.primitives( [ -1.0, -10.0 ] ) + true + > bool = {{alias}}.primitives( [ -1.0, 0.0, -10.0 ] ) + false + > bool = {{alias}}.primitives( [ -3.0, new Number(-1.0) ] ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only number objects + having negative number values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + number objects having negative number values. + + Examples + -------- + > var bool = {{alias}}.objects( [ new Number(-1.0), new Number(-10.0) ] ) + true + > bool = {{alias}}.objects( [ -1.0, 0.0, -10.0 ] ) + false + > bool = {{alias}}.objects( [ -3.0, new Number(-1.0) ] ) + false + + See Also + -------- + diff --git a/is-negative-number-array/docs/types/index.d.ts b/is-negative-number-array/docs/types/index.d.ts new file mode 100644 index 00000000..dd15e820 --- /dev/null +++ b/is-negative-number-array/docs/types/index.d.ts @@ -0,0 +1,109 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNegativeNumberArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsNegativeNumberArray { + /** + * Tests if a value is an array-like object containing only negative numbers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only negative numbers + * + * @example + * var bool = isNegativeNumberArray( [ -3.7, new Number(-3.0) ] ); + * // returns true + * + * @example + * var bool = isNegativeNumberArray( [ -3.0, '-3.0' ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only negative primitive number values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only negative primitive number values + * + * @example + * var bool = isNegativeNumberArray.primitives( [ -1.0, -10.0 ] ); + * // returns true + * + * @example + * var bool = isNegativeNumberArray.primitives( [ -1.0, 0.0, -10.0 ] ); + * // returns false + * + * @example + * var bool = isNegativeNumberArray.primitives( [ -3.0, new Number(-1.0) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects having negative number values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number objects having negative number values + * + * @example + * var bool = isNegativeNumberArray.objects( [ new Number(-1.3), new Number(-10.8) ] ); + * // returns true + * + * @example + * var bool = isNegativeNumberArray.objects( [ -1.0, 0.0, -10.0 ] ); + * // returns false + * + * @example + * var bool = isNegativeNumberArray.objects( [ -3.0, new Number(-1.0) ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object containing only negative numbers. +* +* @param value - value to test +* @returns boolean indicating whether value is an array-like object containing only negative numbers +* +* @example +* var bool = isNegativeNumberArray( [ -3.7, new Number(-3.0) ] ); +* // returns true +* +* @example +* var bool = isNegativeNumberArray( [ -3.7, '-3.0' ] ); +* // returns false +* +* @example +* var bool = isNegativeNumberArray.primitives( [ -1.3, -10.0 ] ); +* // returns true +* +* @example +* var bool = isNegativeNumberArray.objects( [ new Number(-1.3), new Number(-10.0) ] ); +* // returns true +*/ +declare var isNegativeNumberArray: IsNegativeNumberArray; + + +// EXPORTS // + +export = isNegativeNumberArray; diff --git a/is-negative-number-array/docs/types/test.ts b/is-negative-number-array/docs/types/test.ts new file mode 100644 index 00000000..5dd4f1ea --- /dev/null +++ b/is-negative-number-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNegativeNumberArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNegativeNumberArray( [ 4 ] ); // $ExpectType boolean + isNegativeNumberArray( [ -2.9 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNegativeNumberArray(); // $ExpectError + isNegativeNumberArray( [ -3 ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNegativeNumberArray.primitives( [ new Number( -3 ) ] ); // $ExpectType boolean + isNegativeNumberArray.primitives( [ -3 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isNegativeNumberArray.primitives(); // $ExpectError + isNegativeNumberArray.primitives( [ -2 ], 123 ); // $ExpectError +} + + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNegativeNumberArray.objects( [ new Number( -2 ) ] ); // $ExpectType boolean + isNegativeNumberArray.objects( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isNegativeNumberArray.objects(); // $ExpectError + isNegativeNumberArray.objects( [ -2 ], 123 ); // $ExpectError +} diff --git a/is-negative-number-array/examples/index.js b/is-negative-number-array/examples/index.js new file mode 100644 index 00000000..4b3b893d --- /dev/null +++ b/is-negative-number-array/examples/index.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNegativeNumberArray = require( './../lib' ); + +var bool = isNegativeNumberArray( [ -5.0, -0.2, -3.9 ] ); +console.log( bool ); +// => true + +bool = isNegativeNumberArray( [ -1, -2, -3 ] ); +console.log( bool ); +// => true + +bool = isNegativeNumberArray( [ -1, new Number( -6 ), -3 ] ); +console.log( bool ); +// => true + +bool = isNegativeNumberArray( [ 0, -2, -4 ] ); +console.log( bool ); +// => false + +bool = isNegativeNumberArray( [ -1, 'abc', -3 ] ); +console.log( bool ); +// => false + +bool = isNegativeNumberArray( -78.0 ); +console.log( bool ); +// => false + +bool = isNegativeNumberArray( [] ); +console.log( bool ); +// => false diff --git a/is-negative-number-array/lib/index.js b/is-negative-number-array/lib/index.js new file mode 100644 index 00000000..a56e7102 --- /dev/null +++ b/is-negative-number-array/lib/index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only negative numbers. +* +* @module @stdlib/assert/is-negative-number-array +* +* @example +* var isNegativeNumberArray = require( '@stdlib/assert/is-negative-number-array' ); +* +* var bool = isNegativeNumberArray( [ -3.0, new Number(-3.0) ] ); +* // returns true +* +* bool = isNegativeNumberArray( [ -3.0, '-3.0' ] ); +* // returns false +* +* @example +* var isNegativeNumberArray = require( '@stdlib/assert/is-negative-number-array' ).primitives; +* +* var bool = isNegativeNumberArray( [ -1.0, -5.0, -10.0 ] ); +* // returns true +* +* bool = isNegativeNumberArray( [ -3.0, new Number(-1.0) ] ); +* // returns false +* +* @example +* var isNegativeNumberArray = require( '@stdlib/assert/is-negative-number-array' ).objects; +* +* var bool = isNegativeNumberArray( [ new Number(-3.0), new Number(-1.0) ] ); +* // returns true +* +* bool = isNegativeNumberArray( [ -1.0, -5.0, -10.0 ] ); +* // returns false +*/ + +// MODULES // + +var isNegativeNumber = require( '@stdlib/assert/is-negative-number' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); + + +// MAIN // + +var isNegativeNumberArray = arrayfun( isNegativeNumber ); +setReadOnly( isNegativeNumberArray, 'primitives', arrayfun( isNegativeNumber.isPrimitive ) ); +setReadOnly( isNegativeNumberArray, 'objects', arrayfun( isNegativeNumber.isObject ) ); + + +// EXPORTS // + +module.exports = isNegativeNumberArray; diff --git a/is-negative-number-array/package.json b/is-negative-number-array/package.json new file mode 100644 index 00000000..2d571e9d --- /dev/null +++ b/is-negative-number-array/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-negative-number-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only negative numbers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "number", + "numeric", + "negative", + "isnumber", + "isnumeric", + "type", + "check" + ] +} diff --git a/is-negative-number-array/test/test.js b/is-negative-number-array/test/test.js new file mode 100644 index 00000000..ccb934ec --- /dev/null +++ b/is-negative-number-array/test/test.js @@ -0,0 +1,101 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Number = require( '@stdlib/number/ctor' ); +var isNegativeNumberArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNegativeNumberArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only negative numbers', function test( t ) { + var arr; + + arr = [ -5.0, new Number( -5 ), -1.0 ]; + t.equal( isNegativeNumberArray( arr ), true, 'returns true' ); + + arr = [ -5.0, '-3', null ]; + t.equal( isNegativeNumberArray( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': -3.14, + '1': -2.0 + }; + t.equal( isNegativeNumberArray( arr ), true, 'returns true' ); + + arr = new Float32Array( [ -5.0, -1.5 ] ); + t.equal( isNegativeNumberArray( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object having only negative number primitives', function test( t ) { + var arr; + + arr = [ -5.0, -2.0 ]; + t.equal( isNegativeNumberArray.primitives( arr ), true, 'returns true' ); + + arr = [ new Number( -5 ), -1.0, -1.0 ]; + t.equal( isNegativeNumberArray.primitives( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': -3.14, + '1': -1.0 + }; + t.equal( isNegativeNumberArray.primitives( arr ), true, 'returns true' ); + + arr = new Float32Array( [ -5.0, -1.5 ] ); + t.equal( isNegativeNumberArray.primitives( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object having only negative number objects', function test( t ) { + var arr; + + arr = [ new Number( -5 ), new Number( -5 ) ]; + t.equal( isNegativeNumberArray.objects( arr ), true, 'returns true' ); + + arr = [ -5.0, -1.0 ]; + t.equal( isNegativeNumberArray.objects( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': new Number( -3.14 ), + '1': new Number( -2.0 ) + }; + t.equal( isNegativeNumberArray.objects( arr ), true, 'returns true' ); + + arr = new Float32Array( [ -5.0, -1.5 ] ); + t.equal( isNegativeNumberArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-negative-number/README.md b/is-negative-number/README.md new file mode 100644 index 00000000..d24f3a94 --- /dev/null +++ b/is-negative-number/README.md @@ -0,0 +1,137 @@ + + +# isNegativeNumber + +> Test if a value is a number having a negative value. + +
+ +## Usage + +```javascript +var isNegativeNumber = require( '@stdlib/assert/is-negative-number' ); +``` + +#### isNegativeNumber( value ) + +Tests if a `value` is a `number` having a negative value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeNumber( -5.0 ); +// returns true + +bool = isNegativeNumber( new Number( -5.0 ) ); +// returns true + +bool = isNegativeNumber( -3.14 ); +// returns true + +bool = isNegativeNumber( 5.0 ); +// returns false + +bool = isNegativeNumber( null ); +// returns false +``` + +#### isNegativeNumber.isPrimitive( value ) + +Tests if a `value` is a primitive `number` having a negative value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeNumber.isPrimitive( -3.0 ); +// returns true + +bool = isNegativeNumber.isPrimitive( new Number( -3.0 ) ); +// returns false +``` + +#### isNegativeNumber.isObject( value ) + +Tests if a `value` is a `Number` object having a negative value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeNumber.isObject( -3.0 ); +// returns false + +bool = isNegativeNumber.isObject( new Number( -3.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var isNegativeNumber = require( '@stdlib/assert/is-negative-number' ); + +var bool = isNegativeNumber( -5.0 ); +// returns true + +bool = isNegativeNumber( new Number( -5.0 ) ); +// returns true + +bool = isNegativeNumber( -3.14 ); +// returns true + +bool = isNegativeNumber( 0.0 ); +// returns false + +bool = isNegativeNumber( 5.0 ); +// returns false + +bool = isNegativeNumber( '-5' ); +// returns false + +bool = isNegativeNumber( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-negative-number/benchmark/benchmark.js b/is-negative-number/benchmark/benchmark.js new file mode 100644 index 00000000..6685632f --- /dev/null +++ b/is-negative-number/benchmark/benchmark.js @@ -0,0 +1,221 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isNegativeNumber = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-negative-number/docs/repl.txt b/is-negative-number/docs/repl.txt new file mode 100644 index 00000000..bd8ba950 --- /dev/null +++ b/is-negative-number/docs/repl.txt @@ -0,0 +1,74 @@ + +{{alias}}( value ) + Tests if a value is a negative number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a negative number. + + Examples + -------- + > var bool = {{alias}}( -5.0 ) + true + > bool = {{alias}}( new Number( -5.0 ) ) + true + > bool = {{alias}}( -3.14 ) + true + > bool = {{alias}}( 5.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive having a negative value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive having a negative + value. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( -3.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( -3.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a negative value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a negative + value. + + Examples + -------- + > var bool = {{alias}}.isObject( -3.0 ) + false + > bool = {{alias}}.isObject( new Number( -3.0 ) ) + true + + See Also + -------- + diff --git a/is-negative-number/docs/types/index.d.ts b/is-negative-number/docs/types/index.d.ts new file mode 100644 index 00000000..9aaacd3b --- /dev/null +++ b/is-negative-number/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNegativeNumber` with methods for testing for primitives and objects, respectively. +*/ +interface IsNegativeNumber { + /** + * Tests if a value is a negative number. + * + * @param value - value to test + * @returns boolean indicating whether value is a negative number + * + * @example + * var bool = isNegativeNumber( -5.0 ); + * // returns true + * + * @example + * var bool = isNegativeNumber( new Number( -5.0 ) ); + * // returns true + * + * @example + * var bool = isNegativeNumber( -3.14 ); + * // returns true + * + * @example + * var bool = isNegativeNumber( 5.0 ); + * // returns false + * + * @example + * var bool = isNegativeNumber( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a negative value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a negative value + * + * @example + * var bool = isNegativeNumber.isPrimitive( -3.0 ); + * // returns true + * + * @example + * var bool = isNegativeNumber.isPrimitive( new Number( -3.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a negative value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a negative value + * + * @example + * var bool = isNegativeNumber.isObject( -3.0 ); + * // returns false + * + * @example + * var bool = isNegativeNumber.isObject( new Number( -3.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a negative number. +* +* @param value - value to test +* @returns boolean indicating whether value is a negative number +* +* @example +* var bool = isNegativeNumber( -5.0 ); +* // returns true +* +* @example +* var bool = isNegativeNumber( new Number( -5.0 ) ); +* // returns true +* +* @example +* var bool = isNegativeNumber( -3.14 ); +* // returns true +* +* @example +* var bool = isNegativeNumber( 5.0 ); +* // returns false +* +* @example +* var bool = isNegativeNumber( null ); +* // returns false +* +* @example +* var bool = isNegativeNumber.isPrimitive( -3.0 ); +* // returns true +* +* @example +* var bool = isNegativeNumber.isObject( new Number( -3.0 ) ); +* // returns true +*/ +declare var isNegativeNumber: IsNegativeNumber; + + +// EXPORTS // + +export = isNegativeNumber; diff --git a/is-negative-number/docs/types/test.ts b/is-negative-number/docs/types/test.ts new file mode 100644 index 00000000..0e3d4154 --- /dev/null +++ b/is-negative-number/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNegativeNumber = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNegativeNumber( 3 ); // $ExpectType boolean + isNegativeNumber( -2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNegativeNumber(); // $ExpectError + isNegativeNumber( -2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNegativeNumber.isPrimitive( new Number( -2 ) ); // $ExpectType boolean + isNegativeNumber.isPrimitive( -2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isNegativeNumber.isPrimitive(); // $ExpectError + isNegativeNumber.isPrimitive( -2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNegativeNumber.isObject( new Number( -2 ) ); // $ExpectType boolean + isNegativeNumber.isObject( -2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isNegativeNumber.isObject(); // $ExpectError + isNegativeNumber.isObject( -2, 123 ); // $ExpectError +} diff --git a/is-negative-number/examples/index.js b/is-negative-number/examples/index.js new file mode 100644 index 00000000..f49f6792 --- /dev/null +++ b/is-negative-number/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNegativeNumber = require( './../lib' ); + +console.log( isNegativeNumber( -5.0 ) ); +// => true + +console.log( isNegativeNumber( new Number( -5.0 ) ) ); +// => true + +console.log( isNegativeNumber( -3.14 ) ); +// => true + +console.log( isNegativeNumber( 0.0 ) ); +// => false + +console.log( isNegativeNumber( 5.0 ) ); +// => false + +console.log( isNegativeNumber( '-5' ) ); +// => false + +console.log( isNegativeNumber( null ) ); +// => false diff --git a/is-negative-number/lib/index.js b/is-negative-number/lib/index.js new file mode 100644 index 00000000..adcff44d --- /dev/null +++ b/is-negative-number/lib/index.js @@ -0,0 +1,81 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a negative number. +* +* @module @stdlib/assert/is-negative-number +* +* @example +* var isNegativeNumber = require( '@stdlib/assert/is-negative-number' ); +* +* var bool = isNegativeNumber( -5.0 ); +* // returns true +* +* bool = isNegativeNumber( new Number( -5.0 ) ); +* // returns true +* +* bool = isNegativeNumber( -3.14 ); +* // returns true +* +* bool = isNegativeNumber( 5.0 ); +* // returns false +* +* bool = isNegativeNumber( null ); +* // returns false +* +* @example +* // Use interface to check for negative number primitives... +* var isNegativeNumber = require( '@stdlib/assert/is-negative-number' ).isPrimitive; +* +* var bool = isNegativeNumber( -3.0 ); +* // returns true +* +* bool = isNegativeNumber( new Number( -3.0 ) ); +* // returns false +* +* @example +* // Use interface to check for negative number objects... +* var isNegativeNumber = require( '@stdlib/assert/is-negative-number' ).isObject; +* +* var bool = isNegativeNumber( -3.0 ); +* // returns false +* +* bool = isNegativeNumber( new Number( -3.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isNegativeNumber = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isNegativeNumber, 'isPrimitive', isPrimitive ); +setReadOnly( isNegativeNumber, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isNegativeNumber; diff --git a/is-negative-number/lib/main.js b/is-negative-number/lib/main.js new file mode 100644 index 00000000..e632d887 --- /dev/null +++ b/is-negative-number/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a negative number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a negative number +* +* @example +* var bool = isNegativeNumber( -5.0 ); +* // returns true +* +* @example +* var bool = isNegativeNumber( new Number( -5.0 ) ); +* // returns true +* +* @example +* var bool = isNegativeNumber( -3.14 ); +* // returns true +* +* @example +* var bool = isNegativeNumber( 5.0 ); +* // returns false +* +* @example +* var bool = isNegativeNumber( null ); +* // returns false +*/ +function isNegativeNumber( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isNegativeNumber; diff --git a/is-negative-number/lib/object.js b/is-negative-number/lib/object.js new file mode 100644 index 00000000..f21d04c6 --- /dev/null +++ b/is-negative-number/lib/object.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; + + +// MAIN // + +/** +* Tests if a value is a number object having a negative value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a negative value +* +* @example +* var bool = isNegativeNumber( -3.0 ); +* // returns false +* +* @example +* var bool = isNegativeNumber( new Number( -3.0 ) ); +* // returns true +*/ +function isNegativeNumber( value ) { + return ( + isNumber( value ) && + value.valueOf() < 0.0 + ); +} + + +// EXPORTS // + +module.exports = isNegativeNumber; diff --git a/is-negative-number/lib/primitive.js b/is-negative-number/lib/primitive.js new file mode 100644 index 00000000..3e23d90b --- /dev/null +++ b/is-negative-number/lib/primitive.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a number primitive having a negative value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a negative value +* +* @example +* var bool = isNegativeNumber( -3.0 ); +* // returns true +* +* @example +* var bool = isNegativeNumber( new Number( -3.0 ) ); +* // returns false +*/ +function isNegativeNumber( value ) { + return ( + isNumber( value ) && + value < 0.0 + ); +} + + +// EXPORTS // + +module.exports = isNegativeNumber; diff --git a/is-negative-number/package.json b/is-negative-number/package.json new file mode 100644 index 00000000..2baaeae6 --- /dev/null +++ b/is-negative-number/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-negative-number", + "version": "0.0.0", + "description": "Test if a value is a number having a negative value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "negative", + "is", + "isnumber", + "isnumeric", + "type", + "check", + "primitive", + "object" + ] +} diff --git a/is-negative-number/test/test.js b/is-negative-number/test/test.js new file mode 100644 index 00000000..435f89f9 --- /dev/null +++ b/is-negative-number/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNegativeNumber = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNegativeNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number having a negative value', function test( t ) { + t.equal( typeof isNegativeNumber.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a negative value', function test( t ) { + t.equal( typeof isNegativeNumber.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-negative-number/test/test.main.js b/is-negative-number/test/test.main.js new file mode 100644 index 00000000..c5840089 --- /dev/null +++ b/is-negative-number/test/test.main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNegativeNumber = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNegativeNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number having a negative number value', function test( t ) { + t.equal( isNegativeNumber( -5.0 ), true, 'returns true' ); + t.equal( isNegativeNumber( new Number( -5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number having a negative number value', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + 1.0, + 0.0, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNegativeNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-negative-number/test/test.object.js b/is-negative-number/test/test.object.js new file mode 100644 index 00000000..2455996f --- /dev/null +++ b/is-negative-number/test/test.object.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNegativeNumber = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNegativeNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a negative value', function test( t ) { + t.equal( isNegativeNumber( new Number( -5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is a negative value', function test( t ) { + t.equal( isNegativeNumber( -3.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a negative number', function test( t ) { + var values; + var i; + + values = [ + '5', + new Number( 0.0 ), // eslint-disable-line no-new-wrappers + new Number( 2.0 ), // eslint-disable-line no-new-wrappers + 3.14, + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNegativeNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-negative-number/test/test.primitive.js b/is-negative-number/test/test.primitive.js new file mode 100644 index 00000000..e7f09a27 --- /dev/null +++ b/is-negative-number/test/test.primitive.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNegativeNumber = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNegativeNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number having a negative value', function test( t ) { + t.equal( isNegativeNumber( -3.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has a negative value', function test( t ) { + t.equal( isNegativeNumber( new Number( -5.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a negative number', function test( t ) { + var values; + var i; + + values = [ + '5', + 0.0, + new Number( 2.0 ), // eslint-disable-line no-new-wrappers + 3.14, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNegativeNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-negative-zero/README.md b/is-negative-zero/README.md new file mode 100644 index 00000000..38347f56 --- /dev/null +++ b/is-negative-zero/README.md @@ -0,0 +1,136 @@ + + +# isNegativeZero + +> Test if a value is a number equal to negative zero. + +
+ +## Usage + +```javascript +var isNegativeZero = require( '@stdlib/assert/is-negative-zero' ); +``` + +#### isNegativeZero( value ) + +Tests if a `value` is a `number` having a value equal to negative zero. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeZero( -0.0 ); +// returns true + +bool = isNegativeZero( new Number( -0.0 ) ); +// returns true + +bool = isNegativeZero( -3.14 ); +// returns false + +bool = isNegativeZero( 0.0 ); +// returns false + +bool = isNegativeZero( null ); +// returns false +``` + +#### isNegativeZero.isPrimitive( value ) + +Tests if a `value` is a primitive `number` equal to negative zero. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeZero.isPrimitive( -0.0 ); +// returns true + +bool = isNegativeZero.isPrimitive( new Number( -0.0 ) ); +// returns false +``` + +#### isNegativeZero.isObject( value ) + +Tests if a `value` is a `Number` object having a value equal to negative zero. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNegativeZero.isObject( -0.0 ); +// returns false + +bool = isNegativeZero.isObject( new Number( -0.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNegativeZero = require( '@stdlib/assert/is-negative-zero' ); + +var bool = isNegativeZero( -0.0 ); +// returns true + +bool = isNegativeZero( new Number( -0.0 ) ); +// returns true + +bool = isNegativeZero( -3.14 ); +// returns false + +bool = isNegativeZero( 0.0 ); +// returns false + +bool = isNegativeZero( 5.0 ); +// returns false + +bool = isNegativeZero( '-0' ); +// returns false + +bool = isNegativeZero( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-negative-zero/benchmark/benchmark.js b/is-negative-zero/benchmark/benchmark.js new file mode 100644 index 00000000..8a0806ed --- /dev/null +++ b/is-negative-zero/benchmark/benchmark.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isNegativeZero = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + 0.0, + -0.0, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeZero( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -0.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeZero( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + 0.0, + -0.0, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeZero.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -0.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeZero.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + 0.0, + -0.0, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeZero.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -0.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNegativeZero.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-negative-zero/docs/repl.txt b/is-negative-zero/docs/repl.txt new file mode 100644 index 00000000..986ce32b --- /dev/null +++ b/is-negative-zero/docs/repl.txt @@ -0,0 +1,74 @@ + +{{alias}}( value ) + Tests if a value is negative zero. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is negative zero. + + Examples + -------- + > var bool = {{alias}}( -0.0 ) + true + > bool = {{alias}}( new Number( -0.0 ) ) + true + > bool = {{alias}}( -3.14 ) + false + > bool = {{alias}}( 0.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive equal to negative zero. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a number primitive equal to + negative zero. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( -0.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( -0.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a value equal to negative zero. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a number object having a value + equal to negative zero. + + Examples + -------- + > var bool = {{alias}}.isObject( -0.0 ) + false + > bool = {{alias}}.isObject( new Number( -0.0 ) ) + true + + See Also + -------- + diff --git a/is-negative-zero/docs/types/index.d.ts b/is-negative-zero/docs/types/index.d.ts new file mode 100644 index 00000000..aa48e6ac --- /dev/null +++ b/is-negative-zero/docs/types/index.d.ts @@ -0,0 +1,133 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNegativeZero` with methods for testing for primitives and objects, respectively. +*/ +interface IsNegativeZero { + /** + * Tests if a value is equal to negative zero. + * + * @param value - value to test + * @returns boolean indicating whether value is equal to negative zero + * + * @example + * var bool = isNegativeZero( -0.0 ); + * // returns true + * + * @example + * var bool = isNegativeZero( new Number( -0.0 ) ); + * // returns true + * + * @example + * var bool = isNegativeZero( -3.14 ); + * // returns false + * + * @example + * var bool = isNegativeZero( 5.0 ); + * // returns false + * + * @example + * var bool = isNegativeZero( 0.0 ); + * // returns false + * + * @example + * var bool = isNegativeZero( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive equal to negative zero. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive equal to negative zero + * + * @example + * var bool = isNegativeZero.isPrimitive( -0.0 ); + * // returns true + * + * @example + * var bool = isNegativeZero.isPrimitive( new Number( -0.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a value equal to negative zero. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a value equal to negative zero + * + * @example + * var bool = isNegativeZero.isObject( -0.0 ); + * // returns false + * + * @example + * var bool = isNegativeZero.isObject( new Number( -0.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is equal to negative zero. +* +* @param value - value to test +* @returns boolean indicating whether value is equal to negative zero +* +* @example +* var bool = isNegativeZero( -0.0 ); +* // returns true +* +* @example +* var bool = isNegativeZero( new Number( -0.0 ) ); +* // returns true +* +* @example +* var bool = isNegativeZero( -3.14 ); +* // returns false +* +* @example +* var bool = isNegativeZero( 5.0 ); +* // returns false +* +* @example +* var bool = isNegativeZero( 0.0 ); +* // returns false +* +* @example +* var bool = isNegativeZero( null ); +* // returns false +* +* @example +* var bool = isNegativeZero.isPrimitive( -0.0 ); +* // returns true +* +* @example +* var bool = isNegativeZero.isObject( new Number( -0.0 ) ); +* // returns true +*/ +declare var isNegativeZero: IsNegativeZero; + + +// EXPORTS // + +export = isNegativeZero; diff --git a/is-negative-zero/docs/types/test.ts b/is-negative-zero/docs/types/test.ts new file mode 100644 index 00000000..3111fc16 --- /dev/null +++ b/is-negative-zero/docs/types/test.ts @@ -0,0 +1,62 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNegativeZero = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNegativeZero( 3 ); // $ExpectType boolean + isNegativeZero( -0 ); // $ExpectType boolean + isNegativeZero( 0 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNegativeZero(); // $ExpectError + isNegativeZero( -0, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNegativeZero.isPrimitive( new Number( -0 ) ); // $ExpectType boolean + isNegativeZero.isPrimitive( -0 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isNegativeZero.isPrimitive(); // $ExpectError + isNegativeZero.isPrimitive( -0, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNegativeZero.isObject( new Number( -0 ) ); // $ExpectType boolean + isNegativeZero.isObject( -0 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isNegativeZero.isObject(); // $ExpectError + isNegativeZero.isObject( -0, 123 ); // $ExpectError +} diff --git a/is-negative-zero/examples/index.js b/is-negative-zero/examples/index.js new file mode 100644 index 00000000..c6f90837 --- /dev/null +++ b/is-negative-zero/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNegativeZero = require( './../lib' ); + +console.log( isNegativeZero( -0.0 ) ); +// => true + +console.log( isNegativeZero( new Number( -0.0 ) ) ); +// => true + +console.log( isNegativeZero( -3.14 ) ); +// => false + +console.log( isNegativeZero( 0.0 ) ); +// => false + +console.log( isNegativeZero( 5.0 ) ); +// => false + +console.log( isNegativeZero( '-0' ) ); +// => false + +console.log( isNegativeZero( null ) ); +// => false diff --git a/is-negative-zero/lib/index.js b/is-negative-zero/lib/index.js new file mode 100644 index 00000000..49b44034 --- /dev/null +++ b/is-negative-zero/lib/index.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is equal to negative zero. +* +* @module @stdlib/assert/is-negative-zero +* +* @example +* var isNegativeZero = require( '@stdlib/assert/is-negative-zero' ); +* +* var bool = isNegativeZero( -0.0 ); +* // returns true +* +* bool = isNegativeZero( new Number( -0.0 ) ); +* // returns true +* +* bool = isNegativeZero( -3.14 ); +* // returns false +* +* bool = isNegativeZero( 5.0 ); +* // returns false +* +* bool = isNegativeZero( 0.0 ); +* // returns false +* +* bool = isNegativeZero( null ); +* // returns false +* +* @example +* var isNegativeZero = require( '@stdlib/assert/is-negative-zero' ).isPrimitive; +* +* var bool = isNegativeZero( -0.0 ); +* // returns true +* +* bool = isNegativeZero( new Number( -0.0 ) ); +* // returns false +* +* @example +* var isNegativeZero = require( '@stdlib/assert/is-negative-zero' ).isObject; +* +* var bool = isNegativeZero( -0.0 ); +* // returns false +* +* bool = isNegativeZero( new Number( -0.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isNegativeZero = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isNegativeZero, 'isPrimitive', isPrimitive ); +setReadOnly( isNegativeZero, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isNegativeZero; diff --git a/is-negative-zero/lib/main.js b/is-negative-zero/lib/main.js new file mode 100644 index 00000000..0adb705f --- /dev/null +++ b/is-negative-zero/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is equal to negative zero. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is equal to negative zero +* +* @example +* var bool = isNegativeZero( -0.0 ); +* // returns true +* +* @example +* var bool = isNegativeZero( new Number( -0.0 ) ); +* // returns true +* +* @example +* var bool = isNegativeZero( -3.14 ); +* // returns false +* +* @example +* var bool = isNegativeZero( 5.0 ); +* // returns false +* +* @example +* var bool = isNegativeZero( 0.0 ); +* // returns false +* +* @example +* var bool = isNegativeZero( null ); +* // returns false +*/ +function isNegativeZero( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isNegativeZero; diff --git a/is-negative-zero/lib/object.js b/is-negative-zero/lib/object.js new file mode 100644 index 00000000..7a44e6fe --- /dev/null +++ b/is-negative-zero/lib/object.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; +var NINF = require( '@stdlib/constants/math/float64-ninf' ); + + +// MAIN // + +/** +* Tests if a value is a number object having a value equal to negative zero. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a value equal to negative zero +* +* @example +* var bool = isNegativeZero( -0.0 ); +* // returns false +* +* @example +* var bool = isNegativeZero( new Number( -0.0 ) ); +* // returns true +*/ +function isNegativeZero( value ) { + return ( + isNumber( value ) && + ( 1.0 / value.valueOf() ) === NINF + ); +} + + +// EXPORTS // + +module.exports = isNegativeZero; diff --git a/is-negative-zero/lib/primitive.js b/is-negative-zero/lib/primitive.js new file mode 100644 index 00000000..f2d65ea8 --- /dev/null +++ b/is-negative-zero/lib/primitive.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var NINF = require( '@stdlib/constants/math/float64-ninf' ); + + +// MAIN // + +/** +* Tests if a value is a number primitive equal to negative zero. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive equal to negative zero +* +* @example +* var bool = isNegativeZero( -0.0 ); +* // returns true +* +* @example +* var bool = isNegativeZero( new Number( -0.0 ) ); +* // returns false +*/ +function isNegativeZero( value ) { + return ( + isNumber( value ) && + ( 1.0 / value ) === NINF + ); +} + + +// EXPORTS // + +module.exports = isNegativeZero; diff --git a/is-negative-zero/package.json b/is-negative-zero/package.json new file mode 100644 index 00000000..be5f59e4 --- /dev/null +++ b/is-negative-zero/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-negative-zero", + "version": "0.0.0", + "description": "Test if a value is equal to negative zero.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "negative", + "zero", + "is", + "isnumber", + "isnumeric", + "type", + "check", + "primitive", + "object", + "validate", + "valid", + "test" + ] +} diff --git a/is-negative-zero/test/test.js b/is-negative-zero/test/test.js new file mode 100644 index 00000000..de359384 --- /dev/null +++ b/is-negative-zero/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNegativeZero = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNegativeZero, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number equal to negative zero', function test( t ) { + t.strictEqual( typeof isNegativeZero.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a value equal to negative zero', function test( t ) { + t.strictEqual( typeof isNegativeZero.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-negative-zero/test/test.main.js b/is-negative-zero/test/test.main.js new file mode 100644 index 00000000..f3da66a4 --- /dev/null +++ b/is-negative-zero/test/test.main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNegativeZero = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNegativeZero, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number equal to negative zero', function test( t ) { + t.strictEqual( isNegativeZero( -0.0 ), true, 'returns true' ); + t.strictEqual( isNegativeZero( new Number( -0.0 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number equal to negative zero', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + 1.0, + 0.0, + -3.14, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNegativeZero( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-negative-zero/test/test.object.js b/is-negative-zero/test/test.object.js new file mode 100644 index 00000000..5af01227 --- /dev/null +++ b/is-negative-zero/test/test.object.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNegativeZero = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNegativeZero, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a value equal to negative zero', function test( t ) { + t.strictEqual( isNegativeZero( new Number( -0.0 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is equal to negative zero', function test( t ) { + t.strictEqual( isNegativeZero( -0.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided negative zero', function test( t ) { + var values; + var i; + + values = [ + '5', + new Number( 0.0 ), + new Number( 2.0 ), + new Number( -2.0 ), + 3.14, + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNegativeZero( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-negative-zero/test/test.primitive.js b/is-negative-zero/test/test.primitive.js new file mode 100644 index 00000000..7818021a --- /dev/null +++ b/is-negative-zero/test/test.primitive.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNegativeZero = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNegativeZero, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number equal to negative zero', function test( t ) { + t.strictEqual( isNegativeZero( -0.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has a value equal to negative zero', function test( t ) { + t.strictEqual( isNegativeZero( new Number( -0.0 ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided negative zero', function test( t ) { + var values; + var i; + + values = [ + '5', + 0.0, + new Number( 2.0 ), + 3.14, + -1.0, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNegativeZero( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-node-builtin/README.md b/is-node-builtin/README.md new file mode 100644 index 00000000..5e3b6a3a --- /dev/null +++ b/is-node-builtin/README.md @@ -0,0 +1,137 @@ + + +# isNodeBuiltin + +> Test whether a string matches a Node.js built-in module name. + +
+ +## Usage + +```javascript +var isNodeBuiltin = require( '@stdlib/assert/is-node-builtin' ); +``` + +#### isNodeBuiltin( value ) + +Tests whether a string matches a Node.js built-in module name. + +```javascript +var bool = isNodeBuiltin( 'util' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- For non-string values, the function returns `false`. + +
+ + + +
+ +## Examples + + + +```javascript +var isNodeBuiltin = require( '@stdlib/assert/is-node-builtin' ); + +var out = isNodeBuiltin( 'fs' ); +// returns true + +out = isNodeBuiltin( 'child_process' ); +// returns true + +out = isNodeBuiltin( 'fs-extra' ); +// returns false + +out = isNodeBuiltin( 'moment' ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-node-builtin [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-node-builtin crypto +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n 'crypto\ncluster\nfs-extra' | is-node-builtin +true +true +false +``` + +
+ + + +
+ + + + + + diff --git a/is-node-builtin/benchmark/benchmark.js b/is-node-builtin/benchmark/benchmark.js new file mode 100644 index 00000000..e739850a --- /dev/null +++ b/is-node-builtin/benchmark/benchmark.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNodeBuiltin = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var str; + var i; + + values = [ + 'fs', + 'crypto', + 'util', + '@stdlib', + 'object-keys', + 'd3' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = values[ i % values.length ]; + bool = isNodeBuiltin( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-node-builtin/bin/cli b/is-node-builtin/bin/cli new file mode 100644 index 00000000..c6683e7b --- /dev/null +++ b/is-node-builtin/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isNodeBuiltin = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isNodeBuiltin( String( args[ 0 ] ) ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isNodeBuiltin( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-node-builtin/docs/repl.txt b/is-node-builtin/docs/repl.txt new file mode 100644 index 00000000..d26c7f1a --- /dev/null +++ b/is-node-builtin/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( str ) + Tests whether a string matches a Node.js built-in module name. + + Parameters + ---------- + str: string + Input string. + + Returns + ------- + bool: boolean + Boolean indicating whether a string matches a Node.js built-in module + name. + + Examples + -------- + > var bool = {{alias}}( 'cluster' ) + true + > bool = {{alias}}( 'crypto' ) + true + > bool = {{alias}}( 'fs-extra' ) + false + > bool = {{alias}}( '' ) + false + + See Also + -------- + diff --git a/is-node-builtin/docs/types/index.d.ts b/is-node-builtin/docs/types/index.d.ts new file mode 100644 index 00000000..e05f9314 --- /dev/null +++ b/is-node-builtin/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests whether a string matches a Node.js built-in module name. +* +* @param x - value to test +* @returns boolean indicating whether a string matches a Node.js built-in module name +* +* @example +* var out = isNodeBuiltin( 'cluster' ); +* // returns true +* +* @example +* var out = isNodeBuiltin( 'crypto' ); +* // returns true +* +* @example +* var out = isNodeBuiltin( 'fs-extra' ); +* // returns false +* +* @example +* var out = isNodeBuiltin( '' ); +* // returns false +*/ +declare function isNodeBuiltin( x: any ): boolean; + + +// EXPORTS // + +export = isNodeBuiltin; diff --git a/is-node-builtin/docs/types/test.ts b/is-node-builtin/docs/types/test.ts new file mode 100644 index 00000000..b5d5808e --- /dev/null +++ b/is-node-builtin/docs/types/test.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNodeBuiltin = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNodeBuiltin( 'cluster' ); // $ExpectType boolean + isNodeBuiltin( 'fs-extra' ); // $ExpectType boolean + isNodeBuiltin( null ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNodeBuiltin(); // $ExpectError + isNodeBuiltin( null, 123 ); // $ExpectError +} diff --git a/is-node-builtin/docs/usage.txt b/is-node-builtin/docs/usage.txt new file mode 100644 index 00000000..5ca60869 --- /dev/null +++ b/is-node-builtin/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-node-builtin [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-node-builtin/etc/cli_opts.json b/is-node-builtin/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-node-builtin/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-node-builtin/examples/index.js b/is-node-builtin/examples/index.js new file mode 100644 index 00000000..44811e72 --- /dev/null +++ b/is-node-builtin/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isNodeBuiltin = require( './../lib' ); + +console.log( isNodeBuiltin( 'fs' ) ); +// => true + +console.log( isNodeBuiltin( 'child_process' ) ); +// => true + +console.log( isNodeBuiltin( 'fs-extra' ) ); +// => false + +console.log( isNodeBuiltin( 'moment' ) ); +// => false diff --git a/is-node-builtin/lib/builtins.json b/is-node-builtin/lib/builtins.json new file mode 100644 index 00000000..329db584 --- /dev/null +++ b/is-node-builtin/lib/builtins.json @@ -0,0 +1,68 @@ +[ + "assert", + "async_hooks", + "buffer", + "child_process", + "cluster", + "console", + "constants", + "crypto", + "dgram", + "dns", + "domain", + "events", + "fs", + "http", + "http2", + "https", + "_http_agent", + "_http_client", + "_http_common", + "_http_incoming", + "_http_outgoing", + "_http_server", + "inspector", + "module", + "net", + "os", + "path", + "perf_hooks", + "process", + "punycode", + "querystring", + "readline", + "repl", + "stream", + "_stream_readable", + "_stream_writable", + "_stream_duplex", + "_stream_transform", + "_stream_passthrough", + "_stream_wrap", + "string_decoder", + "sys", + "timers", + "tls", + "_tls_common", + "_tls_legacy", + "_tls_wrap", + "tty", + "url", + "util", + "v8", + "vm", + "zlib", + "v8/tools/splaytree", + "v8/tools/codemap", + "v8/tools/consarray", + "v8/tools/csvparser", + "v8/tools/profile", + "v8/tools/profile_view", + "v8/tools/logreader", + "v8/tools/tickprocessor", + "v8/tools/SourceMap", + "v8/tools/tickprocessor-driver", + "node-inspect/lib/_inspect", + "node-inspect/lib/internal/inspect_client", + "node-inspect/lib/internal/inspect_repl" +] diff --git a/is-node-builtin/lib/index.js b/is-node-builtin/lib/index.js new file mode 100644 index 00000000..6ebba908 --- /dev/null +++ b/is-node-builtin/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether a string matches a Node.js built-in module name. +* +* @module @stdlib/assert/is-node-builtin +* +* @example +* var isNodeBuiltin = require( '@stdlib/assert/is-node-builtin' ); +* +* var out = isNodeBuiltin( 'crypto' ); +* // returns true +* +* out = isNodeBuiltin( 'cluster' ); +* // returns true +* +* out = isNodeBuiltin( '' ); +* // returns false +*/ + +// MODULES // + +var isNodeBuiltin = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNodeBuiltin; diff --git a/is-node-builtin/lib/main.js b/is-node-builtin/lib/main.js new file mode 100644 index 00000000..888836e3 --- /dev/null +++ b/is-node-builtin/lib/main.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var mod = require( 'module' ); +var contains = require( '@stdlib/assert/contains' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var BUILTINS = require( './builtins.json' ); + + +// VARIABLES // + +var LIST = mod.builtinModules || BUILTINS; + + +// MAIN // + +/** +* Tests whether a string matches a Node.js built-in module name. +* +* @param {*} x - value to test +* @returns {boolean} boolean indicating whether a string matches a Node.js built-in module name +* +* @example +* var out = isNodeBuiltin( 'cluster' ); +* // returns true +* +* @example +* var out = isNodeBuiltin( 'crypto' ); +* // returns true +* +* @example +* var out = isNodeBuiltin( 'fs-extra' ); +* // returns false +* +* @example +* var out = isNodeBuiltin( '' ); +* // returns false +*/ +function isNodeBuiltin( x ) { + if ( !isString( x ) ) { + return false; + } + return contains( LIST, x ); +} + + +// EXPORTS // + +module.exports = isNodeBuiltin; diff --git a/is-node-builtin/package.json b/is-node-builtin/package.json new file mode 100644 index 00000000..f6590519 --- /dev/null +++ b/is-node-builtin/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-node-builtin", + "version": "0.0.0", + "description": "Test whether a string matches a Node.js built-in module name.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-node-builtin": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "string", + "str", + "is", + "isnodebuiltin", + "isbuiltin", + "builtin", + "built-in", + "validate", + "valid", + "isvalid", + "test", + "check" + ] +} diff --git a/is-node-builtin/test/fixtures/stdin_error.js.txt b/is-node-builtin/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-node-builtin/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-node-builtin/test/test.cli.js b/is-node-builtin/test/test.cli.js new file mode 100644 index 00000000..ae8502a2 --- /dev/null +++ b/is-node-builtin/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string matches the name of a built-in Node.js module', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'fs\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "crypto\nfs-extra"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\nfalse\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-node-builtin/test/test.js b/is-node-builtin/test/test.js new file mode 100644 index 00000000..b0d4a94f --- /dev/null +++ b/is-node-builtin/test/test.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNodeBuiltin = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNodeBuiltin, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a string primitive matching a Node.js built-in module name', function test( t ) { + t.strictEqual( isNodeBuiltin( 'fs' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a string primitive matching a Node.js built-in module name', function test( t ) { + var values; + var i; + + values = [ + 'fs-extra', + 'd3', + '@stdlib', + new String( 'fs' ), // eslint-disable-line no-new-wrappers + 5, + null, + NaN, + undefined, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNodeBuiltin( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-node-duplex-stream-like/README.md b/is-node-duplex-stream-like/README.md new file mode 100644 index 00000000..6257eda2 --- /dev/null +++ b/is-node-duplex-stream-like/README.md @@ -0,0 +1,113 @@ + + +# Duplex Stream-like + +> Test if a value is Node [duplex stream][nodejs-stream]-like. + +
+ +## Usage + +```javascript +var isNodeDuplexStreamLike = require( '@stdlib/assert/is-node-duplex-stream-like' ); +``` + +#### isNodeDuplexStreamLike( value ) + +Tests if a `value` is Node [duplex stream][nodejs-stream]-like. + +```javascript +var transformStream = require( '@stdlib/streams/node/transform' ); + +var bool = isNodeDuplexStreamLike( transformStream() ); +// returns true +``` + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var stream = require( 'stream' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeDuplexStreamLike = require( '@stdlib/assert/is-node-duplex-stream-like' ); + +var bool = isNodeDuplexStreamLike( new stream.Duplex() ); +// returns true + +bool = isNodeDuplexStreamLike( new stream.Transform() ); +// returns true + +bool = isNodeDuplexStreamLike( transformStream() ); +// returns true + +bool = isNodeDuplexStreamLike( new stream.Writable() ); +// returns false + +bool = isNodeDuplexStreamLike( new stream.Readable() ); +// returns false + +bool = isNodeDuplexStreamLike( new stream.Stream() ); +// returns false + +bool = isNodeDuplexStreamLike( {} ); +// returns false + +bool = isNodeDuplexStreamLike( [] ); +// returns false + +bool = isNodeDuplexStreamLike( null ); +// returns false + +function Stream() { + return this; +} + +bool = isNodeDuplexStreamLike( Stream ); +// returns false + +bool = isNodeDuplexStreamLike( new Stream() ); +// returns false +``` + +
+ + + + + + diff --git a/is-node-duplex-stream-like/benchmark/benchmark.js b/is-node-duplex-stream-like/benchmark/benchmark.js new file mode 100644 index 00000000..8c9a37a8 --- /dev/null +++ b/is-node-duplex-stream-like/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNodeDuplexStreamLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function stream() {}, + transformStream(), + transformStream(), + transformStream(), + transformStream(), + transformStream() + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNodeDuplexStreamLike( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-node-duplex-stream-like/docs/repl.txt b/is-node-duplex-stream-like/docs/repl.txt new file mode 100644 index 00000000..bb011b42 --- /dev/null +++ b/is-node-duplex-stream-like/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( value ) + Tests if a value is Node duplex stream-like. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is Node duplex stream-like. + + Examples + -------- + > var Stream = require( 'stream' ).Duplex; + > s = new Stream(); + > var bool = {{alias}}( s ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-node-duplex-stream-like/docs/types/index.d.ts b/is-node-duplex-stream-like/docs/types/index.d.ts new file mode 100644 index 00000000..4608cfa1 --- /dev/null +++ b/is-node-duplex-stream-like/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is Node duplex stream-like. +* +* @param value - value to test +* @returns boolean indicating if a value is Node duplex stream-like +* +* @example +* var transformStream = require( `@stdlib/streams/node/transform` ); +* +* var stream = transformStream(); +* +* var bool = isNodeDuplexStreamLike( stream ); +* // returns true +* +* bool = isNodeDuplexStreamLike( {} ); +* // returns false +*/ +declare function isNodeDuplexStreamLike( value: any ): boolean; + + +// EXPORTS // + +export = isNodeDuplexStreamLike; diff --git a/is-node-duplex-stream-like/docs/types/test.ts b/is-node-duplex-stream-like/docs/types/test.ts new file mode 100644 index 00000000..d45abfc1 --- /dev/null +++ b/is-node-duplex-stream-like/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNodeDuplexStreamLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNodeDuplexStreamLike( [ {}, {} ] ); // $ExpectType boolean + isNodeDuplexStreamLike( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNodeDuplexStreamLike(); // $ExpectError + isNodeDuplexStreamLike( {}, 123 ); // $ExpectError +} diff --git a/is-node-duplex-stream-like/examples/index.js b/is-node-duplex-stream-like/examples/index.js new file mode 100644 index 00000000..1b0506b4 --- /dev/null +++ b/is-node-duplex-stream-like/examples/index.js @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var stream = require( 'stream' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeDuplexStreamLike = require( './../lib' ); + +var bool = isNodeDuplexStreamLike( new stream.Duplex() ); +console.log( bool ); +// => true + +bool = isNodeDuplexStreamLike( new stream.Transform() ); +console.log( bool ); +// => true + +bool = isNodeDuplexStreamLike( transformStream() ); +console.log( bool ); +// => true + +bool = isNodeDuplexStreamLike( new stream.Writable() ); +console.log( bool ); +// => false + +bool = isNodeDuplexStreamLike( new stream.Readable() ); +console.log( bool ); +// => false + +bool = isNodeDuplexStreamLike( new stream.Stream() ); +console.log( bool ); +// => false + +bool = isNodeDuplexStreamLike( {} ); +console.log( bool ); +// => false + +bool = isNodeDuplexStreamLike( [] ); +console.log( bool ); +// => false + +bool = isNodeDuplexStreamLike( null ); +console.log( bool ); +// => false + +function Stream() { + return this; +} + +bool = isNodeDuplexStreamLike( Stream ); +console.log( bool ); +// => false + +bool = isNodeDuplexStreamLike( new Stream() ); +console.log( bool ); +// => false diff --git a/is-node-duplex-stream-like/lib/index.js b/is-node-duplex-stream-like/lib/index.js new file mode 100644 index 00000000..7638faa6 --- /dev/null +++ b/is-node-duplex-stream-like/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is Node duplex stream-like. +* +* @module @stdlib/assert/is-node-duplex-stream-like +* +* @example +* var transformStream = require( '@stdlib/streams/node/transform' ); +* var isNodeDuplexStreamLike = require( '@stdlib/assert/is-node-duplex-stream-like' ); +* +* var stream = transformStream(); +* +* var bool = isNodeDuplexStreamLike( stream ); +* // returns true +* +* bool = isNodeDuplexStreamLike( {} ); +* // returns false +*/ + +// MODULES // + +var isNodeDuplexStreamLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNodeDuplexStreamLike; diff --git a/is-node-duplex-stream-like/lib/main.js b/is-node-duplex-stream-like/lib/main.js new file mode 100644 index 00000000..35b4d248 --- /dev/null +++ b/is-node-duplex-stream-like/lib/main.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNodeWritableStreamLike = require( '@stdlib/assert/is-node-writable-stream-like' ); +var isNodeReadableStreamLike = require( '@stdlib/assert/is-node-readable-stream-like' ); + + +// MAIN // + +/** +* Tests if a value is Node duplex stream-like. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is Node duplex stream-like +* +* @example +* var transformStream = require( '@stdlib/streams/node/transform' ); +* +* var stream = transformStream(); +* +* var bool = isNodeDuplexStreamLike( stream ); +* // returns true +* +* bool = isNodeDuplexStreamLike( {} ); +* // returns false +*/ +function isNodeDuplexStreamLike( value ) { + return ( + // Must be both readable and writable: + isNodeWritableStreamLike( value ) && + isNodeReadableStreamLike( value ) + ); +} + + +// EXPORTS // + +module.exports = isNodeDuplexStreamLike; diff --git a/is-node-duplex-stream-like/package.json b/is-node-duplex-stream-like/package.json new file mode 100644 index 00000000..4971327f --- /dev/null +++ b/is-node-duplex-stream-like/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-node-duplex-stream-like", + "version": "0.0.0", + "description": "Test if a value is Node duplex stream-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "stream", + "readable", + "writable", + "duplex", + "node", + "nodejs", + "is", + "isstream", + "isreadable", + "iswritable", + "isduplex", + "test", + "check", + "validate", + "verify", + "isvalid", + "valid" + ] +} diff --git a/is-node-duplex-stream-like/test/test.js b/is-node-duplex-stream-like/test/test.js new file mode 100644 index 00000000..c26b24f2 --- /dev/null +++ b/is-node-duplex-stream-like/test/test.js @@ -0,0 +1,102 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var stream = require( 'stream' ); +var EventEmitter = require( 'events' ).EventEmitter; +var tape = require( 'tape' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeDuplexStreamLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNodeDuplexStreamLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a value which is Node duplex stream-like', function test( t ) { + var values; + var s; + var i; + + function pipe() { + // pipe function + } + + function read() { + // read function + } + + function write() { + // write function + } + + s = new EventEmitter(); + s.pipe = pipe; + s._read = read; + s._write = write; + s._readableState = {}; + s._writableState = {}; + + values = [ + new stream.Transform(), + new stream.Duplex(), + transformStream(), + s + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNodeDuplexStreamLike( values[ i ] ), true, 'returns true for value '+i ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a value which is Node duplex stream-like', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined, + [], + {}, + function pipe() {}, + new EventEmitter(), + new stream.Writable(), + new stream.Readable(), + new stream.Stream() + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNodeDuplexStreamLike( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); diff --git a/is-node-readable-stream-like/README.md b/is-node-readable-stream-like/README.md new file mode 100644 index 00000000..80ff817f --- /dev/null +++ b/is-node-readable-stream-like/README.md @@ -0,0 +1,113 @@ + + +# Readable Stream-like + +> Test if a value is Node [readable stream][nodejs-stream]-like. + +
+ +## Usage + +```javascript +var isNodeReadableStreamLike = require( '@stdlib/assert/is-node-readable-stream-like' ); +``` + +#### isNodeReadableStreamLike( value ) + +Tests if a `value` is Node [readable stream][nodejs-stream]-like. + +```javascript +var transformStream = require( '@stdlib/streams/node/transform' ); + +var bool = isNodeReadableStreamLike( transformStream() ); +// returns true +``` + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var stream = require( 'stream' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeReadableStreamLike = require( '@stdlib/assert/is-node-readable-stream-like' ); + +var bool = isNodeReadableStreamLike( new stream.Readable() ); +// returns true + +bool = isNodeReadableStreamLike( new stream.Duplex() ); +// returns true + +bool = isNodeReadableStreamLike( new stream.Transform() ); +// returns true + +bool = isNodeReadableStreamLike( transformStream() ); +// returns true + +bool = isNodeReadableStreamLike( new stream.Writable() ); +// returns false + +bool = isNodeReadableStreamLike( new stream.Stream() ); +// returns false + +bool = isNodeReadableStreamLike( {} ); +// returns false + +bool = isNodeReadableStreamLike( [] ); +// returns false + +bool = isNodeReadableStreamLike( null ); +// returns false + +function Stream() { + return this; +} + +bool = isNodeReadableStreamLike( Stream ); +// returns false + +bool = isNodeReadableStreamLike( new Stream() ); +// returns false +``` + +
+ + + + + + diff --git a/is-node-readable-stream-like/benchmark/benchmark.js b/is-node-readable-stream-like/benchmark/benchmark.js new file mode 100644 index 00000000..cfc2651e --- /dev/null +++ b/is-node-readable-stream-like/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNodeReadableStreamLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function stream() {}, + transformStream(), + transformStream(), + transformStream(), + transformStream(), + transformStream() + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNodeReadableStreamLike( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-node-readable-stream-like/docs/repl.txt b/is-node-readable-stream-like/docs/repl.txt new file mode 100644 index 00000000..dc6e68bc --- /dev/null +++ b/is-node-readable-stream-like/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( value ) + Tests if a value is Node readable stream-like. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is Node readable stream-like. + + Examples + -------- + > var Stream = require( 'stream' ).Readable; + > s = new Stream(); + > var bool = {{alias}}( s ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-node-readable-stream-like/docs/types/index.d.ts b/is-node-readable-stream-like/docs/types/index.d.ts new file mode 100644 index 00000000..6612e9f3 --- /dev/null +++ b/is-node-readable-stream-like/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is Node readable stream-like. +* +* @param value - value to test +* @returns boolean indicating if a value is Node readable stream-like +* +* @example +* var transformStream = require( `@stdlib/streams/node/transform` ); +* +* var stream = transformStream(); +* +* var bool = isNodeReadableStreamLike( stream ); +* // returns true +* +* bool = isNodeReadableStreamLike( {} ); +* // returns false +*/ +declare function isNodeReadableStreamLike( value: any ): boolean; + + +// EXPORTS // + +export = isNodeReadableStreamLike; diff --git a/is-node-readable-stream-like/docs/types/test.ts b/is-node-readable-stream-like/docs/types/test.ts new file mode 100644 index 00000000..2b972856 --- /dev/null +++ b/is-node-readable-stream-like/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNodeReadableStreamLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNodeReadableStreamLike( [ {}, {} ] ); // $ExpectType boolean + isNodeReadableStreamLike( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNodeReadableStreamLike(); // $ExpectError + isNodeReadableStreamLike( {}, 123 ); // $ExpectError +} diff --git a/is-node-readable-stream-like/examples/index.js b/is-node-readable-stream-like/examples/index.js new file mode 100644 index 00000000..70e04108 --- /dev/null +++ b/is-node-readable-stream-like/examples/index.js @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var stream = require( 'stream' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeReadableStreamLike = require( './../lib' ); + +var bool = isNodeReadableStreamLike( new stream.Readable() ); +console.log( bool ); +// => true + +bool = isNodeReadableStreamLike( new stream.Duplex() ); +console.log( bool ); +// => true + +bool = isNodeReadableStreamLike( new stream.Transform() ); +console.log( bool ); +// => true + +bool = isNodeReadableStreamLike( transformStream() ); +console.log( bool ); +// => true + +bool = isNodeReadableStreamLike( new stream.Writable() ); +console.log( bool ); +// => false + +bool = isNodeReadableStreamLike( new stream.Stream() ); +console.log( bool ); +// => false + +bool = isNodeReadableStreamLike( {} ); +console.log( bool ); +// => false + +bool = isNodeReadableStreamLike( [] ); +console.log( bool ); +// => false + +bool = isNodeReadableStreamLike( null ); +console.log( bool ); +// => false + +function Stream() { + return this; +} + +bool = isNodeReadableStreamLike( Stream ); +console.log( bool ); +// => false + +bool = isNodeReadableStreamLike( new Stream() ); +console.log( bool ); +// => false diff --git a/is-node-readable-stream-like/lib/index.js b/is-node-readable-stream-like/lib/index.js new file mode 100644 index 00000000..b1fa9b3f --- /dev/null +++ b/is-node-readable-stream-like/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is Node readable stream-like. +* +* @module @stdlib/assert/is-node-readable-stream-like +* +* @example +* var transformStream = require( '@stdlib/streams/node/transform' ); +* var isNodeReadableStreamLike = require( '@stdlib/assert/is-node-readable-stream-like' ); +* +* var stream = transformStream(); +* +* var bool = isNodeReadableStreamLike( stream ); +* // returns true +* +* bool = isNodeReadableStreamLike( {} ); +* // returns false +*/ + +// MODULES // + +var isNodeReadableStreamLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNodeReadableStreamLike; diff --git a/is-node-readable-stream-like/lib/main.js b/is-node-readable-stream-like/lib/main.js new file mode 100644 index 00000000..cba328e3 --- /dev/null +++ b/is-node-readable-stream-like/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var isNodeStreamLike = require( '@stdlib/assert/is-node-stream-like' ); + + +// MAIN // + +/** +* Tests if a value is Node readable stream-like. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is Node readable stream-like +* +* @example +* var transformStream = require( '@stdlib/streams/node/transform' ); +* +* var stream = transformStream(); +* +* var bool = isNodeReadableStreamLike( stream ); +* // returns true +* +* bool = isNodeReadableStreamLike( {} ); +* // returns false +*/ +function isNodeReadableStreamLike( value ) { + return ( + // Must be stream-like: + isNodeStreamLike( value ) && + + // Should have readable stream methods: + typeof value._read === 'function' && + + // Should have readable stream state: + typeof value._readableState === 'object' + ); +} + + +// EXPORTS // + +module.exports = isNodeReadableStreamLike; diff --git a/is-node-readable-stream-like/package.json b/is-node-readable-stream-like/package.json new file mode 100644 index 00000000..c5bee5f5 --- /dev/null +++ b/is-node-readable-stream-like/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-node-readable-stream-like", + "version": "0.0.0", + "description": "Test if a value is Node readable stream-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "stream", + "readable", + "node", + "nodejs", + "is", + "isstream", + "isreadable", + "test", + "check", + "validate", + "verify", + "isvalid", + "valid" + ] +} diff --git a/is-node-readable-stream-like/test/test.js b/is-node-readable-stream-like/test/test.js new file mode 100644 index 00000000..febdd010 --- /dev/null +++ b/is-node-readable-stream-like/test/test.js @@ -0,0 +1,95 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var stream = require( 'stream' ); +var EventEmitter = require( 'events' ).EventEmitter; +var tape = require( 'tape' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeReadableStreamLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNodeReadableStreamLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a value which is Node readable stream-like', function test( t ) { + var values; + var s; + var i; + + function pipe() { + // pipe function + } + + function read() { + // read function + } + + s = new EventEmitter(); + s.pipe = pipe; + s._read = read; + s._readableState = {}; + + values = [ + new stream.Transform(), + new stream.Readable(), + transformStream(), + s + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNodeReadableStreamLike( values[ i ] ), true, 'returns true for value '+i ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a value which is Node readable stream-like', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined, + [], + {}, + function pipe() {}, + new EventEmitter(), + new stream.Writable(), + new stream.Stream() + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNodeReadableStreamLike( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); diff --git a/is-node-repl/README.md b/is-node-repl/README.md new file mode 100644 index 00000000..7948c474 --- /dev/null +++ b/is-node-repl/README.md @@ -0,0 +1,69 @@ + + +# isNodeREPL + +> Check if running in a [Node.js][node-js] REPL environment. + +
+ +## Usage + +```javascript +var isNodeREPL = require( '@stdlib/assert/is-node-repl' ); +``` + +#### isNodeREPL() + +Returns a `boolean` indicating if running in a [Node.js][node-js] REPL environment. + +```javascript +var bool = isNodeREPL(); +// returns +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isNodeREPL = require( '@stdlib/assert/is-node-repl' ); + +console.log( isNodeREPL() ); +// => +``` + +
+ + + + + + diff --git a/is-node-repl/docs/repl.txt b/is-node-repl/docs/repl.txt new file mode 100644 index 00000000..4ac9ef9f --- /dev/null +++ b/is-node-repl/docs/repl.txt @@ -0,0 +1,17 @@ + +{{alias}}() + Returns a boolean indicating if running in a Node.js REPL environment. + + Returns + ------- + bool: boolean + Boolean indicating if running in a Node.js REPL environment. + + Examples + -------- + > var bool = {{alias}}() + + + See Also + -------- + diff --git a/is-node-repl/docs/types/index.d.ts b/is-node-repl/docs/types/index.d.ts new file mode 100644 index 00000000..56634cd3 --- /dev/null +++ b/is-node-repl/docs/types/index.d.ts @@ -0,0 +1,41 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Returns a boolean indicating if the function is called from a Node.js REPL environment. +* +* ## Notes +* +* - False positives are possible due to the existence of a userland package having the same module `id` (see [repl][repl-template-lib]) as the builtin Node.js `repl`. +* +* [repl-template-lib]: https://www.npmjs.com/package/repl +* +* @returns boolean indicating if the function is called from a Node.js REPL environment +* +* @example +* var bool = isNodeREPL(); +* // returns +*/ +declare function isNodeREPL(): boolean; + + +// EXPORTS // + +export = isNodeREPL; diff --git a/is-node-repl/docs/types/test.ts b/is-node-repl/docs/types/test.ts new file mode 100644 index 00000000..12512fb0 --- /dev/null +++ b/is-node-repl/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNodeREPL = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNodeREPL(); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided arguments... +{ + isNodeREPL( true ); // $ExpectError + isNodeREPL( [], 123 ); // $ExpectError +} diff --git a/is-node-repl/examples/index.js b/is-node-repl/examples/index.js new file mode 100644 index 00000000..657858ef --- /dev/null +++ b/is-node-repl/examples/index.js @@ -0,0 +1,24 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isNodeREPL = require( './../lib' ); + +console.log( isNodeREPL() ); +// => diff --git a/is-node-repl/lib/browser.js b/is-node-repl/lib/browser.js new file mode 100644 index 00000000..56153864 --- /dev/null +++ b/is-node-repl/lib/browser.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var constantFcn = require( '@stdlib/utils/constant-function' ); + + +// MAIN // + +var f = constantFcn( false ); + + +// EXPORTS // + +module.exports = f; diff --git a/is-node-repl/lib/index.js b/is-node-repl/lib/index.js new file mode 100644 index 00000000..d59093d0 --- /dev/null +++ b/is-node-repl/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Check if running within a Node.js REPL environment. +* +* @module @stdlib/assert/is-node-repl +* +* @example +* var isNodeREPL = require( '@stdlib/assert/is-node-repl' ); +* +* var bool = isNodeREPL(); +* // returns +*/ + +// MODULES // + +var isNodeREPL = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNodeREPL; diff --git a/is-node-repl/lib/main.js b/is-node-repl/lib/main.js new file mode 100644 index 00000000..3f035345 --- /dev/null +++ b/is-node-repl/lib/main.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var IS_NODE = require( '@stdlib/assert/is-node' ); +var p = require( './parent.js' ); +var stacktrace = require( './stacktrace.js' ); + + +// VARIABLES // + +// The module id for the builtin REPL was `repl` in Node versions <4 and then changed to `` for Node versions >=4 (see https://github.com/nodejs/node/commit/ee72ee753118f2576bfd1ccf58fb2ff651e8bfb0#diff-b13d72249263845d8e8341db0426f9d3R527). +var RE_MODULE_ID = /^repl$|^$/; + +// This is V8 specific (!!), as this assumes the V8 stacktrace API. TODO: rely on an engine agnostic approach to determining callsites. +var RE_ERROR_STACK = /at REPLServer/; + + +// MAIN // + +/** +* Returns a boolean indicating if the function is called from a Node.js REPL environment. +* +* ## Notes +* +* - False positives are possible due to the existence of a userland package having the same module `id` (see [repl][repl-template-lib]) as the builtin Node.js `repl`. +* +* [repl-template-lib]: https://www.npmjs.com/package/repl +* +* @returns {boolean} boolean indicating if the function is called from a Node.js REPL environment +* +* @example +* var bool = isNodeREPL(); +* // returns +*/ +function isNodeREPL() { + var stack; + var m; + if ( !IS_NODE ) { + return false; + } + // If this module was required in a REPL environment, we can walk up the module dependency tree to find a `repl` ancestor... + m = p(); + while ( m ) { + if ( RE_MODULE_ID.test( m.id ) ) { + return true; + } + m = m.parent; + } + // Unable to find a `repl` ancestor, so try determining if this function was called from a REPL environment... + stack = stacktrace(); + if ( stack ) { + return RE_ERROR_STACK.test( stack ); + } + return false; +} + + +// EXPORTS // + +module.exports = isNodeREPL; diff --git a/is-node-repl/lib/parent.js b/is-node-repl/lib/parent.js new file mode 100644 index 00000000..79931b78 --- /dev/null +++ b/is-node-repl/lib/parent.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Returns the parent module. +* +* @private +* @returns {Module} parent module +*/ +function parent() { // eslint-disable-line stdlib/no-redeclare + return module; +} + + +// EXPORTS // + +module.exports = parent; diff --git a/is-node-repl/lib/stacktrace.js b/is-node-repl/lib/stacktrace.js new file mode 100644 index 00000000..5d333909 --- /dev/null +++ b/is-node-repl/lib/stacktrace.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Returns a stacktrace. +* +* @private +* @returns {(string|void)} stacktrace +*/ +function stacktrace() { + var err = new Error(); + return err.stack; +} + + +// EXPORTS // + +module.exports = stacktrace; diff --git a/is-node-repl/package.json b/is-node-repl/package.json new file mode 100644 index 00000000..8ee4e87f --- /dev/null +++ b/is-node-repl/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/assert/is-node-repl", + "version": "0.0.0", + "description": "Check if running in a Node.js REPL environment.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "browser": "./lib/browser.js", + "directories": { + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "runtime", + "environment", + "env", + "repl", + "node.js", + "detect" + ] +} diff --git a/is-node-repl/test/test.browser.js b/is-node-repl/test/test.browser.js new file mode 100644 index 00000000..4d36544f --- /dev/null +++ b/is-node-repl/test/test.browser.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNodeREPL = require( './../lib/browser.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNodeREPL, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function always returns `false`', function test( t ) { + t.strictEqual( isNodeREPL(), false, 'returns false' ); + t.strictEqual( isNodeREPL(), false, 'returns false' ); + t.strictEqual( isNodeREPL(), false, 'returns false' ); + t.strictEqual( isNodeREPL(), false, 'returns false' ); + t.strictEqual( isNodeREPL(), false, 'returns false' ); + t.strictEqual( isNodeREPL(), false, 'returns false' ); + t.strictEqual( isNodeREPL(), false, 'returns false' ); + t.strictEqual( isNodeREPL(), false, 'returns false' ); + t.end(); +}); diff --git a/is-node-repl/test/test.js b/is-node-repl/test/test.js new file mode 100644 index 00000000..963f6483 --- /dev/null +++ b/is-node-repl/test/test.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNodeREPL = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNodeREPL, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a boolean', function test( t ) { + t.strictEqual( typeof isNodeREPL(), 'boolean', 'returns a boolean' ); + t.end(); +}); diff --git a/is-node-repl/test/test.main.js b/is-node-repl/test/test.main.js new file mode 100644 index 00000000..32103acd --- /dev/null +++ b/is-node-repl/test/test.main.js @@ -0,0 +1,237 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var isNodeREPL = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNodeREPL, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if the runtime environment is not Node.js', function test( t ) { + var isNodeREPL = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-node': false + }); + + t.strictEqual( isNodeREPL(), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `true` if a parent module is the Node.js `repl` builtin (Node versions >=4; root)', function test( t ) { + var isNodeREPL = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-node': true, + './parent.js': mock + }); + + t.strictEqual( isNodeREPL(), true, 'returns true' ); + t.end(); + + function mock() { + return { + 'id': 'beep', + 'parent': { + 'id': 'boop', + 'parent': { + 'id': 'foo', + 'parent': { + 'id': 'bar', + 'parent': { + 'id': '', + 'parent': void 0 + } + } + } + } + }; + } +}); + +tape( 'the function returns `true` if a parent module is the Node.js `repl` builtin (Node versions <4; root)', function test( t ) { + var isNodeREPL = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-node': true, + './parent.js': mock + }); + + t.strictEqual( isNodeREPL(), true, 'returns true' ); + t.end(); + + function mock() { + return { + 'id': 'beep', + 'parent': { + 'id': 'boop', + 'parent': { + 'id': 'foo', + 'parent': { + 'id': 'bar', + 'parent': { + 'id': 'repl', + 'parent': void 0 + } + } + } + } + }; + } +}); + +tape( 'the function returns `true` if a parent module is the Node.js `repl` builtin (Node versions >=4; non-root)', function test( t ) { + var isNodeREPL = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-node': true, + './parent.js': mock + }); + + t.strictEqual( isNodeREPL(), true, 'returns true' ); + t.end(); + + function mock() { + return { + 'id': 'beep', + 'parent': { + 'id': 'boop', + 'parent': { + 'id': '', + 'parent': { + 'id': 'bar', + 'parent': { + 'id': 'foo', + 'parent': void 0 + } + } + } + } + }; + } +}); + +tape( 'the function returns `true` if a parent module is the Node.js `repl` builtin (Node versions <4; non-root)', function test( t ) { + var isNodeREPL = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-node': true, + './parent.js': mock + }); + + t.strictEqual( isNodeREPL(), true, 'returns true' ); + t.end(); + + function mock() { + return { + 'id': 'beep', + 'parent': { + 'id': 'boop', + 'parent': { + 'id': 'repl', + 'parent': { + 'id': 'bar', + 'parent': { + 'id': 'foo', + 'parent': void 0 + } + } + } + } + }; + } +}); + +tape( 'the function returns `false` if the function is not either required or called from a Node.js REPL (shallow require)', function test( t ) { + var isNodeREPL = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-node': true, + './parent.js': mock, + './stacktrace.js': stacktrace + }); + + t.strictEqual( isNodeREPL(), false, 'returns false' ); + t.end(); + + function mock() { + return { + 'id': 'beep', + 'parent': void 0 + }; + } + + function stacktrace() { + return ''; + } +}); + +tape( 'the function returns `false` if the function is not either required or called from a Node.js REPL (deep require)', function test( t ) { + var isNodeREPL = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-node': true, + './parent.js': mock, + './stacktrace.js': stacktrace + }); + + t.strictEqual( isNodeREPL(), false, 'returns false' ); + t.end(); + + function mock() { + return { + 'id': 'beep', + 'parent': { + 'id': 'boop', + 'parent': { + 'id': 'foo', + 'parent': { + 'id': 'bar', + 'parent': { + 'id': '.', + 'parent': void 0 + } + } + } + } + }; + } + + function stacktrace() { + return ''; + } +}); + +tape( 'the function returns `true` if the function is called from a Node.js REPL', function test( t ) { + var isNodeREPL = proxyquire( './../lib/main.js', { + '@stdlib/assert/is-node': true, + './parent.js': mock, + './stacktrace.js': stacktrace + }); + + t.strictEqual( isNodeREPL(), true, 'returns true' ); + t.end(); + + function mock() { + return { + 'id': 'beep', + 'parent': void 0 + }; + } + + function stacktrace() { + return 'Error\n at repl:1:2\n at ContextifyScript.Script.runInContext (vm.js:53:29)\n at REPLServer.defaultEval (repl.js:241:29)\n at bound (domain.js:301:14)\n at REPLServer.runBound [as eval] (domain.js:314:12)\n at REPLServer.onLine (repl.js:433:10)\n at emitOne (events.js:115:13)\n at REPLServer.emit (events.js:210:7)\n at REPLServer.Interface._onLine (readline.js:278:10)\n at REPLServer.Interface._line (readline.js:625:8)'; + } +}); diff --git a/is-node-stream-like/README.md b/is-node-stream-like/README.md new file mode 100644 index 00000000..20369cfd --- /dev/null +++ b/is-node-stream-like/README.md @@ -0,0 +1,113 @@ + + +# Stream-like + +> Test if a value is Node [stream][nodejs-stream]-like. + +
+ +## Usage + +```javascript +var isNodeStreamLike = require( '@stdlib/assert/is-node-stream-like' ); +``` + +#### isNodeStreamLike( value ) + +Tests if a `value` is Node [stream][nodejs-stream]-like. + +```javascript +var transformStream = require( '@stdlib/streams/node/transform' ); + +var bool = isNodeStreamLike( transformStream() ); +// returns true +``` + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var stream = require( 'stream' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeStreamLike = require( '@stdlib/assert/is-node-stream-like' ); + +var bool = isNodeStreamLike( new stream.Stream() ); +// returns true + +bool = isNodeStreamLike( new stream.Readable() ); +// returns true + +bool = isNodeStreamLike( new stream.Writable() ); +// returns true + +bool = isNodeStreamLike( new stream.Duplex() ); +// returns true + +bool = isNodeStreamLike( new stream.Transform() ); +// returns true + +bool = isNodeStreamLike( transformStream() ); +// returns true + +bool = isNodeStreamLike( {} ); +// returns false + +bool = isNodeStreamLike( [] ); +// returns false + +bool = isNodeStreamLike( null ); +// returns false + +function Stream() { + return this; +} + +bool = isNodeStreamLike( Stream ); +// returns false + +bool = isNodeStreamLike( new Stream() ); +// returns false +``` + +
+ + + + + + diff --git a/is-node-stream-like/benchmark/benchmark.js b/is-node-stream-like/benchmark/benchmark.js new file mode 100644 index 00000000..c50cb022 --- /dev/null +++ b/is-node-stream-like/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNodeStreamLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function stream() {}, + transformStream(), + transformStream(), + transformStream(), + transformStream(), + transformStream() + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNodeStreamLike( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-node-stream-like/docs/repl.txt b/is-node-stream-like/docs/repl.txt new file mode 100644 index 00000000..f7a82d99 --- /dev/null +++ b/is-node-stream-like/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( value ) + Tests if a value is Node stream-like. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is Node stream-like. + + Examples + -------- + > var Stream = require( 'stream' ).Stream; + > s = new Stream(); + > var bool = {{alias}}( s ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-node-stream-like/docs/types/index.d.ts b/is-node-stream-like/docs/types/index.d.ts new file mode 100644 index 00000000..ac47bed9 --- /dev/null +++ b/is-node-stream-like/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is Node stream-like. +* +* @param value - value to test +* @returns boolean indicating if a value is Node stream-like +* +* @example +* var transformStream = require( `@stdlib/streams/node/transform` ); +* +* var stream = transformStream(); +* +* var bool = isNodeStreamLike( stream ); +* // returns true +* +* bool = isNodeStreamLike( {} ); +* // returns false +*/ +declare function isNodeStreamLike( value: any ): boolean; + + +// EXPORTS // + +export = isNodeStreamLike; diff --git a/is-node-stream-like/docs/types/test.ts b/is-node-stream-like/docs/types/test.ts new file mode 100644 index 00000000..8e924520 --- /dev/null +++ b/is-node-stream-like/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNodeStreamLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNodeStreamLike( [ {}, {} ] ); // $ExpectType boolean + isNodeStreamLike( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNodeStreamLike(); // $ExpectError + isNodeStreamLike( {}, 123 ); // $ExpectError +} diff --git a/is-node-stream-like/examples/index.js b/is-node-stream-like/examples/index.js new file mode 100644 index 00000000..ee28bb43 --- /dev/null +++ b/is-node-stream-like/examples/index.js @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var stream = require( 'stream' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeStreamLike = require( './../lib' ); + +var bool = isNodeStreamLike( new stream.Stream() ); +console.log( bool ); +// => true + +bool = isNodeStreamLike( new stream.Readable() ); +console.log( bool ); +// => true + +bool = isNodeStreamLike( new stream.Writable() ); +console.log( bool ); +// => true + +bool = isNodeStreamLike( new stream.Duplex() ); +console.log( bool ); +// => true + +bool = isNodeStreamLike( new stream.Transform() ); +console.log( bool ); +// => true + +bool = isNodeStreamLike( transformStream() ); +console.log( bool ); +// => true + +bool = isNodeStreamLike( {} ); +console.log( bool ); +// => false + +bool = isNodeStreamLike( [] ); +console.log( bool ); +// => false + +bool = isNodeStreamLike( null ); +console.log( bool ); +// => false + +function Stream() { + return this; +} + +bool = isNodeStreamLike( Stream ); +console.log( bool ); +// => false + +bool = isNodeStreamLike( new Stream() ); +console.log( bool ); +// => false diff --git a/is-node-stream-like/lib/index.js b/is-node-stream-like/lib/index.js new file mode 100644 index 00000000..2539b83d --- /dev/null +++ b/is-node-stream-like/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is Node stream-like. +* +* @module @stdlib/assert/is-node-stream-like +* +* @example +* var transformStream = require( '@stdlib/streams/node/transform' ); +* var isNodeStreamLike = require( '@stdlib/assert/is-node-stream-like' ); +* +* var stream = transformStream(); +* +* var bool = isNodeStreamLike( stream ); +* // returns true +* +* bool = isNodeStreamLike( {} ); +* // returns false +*/ + +// MODULES // + +var isNodeStreamLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNodeStreamLike; diff --git a/is-node-stream-like/lib/main.js b/is-node-stream-like/lib/main.js new file mode 100644 index 00000000..38b1a0b7 --- /dev/null +++ b/is-node-stream-like/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests if a value is Node stream-like. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is Node stream-like +* +* @example +* var transformStream = require( '@stdlib/streams/node/transform' ); +* +* var stream = transformStream(); +* +* var bool = isNodeStreamLike( stream ); +* // returns true +* +* bool = isNodeStreamLike( {} ); +* // returns false +*/ +function isNodeStreamLike( value ) { + return ( + // Must be an object: + value !== null && + typeof value === 'object' && + + // Should be an event emitter: + typeof value.on === 'function' && + typeof value.once === 'function' && + typeof value.emit === 'function' && + typeof value.addListener === 'function' && + typeof value.removeListener === 'function' && + typeof value.removeAllListeners === 'function' && + + // Should have a `pipe` method (Node streams inherit from `Stream`, including writable streams): + typeof value.pipe === 'function' + ); +} + + +// EXPORTS // + +module.exports = isNodeStreamLike; diff --git a/is-node-stream-like/package.json b/is-node-stream-like/package.json new file mode 100644 index 00000000..ae2a1aa1 --- /dev/null +++ b/is-node-stream-like/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-node-stream-like", + "version": "0.0.0", + "description": "Test if a value is Node stream-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "stream", + "node", + "nodejs", + "is", + "isstream", + "test", + "check", + "validate", + "verify", + "isvalid", + "valid" + ] +} diff --git a/is-node-stream-like/test/test.js b/is-node-stream-like/test/test.js new file mode 100644 index 00000000..729a95a6 --- /dev/null +++ b/is-node-stream-like/test/test.js @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var stream = require( 'stream' ); +var EventEmitter = require( 'events' ).EventEmitter; +var tape = require( 'tape' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeStreamLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNodeStreamLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a value which is Node stream-like', function test( t ) { + var values; + var s; + var i; + + function pipe() { + // pipe function + } + + s = new EventEmitter(); + s.pipe = pipe; + + values = [ + new stream.Transform(), + new stream.Stream(), + new stream.Readable(), + new stream.Writable(), + transformStream(), + s + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNodeStreamLike( values[ i ] ), true, 'returns true for value '+i ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a value which is Node stream-like', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined, + [], + {}, + function pipe() {}, + new EventEmitter() + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNodeStreamLike( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); diff --git a/is-node-transform-stream-like/README.md b/is-node-transform-stream-like/README.md new file mode 100644 index 00000000..cb5f9a6c --- /dev/null +++ b/is-node-transform-stream-like/README.md @@ -0,0 +1,113 @@ + + +# Transform Stream-like + +> Test if a value is Node [transform stream][nodejs-stream]-like. + +
+ +## Usage + +```javascript +var isNodeTransformStreamLike = require( '@stdlib/assert/is-node-transform-stream-like' ); +``` + +#### isNodeTransformStreamLike( value ) + +Tests if a `value` is Node [transform stream][nodejs-stream]-like. + +```javascript +var transformStream = require( '@stdlib/streams/node/transform' ); + +var bool = isNodeTransformStreamLike( transformStream() ); +// returns true +``` + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var stream = require( 'stream' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeTransformStreamLike = require( '@stdlib/assert/is-node-transform-stream-like' ); + +var bool = isNodeTransformStreamLike( new stream.Transform() ); +// returns true + +bool = isNodeTransformStreamLike( transformStream() ); +// returns true + +bool = isNodeTransformStreamLike( new stream.Stream() ); +// returns false + +bool = isNodeTransformStreamLike( new stream.Readable() ); +// returns false + +bool = isNodeTransformStreamLike( new stream.Writable() ); +// returns false + +bool = isNodeTransformStreamLike( new stream.Duplex() ); +// returns false + +bool = isNodeTransformStreamLike( {} ); +// returns false + +bool = isNodeTransformStreamLike( [] ); +// returns false + +bool = isNodeTransformStreamLike( null ); +// returns false + +function Stream() { + return this; +} + +bool = isNodeTransformStreamLike( Stream ); +// returns false + +bool = isNodeTransformStreamLike( new Stream() ); +// returns false +``` + +
+ + + + + + diff --git a/is-node-transform-stream-like/benchmark/benchmark.js b/is-node-transform-stream-like/benchmark/benchmark.js new file mode 100644 index 00000000..e3d9e9b9 --- /dev/null +++ b/is-node-transform-stream-like/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNodeTransformStreamLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function stream() {}, + transformStream(), + transformStream(), + transformStream(), + transformStream(), + transformStream() + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNodeTransformStreamLike( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-node-transform-stream-like/docs/repl.txt b/is-node-transform-stream-like/docs/repl.txt new file mode 100644 index 00000000..57edb125 --- /dev/null +++ b/is-node-transform-stream-like/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( value ) + Tests if a value is Node transform stream-like. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is Node transform stream-like. + + Examples + -------- + > var Stream = require( 'stream' ).Transform; + > s = new Stream(); + > var bool = {{alias}}( s ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-node-transform-stream-like/docs/types/index.d.ts b/is-node-transform-stream-like/docs/types/index.d.ts new file mode 100644 index 00000000..08675435 --- /dev/null +++ b/is-node-transform-stream-like/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is Node transform stream-like. +* +* @param value - value to test +* @returns boolean indicating if a value is Node transform stream-like +* +* @example +* var transformStream = require( `@stdlib/streams/node/transform` ); +* +* var stream = transformStream(); +* +* var bool = isNodeTransformStreamLike( stream ); +* // returns true +* +* bool = isNodeTransformStreamLike( {} ); +* // returns false +*/ +declare function isNodeTransformStreamLike( value: any ): boolean; + + +// EXPORTS // + +export = isNodeTransformStreamLike; diff --git a/is-node-transform-stream-like/docs/types/test.ts b/is-node-transform-stream-like/docs/types/test.ts new file mode 100644 index 00000000..1ba22e4b --- /dev/null +++ b/is-node-transform-stream-like/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNodeTransformStreamLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNodeTransformStreamLike( [ {}, {} ] ); // $ExpectType boolean + isNodeTransformStreamLike( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNodeTransformStreamLike(); // $ExpectError + isNodeTransformStreamLike( {}, 123 ); // $ExpectError +} diff --git a/is-node-transform-stream-like/examples/index.js b/is-node-transform-stream-like/examples/index.js new file mode 100644 index 00000000..1097f41c --- /dev/null +++ b/is-node-transform-stream-like/examples/index.js @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var stream = require( 'stream' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeTransformStreamLike = require( './../lib' ); + +var bool = isNodeTransformStreamLike( new stream.Transform() ); +console.log( bool ); +// => true + +bool = isNodeTransformStreamLike( transformStream() ); +console.log( bool ); +// => true + +bool = isNodeTransformStreamLike( new stream.Stream() ); +console.log( bool ); +// => false + +bool = isNodeTransformStreamLike( new stream.Readable() ); +console.log( bool ); +// => false + +bool = isNodeTransformStreamLike( new stream.Writable() ); +console.log( bool ); +// => false + +bool = isNodeTransformStreamLike( new stream.Duplex() ); +console.log( bool ); +// => false + +bool = isNodeTransformStreamLike( {} ); +console.log( bool ); +// => false + +bool = isNodeTransformStreamLike( [] ); +console.log( bool ); +// => false + +bool = isNodeTransformStreamLike( null ); +console.log( bool ); +// => false + +function Stream() { + return this; +} + +bool = isNodeTransformStreamLike( Stream ); +console.log( bool ); +// => false + +bool = isNodeTransformStreamLike( new Stream() ); +console.log( bool ); +// => false diff --git a/is-node-transform-stream-like/lib/index.js b/is-node-transform-stream-like/lib/index.js new file mode 100644 index 00000000..b02fd08f --- /dev/null +++ b/is-node-transform-stream-like/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is Node transform stream-like. +* +* @module @stdlib/assert/is-node-transform-stream-like +* +* @example +* var transformStream = require( '@stdlib/streams/node/transform' ); +* var isNodeTransformStreamLike = require( '@stdlib/assert/is-node-transform-stream-like' ); +* +* var stream = transformStream(); +* +* var bool = isNodeTransformStreamLike( stream ); +* // returns true +* +* bool = isNodeTransformStreamLike( {} ); +* // returns false +*/ + +// MODULES // + +var isNodeTransformStreamLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNodeTransformStreamLike; diff --git a/is-node-transform-stream-like/lib/main.js b/is-node-transform-stream-like/lib/main.js new file mode 100644 index 00000000..636adb2a --- /dev/null +++ b/is-node-transform-stream-like/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var isNodeDuplexStreamLike = require( '@stdlib/assert/is-node-duplex-stream-like' ); + + +// MAIN // + +/** +* Tests if a value is Node transform stream-like. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is Node transform stream-like +* +* @example +* var transformStream = require( '@stdlib/streams/node/transform' ); +* +* var stream = transformStream(); +* +* var bool = isNodeTransformStreamLike( stream ); +* // returns true +* +* bool = isNodeTransformStreamLike( {} ); +* // returns false +*/ +function isNodeTransformStreamLike( value ) { + return ( + // Must be duplex stream-like: + isNodeDuplexStreamLike( value ) && + + // Should have transform stream methods: + typeof value._transform === 'function' && + + // Should have transform stream state: + typeof value._transformState === 'object' + ); +} + + +// EXPORTS // + +module.exports = isNodeTransformStreamLike; diff --git a/is-node-transform-stream-like/package.json b/is-node-transform-stream-like/package.json new file mode 100644 index 00000000..58efb093 --- /dev/null +++ b/is-node-transform-stream-like/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/assert/is-node-transform-stream-like", + "version": "0.0.0", + "description": "Test if a value is Node transform stream-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "stream", + "readable", + "writable", + "duplex", + "transform", + "node", + "nodejs", + "is", + "isstream", + "isreadable", + "iswritable", + "isduplex", + "istransform", + "test", + "check", + "validate", + "verify", + "isvalid", + "valid" + ] +} diff --git a/is-node-transform-stream-like/test/test.js b/is-node-transform-stream-like/test/test.js new file mode 100644 index 00000000..3d91a4de --- /dev/null +++ b/is-node-transform-stream-like/test/test.js @@ -0,0 +1,108 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var stream = require( 'stream' ); +var EventEmitter = require( 'events' ).EventEmitter; +var tape = require( 'tape' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeTransformStreamLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNodeTransformStreamLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a value which is Node transform stream-like', function test( t ) { + var values; + var s; + var i; + + function pipe() { + // pipe function + } + + function read() { + // read function + } + + function write() { + // write function + } + + function transform() { + // transform function + } + + s = new EventEmitter(); + s.pipe = pipe; + s._read = read; + s._readableState = {}; + s._write = write; + s._writableState = {}; + s._transform = transform; + s._transformState = {}; + + values = [ + new stream.Transform(), + transformStream(), + s + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNodeTransformStreamLike( values[ i ] ), true, 'returns true for value '+i ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a value which is Node transform stream-like', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined, + [], + {}, + function pipe() {}, + new EventEmitter(), + new stream.Writable(), + new stream.Readable(), + new stream.Duplex(), + new stream.Stream() + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNodeTransformStreamLike( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); diff --git a/is-node-writable-stream-like/README.md b/is-node-writable-stream-like/README.md new file mode 100644 index 00000000..cb3fa958 --- /dev/null +++ b/is-node-writable-stream-like/README.md @@ -0,0 +1,113 @@ + + +# Writable Stream-like + +> Test if a value is Node [writable stream][nodejs-stream]-like. + +
+ +## Usage + +```javascript +var isNodeWritableStreamLike = require( '@stdlib/assert/is-node-writable-stream-like' ); +``` + +#### isNodeWritableStreamLike( value ) + +Tests if a `value` is Node [writable stream][nodejs-stream]-like. + +```javascript +var transformStream = require( '@stdlib/streams/node/transform' ); + +var bool = isNodeWritableStreamLike( transformStream() ); +// returns true +``` + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var stream = require( 'stream' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeWritableStreamLike = require( '@stdlib/assert/is-node-writable-stream-like' ); + +var bool = isNodeWritableStreamLike( new stream.Writable() ); +// returns true + +bool = isNodeWritableStreamLike( new stream.Transform() ); +// returns true + +bool = isNodeWritableStreamLike( new stream.Duplex() ); +// returns true + +bool = isNodeWritableStreamLike( transformStream() ); +// returns true + +bool = isNodeWritableStreamLike( new stream.Stream() ); +// returns false + +bool = isNodeWritableStreamLike( new stream.Readable() ); +// returns false + +bool = isNodeWritableStreamLike( {} ); +// returns false + +bool = isNodeWritableStreamLike( [] ); +// returns false + +bool = isNodeWritableStreamLike( null ); +// returns false + +function Stream() { + return this; +} + +bool = isNodeWritableStreamLike( Stream ); +// returns false + +bool = isNodeWritableStreamLike( new Stream() ); +// returns false +``` + +
+ + + + + + diff --git a/is-node-writable-stream-like/benchmark/benchmark.js b/is-node-writable-stream-like/benchmark/benchmark.js new file mode 100644 index 00000000..2829213f --- /dev/null +++ b/is-node-writable-stream-like/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNodeWritableStreamLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function stream() {}, + transformStream(), + transformStream(), + transformStream(), + transformStream(), + transformStream() + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNodeWritableStreamLike( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-node-writable-stream-like/docs/repl.txt b/is-node-writable-stream-like/docs/repl.txt new file mode 100644 index 00000000..765a8348 --- /dev/null +++ b/is-node-writable-stream-like/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( value ) + Tests if a value is Node writable stream-like. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is Node writable stream-like. + + Examples + -------- + > var Stream = require( 'stream' ).Writable; + > s = new Stream(); + > var bool = {{alias}}( s ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-node-writable-stream-like/docs/types/index.d.ts b/is-node-writable-stream-like/docs/types/index.d.ts new file mode 100644 index 00000000..3e1fe541 --- /dev/null +++ b/is-node-writable-stream-like/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is Node writable stream-like. +* +* @param value - value to test +* @returns boolean indicating if a value is Node writable stream-like +* +* @example +* var transformStream = require( `@stdlib/streams/node/transform` ); +* +* var stream = transformStream(); +* +* var bool = isNodeWritableStreamLike( stream ); +* // returns true +* +* bool = isNodeWritableStreamLike( {} ); +* // returns false +*/ +declare function isNodeWritableStreamLike( value: any ): boolean; + + +// EXPORTS // + +export = isNodeWritableStreamLike; diff --git a/is-node-writable-stream-like/docs/types/test.ts b/is-node-writable-stream-like/docs/types/test.ts new file mode 100644 index 00000000..0eced78c --- /dev/null +++ b/is-node-writable-stream-like/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNodeWritableStreamLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNodeWritableStreamLike( [ {}, {} ] ); // $ExpectType boolean + isNodeWritableStreamLike( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNodeWritableStreamLike(); // $ExpectError + isNodeWritableStreamLike( {}, 123 ); // $ExpectError +} diff --git a/is-node-writable-stream-like/examples/index.js b/is-node-writable-stream-like/examples/index.js new file mode 100644 index 00000000..308a709d --- /dev/null +++ b/is-node-writable-stream-like/examples/index.js @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var stream = require( 'stream' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeWritableStreamLike = require( './../lib' ); + +var bool = isNodeWritableStreamLike( new stream.Writable() ); +console.log( bool ); +// => true + +bool = isNodeWritableStreamLike( new stream.Transform() ); +console.log( bool ); +// => true + +bool = isNodeWritableStreamLike( new stream.Duplex() ); +console.log( bool ); +// => true + +bool = isNodeWritableStreamLike( transformStream() ); +console.log( bool ); +// => true + +bool = isNodeWritableStreamLike( new stream.Stream() ); +console.log( bool ); +// => false + +bool = isNodeWritableStreamLike( new stream.Readable() ); +console.log( bool ); +// => false + +bool = isNodeWritableStreamLike( {} ); +console.log( bool ); +// => false + +bool = isNodeWritableStreamLike( [] ); +console.log( bool ); +// => false + +bool = isNodeWritableStreamLike( null ); +console.log( bool ); +// => false + +function Stream() { + return this; +} + +bool = isNodeWritableStreamLike( Stream ); +console.log( bool ); +// => false + +bool = isNodeWritableStreamLike( new Stream() ); +console.log( bool ); +// => false diff --git a/is-node-writable-stream-like/lib/index.js b/is-node-writable-stream-like/lib/index.js new file mode 100644 index 00000000..6be0f2e2 --- /dev/null +++ b/is-node-writable-stream-like/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is Node writable stream-like. +* +* @module @stdlib/assert/is-node-writable-stream-like +* +* @example +* var transformStream = require( '@stdlib/streams/node/transform' ); +* var isNodeWritableStreamLike = require( '@stdlib/assert/is-node-writable-stream-like' ); +* +* var stream = transformStream(); +* +* var bool = isNodeWritableStreamLike( stream ); +* // returns true +* +* bool = isNodeWritableStreamLike( {} ); +* // returns false +*/ + +// MODULES // + +var isNodeWritableStreamLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNodeWritableStreamLike; diff --git a/is-node-writable-stream-like/lib/main.js b/is-node-writable-stream-like/lib/main.js new file mode 100644 index 00000000..41b5c8aa --- /dev/null +++ b/is-node-writable-stream-like/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var isNodeStreamLike = require( '@stdlib/assert/is-node-stream-like' ); + + +// MAIN // + +/** +* Tests if a value is Node writable stream-like. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is Node writable stream-like +* +* @example +* var transformStream = require( '@stdlib/streams/node/transform' ); +* +* var stream = transformStream(); +* +* var bool = isNodeWritableStreamLike( stream ); +* // returns true +* +* bool = isNodeWritableStreamLike( {} ); +* // returns false +*/ +function isNodeWritableStreamLike( value ) { + return ( + // Must be stream-like: + isNodeStreamLike( value ) && + + // Should have writable stream methods: + typeof value._write === 'function' && + + // Should have writable stream state: + typeof value._writableState === 'object' + ); +} + + +// EXPORTS // + +module.exports = isNodeWritableStreamLike; diff --git a/is-node-writable-stream-like/package.json b/is-node-writable-stream-like/package.json new file mode 100644 index 00000000..feda88d6 --- /dev/null +++ b/is-node-writable-stream-like/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-node-writable-stream-like", + "version": "0.0.0", + "description": "Test if a value is Node writable stream-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "stream", + "writable", + "node", + "nodejs", + "is", + "isstream", + "iswritable", + "test", + "check", + "validate", + "verify", + "isvalid", + "valid" + ] +} diff --git a/is-node-writable-stream-like/test/test.js b/is-node-writable-stream-like/test/test.js new file mode 100644 index 00000000..badc982f --- /dev/null +++ b/is-node-writable-stream-like/test/test.js @@ -0,0 +1,95 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-underscore-dangle */ + +'use strict'; + +// MODULES // + +var stream = require( 'stream' ); +var EventEmitter = require( 'events' ).EventEmitter; +var tape = require( 'tape' ); +var transformStream = require( '@stdlib/streams/node/transform' ); +var isNodeWritableStreamLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNodeWritableStreamLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a value which is Node writable stream-like', function test( t ) { + var values; + var s; + var i; + + function pipe() { + // pipe function + } + + function write() { + // write function + } + + s = new EventEmitter(); + s.pipe = pipe; + s._write = write; + s._writableState = {}; + + values = [ + new stream.Transform(), + new stream.Writable(), + transformStream(), + s + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNodeWritableStreamLike( values[ i ] ), true, 'returns true for value '+i ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a value which is Node writable stream-like', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined, + [], + {}, + function pipe() {}, + new EventEmitter(), + new stream.Readable(), + new stream.Stream() + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNodeWritableStreamLike( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); diff --git a/is-node/README.md b/is-node/README.md new file mode 100644 index 00000000..2418aa54 --- /dev/null +++ b/is-node/README.md @@ -0,0 +1,81 @@ + + +# IS_NODE + +> Check if the runtime is [Node.js][node-js]. + +
+ +## Usage + +```javascript +var IS_NODE = require( '@stdlib/assert/is-node' ); +``` + +#### IS_NODE + +`Boolean` indicating if the runtime is [Node.js][node-js]. + +```javascript +var bool = IS_NODE; +// returns +``` + +
+ + + +
+ +## Notes + +- In order to determine whether the runtime is [Node.js][node-js], the implementation must resolve the global scope, thus requiring function generation. The use of function generation may be problematic in browser contexts enforcing a strict [content security policy][mdn-csp] (CSP). + +
+ + + +
+ +## Examples + + + +```javascript +var IS_NODE = require( '@stdlib/assert/is-node' ); + +console.log( IS_NODE ); +// => +``` + +
+ + + + + + diff --git a/is-node/docs/repl.txt b/is-node/docs/repl.txt new file mode 100644 index 00000000..87035961 --- /dev/null +++ b/is-node/docs/repl.txt @@ -0,0 +1,12 @@ + +{{alias}} + Boolean indicating if the runtime is Node.js. + + Examples + -------- + > {{alias}} + + + See Also + -------- + diff --git a/is-node/docs/types/index.d.ts b/is-node/docs/types/index.d.ts new file mode 100644 index 00000000..014da51d --- /dev/null +++ b/is-node/docs/types/index.d.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Boolean indicating if the runtime is Node.js. +* +* @example +* var bool = IS_NODE; +* // returns +*/ +declare const IS_NODE: boolean; + + +// EXPORTS // + +export = IS_NODE; diff --git a/is-node/docs/types/test.ts b/is-node/docs/types/test.ts new file mode 100644 index 00000000..d5835c7d --- /dev/null +++ b/is-node/docs/types/test.ts @@ -0,0 +1,28 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import IS_NODE = require( './index' ); + + +// TESTS // + +// The variable is a boolean... +{ + // tslint:disable-next-line:no-unused-expression + IS_NODE; // $ExpectType boolean +} diff --git a/is-node/examples/index.js b/is-node/examples/index.js new file mode 100644 index 00000000..d1fdd58d --- /dev/null +++ b/is-node/examples/index.js @@ -0,0 +1,24 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var IS_NODE = require( './../lib' ); + +console.log( IS_NODE ); +// => diff --git a/is-node/lib/browser.js b/is-node/lib/browser.js new file mode 100644 index 00000000..5f913971 --- /dev/null +++ b/is-node/lib/browser.js @@ -0,0 +1,23 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// EXPORTS // + +module.exports = false; diff --git a/is-node/lib/global_scope.js b/is-node/lib/global_scope.js new file mode 100644 index 00000000..3dbafbbe --- /dev/null +++ b/is-node/lib/global_scope.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// VARIABLES // + +var Global; + + +// FUNCTIONS // + +/** +* Test if the global scope is bound to the "global" variable present in Node.js environments. When creating a new function using the `Function(){}` constructor, the execution scope aliased by the `this` variable is the global scope. +* +* @private +* @returns {boolean} boolean indicating if global scope is bound to "global" variable +*/ +function globalScope() { + var fcn = ''; + fcn += 'try {'; + fcn += 'return this === global;'; + fcn += '} catch ( err ) {'; + fcn += 'return false;'; + fcn += '}'; + return (new Function( fcn ))(); // eslint-disable-line no-new-func +} + + +// MAIN // + +Global = globalScope(); + + +// EXPORTS // + +module.exports = Global; diff --git a/is-node/lib/index.js b/is-node/lib/index.js new file mode 100644 index 00000000..9770f1c9 --- /dev/null +++ b/is-node/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Boolean indicating if the runtime is Node.js. +* +* @module @stdlib/assert/is-node +* +* @example +* var IS_NODE = require( '@stdlib/assert/is-node' ); +* +* var bool = IS_NODE; +* // returns +*/ + +// MODULES // + +var isNode = require( './main.js' ); + + +// MAIN // + +var bool = isNode(); + + +// EXPORTS // + +module.exports = bool; diff --git a/is-node/lib/main.js b/is-node/lib/main.js new file mode 100644 index 00000000..b014af66 --- /dev/null +++ b/is-node/lib/main.js @@ -0,0 +1,113 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var proc = require( 'process' ); +var getGlobal = require( '@stdlib/utils/global' ); +var nativeClass = require( '@stdlib/utils/native-class' ); +var isObject = require( '@stdlib/assert/is-plain-object' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var globalScope = require( './global_scope.js' ); + + +// VARIABLES // + +var Global = getGlobal(); +var RE = /node|io\.js/; + + +// MAIN // + +/** +* Returns a boolean indicating if the runtime is Node.js. +* +* @returns {boolean} boolean indicating if runtime is Node.js +* +* @example +* var bool = isNode(); +* // returns +*/ +function isNode() { + return ( + // Check for presence of `global` variable: + typeof global === 'object' && + + // Check that the `global` variable matches the determined global variable: + global === Global && + + // Check for a circular reference to the global variable: + Global === Global.global && + + // Check that the global variable has the expected internal class: + ( + // Node < v7 + nativeClass( Global ) === '[object global]' || + + // Node >= v7 (https://github.com/nodejs/node/issues/9274) + nativeClass( Global ) === '[object Object]' + ) && + + // Check that the `global` variable is equal to the global scope: + globalScope === true && + + // Check for a `require` global variable: + typeof require === 'function' && + + // Check for a `process` global variable: + typeof proc === 'object' && + + // Check that the `process` global variable has the expected internal class: + ( + // Node < v14.6.0 + nativeClass( proc ) === '[object process]' || + + // Node >= v14.6.0 + nativeClass( proc ) === '[object Object]' + ) && + + // Check for a `versions` property: + isObject( proc.versions ) && + + // Check for a `node` property: + isString( proc.versions.node ) && + + ( + // `process.release` was added in Node v3.0.0 via io.js: + typeof proc.release === 'undefined' || + + ( + // Check for a `release` property: + isObject( proc.release ) && + + // Check for a `name` property: + isString( proc.release.name ) && + + // Check that the release name contains either `node` or `io.js` (in Node.js/io.js, the release name is read-only): + RE.test( proc.release.name ) + ) + ) + ); +} + + +// EXPORTS // + +module.exports = isNode; diff --git a/is-node/package.json b/is-node/package.json new file mode 100644 index 00000000..de56465d --- /dev/null +++ b/is-node/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/assert/is-node", + "version": "0.0.0", + "description": "Check if the runtime is Node.js.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "browser": "./lib/browser.js", + "directories": { + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "runtime", + "environment", + "browser", + "node.js" + ] +} diff --git a/is-node/test/test.browser.js b/is-node/test/test.browser.js new file mode 100644 index 00000000..c3388118 --- /dev/null +++ b/is-node/test/test.browser.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var IS_NODE = require( './../lib/browser.js' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_NODE, 'boolean', 'main export is a boolean' ); + t.end(); +}); + +tape( 'the exported value is always `false`', function test( t ) { + t.equal( IS_NODE, false, 'returns false' ); + t.end(); +}); diff --git a/is-node/test/test.js b/is-node/test/test.js new file mode 100644 index 00000000..d936e01a --- /dev/null +++ b/is-node/test/test.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var IS_NODE = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_NODE, 'boolean', 'main export is a boolean' ); + t.end(); +}); diff --git a/is-node/test/test.main.js b/is-node/test/test.main.js new file mode 100644 index 00000000..d60476b5 --- /dev/null +++ b/is-node/test/test.main.js @@ -0,0 +1,361 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var proc = require( 'process' ); +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var isNode = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNode, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if runtime is Node.js (Node.js < v7)', function test( t ) { + var isNode; + + function nativeClass( val ) { + if ( val === proc ) { + return '[object process]'; + } + // Node.js version <7 (https://github.com/nodejs/node/issues/9274): + return '[object global]'; + } + + function isString() {} + isString.isPrimitive = alwaysTrue; + + function alwaysTrue() { + return true; + } + + isNode = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + '@stdlib/assert/is-plain-object': alwaysTrue, + '@stdlib/assert/is-string': isString, + './global_scope.js': true + }); + + t.equal( isNode(), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if runtime is Node.js (Node.js >= v7)', function test( t ) { + var isNode; + + function nativeClass( val ) { + if ( val === proc ) { + return '[object process]'; + } + // Node.js version >=7 (https://github.com/nodejs/node/issues/9274): + return '[object Object]'; + } + + function isString() {} + isString.isPrimitive = alwaysTrue; + + function alwaysTrue() { + return true; + } + + isNode = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + '@stdlib/assert/is-plain-object': alwaysTrue, + '@stdlib/assert/is-string': isString, + './global_scope.js': true + }); + + t.equal( isNode(), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if runtime is not Node.js (`global` variable does not equal the detected global variable)', function test( t ) { + var isNode; + + function getGlobal() { + return {}; + } + + function nativeClass( val ) { + if ( val === proc ) { + return '[object process]'; + } + // Node.js version <7 (https://github.com/nodejs/node/issues/9274): + return '[object global]'; + } + + function isString() {} + isString.isPrimitive = alwaysTrue; + + function alwaysTrue() { + return true; + } + + isNode = proxyquire( './../lib/main.js', { + '@stdlib/utils/global': getGlobal, + '@stdlib/utils/native-class': nativeClass, + '@stdlib/assert/is-plain-object': alwaysTrue, + '@stdlib/assert/is-string': isString, + './global_scope.js': true + }); + t.equal( isNode(), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if runtime is not Node.js (`global` variable has wrong class)', function test( t ) { + var isNode; + + function nativeClass( val ) { + if ( val === proc ) { + return '[object process]'; + } + return '[object beeeeeeep]'; + } + + function isString() {} + isString.isPrimitive = alwaysTrue; + + function alwaysTrue() { + return true; + } + + isNode = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + '@stdlib/assert/is-plain-object': alwaysTrue, + '@stdlib/assert/is-string': isString, + './global_scope.js': true + }); + t.equal( isNode(), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if runtime is not Node.js (`global` variable is not the global scope)', function test( t ) { + var isNode; + + function nativeClass( val ) { + if ( val === proc ) { + return '[object process]'; + } + // Node.js version <7 (https://github.com/nodejs/node/issues/9274): + return '[object global]'; + } + + function isString() {} + isString.isPrimitive = alwaysTrue; + + function alwaysTrue() { + return true; + } + + isNode = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + '@stdlib/assert/is-plain-object': alwaysTrue, + '@stdlib/assert/is-string': isString, + './global_scope.js': false + }); + t.equal( isNode(), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if runtime is not Node.js (`process` variable has wrong class)', function test( t ) { + var isNode; + + function nativeClass( val ) { + if ( val === proc ) { + return '[object beeeeeeep]'; + } + // Node.js version <7 (https://github.com/nodejs/node/issues/9274): + return '[object global]'; + } + + function isString() {} + isString.isPrimitive = alwaysTrue; + + function alwaysTrue() { + return true; + } + + isNode = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + '@stdlib/assert/is-plain-object': alwaysTrue, + '@stdlib/assert/is-string': isString, + './global_scope.js': true + }); + t.equal( isNode(), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if runtime is not Node.js (`process.versions` is not an object)', function test( t ) { + var isNode; + + function nativeClass( val ) { + if ( val === proc ) { + return '[object process]'; + } + // Node.js version <7 (https://github.com/nodejs/node/issues/9274): + return '[object global]'; + } + + function isString() {} + isString.isPrimitive = alwaysTrue; + + function isObject( val ) { + if ( val === proc.versions ) { + return false; + } + return ( typeof val === 'object' ); + } + + function alwaysTrue() { + return true; + } + + isNode = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + '@stdlib/assert/is-plain-object': isObject, + '@stdlib/assert/is-string': isString, + './global_scope.js': true + }); + t.equal( isNode(), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if runtime is not Node.js (`process.versions.node` is not a string primitive)', function test( t ) { + var isNode; + + function nativeClass( val ) { + if ( val === proc ) { + return '[object process]'; + } + // Node.js version <7 (https://github.com/nodejs/node/issues/9274): + return '[object global]'; + } + + function isString() {} + isString.isPrimitive = isPrimitive; + + function isPrimitive( val ) { + if ( val === proc.versions.node ) { + return false; + } + return ( typeof val === 'string' ); + } + + function alwaysTrue() { + return true; + } + + isNode = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + '@stdlib/assert/is-plain-object': alwaysTrue, + '@stdlib/assert/is-string': isString, + './global_scope.js': true + }); + t.equal( isNode(), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if runtime is not Node.js (`process.release` is not an object; Node >=v3.0.0)', function test( t ) { + var isNode; + + function nativeClass( val ) { + if ( val === proc ) { + return '[object process]'; + } + // Node.js version <7 (https://github.com/nodejs/node/issues/9274): + return '[object global]'; + } + + function isString() {} + isString.isPrimitive = alwaysTrue; + + function isObject( val ) { + if ( proc.release && val === proc.release ) { + return false; + } + return ( typeof val === 'object' ); + } + + function alwaysTrue() { + return true; + } + + isNode = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + '@stdlib/assert/is-plain-object': isObject, + '@stdlib/assert/is-string': isString, + './global_scope.js': true + }); + + // `process.release` was added in Node v3.0.0 + if ( proc.release ) { + t.equal( isNode(), false, 'returns false' ); + } else { + t.equal( isNode(), true, 'returns true' ); + } + t.end(); +}); + +tape( 'the function returns `false` if runtime is not Node.js (`process.release.name` is not a string primitive; Node >=v3.0.0)', function test( t ) { + var isNode; + + function nativeClass( val ) { + if ( val === proc ) { + return '[object process]'; + } + // Node.js version <7 (https://github.com/nodejs/node/issues/9274): + return '[object global]'; + } + + function isString() {} + isString.isPrimitive = isPrimitive; + + function isPrimitive( val ) { + if ( proc.release && val === proc.release.name ) { + return false; + } + return ( typeof val === 'string' ); + } + + function alwaysTrue() { + return true; + } + + isNode = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass, + '@stdlib/assert/is-plain-object': alwaysTrue, + '@stdlib/assert/is-string': isString, + './global_scope.js': true + }); + + // `process.release` was added in Node v3.0.0 + if ( proc.release ) { + t.equal( isNode(), false, 'returns false' ); + } else { + t.equal( isNode(), true, 'returns true' ); + } + t.end(); +}); diff --git a/is-nonconfigurable-property-in/README.md b/is-nonconfigurable-property-in/README.md new file mode 100644 index 00000000..ffb4026e --- /dev/null +++ b/is-nonconfigurable-property-in/README.md @@ -0,0 +1,162 @@ + + +# isNonConfigurablePropertyIn + +> Test if an object's own or inherited property is non-configurable. + +
+ +## Usage + + + +```javascript +var isNonConfigurablePropertyIn = require( '@stdlib/assert/is-nonconfigurable-property-in' ); +``` + +#### isNonConfigurablePropertyIn( value, property ) + +Returns a `boolean` indicating if a `value` has a non-configurable `property` (i.e., a property which cannot be deleted and whose descriptor cannot be changed). + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var bool; +var obj; + +function Foo() { + this.foo = 'bar'; + return this; +} + +defineProperty( Foo.prototype, 'beep', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': true +}); + +defineProperty( Foo.prototype, 'boop', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': true +}); + +obj = new Foo(); + +bool = isNonConfigurablePropertyIn( obj, 'foo' ); +// returns false + +bool = isNonConfigurablePropertyIn( obj, 'beep' ); +// returns false + +bool = isNonConfigurablePropertyIn( obj, 'boop' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isNonConfigurablePropertyIn( 'beep', 'length' ); + // returns true + ``` + +- Non-symbol property arguments are coerced to `strings`. + + ```javascript + var defineProperty = require( '@stdlib/utils/define-property' ); + + var obj = {}; + + defineProperty( obj, 'null', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': true + }); + + var bool = isNonConfigurablePropertyIn( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isNonConfigurablePropertyIn = require( '@stdlib/assert/is-nonconfigurable-property-in' ); + +var bool = isNonConfigurablePropertyIn( [ 'a' ], 'length' ); +// returns true + +bool = isNonConfigurablePropertyIn( {}, 'toString' ); +// returns false + +bool = isNonConfigurablePropertyIn( {}, 'hasOwnProperty' ); +// returns false + +bool = isNonConfigurablePropertyIn( { 'a': 'b' }, 'a' ); +// returns false + +bool = isNonConfigurablePropertyIn( [ 'a' ], 0 ); +// returns false + +bool = isNonConfigurablePropertyIn( { 'null': false }, null ); +// returns false + +bool = isNonConfigurablePropertyIn( { '[object Object]': false }, {} ); +// returns false + +bool = isNonConfigurablePropertyIn( null, 'a' ); +// returns false + +bool = isNonConfigurablePropertyIn( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-nonconfigurable-property-in/benchmark/benchmark.js b/is-nonconfigurable-property-in/benchmark/benchmark.js new file mode 100644 index 00000000..96dd3533 --- /dev/null +++ b/is-nonconfigurable-property-in/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNonConfigurablePropertyIn = require( './../lib' ); // eslint-disable-line id-length + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonConfigurablePropertyIn( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-nonconfigurable-property-in/docs/repl.txt b/is-nonconfigurable-property-in/docs/repl.txt new file mode 100644 index 00000000..47d5067a --- /dev/null +++ b/is-nonconfigurable-property-in/docs/repl.txt @@ -0,0 +1,38 @@ + +{{alias}}( value, property ) + Tests if an object's own or inherited property is non-configurable. + + A non-configurable property is a property which cannot be deleted and whose + descriptor cannot be changed. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own or inherited property is non- + configurable. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = true; + > desc.writable = true; + > desc.value = true; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + false + > bool = {{alias}}( obj, 'beep' ) + true + + See Also + -------- + diff --git a/is-nonconfigurable-property-in/docs/types/index.d.ts b/is-nonconfigurable-property-in/docs/types/index.d.ts new file mode 100644 index 00000000..8ecaea13 --- /dev/null +++ b/is-nonconfigurable-property-in/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own or inherited property is non-configurable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is non-configurable +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isNonConfigurablePropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isNonConfigurablePropertyIn( obj, 'beep' ); +* // returns true +*/ +declare function isNonConfigurablePropertyIn( value: any, property: any ): boolean; // tslint:disable-line:max-line-length + + +// EXPORTS // + +export = isNonConfigurablePropertyIn; diff --git a/is-nonconfigurable-property-in/docs/types/test.ts b/is-nonconfigurable-property-in/docs/types/test.ts new file mode 100644 index 00000000..8fedcfcc --- /dev/null +++ b/is-nonconfigurable-property-in/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNonConfigurablePropertyIn = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': 'beep' + } ); + isNonConfigurablePropertyIn( obj, 'beep' ); // $ExpectType boolean + isNonConfigurablePropertyIn( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNonConfigurablePropertyIn(); // $ExpectError + isNonConfigurablePropertyIn( {} ); // $ExpectError + isNonConfigurablePropertyIn( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-nonconfigurable-property-in/examples/index.js b/is-nonconfigurable-property-in/examples/index.js new file mode 100644 index 00000000..ed58f617 --- /dev/null +++ b/is-nonconfigurable-property-in/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline, id-length */ + +'use strict'; + +var isNonConfigurablePropertyIn = require( './../lib' ); + +var bool = isNonConfigurablePropertyIn( [ 'a' ], 'length' ); +console.log( bool ); +// => true + +bool = isNonConfigurablePropertyIn( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isNonConfigurablePropertyIn( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isNonConfigurablePropertyIn( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => false + +bool = isNonConfigurablePropertyIn( [ 'a' ], 0 ); +console.log( bool ); +// => false + +bool = isNonConfigurablePropertyIn( { 'null': false }, null ); +console.log( bool ); +// => false + +bool = isNonConfigurablePropertyIn( { '[object Object]': false }, {} ); +console.log( bool ); +// => false + +bool = isNonConfigurablePropertyIn( null, 'a' ); +console.log( bool ); +// => false + +bool = isNonConfigurablePropertyIn( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-nonconfigurable-property-in/lib/index.js b/is-nonconfigurable-property-in/lib/index.js new file mode 100644 index 00000000..198a9cee --- /dev/null +++ b/is-nonconfigurable-property-in/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own or inherited property is non-configurable. +* +* @module @stdlib/assert/is-nonconfigurable-property-in +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isNonConfigurablePropertyIn = require( '@stdlib/assert/is-nonconfigurable-property-in' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isNonConfigurablePropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isNonConfigurablePropertyIn( obj, 'beep' ); +* // returns true +*/ + +// MODULES // + +var isNonConfigurablePropertyIn = require( './main.js' ); // eslint-disable-line id-length + + +// EXPORTS // + +module.exports = isNonConfigurablePropertyIn; diff --git a/is-nonconfigurable-property-in/lib/main.js b/is-nonconfigurable-property-in/lib/main.js new file mode 100644 index 00000000..4bab059e --- /dev/null +++ b/is-nonconfigurable-property-in/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptorIn = require( '@stdlib/utils/property-descriptor-in' ); + + +// MAIN // + +/** +* Tests if an object's own or inherited property is non-configurable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is non-configurable +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isNonConfigurablePropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isNonConfigurablePropertyIn( obj, 'beep' ); +* // returns true +*/ +function isNonConfigurablePropertyIn( value, property ) { // eslint-disable-line id-length + var desc = propertyDescriptorIn( value, property ); + return ( desc !== null && desc.configurable === false ); +} + + +// EXPORTS // + +module.exports = isNonConfigurablePropertyIn; diff --git a/is-nonconfigurable-property-in/package.json b/is-nonconfigurable-property-in/package.json new file mode 100644 index 00000000..510670f6 --- /dev/null +++ b/is-nonconfigurable-property-in/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-nonconfigurable-property-in", + "version": "0.0.0", + "description": "Test if an object's own or inherited property is non-configurable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "inherited", + "in", + "prototype", + "proto", + "configurable", + "non-configurable", + "descriptor", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-nonconfigurable-property-in/test/test.js b/is-nonconfigurable-property-in/test/test.js new file mode 100644 index 00000000..56736fe9 --- /dev/null +++ b/is-nonconfigurable-property-in/test/test.js @@ -0,0 +1,197 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isNonConfigurablePropertyIn = require( './../lib' ); // eslint-disable-line id-length + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonConfigurablePropertyIn, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a non-configurable property', function test( t ) { + var bool; + var obj; + + obj = {}; + + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': true + }); + + bool = isNonConfigurablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isNonConfigurablePropertyIn( [ 'a' ], 'length' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if provided a non-configurable inherited property', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + defineProperty( Foo.prototype, 'a', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': true + }); + + obj = new Foo(); + + bool = isNonConfigurablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object property is configurable', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isNonConfigurablePropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonConfigurablePropertyIn( [ 1, 2, 3 ], '1' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonConfigurablePropertyIn( [ 1, 2, 3 ], 1 ); + t.equal( bool, false, 'returns false' ); + + bool = isNonConfigurablePropertyIn( new Foo(), 'bar' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': 'b' + }); + + bool = isNonConfigurablePropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a configurable inherited property', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + defineProperty( Foo.prototype, 'bar', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': true + }); + + obj = new Foo(); + + bool = isNonConfigurablePropertyIn( obj, 'bar' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonConfigurablePropertyIn( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonConfigurablePropertyIn( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonConfigurablePropertyIn( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isNonConfigurablePropertyIn( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isNonConfigurablePropertyIn( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a non-configurable property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + defineProperty( obj, 'd', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': true + }); + + bool = isNonConfigurablePropertyIn( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonConfigurablePropertyIn( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isNonConfigurablePropertyIn( 'beep', 'length' ); + t.equal( bool, true, 'returns true' ); + + bool = isNonConfigurablePropertyIn( 'beep', 'toString' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); diff --git a/is-nonconfigurable-property/README.md b/is-nonconfigurable-property/README.md new file mode 100644 index 00000000..6e1af066 --- /dev/null +++ b/is-nonconfigurable-property/README.md @@ -0,0 +1,124 @@ + + +# isNonConfigurableProperty + +> Test if an object's own property is non-configurable. + +
+ +## Usage + +```javascript +var isNonConfigurableProperty = require( '@stdlib/assert/is-nonconfigurable-property' ); +``` + +#### isNonConfigurableProperty( value, property ) + +Returns a `boolean` indicating if a `value` has a non-configurable `property` (i.e., a property which cannot be deleted and whose descriptor cannot be changed). + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var obj = { + 'foo': 'bar' +}; + +defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': 'boop' +}); + +var bool = isNonConfigurableProperty( obj, 'beep' ); +// returns true + +bool = isNonConfigurableProperty( obj, 'foo' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isNonConfigurableProperty( 'beep', 'length' ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isNonConfigurableProperty = require( '@stdlib/assert/is-nonconfigurable-property' ); + +var bool = isNonConfigurableProperty( [ 'a' ], 'length' ); +// returns true + +bool = isNonConfigurableProperty( { 'a': 'b' }, 'a' ); +// returns false + +bool = isNonConfigurableProperty( [ 'a' ], 0 ); +// returns false + +bool = isNonConfigurableProperty( {}, 'toString' ); +// returns false + +bool = isNonConfigurableProperty( {}, 'hasOwnProperty' ); +// returns false + +bool = isNonConfigurableProperty( null, 'a' ); +// returns false + +bool = isNonConfigurableProperty( void 0, 'a' ); +// returns false + +bool = isNonConfigurableProperty( { 'null': false }, null ); +// returns false + +bool = isNonConfigurableProperty( { '[object Object]': false }, {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-nonconfigurable-property/benchmark/benchmark.js b/is-nonconfigurable-property/benchmark/benchmark.js new file mode 100644 index 00000000..027a5928 --- /dev/null +++ b/is-nonconfigurable-property/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNonConfigurableProperty = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonConfigurableProperty( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-nonconfigurable-property/docs/repl.txt b/is-nonconfigurable-property/docs/repl.txt new file mode 100644 index 00000000..edb2aae2 --- /dev/null +++ b/is-nonconfigurable-property/docs/repl.txt @@ -0,0 +1,37 @@ + +{{alias}}( value, property ) + Tests if an object's own property is non-configurable. + + A non-configurable property is a property which cannot be deleted and whose + descriptor cannot be changed. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own property is non-configurable. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = true; + > desc.writable = true; + > desc.value = 'beep'; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + false + > bool = {{alias}}( obj, 'beep' ) + true + + See Also + -------- + diff --git a/is-nonconfigurable-property/docs/types/index.d.ts b/is-nonconfigurable-property/docs/types/index.d.ts new file mode 100644 index 00000000..06cba625 --- /dev/null +++ b/is-nonconfigurable-property/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own property is non-configurable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is non-configurable +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': true, +* 'value': 'beep' +* }); +* +* var bool = isNonConfigurableProperty( obj, 'boop' ); +* // returns false +* +* bool = isNonConfigurableProperty( obj, 'beep' ); +* // returns true +*/ +declare function isNonConfigurableProperty( value: any, property: any ): boolean; // tslint:disable-line:max-line-length + + +// EXPORTS // + +export = isNonConfigurableProperty; diff --git a/is-nonconfigurable-property/docs/types/test.ts b/is-nonconfigurable-property/docs/types/test.ts new file mode 100644 index 00000000..e6c38a42 --- /dev/null +++ b/is-nonconfigurable-property/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNonConfigurableProperty = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': 'beep' + } ); + isNonConfigurableProperty( obj, 'beep' ); // $ExpectType boolean + isNonConfigurableProperty( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNonConfigurableProperty(); // $ExpectError + isNonConfigurableProperty( {} ); // $ExpectError + isNonConfigurableProperty( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-nonconfigurable-property/examples/index.js b/is-nonconfigurable-property/examples/index.js new file mode 100644 index 00000000..5233945d --- /dev/null +++ b/is-nonconfigurable-property/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isNonConfigurableProperty = require( './../lib' ); + +var bool = isNonConfigurableProperty( [ 'a' ], 'length' ); +console.log( bool ); +// => true + +bool = isNonConfigurableProperty( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => false + +bool = isNonConfigurableProperty( [ 'a' ], 0 ); +console.log( bool ); +// => false + +bool = isNonConfigurableProperty( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isNonConfigurableProperty( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isNonConfigurableProperty( null, 'a' ); +console.log( bool ); +// => false + +bool = isNonConfigurableProperty( void 0, 'a' ); +console.log( bool ); +// => false + +bool = isNonConfigurableProperty( { 'null': false }, null ); +console.log( bool ); +// => false + +bool = isNonConfigurableProperty( { '[object Object]': false }, {} ); +console.log( bool ); +// => false diff --git a/is-nonconfigurable-property/lib/index.js b/is-nonconfigurable-property/lib/index.js new file mode 100644 index 00000000..0f09ab91 --- /dev/null +++ b/is-nonconfigurable-property/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own property is non-configurable. +* +* @module @stdlib/assert/is-nonconfigurable-property +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isNonConfigurableProperty = require( '@stdlib/assert/is-nonconfigurable-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': true, +* 'value': 'beep' +* }); +* +* var bool = isNonConfigurableProperty( obj, 'boop' ); +* // returns false +* +* bool = isNonConfigurableProperty( obj, 'beep' ); +* // returns true +*/ + +// MODULES // + +var isNonConfigurableProperty = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNonConfigurableProperty; diff --git a/is-nonconfigurable-property/lib/main.js b/is-nonconfigurable-property/lib/main.js new file mode 100644 index 00000000..1517a559 --- /dev/null +++ b/is-nonconfigurable-property/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptor = require( '@stdlib/utils/property-descriptor' ); + + +// MAIN // + +/** +* Tests if an object's own property is non-configurable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is non-configurable +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': true, +* 'value': 'beep' +* }); +* +* var bool = isNonConfigurableProperty( obj, 'boop' ); +* // returns false +* +* bool = isNonConfigurableProperty( obj, 'beep' ); +* // returns true +*/ +function isNonConfigurableProperty( value, property ) { + var desc = propertyDescriptor( value, property ); + return ( desc !== null && desc.configurable === false ); +} + + +// EXPORTS // + +module.exports = isNonConfigurableProperty; diff --git a/is-nonconfigurable-property/package.json b/is-nonconfigurable-property/package.json new file mode 100644 index 00000000..d93fc616 --- /dev/null +++ b/is-nonconfigurable-property/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-nonconfigurable-property", + "version": "0.0.0", + "description": "Test if an object's own property is non-configurable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "configurable", + "non-configurable", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-nonconfigurable-property/test/test.js b/is-nonconfigurable-property/test/test.js new file mode 100644 index 00000000..378df4b5 --- /dev/null +++ b/is-nonconfigurable-property/test/test.js @@ -0,0 +1,132 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isNonConfigurableProperty = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonConfigurableProperty, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isNonConfigurableProperty( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isNonConfigurableProperty( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object property is configurable', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isNonConfigurableProperty( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonConfigurableProperty( [ 1, 2, 3 ], '1' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonConfigurableProperty( [ 1, 2, 3 ], 1 ); + t.equal( bool, false, 'returns false' ); + + bool = isNonConfigurableProperty( new Foo(), 'bar' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a non-configurable property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + bool = isNonConfigurableProperty( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `true` if an object property is non-configurable', function test( t ) { + var bool; + var obj; + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': true, + 'writable': true, + 'value': 'b' + }); + + bool = isNonConfigurableProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isNonConfigurableProperty( [ 'a' ], 'length' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an inherited property', function test( t ) { + var bool; + + bool = isNonConfigurableProperty( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonConfigurableProperty( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonConfigurableProperty( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isNonConfigurableProperty( 'beep', 'length' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/is-nonenumerable-property-in/README.md b/is-nonenumerable-property-in/README.md new file mode 100644 index 00000000..bf55ead9 --- /dev/null +++ b/is-nonenumerable-property-in/README.md @@ -0,0 +1,160 @@ + + +# isNonEnumerablePropertyIn + +> Test if an object's own or inherited property is non-enumerable. + +
+ +## Usage + +```javascript +var isNonEnumerablePropertyIn = require( '@stdlib/assert/is-nonenumerable-property-in' ); +``` + +#### isNonEnumerablePropertyIn( value, property ) + +Returns a `boolean` indicating if a `value` has a non-enumerable `property`. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var bool; +var obj; + +function Foo() { + this.foo = 'bar'; + return this; +} + +defineProperty( Foo.prototype, 'beep', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': true +}); + +defineProperty( Foo.prototype, 'boop', { + 'configurable': true, + 'enumerable': false, + 'writable': true, + 'value': true +}); + +obj = new Foo(); + +bool = isNonEnumerablePropertyIn( obj, 'foo' ); +// returns false + +bool = isNonEnumerablePropertyIn( obj, 'beep' ); +// returns false + +bool = isNonEnumerablePropertyIn( obj, 'boop' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isNonEnumerablePropertyIn( 'beep', 'length' ); + // returns true + ``` + +- Non-symbol property arguments are coerced to `strings`. + + ```javascript + var defineProperty = require( '@stdlib/utils/define-property' ); + + var obj = {}; + + defineProperty( obj, 'null', { + 'configurable': true, + 'enumerable': false, + 'writable': true, + 'value': true + }); + + var bool = isNonEnumerablePropertyIn( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isNonEnumerablePropertyIn = require( '@stdlib/assert/is-nonenumerable-property-in' ); + +var bool = isNonEnumerablePropertyIn( {}, 'toString' ); +// returns true + +bool = isNonEnumerablePropertyIn( {}, 'hasOwnProperty' ); +// returns true + +bool = isNonEnumerablePropertyIn( [ 'a' ], 'length' ); +// returns true + +bool = isNonEnumerablePropertyIn( { 'a': 'b' }, 'a' ); +// returns false + +bool = isNonEnumerablePropertyIn( [ 'a' ], 0 ); +// returns false + +bool = isNonEnumerablePropertyIn( { 'null': false }, null ); +// returns false + +bool = isNonEnumerablePropertyIn( { '[object Object]': false }, {} ); +// returns false + +bool = isNonEnumerablePropertyIn( null, 'a' ); +// returns false + +bool = isNonEnumerablePropertyIn( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-nonenumerable-property-in/benchmark/benchmark.js b/is-nonenumerable-property-in/benchmark/benchmark.js new file mode 100644 index 00000000..73ebb419 --- /dev/null +++ b/is-nonenumerable-property-in/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNonEnumerablePropertyIn = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonEnumerablePropertyIn( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-nonenumerable-property-in/docs/repl.txt b/is-nonenumerable-property-in/docs/repl.txt new file mode 100644 index 00000000..4d33d27c --- /dev/null +++ b/is-nonenumerable-property-in/docs/repl.txt @@ -0,0 +1,35 @@ + +{{alias}}( value, property ) + Tests if an object's own or inherited property is non-enumerable. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own or inherited property is non- + enumerable. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = true; + > desc.enumerable = false; + > desc.writable = true; + > desc.value = true; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + false + > bool = {{alias}}( obj, 'beep' ) + true + + See Also + -------- + diff --git a/is-nonenumerable-property-in/docs/types/index.d.ts b/is-nonenumerable-property-in/docs/types/index.d.ts new file mode 100644 index 00000000..f21ca236 --- /dev/null +++ b/is-nonenumerable-property-in/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own or inherited property is non-enumerable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is non-enumerable +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': true, +* 'enumerable': false, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isNonEnumerablePropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isNonEnumerablePropertyIn( obj, 'beep' ); +* // returns true +*/ +declare function isNonEnumerablePropertyIn( value: any, property: any ): boolean; // tslint:disable-line:max-line-length + + +// EXPORTS // + +export = isNonEnumerablePropertyIn; diff --git a/is-nonenumerable-property-in/docs/types/test.ts b/is-nonenumerable-property-in/docs/types/test.ts new file mode 100644 index 00000000..f097da5b --- /dev/null +++ b/is-nonenumerable-property-in/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNonEnumerablePropertyIn = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + Object.defineProperty( obj, 'beep', { + 'configurable': true, + 'enumerable': false, + 'writable': true, + 'value': 'beep' + } ); + isNonEnumerablePropertyIn( obj, 'beep' ); // $ExpectType boolean + isNonEnumerablePropertyIn( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNonEnumerablePropertyIn(); // $ExpectError + isNonEnumerablePropertyIn( {} ); // $ExpectError + isNonEnumerablePropertyIn( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-nonenumerable-property-in/examples/index.js b/is-nonenumerable-property-in/examples/index.js new file mode 100644 index 00000000..e226abd7 --- /dev/null +++ b/is-nonenumerable-property-in/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isNonEnumerablePropertyIn = require( './../lib' ); + +var bool = isNonEnumerablePropertyIn( {}, 'toString' ); +console.log( bool ); +// => true + +bool = isNonEnumerablePropertyIn( {}, 'hasOwnProperty' ); +console.log( bool ); +// => true + +bool = isNonEnumerablePropertyIn( [ 'a' ], 'length' ); +console.log( bool ); +// => true + +bool = isNonEnumerablePropertyIn( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => false + +bool = isNonEnumerablePropertyIn( [ 'a' ], 0 ); +console.log( bool ); +// => false + +bool = isNonEnumerablePropertyIn( { 'null': false }, null ); +console.log( bool ); +// => false + +bool = isNonEnumerablePropertyIn( { '[object Object]': false }, {} ); +console.log( bool ); +// => false + +bool = isNonEnumerablePropertyIn( null, 'a' ); +console.log( bool ); +// => false + +bool = isNonEnumerablePropertyIn( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-nonenumerable-property-in/lib/index.js b/is-nonenumerable-property-in/lib/index.js new file mode 100644 index 00000000..ba9731d3 --- /dev/null +++ b/is-nonenumerable-property-in/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own or inherited property is non-enumerable. +* +* @module @stdlib/assert/is-nonenumerable-property-in +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isNonEnumerablePropertyIn = require( '@stdlib/assert/is-nonenumerable-property-in' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': true, +* 'enumerable': false, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isNonEnumerablePropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isNonEnumerablePropertyIn( obj, 'beep' ); +* // returns true +*/ + +// MODULES // + +var isNonEnumerablePropertyIn = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNonEnumerablePropertyIn; diff --git a/is-nonenumerable-property-in/lib/main.js b/is-nonenumerable-property-in/lib/main.js new file mode 100644 index 00000000..eacaa8cd --- /dev/null +++ b/is-nonenumerable-property-in/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptorIn = require( '@stdlib/utils/property-descriptor-in' ); + + +// MAIN // + +/** +* Tests if an object's own or inherited property is non-enumerable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is non-enumerable +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': true, +* 'enumerable': false, +* 'writable': true, +* 'value': true +* }); +* +* var bool = isNonEnumerablePropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isNonEnumerablePropertyIn( obj, 'beep' ); +* // returns true +*/ +function isNonEnumerablePropertyIn( value, property ) { + var desc = propertyDescriptorIn( value, property ); + return ( desc !== null && desc.enumerable === false ); +} + + +// EXPORTS // + +module.exports = isNonEnumerablePropertyIn; diff --git a/is-nonenumerable-property-in/package.json b/is-nonenumerable-property-in/package.json new file mode 100644 index 00000000..3d3f530f --- /dev/null +++ b/is-nonenumerable-property-in/package.json @@ -0,0 +1,79 @@ +{ + "name": "@stdlib/assert/is-nonenumerable-property-in", + "version": "0.0.0", + "description": "Test if an object's own or inherited property is non-enumerable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "inherited", + "in", + "prototype", + "proto", + "enumerable", + "non-enumerable", + "for-in", + "descriptor", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-nonenumerable-property-in/test/test.js b/is-nonenumerable-property-in/test/test.js new file mode 100644 index 00000000..e8b0c4af --- /dev/null +++ b/is-nonenumerable-property-in/test/test.js @@ -0,0 +1,197 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isNonEnumerablePropertyIn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonEnumerablePropertyIn, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a non-enumerable property', function test( t ) { + var bool; + var obj; + + obj = {}; + + defineProperty( obj, 'a', { + 'configurable': true, + 'enumerable': false, + 'writable': true, + 'value': true + }); + + bool = isNonEnumerablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isNonEnumerablePropertyIn( [ 'a' ], 'length' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if provided a non-enumerable inherited property', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + defineProperty( Foo.prototype, 'a', { + 'configurable': true, + 'enumerable': false, + 'writable': true, + 'value': true + }); + + obj = new Foo(); + + bool = isNonEnumerablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isNonEnumerablePropertyIn( {}, 'hasOwnProperty' ); + t.equal( bool, true, 'returns true' ); + + bool = isNonEnumerablePropertyIn( {}, 'toString' ); + t.equal( bool, true, 'returns true' ); + + bool = isNonEnumerablePropertyIn( {}, 'constructor' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object property is enumerable', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isNonEnumerablePropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonEnumerablePropertyIn( [ 1, 2, 3 ], '1' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonEnumerablePropertyIn( [ 1, 2, 3 ], 1 ); + t.equal( bool, false, 'returns false' ); + + bool = isNonEnumerablePropertyIn( new Foo(), 'bar' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': 'b' + }); + + bool = isNonEnumerablePropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an enumerable inherited property', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + defineProperty( Foo.prototype, 'bar', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': true + }); + + obj = new Foo(); + + bool = isNonEnumerablePropertyIn( obj, 'bar' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isNonEnumerablePropertyIn( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isNonEnumerablePropertyIn( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a non-enumerable property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + defineProperty( obj, 'd', { + 'configurable': true, + 'enumerable': true, + 'writable': true, + 'value': true + }); + + bool = isNonEnumerablePropertyIn( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonEnumerablePropertyIn( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isNonEnumerablePropertyIn( 'beep', 'length' ); + t.equal( bool, true, 'returns true' ); + + bool = isNonEnumerablePropertyIn( 'beep', 'toString' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/is-nonenumerable-property/README.md b/is-nonenumerable-property/README.md new file mode 100644 index 00000000..ca19c6bf --- /dev/null +++ b/is-nonenumerable-property/README.md @@ -0,0 +1,124 @@ + + +# isNonEnumerableProperty + +> Test if an object's own property is non-enumerable. + +
+ +## Usage + +```javascript +var isNonEnumerableProperty = require( '@stdlib/assert/is-nonenumerable-property' ); +``` + +#### isNonEnumerableProperty( value, property ) + +Returns a `boolean` indicating if a `value` has a non-enumerable `property`. + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var obj = { + 'foo': 'bar' +}; + +defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'boop' +}); + +var bool = isNonEnumerableProperty( obj, 'beep' ); +// returns true + +bool = isNonEnumerableProperty( obj, 'foo' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isNonEnumerableProperty( 'beep', 'length' ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isNonEnumerableProperty = require( '@stdlib/assert/is-nonenumerable-property' ); + +var bool = isNonEnumerableProperty( [ 'a' ], 'length' ); +// returns true + +bool = isNonEnumerableProperty( { 'a': 'b' }, 'a' ); +// returns false + +bool = isNonEnumerableProperty( [ 'a' ], 0 ); +// returns false + +bool = isNonEnumerableProperty( {}, 'toString' ); +// returns false + +bool = isNonEnumerableProperty( {}, 'hasOwnProperty' ); +// returns false + +bool = isNonEnumerableProperty( null, 'a' ); +// returns false + +bool = isNonEnumerableProperty( void 0, 'a' ); +// returns false + +bool = isNonEnumerableProperty( { 'null': false }, null ); +// returns false + +bool = isNonEnumerableProperty( { '[object Object]': false }, {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-nonenumerable-property/benchmark/benchmark.js b/is-nonenumerable-property/benchmark/benchmark.js new file mode 100644 index 00000000..8b35eb92 --- /dev/null +++ b/is-nonenumerable-property/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNonEnumerableProperty = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonEnumerableProperty( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-nonenumerable-property/docs/repl.txt b/is-nonenumerable-property/docs/repl.txt new file mode 100644 index 00000000..6fc38eed --- /dev/null +++ b/is-nonenumerable-property/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, property ) + Tests if an object's own property is non-enumerable. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own property is non-enumerable. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = false; + > desc.writable = true; + > desc.value = 'beep'; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + false + > bool = {{alias}}( obj, 'beep' ) + true + + See Also + -------- + diff --git a/is-nonenumerable-property/docs/types/index.d.ts b/is-nonenumerable-property/docs/types/index.d.ts new file mode 100644 index 00000000..c84fcb7e --- /dev/null +++ b/is-nonenumerable-property/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own property is non-enumerable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is non-enumerable +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'writable': true, +* 'value': 'beep' +* }); +* +* var bool = isNonEnumerableProperty( obj, 'boop' ); +* // returns false +* +* bool = isNonEnumerableProperty( obj, 'beep' ); +* // returns true +*/ +declare function isNonEnumerableProperty( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isNonEnumerableProperty; diff --git a/is-nonenumerable-property/docs/types/test.ts b/is-nonenumerable-property/docs/types/test.ts new file mode 100644 index 00000000..6a8341da --- /dev/null +++ b/is-nonenumerable-property/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNonEnumerableProperty = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + Object.defineProperty( obj, 'beep', { + 'configurable': true, + 'enumerable': false, + 'writable': true, + 'value': 'beep' + } ); + isNonEnumerableProperty( obj, 'beep' ); // $ExpectType boolean + isNonEnumerableProperty( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNonEnumerableProperty(); // $ExpectError + isNonEnumerableProperty( {} ); // $ExpectError + isNonEnumerableProperty( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-nonenumerable-property/examples/index.js b/is-nonenumerable-property/examples/index.js new file mode 100644 index 00000000..4ec1453b --- /dev/null +++ b/is-nonenumerable-property/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isNonEnumerableProperty = require( './../lib' ); + +var bool = isNonEnumerableProperty( [ 'a' ], 'length' ); +console.log( bool ); +// => true + +bool = isNonEnumerableProperty( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => false + +bool = isNonEnumerableProperty( [ 'a' ], 0 ); +console.log( bool ); +// => false + +bool = isNonEnumerableProperty( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isNonEnumerableProperty( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isNonEnumerableProperty( null, 'a' ); +console.log( bool ); +// => false + +bool = isNonEnumerableProperty( void 0, 'a' ); +console.log( bool ); +// => false + +bool = isNonEnumerableProperty( { 'null': false }, null ); +console.log( bool ); +// => false + +bool = isNonEnumerableProperty( { '[object Object]': false }, {} ); +console.log( bool ); +// => false diff --git a/is-nonenumerable-property/lib/index.js b/is-nonenumerable-property/lib/index.js new file mode 100644 index 00000000..accffc1e --- /dev/null +++ b/is-nonenumerable-property/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own property is non-enumerable. +* +* @module @stdlib/assert/is-nonenumerable-property +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isNonEnumerableProperty = require( '@stdlib/assert/is-nonenumerable-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'writable': true, +* 'value': 'beep' +* }); +* +* var bool = isNonEnumerableProperty( obj, 'boop' ); +* // returns false +* +* bool = isNonEnumerableProperty( obj, 'beep' ); +* // returns true +*/ + +// MODULES // + +var isNonEnumerableProperty = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNonEnumerableProperty; diff --git a/is-nonenumerable-property/lib/main.js b/is-nonenumerable-property/lib/main.js new file mode 100644 index 00000000..3243bdd4 --- /dev/null +++ b/is-nonenumerable-property/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isEnumerableProperty = require( '@stdlib/assert/is-enumerable-property' ); + + +// MAIN // + +/** +* Tests if an object's own property is non-enumerable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is non-enumerable +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'writable': true, +* 'value': 'beep' +* }); +* +* var bool = isNonEnumerableProperty( obj, 'boop' ); +* // returns false +* +* bool = isNonEnumerableProperty( obj, 'beep' ); +* // returns true +*/ +function isNonEnumerableProperty( value, property ) { + if ( hasOwnProp( value, property ) === false ) { + return false; + } + return ( isEnumerableProperty( value, property ) === false ); +} + + +// EXPORTS // + +module.exports = isNonEnumerableProperty; diff --git a/is-nonenumerable-property/package.json b/is-nonenumerable-property/package.json new file mode 100644 index 00000000..395635e4 --- /dev/null +++ b/is-nonenumerable-property/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-nonenumerable-property", + "version": "0.0.0", + "description": "Test if an object's own property is non-enumerable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "enumerable", + "non-enumerable", + "enum", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-nonenumerable-property/test/test.js b/is-nonenumerable-property/test/test.js new file mode 100644 index 00000000..c67a584a --- /dev/null +++ b/is-nonenumerable-property/test/test.js @@ -0,0 +1,132 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isNonEnumerableProperty = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonEnumerableProperty, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isNonEnumerableProperty( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isNonEnumerableProperty( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if an object property is enumerable', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isNonEnumerableProperty( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonEnumerableProperty( [ 1, 2, 3 ], '1' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonEnumerableProperty( [ 1, 2, 3 ], 1 ); + t.equal( bool, false, 'returns false' ); + + bool = isNonEnumerableProperty( new Foo(), 'bar' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a non-enumerable property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + bool = isNonEnumerableProperty( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `true` if an object property is non-enumerable', function test( t ) { + var bool; + var obj; + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'b' + }); + + bool = isNonEnumerableProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isNonEnumerableProperty( [ 'a' ], 'length' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an inherited property', function test( t ) { + var bool; + + bool = isNonEnumerableProperty( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonEnumerableProperty( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isNonEnumerableProperty( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isNonEnumerableProperty( 'beep', 'length' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/is-nonnegative-integer-array/README.md b/is-nonnegative-integer-array/README.md new file mode 100644 index 00000000..85cbe3d4 --- /dev/null +++ b/is-nonnegative-integer-array/README.md @@ -0,0 +1,124 @@ + + +# isNonNegativeIntegerArray + +> Test if a value is an array-like object containing only nonnegative integers. + +
+ +## Usage + +```javascript +var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ); +``` + +#### isNonNegativeIntegerArray( value ) + +Tests if a `value` is an array-like object containing **only** nonnegative `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonNegativeIntegerArray( [ 3.0, new Number(3.0) ] ); +// returns true + +bool = isNonNegativeIntegerArray( [ 3.0, '3.0' ] ); +// returns false +``` + +#### isNonNegativeIntegerArray.primitives( value ) + +Tests if a `value` is an array-like object containing **only** nonnegative primitive `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonNegativeIntegerArray.primitives( [ 1.0, 0.0, 10.0 ] ); +// returns true + +bool = isNonNegativeIntegerArray.primitives( [ 3.0, new Number(1.0) ] ); +// returns false +``` + +#### isNonNegativeIntegerArray.objects( value ) + +Tests if a `value` is an array-like object containing **only** nonnegative object `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonNegativeIntegerArray.objects( [ new Number(3.0), new Number(1.0) ] ); +// returns true + +bool = isNonNegativeIntegerArray.objects( [ 1.0, 0.0, 10.0 ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ); + +var bool = isNonNegativeIntegerArray( [ 5, 2, 3 ] ); +// returns true + +bool = isNonNegativeIntegerArray( [ 0, 1, 2, 3, 4 ] ); +// returns true + +bool = isNonNegativeIntegerArray( [ 1, new Number( 6 ), 3 ] ); +// returns true + +bool = isNonNegativeIntegerArray( [ 1, 'abc', 3 ] ); +// returns false + +bool = isNonNegativeIntegerArray( [ 2.3, 1, 3 ] ); +// returns false + +bool = isNonNegativeIntegerArray( [] ); +// returns false +``` + +
+ + + + + + diff --git a/is-nonnegative-integer-array/benchmark/benchmark.js b/is-nonnegative-integer-array/benchmark/benchmark.js new file mode 100644 index 00000000..6a7909e4 --- /dev/null +++ b/is-nonnegative-integer-array/benchmark/benchmark.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var isNonNegativeIntegerArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( 1.0 ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( 1.0 ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isNonNegativeIntegerArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isNonNegativeIntegerArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isNonNegativeIntegerArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-nonnegative-integer-array/docs/repl.txt b/is-nonnegative-integer-array/docs/repl.txt new file mode 100644 index 00000000..d05e3f8d --- /dev/null +++ b/is-nonnegative-integer-array/docs/repl.txt @@ -0,0 +1,74 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only nonnegative + integers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + nonnegative integers. + + Examples + -------- + > var bool = {{alias}}( [ 3.0, new Number(3.0) ] ) + true + > bool = {{alias}}( [ 3.0, '3.0' ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only nonnegative + primitive integer values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + nonnegative primitive integer values. + + Examples + -------- + > var bool = {{alias}}.primitives( [ 1.0, 0.0, 10.0 ] ) + true + > bool = {{alias}}.primitives( [ 3.0, new Number(1.0) ] ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only number objects + having nonnegative integer values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + number objects having nonnegative integer values. + + Examples + -------- + > var bool = {{alias}}.objects( [ new Number(1.0), new Number(10.0) ] ) + true + > bool = {{alias}}.objects( [ 1.0, 0.0, 10.0 ] ) + false + > bool = {{alias}}.objects( [ 3.0, new Number(1.0) ] ) + false + + See Also + -------- + diff --git a/is-nonnegative-integer-array/docs/types/index.d.ts b/is-nonnegative-integer-array/docs/types/index.d.ts new file mode 100644 index 00000000..cf8b12b7 --- /dev/null +++ b/is-nonnegative-integer-array/docs/types/index.d.ts @@ -0,0 +1,101 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNonNegativeIntegerArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsNonNegativeIntegerArray { + /** + * Tests if a value is an array-like object containing only nonnegative integers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only nonnegative integers + * + * @example + * var bool = isNonNegativeIntegerArray( [ 3.0, new (3.0) ] ); + * // returns true + * + * @example + * var bool = isNonNegativeIntegerArray( [ 3.0, '3.0' ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only nonnegative primitive integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only nonnegative primitive integer values + * + * @example + * var bool = isNonNegativeIntegerArray.primitives( [ 1.0, 0.0, 10.0 ] ); + * // returns true + * + * @example + * var bool = isNonNegativeIntegerArray.primitives( [ 3.0, new Number(1.0) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects having nonnegative integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number objects having nonnegative integer values + * + * @example + * var bool = isNonNegativeIntegerArray.objects( [ new Number(3.0), new Number(1.0) ] ); + * // returns true + * + * @example + * var bool = isNonNegativeIntegerArray.objects( [ 1.0, 0.0, 10.0 ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object containing only nonnegative integers. +* +* @param value - value to test +* @returns boolean indicating whether value is an array-like object containing only nonnegative integers +* +* @example +* var bool = isNonNegativeIntegerArray( [ 3.0, new Number(3.0) ] ); +* // returns true +* +* @example +* var bool = isNonNegativeIntegerArray( [ 3.0, '3.0' ] ); +* // returns false +* +* @example +* var bool = isNonNegativeIntegerArray.primitives( [ 1.0, 0.0, 10.0 ] ); +* // returns true +* +* @example +* var bool = isNonNegativeIntegerArray.objects( [ new Number(3.0), new Number(1.0) ] ); +* // returns true +*/ +declare var isNonNegativeIntegerArray: IsNonNegativeIntegerArray; + + +// EXPORTS // + +export = isNonNegativeIntegerArray; diff --git a/is-nonnegative-integer-array/docs/types/test.ts b/is-nonnegative-integer-array/docs/types/test.ts new file mode 100644 index 00000000..42bbceb0 --- /dev/null +++ b/is-nonnegative-integer-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNonNegativeIntegerArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNonNegativeIntegerArray( [ 4 ] ); // $ExpectType boolean + isNonNegativeIntegerArray( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNonNegativeIntegerArray(); // $ExpectError + isNonNegativeIntegerArray( [ 3 ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonNegativeIntegerArray.primitives( [ new Number( 3 ) ] ); // $ExpectType boolean + isNonNegativeIntegerArray.primitives( [ 3 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isNonNegativeIntegerArray.primitives(); // $ExpectError + isNonNegativeIntegerArray.primitives( [ 2 ], 123 ); // $ExpectError +} + + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonNegativeIntegerArray.objects( [ new Number( 2 ) ] ); // $ExpectType boolean + isNonNegativeIntegerArray.objects( [ 2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isNonNegativeIntegerArray.objects(); // $ExpectError + isNonNegativeIntegerArray.objects( [ 2 ], 123 ); // $ExpectError +} diff --git a/is-nonnegative-integer-array/examples/index.js b/is-nonnegative-integer-array/examples/index.js new file mode 100644 index 00000000..489e4697 --- /dev/null +++ b/is-nonnegative-integer-array/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeIntegerArray = require( './../lib' ); + +var bool = isNonNegativeIntegerArray( [ 5, 2, 3 ] ); +console.log( bool ); +// => true + +bool = isNonNegativeIntegerArray( [ 0, 1, 2, 3, 4 ] ); +console.log( bool ); +// => true + +bool = isNonNegativeIntegerArray( [ 1, new Number( 6 ), 3 ] ); +console.log( bool ); +// => true + +bool = isNonNegativeIntegerArray( [ 1, 'abc', 3 ] ); +console.log( bool ); +// => false + +bool = isNonNegativeIntegerArray( [ 2.3, 1, 3 ] ); +console.log( bool ); +// => false + +bool = isNonNegativeIntegerArray( [] ); +console.log( bool ); +// => false diff --git a/is-nonnegative-integer-array/lib/index.js b/is-nonnegative-integer-array/lib/index.js new file mode 100644 index 00000000..f13f6139 --- /dev/null +++ b/is-nonnegative-integer-array/lib/index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only nonnegative integers. +* +* @module @stdlib/assert/is-nonnegative-integer-array +* +* @example +* var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ); +* +* var bool = isNonNegativeIntegerArray( [ 3.0, new Number(3.0) ] ); +* // returns true +* +* bool = isNonNegativeIntegerArray( [ 3.0, '3.0' ] ); +* // returns false +* +* @example +* var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives; +* +* var bool = isNonNegativeIntegerArray( [ 1.0, 0.0, 10.0 ] ); +* // returns true +* +* bool = isNonNegativeIntegerArray( [ 3.0, new Number(1.0) ] ); +* // returns false +* +* @example +* var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).objects; +* +* var bool = isNonNegativeIntegerArray( [ new Number(3.0), new Number(1.0) ] ); +* // returns true +* +* bool = isNonNegativeIntegerArray( [ 1.0, 0.0, 10.0 ] ); +* // returns false +*/ + +// MODULES // + +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); + + +// MAIN // + +var isNonNegativeIntegerArray = arrayfun( isNonNegativeInteger ); +setReadOnly( isNonNegativeIntegerArray, 'primitives', arrayfun( isNonNegativeInteger.isPrimitive ) ); +setReadOnly( isNonNegativeIntegerArray, 'objects', arrayfun( isNonNegativeInteger.isObject ) ); + + +// EXPORTS // + +module.exports = isNonNegativeIntegerArray; diff --git a/is-nonnegative-integer-array/package.json b/is-nonnegative-integer-array/package.json new file mode 100644 index 00000000..a8064afc --- /dev/null +++ b/is-nonnegative-integer-array/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-nonnegative-integer-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only nonnegative integers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "number", + "numeric", + "nonnegative", + "isnumber", + "isnumeric", + "integer", + "int", + "isinteger", + "type", + "check" + ] +} diff --git a/is-nonnegative-integer-array/test/test.js b/is-nonnegative-integer-array/test/test.js new file mode 100644 index 00000000..081b4395 --- /dev/null +++ b/is-nonnegative-integer-array/test/test.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeIntegerArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonNegativeIntegerArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only nonnegative integer values', function test( t ) { + var arr; + + arr = [ 5.0, new Number( 5 ), 0 ]; + t.equal( isNonNegativeIntegerArray( arr ), true, 'returns true' ); + + arr = [ 5.0, '3', null ]; + t.equal( isNonNegativeIntegerArray( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': 5, + '1': 0 + }; + t.equal( isNonNegativeIntegerArray( arr ), true, 'returns true' ); + + arr = new Int32Array( [ 5, 0 ] ); + t.equal( isNonNegativeIntegerArray( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only primitive nonnegative integers', function test( t ) { + var arr; + + arr = [ 5.0, 0.0 ]; + t.equal( isNonNegativeIntegerArray.primitives( arr ), true, 'returns true' ); + + arr = [ new Number( 5 ), 1.0, 1.0 ]; + t.equal( isNonNegativeIntegerArray.primitives( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': 5, + '1': 0 + }; + t.equal( isNonNegativeIntegerArray.primitives( arr ), true, 'returns true' ); + + arr = new Int16Array( [ 5, 0 ] ); + t.equal( isNonNegativeIntegerArray.primitives( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only nonnegative object integers', function test( t ) { + var arr; + + arr = [ new Number( 5 ), new Number( 5 ) ]; + t.equal( isNonNegativeIntegerArray.objects( arr ), true, 'returns true' ); + + arr = [ 5.0, 0.0 ]; + t.equal( isNonNegativeIntegerArray.objects( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': new Number( 5 ), + '1': new Number( 0 ) + }; + t.equal( isNonNegativeIntegerArray.objects( arr ), true, 'returns true' ); + + arr = new Uint32Array( [ 5, 0 ] ); + t.equal( isNonNegativeIntegerArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-nonnegative-integer/README.md b/is-nonnegative-integer/README.md new file mode 100644 index 00000000..04e44490 --- /dev/null +++ b/is-nonnegative-integer/README.md @@ -0,0 +1,136 @@ + + +# isNonNegativeInteger + +> Test if a value is a number having a nonnegative integer value. + +
+ +## Usage + +```javascript +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ); +``` + +#### isNonNegativeInteger( value ) + +Tests if a `value` is a `number` having a nonnegative `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonNegativeInteger( 5.0 ); +// returns true + +bool = isNonNegativeInteger( new Number( 5.0 ) ); +// returns true + +bool = isNonNegativeInteger( -5.0 ); +// returns false + +bool = isNonNegativeInteger( 3.14 ); +// returns false + +bool = isNonNegativeInteger( null ); +// returns false +``` + +#### isNonNegativeInteger.isPrimitive( value ) + +Tests if a `value` is a primitive `number` having a nonnegative `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonNegativeInteger.isPrimitive( 3.0 ); +// returns true + +bool = isNonNegativeInteger.isPrimitive( new Number( 3.0 ) ); +// returns false +``` + +#### isNonNegativeInteger.isObject( value ) + +Tests if a `value` is a `Number` object having a nonnegative `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonNegativeInteger.isObject( 3.0 ); +// returns false + +bool = isNonNegativeInteger.isObject( new Number( 3.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ); + +var bool = isNonNegativeInteger( 5.0 ); +// returns true + +bool = isNonNegativeInteger( new Number( 5.0 ) ); +// returns true + +bool = isNonNegativeInteger( 0.0 ); +// returns true + +bool = isNonNegativeInteger( 3.14 ); +// returns false + +bool = isNonNegativeInteger( -5.0 ); +// returns false + +bool = isNonNegativeInteger( '5' ); +// returns false + +bool = isNonNegativeInteger( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-nonnegative-integer/benchmark/benchmark.js b/is-nonnegative-integer/benchmark/benchmark.js new file mode 100644 index 00000000..958c5f38 --- /dev/null +++ b/is-nonnegative-integer/benchmark/benchmark.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNonNegativeInteger = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonNegativeInteger( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonNegativeInteger( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonNegativeInteger.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonNegativeInteger.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonNegativeInteger.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonNegativeInteger.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-nonnegative-integer/docs/repl.txt b/is-nonnegative-integer/docs/repl.txt new file mode 100644 index 00000000..462be3fe --- /dev/null +++ b/is-nonnegative-integer/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is a nonnegative integer. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a nonnegative integer. + + Examples + -------- + > var bool = {{alias}}( 5.0 ) + true + > bool = {{alias}}( new Number( 5.0 ) ) + true + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( -5.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive having a nonnegative integer value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive having a + nonnegative integer value. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 3.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( 3.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a nonnegative integer value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a nonnegative + integer value. + + Examples + -------- + > var bool = {{alias}}.isObject( 3.0 ) + false + > bool = {{alias}}.isObject( new Number( 3.0 ) ) + true + + + See Also + -------- + diff --git a/is-nonnegative-integer/docs/types/index.d.ts b/is-nonnegative-integer/docs/types/index.d.ts new file mode 100644 index 00000000..9d7cb89b --- /dev/null +++ b/is-nonnegative-integer/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNonNegativeInteger` with methods for testing for primitives and objects, respectively. +*/ +interface IsNonNegativeInteger { + /** + * Tests if a value is a nonnegative integer. + * + * @param value - value to test + * @returns boolean indicating whether value is a nonnegative integer + * + * @example + * var bool = isNonNegativeInteger( 5.0 ); + * // returns true + * + * @example + * var bool = isNonNegativeInteger( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = isNonNegativeInteger( -5.0 ); + * // returns false + * + * @example + * var bool = isNonNegativeInteger( 3.14 ); + * // returns false + * + * @example + * var bool = isNonNegativeInteger( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a nonnegative integer value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a nonnegative integer value + * + * @example + * var bool = isNonNegativeInteger.isPrimitive( 3.0 ); + * // returns true + * + * @example + * var bool = isNonNegativeInteger.isPrimitive( new Number( 3.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a nonnegative integer value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a nonnegative integer value + * + * @example + * var bool = isNonNegativeInteger.isObject( 3.0 ); + * // returns false + * + * @example + * var bool = isNonNegativeInteger.isObject( new Number( 3.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a nonnegative integer. +* +* @param value - value to test +* @returns boolean indicating whether value is a nonnegative integer +* +* @example +* var bool = isNonNegativeInteger( 5.0 ); +* // returns true +* +* @example +* var bool = isNonNegativeInteger( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isNonNegativeInteger( -5.0 ); +* // returns false +* +* @example +* var bool = isNonNegativeInteger( 3.14 ); +* // returns false +* +* @example +* var bool = isNonNegativeInteger( null ); +* // returns false +* +* @example +* var bool = isNonNegativeInteger.isPrimitive( 3.0 ); +* // returns true +* +* @example +* var bool = isNonNegativeInteger.isObject( new Number( 3.0 ) ); +* // returns true +*/ +declare var isNonNegativeInteger: IsNonNegativeInteger; + + +// EXPORTS // + +export = isNonNegativeInteger; diff --git a/is-nonnegative-integer/docs/types/test.ts b/is-nonnegative-integer/docs/types/test.ts new file mode 100644 index 00000000..e1bcdc5f --- /dev/null +++ b/is-nonnegative-integer/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNonNegativeInteger = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNonNegativeInteger( 1.2 ); // $ExpectType boolean + isNonNegativeInteger( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNonNegativeInteger(); // $ExpectError + isNonNegativeInteger( 2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonNegativeInteger.isPrimitive( new Number( 2 ) ); // $ExpectType boolean + isNonNegativeInteger.isPrimitive( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isNonNegativeInteger.isPrimitive(); // $ExpectError + isNonNegativeInteger.isPrimitive( 2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonNegativeInteger.isObject( new Number( 2 ) ); // $ExpectType boolean + isNonNegativeInteger.isObject( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isNonNegativeInteger.isObject(); // $ExpectError + isNonNegativeInteger.isObject( 2, 123 ); // $ExpectError +} diff --git a/is-nonnegative-integer/examples/index.js b/is-nonnegative-integer/examples/index.js new file mode 100644 index 00000000..4bbb537a --- /dev/null +++ b/is-nonnegative-integer/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeInteger = require( './../lib' ); + +console.log( isNonNegativeInteger( 5.0 ) ); +// => true + +console.log( isNonNegativeInteger( new Number( 5.0 ) ) ); +// => true + +console.log( isNonNegativeInteger( 0.0 ) ); +// => true + +console.log( isNonNegativeInteger( 3.14 ) ); +// => false + +console.log( isNonNegativeInteger( -5.0 ) ); +// => false + +console.log( isNonNegativeInteger( '5' ) ); +// => false + +console.log( isNonNegativeInteger( null ) ); +// => false diff --git a/is-nonnegative-integer/lib/index.js b/is-nonnegative-integer/lib/index.js new file mode 100644 index 00000000..9f22521f --- /dev/null +++ b/is-nonnegative-integer/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a nonnegative integer. +* +* @module @stdlib/assert/is-nonnegative-integer +* +* @example +* var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ); +* +* var bool = isNonNegativeInteger( 5.0 ); +* // returns true +* +* bool = isNonNegativeInteger( new Number( 5.0 ) ); +* // returns true +* +* bool = isNonNegativeInteger( -5.0 ); +* // returns false +* +* bool = isNonNegativeInteger( 3.14 ); +* // returns false +* +* bool = isNonNegativeInteger( null ); +* // returns false +* +* @example +* var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +* +* var bool = isNonNegativeInteger( 3.0 ); +* // returns true +* +* bool = isNonNegativeInteger( new Number( 3.0 ) ); +* // returns false +* +* @example +* var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isObject; +* +* var bool = isNonNegativeInteger( 3.0 ); +* // returns false +* +* bool = isNonNegativeInteger( new Number( 3.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isNonNegativeInteger = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isNonNegativeInteger, 'isPrimitive', isPrimitive ); +setReadOnly( isNonNegativeInteger, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isNonNegativeInteger; diff --git a/is-nonnegative-integer/lib/main.js b/is-nonnegative-integer/lib/main.js new file mode 100644 index 00000000..72b3ed8a --- /dev/null +++ b/is-nonnegative-integer/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a nonnegative integer. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a nonnegative integer +* +* @example +* var bool = isNonNegativeInteger( 5.0 ); +* // returns true +* +* @example +* var bool = isNonNegativeInteger( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isNonNegativeInteger( -5.0 ); +* // returns false +* +* @example +* var bool = isNonNegativeInteger( 3.14 ); +* // returns false +* +* @example +* var bool = isNonNegativeInteger( null ); +* // returns false +*/ +function isNonNegativeInteger( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isNonNegativeInteger; diff --git a/is-nonnegative-integer/lib/object.js b/is-nonnegative-integer/lib/object.js new file mode 100644 index 00000000..8eea5533 --- /dev/null +++ b/is-nonnegative-integer/lib/object.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isObject; + + +// MAIN // + +/** +* Tests if a value is a number object having a nonnegative integer value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a nonnegative integer value +* +* @example +* var bool = isNonNegativeInteger( 3.0 ); +* // returns false +* +* @example +* var bool = isNonNegativeInteger( new Number( 3.0 ) ); +* // returns true +*/ +function isNonNegativeInteger( value ) { + return ( + isInteger( value ) && + value.valueOf() >= 0 + ); +} + + +// EXPORTS // + +module.exports = isNonNegativeInteger; diff --git a/is-nonnegative-integer/lib/primitive.js b/is-nonnegative-integer/lib/primitive.js new file mode 100644 index 00000000..71713db4 --- /dev/null +++ b/is-nonnegative-integer/lib/primitive.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a number primitive having a nonnegative integer value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a nonnegative integer value +* +* @example +* var bool = isNonNegativeInteger( 3.0 ); +* // returns true +* +* @example +* var bool = isNonNegativeInteger( new Number( 3.0 ) ); +* // returns false +*/ +function isNonNegativeInteger( value ) { + return ( + isInteger( value ) && + value >= 0 + ); +} + + +// EXPORTS // + +module.exports = isNonNegativeInteger; diff --git a/is-nonnegative-integer/package.json b/is-nonnegative-integer/package.json new file mode 100644 index 00000000..53f1d3ec --- /dev/null +++ b/is-nonnegative-integer/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-nonnegative-integer", + "version": "0.0.0", + "description": "Test if a value is a number having a nonnegative integer value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "integer", + "int", + "nonnegative", + "is", + "isinteger", + "type", + "check", + "primitive", + "object" + ] +} diff --git a/is-nonnegative-integer/test/test.js b/is-nonnegative-integer/test/test.js new file mode 100644 index 00000000..4b6878bd --- /dev/null +++ b/is-nonnegative-integer/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNonNegativeInteger = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonNegativeInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number having a nonnegative integer value', function test( t ) { + t.equal( typeof isNonNegativeInteger.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a nonnegative integer value', function test( t ) { + t.equal( typeof isNonNegativeInteger.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-nonnegative-integer/test/test.main.js b/is-nonnegative-integer/test/test.main.js new file mode 100644 index 00000000..7cee798a --- /dev/null +++ b/is-nonnegative-integer/test/test.main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeInteger = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonNegativeInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number having a nonnegative integer value', function test( t ) { + t.equal( isNonNegativeInteger( 5.0 ), true, 'returns true' ); + t.equal( isNonNegativeInteger( new Number( 5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number having a nonnegative integer value', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + -1.0, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNonNegativeInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nonnegative-integer/test/test.object.js b/is-nonnegative-integer/test/test.object.js new file mode 100644 index 00000000..ea3e9ee1 --- /dev/null +++ b/is-nonnegative-integer/test/test.object.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeInteger = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonNegativeInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a nonnegative integer value', function test( t ) { + t.equal( isNonNegativeInteger( new Number( 5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is a nonnegative integer value', function test( t ) { + t.equal( isNonNegativeInteger( 3.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNonNegativeInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nonnegative-integer/test/test.primitive.js b/is-nonnegative-integer/test/test.primitive.js new file mode 100644 index 00000000..07778703 --- /dev/null +++ b/is-nonnegative-integer/test/test.primitive.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeInteger = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonNegativeInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number having a nonnegative integer value', function test( t ) { + t.equal( isNonNegativeInteger( 3.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has a nonnegative integer value', function test( t ) { + t.equal( isNonNegativeInteger( new Number( 5.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNonNegativeInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nonnegative-number-array/README.md b/is-nonnegative-number-array/README.md new file mode 100644 index 00000000..a75163a2 --- /dev/null +++ b/is-nonnegative-number-array/README.md @@ -0,0 +1,121 @@ + + +# isNonNegativeNumberArray + +> Test if a value is an array-like object containing only nonnegative numbers. + +
+ +## Usage + +```javascript +var isNonNegativeNumberArray = require( '@stdlib/assert/is-nonnegative-number-array' ); +``` + +#### isNonNegativeNumberArray( value ) + +Tests if a `value` is an array-like object containing **only** nonnegative numbers. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonNegativeNumberArray( [ 3.0, new Number(3.0) ] ); +// returns true + +bool = isNonNegativeNumberArray( [ 3.0, '3.0' ] ); +// returns false +``` + +#### isNonNegativeNumberArray.primitives( value ) + +Tests if a `value` is an array-like object `array` containing **only** primitive nonnegative numbers. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonNegativeNumberArray.primitives( [ 1.0, 0.0, 10.0 ] ); +// returns true + +bool = isNonNegativeNumberArray.primitives( [ 3.0, new Number(1.0) ] ); +// returns false +``` + +#### isNonNegativeNumberArray.objects( value ) + +Tests if a `value` is an array-like object `array` containing **only** object nonnegative numbers. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonNegativeNumberArray.objects( [ new Number(1.0), new Number(1.0) ] ); +// returns true + +bool = isNonNegativeNumberArray.objects( [ 1.0, 0.0, 10.0 ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeNumberArray = require( '@stdlib/assert/is-nonnegative-number-array' ); + +var bool = isNonNegativeNumberArray( [ 5.0, 0.2, 3.9 ] ); +// returns true + +bool = isNonNegativeNumberArray( [ 1, 2, 3 ] ); +// returns true + +bool = isNonNegativeNumberArray( [ 1, new Number( 6 ), 3 ] ); +// returns true + +bool = isNonNegativeNumberArray( [ 1, 'abc', 3 ] ); +// returns false + +bool = isNonNegativeNumberArray( 78.0 ); +// returns false +``` + +
+ + + + + + diff --git a/is-nonnegative-number-array/benchmark/benchmark.js b/is-nonnegative-number-array/benchmark/benchmark.js new file mode 100644 index 00000000..90dde594 --- /dev/null +++ b/is-nonnegative-number-array/benchmark/benchmark.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var isNonNegativeNumberArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( 1.0 ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( 1.0 ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = -i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isNonNegativeNumberArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isNonNegativeNumberArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isNonNegativeNumberArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-nonnegative-number-array/docs/repl.txt b/is-nonnegative-number-array/docs/repl.txt new file mode 100644 index 00000000..9fd64307 --- /dev/null +++ b/is-nonnegative-number-array/docs/repl.txt @@ -0,0 +1,74 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only nonnegative + numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + nonnegative numbers. + + Examples + -------- + > var bool = {{alias}}( [ 3.0, new Number(3.0) ] ) + true + > bool = {{alias}}( [ 3.0, '3.0' ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only primitive + nonnegative numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + primitive nonnegative numbers. + + Examples + -------- + > var bool = {{alias}}.primitives( [ 1.0, 0.0, 10.0 ] ) + true + > bool = {{alias}}.primitives( [ 3.0, new Number(1.0) ] ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only number objects + having nonnegative number values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + number objects having nonnegative number values. + + Examples + -------- + > var bool = {{alias}}.objects( [ new Number(1.0), new Number(10.0) ] ) + true + > bool = {{alias}}.objects( [ 1.0, 0.0, 10.0 ] ) + false + > bool = {{alias}}.objects( [ 3.0, new Number(1.0) ] ) + false + + See Also + -------- + diff --git a/is-nonnegative-number-array/docs/types/index.d.ts b/is-nonnegative-number-array/docs/types/index.d.ts new file mode 100644 index 00000000..defb0aae --- /dev/null +++ b/is-nonnegative-number-array/docs/types/index.d.ts @@ -0,0 +1,101 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNonNegativeNumberArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsNonNegativeNumberArray { + /** + * Tests if a value is an array-like object containing only nonnegative numbers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only nonnegative numbers + * + * @example + * var bool = isNonNegativeNumberArray( [ 3.0, new Number(3.0) ] ); + * // returns true + * + * @example + * var bool = isNonNegativeNumberArray( [ 3.0, '3.0' ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only nonnegative primitive number values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only nonnegative primitive number values + * + * @example + * var bool = isNonNegativeNumberArray.primitives( [ 1.0, 0.0, 10.0 ] ); + * // returns true + * + * @example + * var bool = isNonNegativeNumberArray.primitives( [ 3.0, new Number(1.0) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects having nonnegative number values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number objects having nonnegative number values + * + * @example + * var bool = isNonNegativeNumberArray.objects( [ new Number(3.0), new Number(1.0) ] ); + * // returns true + * + * @example + * var bool = isNonNegativeNumberArray.objects( [ 1.0, 0.0, 10.0 ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object containing only nonnegative numbers. +* +* @param value - value to test +* @returns boolean indicating whether value is an array-like object containing only nonnegative numbers +* +* @example +* var bool = isNonNegativeNumberArray( [ 3.0, new Number(3.0) ] ); +* // returns true +* +* @example +* var bool = isNonNegativeNumberArray( [ 3.0, '3.0' ] ); +* // returns false +* +* @example +* var bool = isNonNegativeNumberArray.primitives( [ 1.0, 0.0, 10.0 ] ); +* // returns true +* +* @example +* var bool = isNonNegativeNumberArray.objects( [ new Number(3.0), new Number(1.0) ] ); +* // returns true +*/ +declare var isnonnegativeNumberArray: IsNonNegativeNumberArray; + + +// EXPORTS // + +export = isnonnegativeNumberArray; diff --git a/is-nonnegative-number-array/docs/types/test.ts b/is-nonnegative-number-array/docs/types/test.ts new file mode 100644 index 00000000..4e61dc42 --- /dev/null +++ b/is-nonnegative-number-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNonNegativeNumberArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNonNegativeNumberArray( [ 3.9 ] ); // $ExpectType boolean + isNonNegativeNumberArray( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNonNegativeNumberArray(); // $ExpectError + isNonNegativeNumberArray( [ 2.8 ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonNegativeNumberArray.primitives( [ new Number( 2.8 ) ] ); // $ExpectType boolean + isNonNegativeNumberArray.primitives( [ 2.8 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isNonNegativeNumberArray.primitives(); // $ExpectError + isNonNegativeNumberArray.primitives( [ 2 ], 123 ); // $ExpectError +} + + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonNegativeNumberArray.objects( [ new Number( 2 ) ] ); // $ExpectType boolean + isNonNegativeNumberArray.objects( [ 2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isNonNegativeNumberArray.objects(); // $ExpectError + isNonNegativeNumberArray.objects( [ 2 ], 123 ); // $ExpectError +} diff --git a/is-nonnegative-number-array/examples/index.js b/is-nonnegative-number-array/examples/index.js new file mode 100644 index 00000000..2b0255f6 --- /dev/null +++ b/is-nonnegative-number-array/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeNumberArray = require( './../lib' ); + +var bool = isNonNegativeNumberArray( [ 5.0, 0.2, 3.9 ] ); +console.log( bool ); +// => true + +bool = isNonNegativeNumberArray( [ 1, 2, 3 ] ); +console.log( bool ); +// => true + +bool = isNonNegativeNumberArray( [ 1, new Number( 6 ), 3 ] ); +console.log( bool ); +// => true + +bool = isNonNegativeNumberArray( [ 1, 'abc', 3 ] ); +console.log( bool ); +// => false + +bool = isNonNegativeNumberArray( 78.0 ); +console.log( bool ); +// => false + +bool = isNonNegativeNumberArray( [] ); +console.log( bool ); +// => false diff --git a/is-nonnegative-number-array/lib/index.js b/is-nonnegative-number-array/lib/index.js new file mode 100644 index 00000000..7450c14d --- /dev/null +++ b/is-nonnegative-number-array/lib/index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only nonnegative numbers. +* +* @module @stdlib/assert/is-nonnegative-number-array +* +* @example +* var isNonNegativeNumberArray = require( '@stdlib/assert/is-nonnegative-number-array' ); +* +* var bool = isNonNegativeNumberArray( [ 3.0, new Number(3.0) ] ); +* // returns true +* +* bool = isNonNegativeNumberArray( [ 3.0, '3.0' ] ); +* // returns false +* +* @example +* var isNonNegativeNumberArray = require( '@stdlib/assert/is-nonnegative-number-array' ).primitives; +* +* var bool = isNonNegativeNumberArray( [ 1.0, 0.0, 10.0 ] ); +* // returns true +* +* bool = isNonNegativeNumberArray( [ 3.0, new Number(1.0) ] ); +* // returns false +* +* @example +* var isNonNegativeNumberArray = require( '@stdlib/assert/is-nonnegative-number-array' ).objects; +* +* var bool = isNonNegativeNumberArray( [ new Number(3.0), new Number(1.0) ] ); +* // returns true +* +* bool = isNonNegativeNumberArray( [ 1.0, 0.0, 10.0 ] ); +* // returns false +*/ + +// MODULES // + +var isNonNegativeNumber = require( '@stdlib/assert/is-nonnegative-number' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); + + +// MAIN // + +var isNonNegativeNumberArray = arrayfun( isNonNegativeNumber ); +setReadOnly( isNonNegativeNumberArray, 'primitives', arrayfun( isNonNegativeNumber.isPrimitive ) ); +setReadOnly( isNonNegativeNumberArray, 'objects', arrayfun( isNonNegativeNumber.isObject ) ); + + +// EXPORTS // + +module.exports = isNonNegativeNumberArray; diff --git a/is-nonnegative-number-array/package.json b/is-nonnegative-number-array/package.json new file mode 100644 index 00000000..3bddd7f9 --- /dev/null +++ b/is-nonnegative-number-array/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-nonnegative-number-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only nonnegative numbers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "number", + "numeric", + "nonnegative", + "isnumber", + "isnumeric", + "type", + "check" + ] +} diff --git a/is-nonnegative-number-array/test/test.js b/is-nonnegative-number-array/test/test.js new file mode 100644 index 00000000..e0cfb940 --- /dev/null +++ b/is-nonnegative-number-array/test/test.js @@ -0,0 +1,101 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeNumberArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonNegativeNumberArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only nonnegative numbers', function test( t ) { + var arr; + + arr = [ 5.0, new Number( 5 ), 0 ]; + t.equal( isNonNegativeNumberArray( arr ), true, 'returns true' ); + + arr = [ 5.0, '3', null ]; + t.equal( isNonNegativeNumberArray( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': 3.14, + '1': 0.0 + }; + t.equal( isNonNegativeNumberArray( arr ), true, 'returns true' ); + + arr = new Float32Array( [ 5.0, 1.5 ] ); + t.equal( isNonNegativeNumberArray( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object having only nonnegative number primitives', function test( t ) { + var arr; + + arr = [ 5.0, 0.0 ]; + t.equal( isNonNegativeNumberArray.primitives( arr ), true, 'returns true' ); + + arr = [ new Number( 5 ), 1.0, 1.0 ]; + t.equal( isNonNegativeNumberArray.primitives( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': 3.14, + '1': 0.0 + }; + t.equal( isNonNegativeNumberArray.primitives( arr ), true, 'returns true' ); + + arr = new Float32Array( [ 5.0, 1.5 ] ); + t.equal( isNonNegativeNumberArray.primitives( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object having only nonnegative number objects', function test( t ) { + var arr; + + arr = [ new Number( 5 ), new Number( 5 ) ]; + t.equal( isNonNegativeNumberArray.objects( arr ), true, 'returns true' ); + + arr = [ 5.0, 0.0 ]; + t.equal( isNonNegativeNumberArray.objects( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': new Number( 3.14 ), + '1': new Number( 0.0 ) + }; + t.equal( isNonNegativeNumberArray.objects( arr ), true, 'returns true' ); + + arr = new Float32Array( [ 5.0, 1.5 ] ); + t.equal( isNonNegativeNumberArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-nonnegative-number/README.md b/is-nonnegative-number/README.md new file mode 100644 index 00000000..dc74a49a --- /dev/null +++ b/is-nonnegative-number/README.md @@ -0,0 +1,136 @@ + + +# isNonNegativeNumber + +> Test if a value is a number having a nonnegative value. + +
+ +## Usage + +```javascript +var isNonNegativeNumber = require( '@stdlib/assert/is-nonnegative-number' ); +``` + +#### isNonNegativeNumber( value ) + +Tests if a `value` is a `number` having a nonnegative value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonNegativeNumber( 5.0 ); +// returns true + +bool = isNonNegativeNumber( new Number( 5.0 ) ); +// returns true + +bool = isNonNegativeNumber( 3.14 ); +// returns true + +bool = isNonNegativeNumber( -5.0 ); +// returns false + +bool = isNonNegativeNumber( null ); +// returns false +``` + +#### isNonNegativeNumber.isPrimitive( value ) + +Tests if a `value` is a primitive `number` having a nonnegative value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonNegativeNumber.isPrimitive( 3.0 ); +// returns true + +bool = isNonNegativeNumber.isPrimitive( new Number( 3.0 ) ); +// returns false +``` + +#### isNonNegativeNumber.isObject( value ) + +Tests if a `value` is a `Number` object having a nonnegative value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonNegativeNumber.isObject( 3.0 ); +// returns false + +bool = isNonNegativeNumber.isObject( new Number( 3.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeNumber = require( '@stdlib/assert/is-nonnegative-number' ); + +var bool = isNonNegativeNumber( 5.0 ); +// returns true + +bool = isNonNegativeNumber( new Number( 5.0 ) ); +// returns true + +bool = isNonNegativeNumber( 0.0 ); +// returns true + +bool = isNonNegativeNumber( 3.14 ); +// returns true + +bool = isNonNegativeNumber( -5.0 ); +// returns false + +bool = isNonNegativeNumber( '5' ); +// returns false + +bool = isNonNegativeNumber( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-nonnegative-number/benchmark/benchmark.js b/is-nonnegative-number/benchmark/benchmark.js new file mode 100644 index 00000000..c9e131dd --- /dev/null +++ b/is-nonnegative-number/benchmark/benchmark.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNonNegativeNumber = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonNegativeNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonNegativeNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonNegativeNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonNegativeNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonNegativeNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonNegativeNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-nonnegative-number/docs/repl.txt b/is-nonnegative-number/docs/repl.txt new file mode 100644 index 00000000..28374bdf --- /dev/null +++ b/is-nonnegative-number/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is a nonnegative number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a nonnegative number. + + Examples + -------- + > var bool = {{alias}}( 5.0 ) + true + > bool = {{alias}}( new Number( 5.0 ) ) + true + > bool = {{alias}}( 3.14 ) + true + > bool = {{alias}}( -5.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive having a nonnegative value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive having a + nonnegative value. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 3.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( 3.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a nonnegative value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a nonnegative + value. + + Examples + -------- + > var bool = {{alias}}.isObject( 3.0 ) + false + > bool = {{alias}}.isObject( new Number( 3.0 ) ) + true + + + See Also + -------- + diff --git a/is-nonnegative-number/docs/types/index.d.ts b/is-nonnegative-number/docs/types/index.d.ts new file mode 100644 index 00000000..bd2a7a6e --- /dev/null +++ b/is-nonnegative-number/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNonNegativeNumber` with methods for testing for primitives and objects, respectively. +*/ +interface IsNonNegativeNumber { + /** + * Tests if a value is a nonnegative number. + * + * @param value - value to test + * @returns boolean indicating whether value is a nonnegative number + * + * @example + * var bool = isNonNegativeNumber( 5.0 ); + * // returns true + * + * @example + * var bool = isNonNegativeNumber( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = isNonNegativeNumber( 3.14 ); + * // returns true + * + * @example + * var bool = isNonNegativeNumber( -5.0 ); + * // returns false + * + * @example + * var bool = isNonNegativeNumber( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a nonnegative value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a nonnegative number value + * + * @example + * var bool = isNonNegativeNumber.isPrimitive( 3.0 ); + * // returns true + * + * @example + * var bool = isNonNegativeNumber.isPrimitive( new Number( 3.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a nonnegative value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a nonnegative number value + * + * @example + * var bool = isNonNegativeNumber.isObject( 3.0 ); + * // returns false + * + * @example + * var bool = isNonNegativeNumber.isObject( new Number( 3.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a nonnegative number. +* +* @param value - value to test +* @returns boolean indicating whether value is a nonnegative number +* +* @example +* var bool = isNonNegativeNumber( 5.0 ); +* // returns true +* +* @example +* var bool = isNonNegativeNumber( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isNonNegativeNumber( 3.14 ); +* // returns true +* +* @example +* var bool = isNonNegativeNumber( -5.0 ); +* // returns false +* +* @example +* var bool = isNonNegativeNumber( null ); +* // returns false +* +* @example +* var bool = isNonNegativeNumber.isPrimitive( 3.0 ); +* // returns true +* +* @example +* var bool = isNonNegativeNumber.isObject( new Number( 3.0 ) ); +* // returns true +*/ +declare var isNonNegativeNumber: IsNonNegativeNumber; + + +// EXPORTS // + +export = isNonNegativeNumber; diff --git a/is-nonnegative-number/docs/types/test.ts b/is-nonnegative-number/docs/types/test.ts new file mode 100644 index 00000000..814a363a --- /dev/null +++ b/is-nonnegative-number/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNonNegativeNumber = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNonNegativeNumber( 1.2 ); // $ExpectType boolean + isNonNegativeNumber( -1.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNonNegativeNumber(); // $ExpectError + isNonNegativeNumber( 2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonNegativeNumber.isPrimitive( new Number( 2 ) ); // $ExpectType boolean + isNonNegativeNumber.isPrimitive( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isNonNegativeNumber.isPrimitive(); // $ExpectError + isNonNegativeNumber.isPrimitive( 2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonNegativeNumber.isObject( new Number( 2 ) ); // $ExpectType boolean + isNonNegativeNumber.isObject( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isNonNegativeNumber.isObject(); // $ExpectError + isNonNegativeNumber.isObject( 2, 123 ); // $ExpectError +} diff --git a/is-nonnegative-number/examples/index.js b/is-nonnegative-number/examples/index.js new file mode 100644 index 00000000..cfc72922 --- /dev/null +++ b/is-nonnegative-number/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeNumber = require( './../lib' ); + +console.log( isNonNegativeNumber( 5.0 ) ); +// => true + +console.log( isNonNegativeNumber( new Number( 5.0 ) ) ); +// => true + +console.log( isNonNegativeNumber( 0.0 ) ); +// => true + +console.log( isNonNegativeNumber( 3.14 ) ); +// => true + +console.log( isNonNegativeNumber( -5.0 ) ); +// => false + +console.log( isNonNegativeNumber( '5' ) ); +// => false + +console.log( isNonNegativeNumber( null ) ); +// => false diff --git a/is-nonnegative-number/lib/index.js b/is-nonnegative-number/lib/index.js new file mode 100644 index 00000000..0346e26b --- /dev/null +++ b/is-nonnegative-number/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a nonnegative number. +* +* @module @stdlib/assert/is-nonnegative-number +* +* @example +* var isNonNegativeNumber = require( '@stdlib/assert/is-nonnegative-number' ); +* +* var bool = isNonNegativeNumber( 5.0 ); +* // returns true +* +* bool = isNonNegativeNumber( new Number( 5.0 ) ); +* // returns true +* +* bool = isNonNegativeNumber( 3.14 ); +* // returns true +* +* bool = isNonNegativeNumber( -5.0 ); +* // returns false +* +* bool = isNonNegativeNumber( null ); +* // returns false +* +* @example +* var isNonNegativeNumber = require( '@stdlib/assert/is-nonnegative-number' ).isPrimitive; +* +* var bool = isNonNegativeNumber( 3.0 ); +* // returns true +* +* bool = isNonNegativeNumber( new Number( 3.0 ) ); +* // returns false +* +* @example +* var isNonNegativeNumber = require( '@stdlib/assert/is-nonnegative-number' ).isObject; +* +* var bool = isNonNegativeNumber( 3.0 ); +* // returns false +* +* bool = isNonNegativeNumber( new Number( 3.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isNonNegativeNumber = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isNonNegativeNumber, 'isPrimitive', isPrimitive ); +setReadOnly( isNonNegativeNumber, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isNonNegativeNumber; diff --git a/is-nonnegative-number/lib/main.js b/is-nonnegative-number/lib/main.js new file mode 100644 index 00000000..aee5f9a5 --- /dev/null +++ b/is-nonnegative-number/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a nonnegative number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a nonnegative number +* +* @example +* var bool = isNonNegativeNumber( 5.0 ); +* // returns true +* +* @example +* var bool = isNonNegativeNumber( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isNonNegativeNumber( 3.14 ); +* // returns true +* +* @example +* var bool = isNonNegativeNumber( -5.0 ); +* // returns false +* +* @example +* var bool = isNonNegativeNumber( null ); +* // returns false +*/ +function isNonNegativeNumber( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isNonNegativeNumber; diff --git a/is-nonnegative-number/lib/object.js b/is-nonnegative-number/lib/object.js new file mode 100644 index 00000000..dfa12a70 --- /dev/null +++ b/is-nonnegative-number/lib/object.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; + + +// MAIN // + +/** +* Tests if a value is a number object having a nonnegative value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a nonnegative number value +* +* @example +* var bool = isNonNegativeNumber( 3.0 ); +* // returns false +* +* @example +* var bool = isNonNegativeNumber( new Number( 3.0 ) ); +* // returns true +*/ +function isNonNegativeNumber( value ) { + return ( + isNumber( value ) && + value.valueOf() >= 0.0 + ); +} + + +// EXPORTS // + +module.exports = isNonNegativeNumber; diff --git a/is-nonnegative-number/lib/primitive.js b/is-nonnegative-number/lib/primitive.js new file mode 100644 index 00000000..fe024605 --- /dev/null +++ b/is-nonnegative-number/lib/primitive.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a number primitive having a nonnegative value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a nonnegative number value +* +* @example +* var bool = isNonNegativeNumber( 3.0 ); +* // returns true +* +* @example +* var bool = isNonNegativeNumber( new Number( 3.0 ) ); +* // returns false +*/ +function isNonNegativeNumber( value ) { + return ( + isNumber( value ) && + value >= 0.0 + ); +} + + +// EXPORTS // + +module.exports = isNonNegativeNumber; diff --git a/is-nonnegative-number/package.json b/is-nonnegative-number/package.json new file mode 100644 index 00000000..959ad8a1 --- /dev/null +++ b/is-nonnegative-number/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-nonnegative-number", + "version": "0.0.0", + "description": "Test if a value is a number having a nonnegative value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "nonnegative", + "is", + "isnumber", + "isnumeric", + "type", + "check", + "primitive", + "object" + ] +} diff --git a/is-nonnegative-number/test/test.js b/is-nonnegative-number/test/test.js new file mode 100644 index 00000000..83e9ad43 --- /dev/null +++ b/is-nonnegative-number/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNonNegativeNumber = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonNegativeNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number having a nonnegative number value', function test( t ) { + t.equal( typeof isNonNegativeNumber.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a nonnegative number value', function test( t ) { + t.equal( typeof isNonNegativeNumber.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-nonnegative-number/test/test.main.js b/is-nonnegative-number/test/test.main.js new file mode 100644 index 00000000..3741543a --- /dev/null +++ b/is-nonnegative-number/test/test.main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeNumber = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonNegativeNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number having a nonnegative number value', function test( t ) { + t.equal( isNonNegativeNumber( 5.0 ), true, 'returns true' ); + t.equal( isNonNegativeNumber( new Number( 5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number having a nonnegative number value', function test( t ) { + var values; + var i; + + values = [ + '5', + -3.14, + -1.0, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNonNegativeNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nonnegative-number/test/test.object.js b/is-nonnegative-number/test/test.object.js new file mode 100644 index 00000000..0a8fafee --- /dev/null +++ b/is-nonnegative-number/test/test.object.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeNumber = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonNegativeNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a nonnegative number value', function test( t ) { + t.equal( isNonNegativeNumber( new Number( 5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is a nonnegative number value', function test( t ) { + t.equal( isNonNegativeNumber( 3.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a nonnegative number', function test( t ) { + var values; + var i; + + values = [ + '5', + new Number( -2.0 ), // eslint-disable-line no-new-wrappers + -3.14, + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNonNegativeNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nonnegative-number/test/test.primitive.js b/is-nonnegative-number/test/test.primitive.js new file mode 100644 index 00000000..f4634b69 --- /dev/null +++ b/is-nonnegative-number/test/test.primitive.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonNegativeNumber = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonNegativeNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number having a nonnegative number value', function test( t ) { + t.equal( isNonNegativeNumber( 3.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has a nonnegative number value', function test( t ) { + t.equal( isNonNegativeNumber( new Number( 5.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a nonnegative number', function test( t ) { + var values; + var i; + + values = [ + '5', + new Number( -1.0 ), // eslint-disable-line no-new-wrappers + -3.14, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNonNegativeNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nonpositive-integer-array/README.md b/is-nonpositive-integer-array/README.md new file mode 100644 index 00000000..64ea00ed --- /dev/null +++ b/is-nonpositive-integer-array/README.md @@ -0,0 +1,136 @@ + + +# isNonPositiveIntegerArray + +> Test if a value is an array-like object containing only nonpositive integers. + +
+ +## Usage + +```javascript +var isNonPositiveIntegerArray = require( '@stdlib/assert/is-nonpositive-integer-array' ); +``` + +#### isNonPositiveIntegerArray( value ) + +Tests if a `value` is an array-like object containing **only** nonpositive `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonPositiveIntegerArray( [ -3, new Number(-3) ] ); +// returns true + +bool = isNonPositiveIntegerArray( [ -3, '3.0' ] ); +// returns false +``` + +#### isNonPositiveIntegerArray.primitives( value ) + +Tests if a `value` is an array-like object containing **only** nonpositive primitive `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonPositiveIntegerArray.primitives( [ -1.0, -10.0 ] ); +// returns true + +bool = isNonPositiveIntegerArray.primitives( [ -1.0, 0.0, -10.0 ] ); +// returns true + +bool = isNonPositiveIntegerArray.primitives( [ -3.0, new Number(-1.0) ] ); +// returns false +``` + +#### isNonPositiveIntegerArray.objects( value ) + +Tests if a `value` is an array-like object containing **only** `Number` objects holding nonpositive `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonPositiveIntegerArray.objects( [ new Number(-1.0), new Number(-10.0) ] ); +// returns true + +bool = isNonPositiveIntegerArray.objects( [ -1.0, 0.0, -10.0 ] ); +// returns false + +bool = isNonPositiveIntegerArray.objects( [ -3.0, new Number(-1.0) ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveIntegerArray = require( '@stdlib/assert/is-nonpositive-integer-array' ); + +var bool = isNonPositiveIntegerArray( [ -5, -2, -3 ] ); +// returns true + +bool = isNonPositiveIntegerArray( [ -4, -3, -2, -1 ] ); +// returns true + +bool = isNonPositiveIntegerArray( [ -1, new Number( -6 ), -3 ] ); +// returns true + +bool = isNonPositiveIntegerArray( [ -3, -2, -1, 0 ] ); +// returns true + +bool = isNonPositiveIntegerArray( [ -3, -2, -1, 0, 2 ] ); +// returns false + +bool = isNonPositiveIntegerArray( [ -1, 'abc', -3 ] ); +// returns false + +bool = isNonPositiveIntegerArray( [ -2.3, -1, -3 ] ); +// returns false + +bool = isNonPositiveIntegerArray( [] ); +// returns false +``` + +
+ + + + + + diff --git a/is-nonpositive-integer-array/benchmark/benchmark.js b/is-nonpositive-integer-array/benchmark/benchmark.js new file mode 100644 index 00000000..56b12cea --- /dev/null +++ b/is-nonpositive-integer-array/benchmark/benchmark.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var isNonPositiveIntegerArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( -1.0 ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( -1.0 ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isNonPositiveIntegerArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isNonPositiveIntegerArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isNonPositiveIntegerArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-nonpositive-integer-array/docs/repl.txt b/is-nonpositive-integer-array/docs/repl.txt new file mode 100644 index 00000000..8762f43f --- /dev/null +++ b/is-nonpositive-integer-array/docs/repl.txt @@ -0,0 +1,74 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only nonpositive + integers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + nonpositive integers. + + Examples + -------- + > var bool = {{alias}}( [ -3.0, new Number(-3.0) ] ) + true + > bool = {{alias}}( [ -3.0, '-3.0' ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only nonpositive + primitive integer values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + nonpositive primitive integer values. + + Examples + -------- + > var bool = {{alias}}.primitives( [ -1.0, 0.0, -10.0 ] ) + true + > bool = {{alias}}.primitives( [ -3.0, new Number(-1.0) ] ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only number objects + having nonpositive integer values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + number objects having nonpositive integer values. + + Examples + -------- + > var bool = {{alias}}.objects( [ new Number(-1.0), new Number(-10.0) ] ) + true + > bool = {{alias}}.objects( [ -1.0, 0.0, -10.0 ] ) + false + > bool = {{alias}}.objects( [ -3.0, new Number(-1.0) ] ) + false + + See Also + -------- + diff --git a/is-nonpositive-integer-array/docs/types/index.d.ts b/is-nonpositive-integer-array/docs/types/index.d.ts new file mode 100644 index 00000000..8905f7b4 --- /dev/null +++ b/is-nonpositive-integer-array/docs/types/index.d.ts @@ -0,0 +1,109 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNonPositiveIntegerArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsNonPositiveIntegerArray { + /** + * Tests if a value is an array-like object containing only nonpositive integers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only nonpositive integers + * + * @example + * var bool = isNonPositiveIntegerArray( [ -3.0, new Number(-3.0) ] ); + * // returns true + * + * @example + * var bool = isNonPositiveIntegerArray( [ -3.0, '-3.0' ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only nonpositive primitive integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only nonpositive primitive integer values + * + * @example + * var bool = isNonPositiveIntegerArray.primitives( [ -1.0, -10.0 ] ); + * // returns true + * + * @example + * var bool = isNonPositiveIntegerArray.primitives( [ -1.0, 0.0, -10.0 ] ); + * // returns true + * + * @example + * var bool = isNonPositiveIntegerArray.primitives( [ -3.0, new Number(-1.0) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects having nonpositive integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number objects having nonpositive integer values + * + * @example + * var bool = isNonPositiveIntegerArray.objects( [ new Number(-1.0), new Number(-10.0) ] ); + * // returns true + * + * @example + * var bool = isNonPositiveIntegerArray.objects( [ -1.0, 0.0, -10.0 ] ); + * // returns false + * + * @example + * var bool = isNonPositiveIntegerArray.objects( [ -3.0, new Number(-1.0) ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object containing only nonpositive integers. +* +* @param value - value to test +* @returns boolean indicating whether value is an array-like object containing only nonpositive integers +* +* @example +* var bool = isNonPositiveIntegerArray( [ -3.0, new Number(-3.0) ] ); +* // returns true +* +* @example +* var bool = isNonPositiveIntegerArray( [ -3.0, '-3.0' ] ); +* // returns false +* +* @example +* var bool = isNonPositiveIntegerArray.primitives( [ -1.0, -10.0 ] ); +* // returns true +* +* @example +* var bool = isNonPositiveIntegerArray.objects( [ new Number(-1.0), new Number(-10.0) ] ); +* // returns true +*/ +declare var isNonPositiveIntegerArray: IsNonPositiveIntegerArray; + + +// EXPORTS // + +export = isNonPositiveIntegerArray; diff --git a/is-nonpositive-integer-array/docs/types/test.ts b/is-nonpositive-integer-array/docs/types/test.ts new file mode 100644 index 00000000..2b8eee6d --- /dev/null +++ b/is-nonpositive-integer-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNonPositiveIntegerArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNonPositiveIntegerArray( [ 4 ] ); // $ExpectType boolean + isNonPositiveIntegerArray( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNonPositiveIntegerArray(); // $ExpectError + isNonPositiveIntegerArray( [ -3 ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonPositiveIntegerArray.primitives( [ new Number( -3 ) ] ); // $ExpectType boolean + isNonPositiveIntegerArray.primitives( [ -3 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isNonPositiveIntegerArray.primitives(); // $ExpectError + isNonPositiveIntegerArray.primitives( [ -2 ], 123 ); // $ExpectError +} + + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonPositiveIntegerArray.objects( [ new Number( -2 ) ] ); // $ExpectType boolean + isNonPositiveIntegerArray.objects( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isNonPositiveIntegerArray.objects(); // $ExpectError + isNonPositiveIntegerArray.objects( [ -2 ], 123 ); // $ExpectError +} diff --git a/is-nonpositive-integer-array/examples/index.js b/is-nonpositive-integer-array/examples/index.js new file mode 100644 index 00000000..d54303fb --- /dev/null +++ b/is-nonpositive-integer-array/examples/index.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveIntegerArray = require( './../lib' ); + +var bool = isNonPositiveIntegerArray( [ -5, -2, -3 ] ); +console.log( bool ); +// => true + +bool = isNonPositiveIntegerArray( [ -4, -3, -2, -1 ] ); +console.log( bool ); +// => true + +bool = isNonPositiveIntegerArray( [ -1, new Number( -6 ), -3 ] ); +console.log( bool ); +// => true + +bool = isNonPositiveIntegerArray( [ -3, -2, -1, 0 ] ); +console.log( bool ); +// => true + +bool = isNonPositiveIntegerArray( [ -3, -2, -1, 0, 2 ] ); +console.log( bool ); +// => false + +bool = isNonPositiveIntegerArray( [ -1, 'abc', -3 ] ); +console.log( bool ); +// => false + +bool = isNonPositiveIntegerArray( [ -2.3, -1, -3 ] ); +console.log( bool ); +// => false + +bool = isNonPositiveIntegerArray( [] ); +console.log( bool ); +// => false diff --git a/is-nonpositive-integer-array/lib/index.js b/is-nonpositive-integer-array/lib/index.js new file mode 100644 index 00000000..dec7be1f --- /dev/null +++ b/is-nonpositive-integer-array/lib/index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only nonpositive integers. +* +* @module @stdlib/assert/is-nonpositive-integer-array +* +* @example +* var isNonPositiveIntegerArray = require( '@stdlib/assert/is-nonpositive-integer-array' ); +* +* var bool = isNonPositiveIntegerArray( [ -3.0, new Number(-3.0) ] ); +* // returns true +* +* bool = isNonPositiveIntegerArray( [ -3.0, '-3.0' ] ); +* // returns false +* +* @example +* var isNonPositiveIntegerArray = require( '@stdlib/assert/is-nonpositive-integer-array' ).primitives; +* +* var bool = isNonPositiveIntegerArray( [ -1.0, 0.0, -10.0 ] ); +* // returns true +* +* bool = isNonPositiveIntegerArray( [ -3.0, new Number(-1.0) ] ); +* // returns false +* +* @example +* var isNonPositiveIntegerArray = require( '@stdlib/assert/is-nonpositive-integer-array' ).objects; +* +* var bool = isNonPositiveIntegerArray( [ new Number(-3.0), new Number(-1.0) ] ); +* // returns true +* +* bool = isNonPositiveIntegerArray( [ -1.0, 0.0, -10.0 ] ); +* // returns false +*/ + +// MODULES // + +var isNonPositiveInteger = require( '@stdlib/assert/is-nonpositive-integer' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); + + +// MAIN // + +var isNonPositiveIntegerArray = arrayfun( isNonPositiveInteger ); +setReadOnly( isNonPositiveIntegerArray, 'primitives', arrayfun( isNonPositiveInteger.isPrimitive ) ); +setReadOnly( isNonPositiveIntegerArray, 'objects', arrayfun( isNonPositiveInteger.isObject ) ); + + +// EXPORTS // + +module.exports = isNonPositiveIntegerArray; diff --git a/is-nonpositive-integer-array/package.json b/is-nonpositive-integer-array/package.json new file mode 100644 index 00000000..2cd69405 --- /dev/null +++ b/is-nonpositive-integer-array/package.json @@ -0,0 +1,81 @@ +{ + "name": "@stdlib/assert/is-nonpositive-integer-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only nonpositive integers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "number", + "numeric", + "nonpositive", + "isnumber", + "isnumeric", + "integer", + "int", + "isinteger", + "type", + "check", + "validate", + "valid", + "test" + ] +} diff --git a/is-nonpositive-integer-array/test/test.js b/is-nonpositive-integer-array/test/test.js new file mode 100644 index 00000000..e85dfc58 --- /dev/null +++ b/is-nonpositive-integer-array/test/test.js @@ -0,0 +1,101 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveIntegerArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonPositiveIntegerArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only nonpositive integer values', function test( t ) { + var arr; + + arr = [ -5.0, new Number( -5 ), -1.0 ]; + t.equal( isNonPositiveIntegerArray( arr ), true, 'returns true' ); + + arr = new Int32Array( [ -2, -9, -4, 0 ] ); + t.equal( isNonPositiveIntegerArray( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': new Number( -5 ), + '1': new Number( -2 ) + }; + t.equal( isNonPositiveIntegerArray( arr ), true, 'returns true' ); + + arr = [ -5.0, '3', null ]; + t.equal( isNonPositiveIntegerArray( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only nonpositive primitive integers', function test( t ) { + var arr; + + arr = [ -5.0, -1.0, 0 ]; + t.equal( isNonPositiveIntegerArray.primitives( arr ), true, 'returns true' ); + + arr = new Int32Array( [ -2, -9, -4 ] ); + t.equal( isNonPositiveIntegerArray.primitives( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': new Number( -5 ), + '1': new Number( -2 ) + }; + t.equal( isNonPositiveIntegerArray.primitives( arr ), false, 'returns false' ); + + arr = [ new Number( -5 ), -1.0, -1.0 ]; + t.equal( isNonPositiveIntegerArray.primitives( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only Number objects having nonpositive integer values', function test( t ) { + var arr; + + arr = [ new Number( -5 ), new Number( -5 ) ]; + t.equal( isNonPositiveIntegerArray.objects( arr ), true, 'returns true' ); + + arr = [ -5, -3, 0 ]; + t.equal( isNonPositiveIntegerArray.objects( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': new Number( -5 ), + '1': new Number( -2 ) + }; + t.equal( isNonPositiveIntegerArray.objects( arr ), true, 'returns true' ); + + arr = new Int32Array( [ -2, -9, -4 ] ); + t.equal( isNonPositiveIntegerArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-nonpositive-integer/README.md b/is-nonpositive-integer/README.md new file mode 100644 index 00000000..f7f408d8 --- /dev/null +++ b/is-nonpositive-integer/README.md @@ -0,0 +1,136 @@ + + +# isNonPositiveInteger + +> Test if a value is a number having a nonpositive integer value. + +
+ +## Usage + +```javascript +var isNonPositiveInteger = require( '@stdlib/assert/is-nonpositive-integer' ); +``` + +#### isNonPositiveInteger( value ) + +Tests if a `value` is a `number` having a nonpositive `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonPositiveInteger( -5.0 ); +// returns true + +bool = isNonPositiveInteger( new Number( -5.0 ) ); +// returns true + +bool = isNonPositiveInteger( 5.0 ); +// returns false + +bool = isNonPositiveInteger( -3.14 ); +// returns false + +bool = isNonPositiveInteger( null ); +// returns false +``` + +#### isNonPositiveInteger.isPrimitive( value ) + +Tests if a `value` is a primitive `number` having a nonpositive `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonPositiveInteger.isPrimitive( -3.0 ); +// returns true + +bool = isNonPositiveInteger.isPrimitive( new Number( -3.0 ) ); +// returns false +``` + +#### isNonPositiveInteger.isObject( value ) + +Tests if a `value` is a `Number` object having a nonpositive `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonPositiveInteger.isObject( -3.0 ); +// returns false + +bool = isNonPositiveInteger.isObject( new Number( -3.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveInteger = require( '@stdlib/assert/is-nonpositive-integer' ); + +var bool = isNonPositiveInteger( -5.0 ); +// returns true + +bool = isNonPositiveInteger( new Number( -5.0 ) ); +// returns true + +bool = isNonPositiveInteger( 0.0 ); +// returns true + +bool = isNonPositiveInteger( -3.14 ); +// returns false + +bool = isNonPositiveInteger( 5.0 ); +// returns false + +bool = isNonPositiveInteger( '-5' ); +// returns false + +bool = isNonPositiveInteger( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-nonpositive-integer/benchmark/benchmark.js b/is-nonpositive-integer/benchmark/benchmark.js new file mode 100644 index 00000000..4e3e4620 --- /dev/null +++ b/is-nonpositive-integer/benchmark/benchmark.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNonPositiveInteger = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonPositiveInteger( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonPositiveInteger( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonPositiveInteger.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonPositiveInteger.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonPositiveInteger.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonPositiveInteger.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-nonpositive-integer/docs/repl.txt b/is-nonpositive-integer/docs/repl.txt new file mode 100644 index 00000000..e235a27f --- /dev/null +++ b/is-nonpositive-integer/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is a nonpositive integer. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a nonpositive integer. + + Examples + -------- + > var bool = {{alias}}( -5.0 ) + true + > bool = {{alias}}( new Number( -5.0 ) ) + true + > bool = {{alias}}( 5.0 ) + false + > bool = {{alias}}( -3.14 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive having a nonpositive integer value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive having a + nonpositive integer value. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( -3.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( -3.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a nonpositive integer value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a nonpositive + integer value. + + Examples + -------- + > var bool = {{alias}}.isObject( -3.0 ) + false + > bool = {{alias}}.isObject( new Number( -3.0 ) ) + true + + + See Also + -------- + diff --git a/is-nonpositive-integer/docs/types/index.d.ts b/is-nonpositive-integer/docs/types/index.d.ts new file mode 100644 index 00000000..72c1ffb1 --- /dev/null +++ b/is-nonpositive-integer/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNonPositiveInteger` with methods for testing for primitives and objects, respectively. +*/ +interface IsNonPositiveInteger { + /** + * Tests if a value is a nonpositive integer. + * + * @param value - value to test + * @returns boolean indicating whether value is a nonpositive integer + * + * @example + * var bool = isNonPositiveInteger( -5.0 ); + * // returns true + * + * @example + * var bool = isNonPositiveInteger( new Number( -5.0 ) ); + * // returns true + * + * @example + * var bool = isNonPositiveInteger( 5.0 ); + * // returns false + * + * @example + * var bool = isNonPositiveInteger( -3.14 ); + * // returns false + * + * @example + * var bool = isNonPositiveInteger( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a nonpositive integer value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a nonpositive integer value + * + * @example + * var bool = isNonPositiveInteger.isPrimitive( -3.0 ); + * // returns true + * + * @example + * var bool = isNonPositiveInteger.isPrimitive( new Number( -3.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a nonpositive integer value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a nonpositive integer value + * + * @example + * var bool = isNonPositiveInteger.isObject( -3.0 ); + * // returns false + * + * @example + * var bool = isNonPositiveInteger.isObject( new Number( -3.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a nonpositive integer. +* +* @param value - value to test +* @returns boolean indicating whether value is a nonpositive integer +* +* @example +* var bool = isNonPositiveInteger( -5.0 ); +* // returns true +* +* @example +* var bool = isNonPositiveInteger( new Number( -5.0 ) ); +* // returns true +* +* @example +* var bool = isNonPositiveInteger( 5.0 ); +* // returns false +* +* @example +* var bool = isNonPositiveInteger( -3.14 ); +* // returns false +* +* @example +* var bool = isNonPositiveInteger( null ); +* // returns false +* +* @example +* var bool = isNonPositiveInteger.isPrimitive( -3.0 ); +* // returns true +* +* @example +* var bool = isNonPositiveInteger.isObject( new Number( -3.0 ) ); +* // returns true +*/ +declare var isNonPositiveInteger: IsNonPositiveInteger; + + +// EXPORTS // + +export = isNonPositiveInteger; diff --git a/is-nonpositive-integer/docs/types/test.ts b/is-nonpositive-integer/docs/types/test.ts new file mode 100644 index 00000000..d8ea55ab --- /dev/null +++ b/is-nonpositive-integer/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNonPositiveInteger = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNonPositiveInteger( 1.2 ); // $ExpectType boolean + isNonPositiveInteger( -3 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNonPositiveInteger(); // $ExpectError + isNonPositiveInteger( -2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonPositiveInteger.isPrimitive( new Number( -2 ) ); // $ExpectType boolean + isNonPositiveInteger.isPrimitive( -2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isNonPositiveInteger.isPrimitive(); // $ExpectError + isNonPositiveInteger.isPrimitive( -2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonPositiveInteger.isObject( new Number( -2 ) ); // $ExpectType boolean + isNonPositiveInteger.isObject( -2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isNonPositiveInteger.isObject(); // $ExpectError + isNonPositiveInteger.isObject( -2, 123 ); // $ExpectError +} diff --git a/is-nonpositive-integer/examples/index.js b/is-nonpositive-integer/examples/index.js new file mode 100644 index 00000000..515e79c2 --- /dev/null +++ b/is-nonpositive-integer/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveInteger = require( './../lib' ); + +console.log( isNonPositiveInteger( -5.0 ) ); +// => true + +console.log( isNonPositiveInteger( new Number( -5.0 ) ) ); +// => true + +console.log( isNonPositiveInteger( 0.0 ) ); +// => true + +console.log( isNonPositiveInteger( -3.14 ) ); +// => false + +console.log( isNonPositiveInteger( 5.0 ) ); +// => false + +console.log( isNonPositiveInteger( '-5' ) ); +// => false + +console.log( isNonPositiveInteger( null ) ); +// => false diff --git a/is-nonpositive-integer/lib/index.js b/is-nonpositive-integer/lib/index.js new file mode 100644 index 00000000..d6ab9533 --- /dev/null +++ b/is-nonpositive-integer/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a nonpositive integer. +* +* @module @stdlib/assert/is-nonpositive-integer +* +* @example +* var isNonPositiveInteger = require( '@stdlib/assert/is-nonpositive-integer' ); +* +* var bool = isNonPositiveInteger( -5.0 ); +* // returns true +* +* bool = isNonPositiveInteger( new Number( -5.0 ) ); +* // returns true +* +* bool = isNonPositiveInteger( 5.0 ); +* // returns false +* +* bool = isNonPositiveInteger( -3.14 ); +* // returns false +* +* bool = isNonPositiveInteger( null ); +* // returns false +* +* @example +* var isNonPositiveInteger = require( '@stdlib/assert/is-nonpositive-integer' ).isPrimitive; +* +* var bool = isNonPositiveInteger( -3.0 ); +* // returns true +* +* bool = isNonPositiveInteger( new Number( -3.0 ) ); +* // returns false +* +* @example +* var isNonPositiveInteger = require( '@stdlib/assert/is-nonpositive-integer' ).isObject; +* +* var bool = isNonPositiveInteger( -3.0 ); +* // returns false +* +* bool = isNonPositiveInteger( new Number( -3.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isNonPositiveInteger = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isNonPositiveInteger, 'isPrimitive', isPrimitive ); +setReadOnly( isNonPositiveInteger, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isNonPositiveInteger; diff --git a/is-nonpositive-integer/lib/main.js b/is-nonpositive-integer/lib/main.js new file mode 100644 index 00000000..3e3f5cb0 --- /dev/null +++ b/is-nonpositive-integer/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a nonpositive integer. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a nonpositive integer +* +* @example +* var bool = isNonPositiveInteger( -5.0 ); +* // returns true +* +* @example +* var bool = isNonPositiveInteger( new Number( -5.0 ) ); +* // returns true +* +* @example +* var bool = isNonPositiveInteger( 5.0 ); +* // returns false +* +* @example +* var bool = isNonPositiveInteger( -3.14 ); +* // returns false +* +* @example +* var bool = isNonPositiveInteger( null ); +* // returns false +*/ +function isNonPositiveInteger( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isNonPositiveInteger; diff --git a/is-nonpositive-integer/lib/object.js b/is-nonpositive-integer/lib/object.js new file mode 100644 index 00000000..c20d1e63 --- /dev/null +++ b/is-nonpositive-integer/lib/object.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isObject; + + +// MAIN // + +/** +* Tests if a value is a number object having a nonpositive integer value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a nonpositive integer value +* +* @example +* var bool = isNonPositiveInteger( -3.0 ); +* // returns false +* +* @example +* var bool = isNonPositiveInteger( new Number( -3.0 ) ); +* // returns true +*/ +function isNonPositiveInteger( value ) { + return ( + isInteger( value ) && + value.valueOf() <= 0 + ); +} + + +// EXPORTS // + +module.exports = isNonPositiveInteger; diff --git a/is-nonpositive-integer/lib/primitive.js b/is-nonpositive-integer/lib/primitive.js new file mode 100644 index 00000000..f3e7a99c --- /dev/null +++ b/is-nonpositive-integer/lib/primitive.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a number primitive having a nonpositive integer value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a nonpositive integer value +* +* @example +* var bool = isNonPositiveInteger( -3.0 ); +* // returns true +* +* @example +* var bool = isNonPositiveInteger( new Number( -3.0 ) ); +* // returns false +*/ +function isNonPositiveInteger( value ) { + return ( + isInteger( value ) && + value <= 0 + ); +} + + +// EXPORTS // + +module.exports = isNonPositiveInteger; diff --git a/is-nonpositive-integer/package.json b/is-nonpositive-integer/package.json new file mode 100644 index 00000000..0e894922 --- /dev/null +++ b/is-nonpositive-integer/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-nonpositive-integer", + "version": "0.0.0", + "description": "Test if a value is a number having a nonpositive integer value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "integer", + "int", + "nonnegative", + "is", + "isinteger", + "type", + "check", + "primitive", + "object" + ] +} diff --git a/is-nonpositive-integer/test/test.js b/is-nonpositive-integer/test/test.js new file mode 100644 index 00000000..1f96a996 --- /dev/null +++ b/is-nonpositive-integer/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNonPositiveInteger = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonPositiveInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number having a nonpositive integer value', function test( t ) { + t.equal( typeof isNonPositiveInteger.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a nonpositive integer value', function test( t ) { + t.equal( typeof isNonPositiveInteger.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-nonpositive-integer/test/test.main.js b/is-nonpositive-integer/test/test.main.js new file mode 100644 index 00000000..1f96a1d3 --- /dev/null +++ b/is-nonpositive-integer/test/test.main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveInteger = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonPositiveInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number having a nonpositive integer value', function test( t ) { + t.equal( isNonPositiveInteger( -5.0 ), true, 'returns true' ); + t.equal( isNonPositiveInteger( new Number( -5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number having a nonpositive integer value', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + 1.0, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNonPositiveInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nonpositive-integer/test/test.object.js b/is-nonpositive-integer/test/test.object.js new file mode 100644 index 00000000..523f5c3e --- /dev/null +++ b/is-nonpositive-integer/test/test.object.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveInteger = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonPositiveInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a nonpositive integer value', function test( t ) { + t.equal( isNonPositiveInteger( new Number( -5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is a nonpositive integer value', function test( t ) { + t.equal( isNonPositiveInteger( -3.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNonPositiveInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nonpositive-integer/test/test.primitive.js b/is-nonpositive-integer/test/test.primitive.js new file mode 100644 index 00000000..8e47f473 --- /dev/null +++ b/is-nonpositive-integer/test/test.primitive.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveInteger = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonPositiveInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number having a nonpositive integer value', function test( t ) { + t.equal( isNonPositiveInteger( -3.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has a nonpositive integer value', function test( t ) { + t.equal( isNonPositiveInteger( new Number( -5.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNonPositiveInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nonpositive-number-array/README.md b/is-nonpositive-number-array/README.md new file mode 100644 index 00000000..3b15e3cc --- /dev/null +++ b/is-nonpositive-number-array/README.md @@ -0,0 +1,121 @@ + + +# isNonPositiveNumberArray + +> Test if a value is an array-like object containing only nonpositive numbers. + +
+ +## Usage + +```javascript +var isNonPositiveNumberArray = require( '@stdlib/assert/is-nonpositive-number-array' ); +``` + +#### isNonPositiveNumberArray( value ) + +Tests if a `value` is an array-like object containing **only** nonpositive numbers. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonPositiveNumberArray( [ -3.0, new Number(-3.0) ] ); +// returns true + +bool = isNonPositiveNumberArray( [ -3.0, '-3.0' ] ); +// returns false +``` + +#### isNonPositiveNumberArray.primitives( value ) + +Tests if a `value` is an array-like object `array` containing **only** primitive nonpositive numbers. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonPositiveNumberArray.primitives( [ -1.0, 0.0, -10.0 ] ); +// returns true + +bool = isNonPositiveNumberArray.primitives( [ -3.0, new Number(-1.0) ] ); +// returns false +``` + +#### isNonPositiveNumberArray.objects( value ) + +Tests if a `value` is an array-like object `array` containing **only** object nonpositive numbers. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonPositiveNumberArray.objects( [ new Number(-1.0), new Number(-1.0) ] ); +// returns true + +bool = isNonPositiveNumberArray.objects( [ -1.0, 0.0, -10.0 ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveNumberArray = require( '@stdlib/assert/is-nonpositive-number-array' ); + +var bool = isNonPositiveNumberArray( [ -5.0, -0.2, -3.9 ] ); +// returns true + +bool = isNonPositiveNumberArray( [ -1, -2, -3 ] ); +// returns true + +bool = isNonPositiveNumberArray( [ -1, new Number( -6 ), -3 ] ); +// returns true + +bool = isNonPositiveNumberArray( [ -1, 'abc', -3 ] ); +// returns false + +bool = isNonPositiveNumberArray( -78.0 ); +// returns false +``` + +
+ + + + + + diff --git a/is-nonpositive-number-array/benchmark/benchmark.js b/is-nonpositive-number-array/benchmark/benchmark.js new file mode 100644 index 00000000..09f644b0 --- /dev/null +++ b/is-nonpositive-number-array/benchmark/benchmark.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var isNonPositiveNumberArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( -1.0 ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( -1.0 ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isNonPositiveNumberArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isNonPositiveNumberArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isNonPositiveNumberArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-nonpositive-number-array/docs/repl.txt b/is-nonpositive-number-array/docs/repl.txt new file mode 100644 index 00000000..2667b358 --- /dev/null +++ b/is-nonpositive-number-array/docs/repl.txt @@ -0,0 +1,74 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only nonpositive + numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + nonpositive numbers. + + Examples + -------- + > var bool = {{alias}}( [ -3.0, new Number(-3.0) ] ) + true + > bool = {{alias}}( [ -3.0, '-3.0' ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only primitive + nonpositive numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + primitive nonpositive numbers. + + Examples + -------- + > var bool = {{alias}}.primitives( [ -1.0, 0.0, -10.0 ] ) + true + > bool = {{alias}}.primitives( [ -3.0, new Number(-1.0) ] ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only number objects + having nonpositive number values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + number objects having nonpositive number values. + + Examples + -------- + > var bool = {{alias}}.objects( [ new Number(-1.0), new Number(-10.0) ] ) + true + > bool = {{alias}}.objects( [ -1.0, 0.0, -10.0 ] ) + false + > bool = {{alias}}.objects( [ -3.0, new Number(-1.0) ] ) + false + + See Also + -------- + diff --git a/is-nonpositive-number-array/docs/types/index.d.ts b/is-nonpositive-number-array/docs/types/index.d.ts new file mode 100644 index 00000000..bf3cf267 --- /dev/null +++ b/is-nonpositive-number-array/docs/types/index.d.ts @@ -0,0 +1,109 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNonPositiveNumberArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsNonPositiveNumberArray { + /** + * Tests if a value is an array-like object containing only nonpositive numbers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only nonpositive numbers + * + * @example + * var bool = isNonPositiveNumberArray( [ -3.0, new Number(-3.0) ] ); + * // returns true + * + * @example + * var bool = isNonPositiveNumberArray( [ -3.0, '-3.0' ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only nonpositive primitive integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only nonpositive primitive integer values + * + * @example + * var bool = isNonPositiveNumberArray.primitives( [ -1.0, -10.0 ] ); + * // returns true + * + * @example + * var bool = isNonPositiveNumberArray.primitives( [ -1.0, 0.0, -10.0 ] ); + * // returns true + * + * @example + * var bool = isNonPositiveNumberArray.primitives( [ -3.0, new Number(-1.0) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects having nonpositive integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number objects having nonpositive integer values + * + * @example + * var bool = isNonPositiveNumberArray.objects( [ new Number(-1.0), new Number(-10.0) ] ); + * // returns true + * + * @example + * var bool = isNonPositiveNumberArray.objects( [ -1.0, 0.0, -10.0 ] ); + * // returns false + * + * @example + * var bool = isNonPositiveNumberArray.objects( [ -3.0, new Number(-1.0) ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object containing only nonpositive numbers. +* +* @param value - value to test +* @returns boolean indicating whether value is an array-like object containing only nonpositive numbers +* +* @example +* var bool = isNonPositiveNumberArray( [ -3.0, new Number(-3.0) ] ); +* // returns true +* +* @example +* var bool = isNonPositiveNumberArray( [ -3.0, '-3.0' ] ); +* // returns false +* +* @example +* var bool = isNonPositiveNumberArray.primitives( [ -1.0, -10.0 ] ); +* // returns true +* +* @example +* var bool = isNonPositiveNumberArray.objects( [ new Number(-1.0), new Number(-10.0) ] ); +* // returns true +*/ +declare var isNonPositiveNumberArray: IsNonPositiveNumberArray; + + +// EXPORTS // + +export = isNonPositiveNumberArray; diff --git a/is-nonpositive-number-array/docs/types/test.ts b/is-nonpositive-number-array/docs/types/test.ts new file mode 100644 index 00000000..938c857b --- /dev/null +++ b/is-nonpositive-number-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNonPositiveNumberArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNonPositiveNumberArray( [ 4 ] ); // $ExpectType boolean + isNonPositiveNumberArray( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNonPositiveNumberArray(); // $ExpectError + isNonPositiveNumberArray( [ -3 ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonPositiveNumberArray.primitives( [ new Number( -3 ) ] ); // $ExpectType boolean + isNonPositiveNumberArray.primitives( [ -3 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isNonPositiveNumberArray.primitives(); // $ExpectError + isNonPositiveNumberArray.primitives( [ -2 ], 123 ); // $ExpectError +} + + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonPositiveNumberArray.objects( [ new Number( -2 ) ] ); // $ExpectType boolean + isNonPositiveNumberArray.objects( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isNonPositiveNumberArray.objects(); // $ExpectError + isNonPositiveNumberArray.objects( [ -2 ], 123 ); // $ExpectError +} diff --git a/is-nonpositive-number-array/examples/index.js b/is-nonpositive-number-array/examples/index.js new file mode 100644 index 00000000..d1d15a4f --- /dev/null +++ b/is-nonpositive-number-array/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveNumberArray = require( './../lib' ); + +var bool = isNonPositiveNumberArray( [ -5.0, -0.2, -3.9 ] ); +console.log( bool ); +// => true + +bool = isNonPositiveNumberArray( [ -1, -2, -3 ] ); +console.log( bool ); +// => true + +bool = isNonPositiveNumberArray( [ -1, new Number( -6 ), -3 ] ); +console.log( bool ); +// => true + +bool = isNonPositiveNumberArray( [ -1, 'abc', -3 ] ); +console.log( bool ); +// => false + +bool = isNonPositiveNumberArray( -78.0 ); +console.log( bool ); +// => false + +bool = isNonPositiveNumberArray( [] ); +console.log( bool ); +// => false diff --git a/is-nonpositive-number-array/lib/index.js b/is-nonpositive-number-array/lib/index.js new file mode 100644 index 00000000..170fca70 --- /dev/null +++ b/is-nonpositive-number-array/lib/index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only nonpositive numbers. +* +* @module @stdlib/assert/is-nonpositive-number-array +* +* @example +* var isNonPositiveNumberArray = require( '@stdlib/assert/is-nonpositive-number-array' ); +* +* var bool = isNonPositiveNumberArray( [ -3.0, new Number(-3.0) ] ); +* // returns true +* +* bool = isNonPositiveNumberArray( [ -3.0, '-3.0' ] ); +* // returns false +* +* @example +* var isNonPositiveNumberArray = require( '@stdlib/assert/is-nonpositive-number-array' ).primitives; +* +* var bool = isNonPositiveNumberArray( [ -1.0, 0.0, -10.0 ] ); +* // returns true +* +* bool = isNonPositiveNumberArray( [ -3.0, new Number(-1.0) ] ); +* // returns false +* +* @example +* var isNonPositiveNumberArray = require( '@stdlib/assert/is-nonpositive-number-array' ).objects; +* +* var bool = isNonPositiveNumberArray( [ new Number(-3.0), new Number(-1.0) ] ); +* // returns true +* +* bool = isNonPositiveNumberArray( [ -1.0, 0.0, -10.0 ] ); +* // returns false +*/ + +// MODULES // + +var isNonPositiveNumber = require( '@stdlib/assert/is-nonpositive-number' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); + + +// MAIN // + +var isNonPositiveNumberArray = arrayfun( isNonPositiveNumber ); +setReadOnly( isNonPositiveNumberArray, 'primitives', arrayfun( isNonPositiveNumber.isPrimitive ) ); +setReadOnly( isNonPositiveNumberArray, 'objects', arrayfun( isNonPositiveNumber.isObject ) ); + + +// EXPORTS // + +module.exports = isNonPositiveNumberArray; diff --git a/is-nonpositive-number-array/package.json b/is-nonpositive-number-array/package.json new file mode 100644 index 00000000..640553b1 --- /dev/null +++ b/is-nonpositive-number-array/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-nonpositive-number-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only nonpositive numbers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "number", + "numeric", + "nonpositive", + "isnumber", + "isnumeric", + "type", + "check" + ] +} diff --git a/is-nonpositive-number-array/test/test.js b/is-nonpositive-number-array/test/test.js new file mode 100644 index 00000000..4c980607 --- /dev/null +++ b/is-nonpositive-number-array/test/test.js @@ -0,0 +1,101 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveNumberArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonPositiveNumberArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only nonpositive numbers', function test( t ) { + var arr; + + arr = [ -5.0, new Number( -5 ), 0 ]; + t.equal( isNonPositiveNumberArray( arr ), true, 'returns true' ); + + arr = [ -5.0, '-3', null ]; + t.equal( isNonPositiveNumberArray( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': -3.14, + '1': 0.0 + }; + t.equal( isNonPositiveNumberArray( arr ), true, 'returns true' ); + + arr = new Float32Array( [-5.0, -1.5] ); + t.equal( isNonPositiveNumberArray( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object having only nonpositive number primitives', function test( t ) { + var arr; + + arr = [ -5.0, 0.0 ]; + t.equal( isNonPositiveNumberArray.primitives( arr ), true, 'returns true' ); + + arr = [ new Number( -5 ), -1.0, -1.0 ]; + t.equal( isNonPositiveNumberArray.primitives( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': -3.14, + '1': 0.0 + }; + t.equal( isNonPositiveNumberArray.primitives( arr ), true, 'returns true' ); + + arr = new Float32Array( [-5.0, -1.5] ); + t.equal( isNonPositiveNumberArray.primitives( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object having only nonpositive number objects', function test( t ) { + var arr; + + arr = [ new Number( -5 ), new Number( -5 ) ]; + t.equal( isNonPositiveNumberArray.objects( arr ), true, 'returns true' ); + + arr = [ -5.0, 0.0 ]; + t.equal( isNonPositiveNumberArray.objects( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': new Number( -3.14 ), + '1': new Number( 0.0 ) + }; + t.equal( isNonPositiveNumberArray.objects( arr ), true, 'returns true' ); + + arr = new Float32Array( [-5.0, -1.5] ); + t.equal( isNonPositiveNumberArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-nonpositive-number/README.md b/is-nonpositive-number/README.md new file mode 100644 index 00000000..21701c81 --- /dev/null +++ b/is-nonpositive-number/README.md @@ -0,0 +1,136 @@ + + +# isNonPositiveNumber + +> Test if a value is a number having a nonpositive value. + +
+ +## Usage + +```javascript +var isNonPositiveNumber = require( '@stdlib/assert/is-nonpositive-number' ); +``` + +#### isNonPositiveNumber( value ) + +Tests if a `value` is a `number` having a nonpositive value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonPositiveNumber( -5.0 ); +// returns true + +bool = isNonPositiveNumber( new Number( -5.0 ) ); +// returns true + +bool = isNonPositiveNumber( -3.14 ); +// returns true + +bool = isNonPositiveNumber( 5.0 ); +// returns false + +bool = isNonPositiveNumber( null ); +// returns false +``` + +#### isNonPositiveNumber.isPrimitive( value ) + +Tests if a `value` is a primitive `number` having a nonpositive value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonPositiveNumber.isPrimitive( -3.0 ); +// returns true + +bool = isNonPositiveNumber.isPrimitive( new Number( -3.0 ) ); +// returns false +``` + +#### isNonPositiveNumber.isObject( value ) + +Tests if a `value` is a `Number` object having a nonpositive value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNonPositiveNumber.isObject( -3.0 ); +// returns false + +bool = isNonPositiveNumber.isObject( new Number( -3.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveNumber = require( '@stdlib/assert/is-nonpositive-number' ); + +var bool = isNonPositiveNumber( -5.0 ); +// returns true + +bool = isNonPositiveNumber( new Number( -5.0 ) ); +// returns true + +bool = isNonPositiveNumber( 0.0 ); +// returns true + +bool = isNonPositiveNumber( -3.14 ); +// returns true + +bool = isNonPositiveNumber( 5.0 ); +// returns false + +bool = isNonPositiveNumber( '-5' ); +// returns false + +bool = isNonPositiveNumber( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-nonpositive-number/benchmark/benchmark.js b/is-nonpositive-number/benchmark/benchmark.js new file mode 100644 index 00000000..f86df5c7 --- /dev/null +++ b/is-nonpositive-number/benchmark/benchmark.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNonPositiveNumber = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonPositiveNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonPositiveNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonPositiveNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonPositiveNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonPositiveNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonPositiveNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-nonpositive-number/docs/repl.txt b/is-nonpositive-number/docs/repl.txt new file mode 100644 index 00000000..49f9eb77 --- /dev/null +++ b/is-nonpositive-number/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is a nonpositive number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a nonpositive number. + + Examples + -------- + > var bool = {{alias}}( -5.0 ) + true + > bool = {{alias}}( new Number( -5.0 ) ) + true + > bool = {{alias}}( -3.14 ) + true + > bool = {{alias}}( 5.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive having a nonpositive value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive having a + nonpositive value. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( -3.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( -3.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a nonpositive value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a nonpositive + value. + + Examples + -------- + > var bool = {{alias}}.isObject( -3.0 ) + false + > bool = {{alias}}.isObject( new Number( -3.0 ) ) + true + + + See Also + -------- + diff --git a/is-nonpositive-number/docs/types/index.d.ts b/is-nonpositive-number/docs/types/index.d.ts new file mode 100644 index 00000000..576036e4 --- /dev/null +++ b/is-nonpositive-number/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNonPositiveNumber` with methods for testing for primitives and objects, respectively. +*/ +interface IsNonPositiveNumber { + /** + * Tests if a value is a nonpositive number. + * + * @param value - value to test + * @returns boolean indicating whether value is a nonpositive number + * + * @example + * var bool = isNonPositiveNumber( -5.0 ); + * // returns true + * + * @example + * var bool = isNonPositiveNumber( new Number( -5.0 ) ); + * // returns true + * + * @example + * var bool = isNonPositiveNumber( -3.14 ); + * // returns true + * + * @example + * var bool = isNonPositiveNumber( 5.0 ); + * // returns false + * + * @example + * var bool = isNonPositiveNumber( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a nonpositive value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a nonpositive number value + * + * @example + * var bool = isNonPositiveNumber.isPrimitive( -3.0 ); + * // returns true + * + * @example + * var bool = isNonPositiveNumber.isPrimitive( new Number( -3.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a nonpositive value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a nonpositive number value + * + * @example + * var bool = isNonPositiveNumber.isObject( -3.0 ); + * // returns false + * + * @example + * var bool = isNonPositiveNumber.isObject( new Number( -3.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a nonpositive number. +* +* @param value - value to test +* @returns boolean indicating whether value is a nonpositive number +* +* @example +* var bool = isNonPositiveNumber( -5.0 ); +* // returns true +* +* @example +* var bool = isNonPositiveNumber( new Number( -5.0 ) ); +* // returns true +* +* @example +* var bool = isNonPositiveNumber( -3.14 ); +* // returns true +* +* @example +* var bool = isNonPositiveNumber( 5.0 ); +* // returns false +* +* @example +* var bool = isNonPositiveNumber( null ); +* // returns false +* +* @example +* var bool = isNonPositiveNumber.isPrimitive( -3.0 ); +* // returns true +* +* @example +* var bool = isNonPositiveNumber.isObject( new Number( -3.0 ) ); +* // returns true +*/ +declare var isNonPositiveNumber: IsNonPositiveNumber; + + +// EXPORTS // + +export = isNonPositiveNumber; diff --git a/is-nonpositive-number/docs/types/test.ts b/is-nonpositive-number/docs/types/test.ts new file mode 100644 index 00000000..61ba4493 --- /dev/null +++ b/is-nonpositive-number/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNonPositiveNumber = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNonPositiveNumber( 1.2 ); // $ExpectType boolean + isNonPositiveNumber( -3 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNonPositiveNumber(); // $ExpectError + isNonPositiveNumber( -2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonPositiveNumber.isPrimitive( new Number( -2 ) ); // $ExpectType boolean + isNonPositiveNumber.isPrimitive( -2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isNonPositiveNumber.isPrimitive(); // $ExpectError + isNonPositiveNumber.isPrimitive( -2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNonPositiveNumber.isObject( new Number( -2 ) ); // $ExpectType boolean + isNonPositiveNumber.isObject( -2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isNonPositiveNumber.isObject(); // $ExpectError + isNonPositiveNumber.isObject( -2, 123 ); // $ExpectError +} diff --git a/is-nonpositive-number/examples/index.js b/is-nonpositive-number/examples/index.js new file mode 100644 index 00000000..242c47d9 --- /dev/null +++ b/is-nonpositive-number/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveNumber = require( './../lib' ); + +console.log( isNonPositiveNumber( -5.0 ) ); +// => true + +console.log( isNonPositiveNumber( new Number( -5.0 ) ) ); +// => true + +console.log( isNonPositiveNumber( 0.0 ) ); +// => true + +console.log( isNonPositiveNumber( -3.14 ) ); +// => true + +console.log( isNonPositiveNumber( 5.0 ) ); +// => false + +console.log( isNonPositiveNumber( '-5' ) ); +// => false + +console.log( isNonPositiveNumber( null ) ); +// => false diff --git a/is-nonpositive-number/lib/index.js b/is-nonpositive-number/lib/index.js new file mode 100644 index 00000000..94a98185 --- /dev/null +++ b/is-nonpositive-number/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a nonpositive number. +* +* @module @stdlib/assert/is-nonpositive-number +* +* @example +* var isNonPositiveNumber = require( '@stdlib/assert/is-nonpositive-number' ); +* +* var bool = isNonPositiveNumber( -5.0 ); +* // returns true +* +* bool = isNonPositiveNumber( new Number( -5.0 ) ); +* // returns true +* +* bool = isNonPositiveNumber( -3.14 ); +* // returns true +* +* bool = isNonPositiveNumber( 5.0 ); +* // returns false +* +* bool = isNonPositiveNumber( null ); +* // returns false +* +* @example +* var isNonPositiveNumber = require( '@stdlib/assert/is-nonpositive-number' ).isPrimitive; +* +* var bool = isNonPositiveNumber( -3.0 ); +* // returns true +* +* bool = isNonPositiveNumber( new Number( -3.0 ) ); +* // returns false +* +* @example +* var isNonPositiveNumber = require( '@stdlib/assert/is-nonpositive-number' ).isObject; +* +* var bool = isNonPositiveNumber( -3.0 ); +* // returns false +* +* bool = isNonPositiveNumber( new Number( -3.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isNonPositiveNumber = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isNonPositiveNumber, 'isPrimitive', isPrimitive ); +setReadOnly( isNonPositiveNumber, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isNonPositiveNumber; diff --git a/is-nonpositive-number/lib/main.js b/is-nonpositive-number/lib/main.js new file mode 100644 index 00000000..5e02f894 --- /dev/null +++ b/is-nonpositive-number/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a nonpositive number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a nonpositive number +* +* @example +* var bool = isNonPositiveNumber( -5.0 ); +* // returns true +* +* @example +* var bool = isNonPositiveNumber( new Number( -5.0 ) ); +* // returns true +* +* @example +* var bool = isNonPositiveNumber( -3.14 ); +* // returns true +* +* @example +* var bool = isNonPositiveNumber( 5.0 ); +* // returns false +* +* @example +* var bool = isNonPositiveNumber( null ); +* // returns false +*/ +function isNonPositiveNumber( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isNonPositiveNumber; diff --git a/is-nonpositive-number/lib/object.js b/is-nonpositive-number/lib/object.js new file mode 100644 index 00000000..929e0003 --- /dev/null +++ b/is-nonpositive-number/lib/object.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; + + +// MAIN // + +/** +* Tests if a value is a number object having a nonpositive value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a nonpositive number value +* +* @example +* var bool = isNonPositiveNumber( -3.0 ); +* // returns false +* +* @example +* var bool = isNonPositiveNumber( new Number( -3.0 ) ); +* // returns true +*/ +function isNonPositiveNumber( value ) { + return ( + isNumber( value ) && + value.valueOf() <= 0.0 + ); +} + + +// EXPORTS // + +module.exports = isNonPositiveNumber; diff --git a/is-nonpositive-number/lib/primitive.js b/is-nonpositive-number/lib/primitive.js new file mode 100644 index 00000000..7c220c9e --- /dev/null +++ b/is-nonpositive-number/lib/primitive.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a number primitive having a nonpositive value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a nonpositive number value +* +* @example +* var bool = isNonPositiveNumber( -3.0 ); +* // returns true +* +* @example +* var bool = isNonPositiveNumber( new Number( -3.0 ) ); +* // returns false +*/ +function isNonPositiveNumber( value ) { + return ( + isNumber( value ) && + value <= 0.0 + ); +} + + +// EXPORTS // + +module.exports = isNonPositiveNumber; diff --git a/is-nonpositive-number/package.json b/is-nonpositive-number/package.json new file mode 100644 index 00000000..4d32171a --- /dev/null +++ b/is-nonpositive-number/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-nonpositive-number", + "version": "0.0.0", + "description": "Test if a value is a number having a nonpositive value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "nonpositive", + "is", + "isnumber", + "isnumeric", + "type", + "check", + "primitive", + "object" + ] +} diff --git a/is-nonpositive-number/test/test.js b/is-nonpositive-number/test/test.js new file mode 100644 index 00000000..64d7c7b5 --- /dev/null +++ b/is-nonpositive-number/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNonPositiveNumber = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonPositiveNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number having a nonpositive number value', function test( t ) { + t.equal( typeof isNonPositiveNumber.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a nonpositive number value', function test( t ) { + t.equal( typeof isNonPositiveNumber.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-nonpositive-number/test/test.main.js b/is-nonpositive-number/test/test.main.js new file mode 100644 index 00000000..06439e9b --- /dev/null +++ b/is-nonpositive-number/test/test.main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveNumber = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonPositiveNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number having a nonpositive number value', function test( t ) { + t.equal( isNonPositiveNumber( -5.0 ), true, 'returns true' ); + t.equal( isNonPositiveNumber( new Number( -5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number having a nonpositive number value', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + 1.0, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNonPositiveNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nonpositive-number/test/test.object.js b/is-nonpositive-number/test/test.object.js new file mode 100644 index 00000000..b9e5f906 --- /dev/null +++ b/is-nonpositive-number/test/test.object.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveNumber = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonPositiveNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a nonpositive number value', function test( t ) { + t.equal( isNonPositiveNumber( new Number( -5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is a nonpositive number value', function test( t ) { + t.equal( isNonPositiveNumber( -3.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNonPositiveNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nonpositive-number/test/test.primitive.js b/is-nonpositive-number/test/test.primitive.js new file mode 100644 index 00000000..c2b2d436 --- /dev/null +++ b/is-nonpositive-number/test/test.primitive.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNonPositiveNumber = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonPositiveNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number having a nonpositive number value', function test( t ) { + t.equal( isNonPositiveNumber( -3.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has a nonpositive number value', function test( t ) { + t.equal( isNonPositiveNumber( new Number( -5.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '-5', + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNonPositiveNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-nonsymmetric-matrix/README.md b/is-nonsymmetric-matrix/README.md new file mode 100644 index 00000000..e2ee6a52 --- /dev/null +++ b/is-nonsymmetric-matrix/README.md @@ -0,0 +1,83 @@ + + +# isNonSymmetricMatrix + +> Test if a value is a [non-symmetric matrix][symmetric-matrix]. + +
+ +## Usage + +```javascript +var isNonSymmetricMatrix = require( '@stdlib/assert/is-nonsymmetric-matrix' ); +``` + +#### isNonSymmetricMatrix( value ) + +Tests if a value is a [non-symmetric matrix][symmetric-matrix]. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var bool = isNonSymmetricMatrix( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isNonSymmetricMatrix = require( '@stdlib/assert/is-nonsymmetric-matrix' ); + +var arr = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var out = isNonSymmetricMatrix( arr ); +// returns true + +out = isNonSymmetricMatrix( [ 1, 2, 3, 4 ] ); +// returns false + +out = isNonSymmetricMatrix( {} ); +// returns false + +out = isNonSymmetricMatrix( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-nonsymmetric-matrix/benchmark/benchmark.js b/is-nonsymmetric-matrix/benchmark/benchmark.js new file mode 100644 index 00000000..fb7617cb --- /dev/null +++ b/is-nonsymmetric-matrix/benchmark/benchmark.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var isNonSymmetricMatrix = require( './../lib' ); + + +// MAIN // + +bench( pkg + '::true', function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = [ 0, 0, 0, 0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + + values = [ + arr, + arr, + arr, + arr, + arr + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonSymmetricMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg + '::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNonSymmetricMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-nonsymmetric-matrix/benchmark/benchmark.size.js b/is-nonsymmetric-matrix/benchmark/benchmark.size.js new file mode 100644 index 00000000..e0a40e59 --- /dev/null +++ b/is-nonsymmetric-matrix/benchmark/benchmark.size.js @@ -0,0 +1,109 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isNonSymmetricMatrix = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} size - matrix size +* @param {PositiveInteger} N - matrix order +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, size, N ) { + var strides; + var buffer; + var shape; + var x; + + buffer = new Float64Array( size ); + shape = [ N, N ]; + strides = [ N, 1 ]; + x = ndarray( 'float64', buffer, shape, strides, 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire matrix. + bool = fcn( x ); + if ( bool !== false ) { + b.fail( 'should return false' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var sz; + var N; + var f; + var i; + + min = 4; // minimum number of elements + + for ( i = 0; i <= 16; i += 2 ) { + sz = min * pow( 2, i ); + N = sqrt( sz ); + f = createBenchmark( isNonSymmetricMatrix, sz, N ); + bench( pkg+':size='+sz+',dims='+N+'x'+N, f ); + } +} + +main(); diff --git a/is-nonsymmetric-matrix/docs/repl.txt b/is-nonsymmetric-matrix/docs/repl.txt new file mode 100644 index 00000000..b21b0156 --- /dev/null +++ b/is-nonsymmetric-matrix/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( value ) + Tests if a value is a non-symmetric matrix. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a non-symmetric matrix. + + Examples + -------- + > var buf = [ 1, 2, 3, 4 ]; + > var sh = [ 2, 2 ]; + > var st = [ 2, 1 ]; + > var M = {{alias:@stdlib/ndarray/ctor}}( 'generic', buf, sh, st, 0, 'row-major' ); + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-nonsymmetric-matrix/docs/types/index.d.ts b/is-nonsymmetric-matrix/docs/types/index.d.ts new file mode 100644 index 00000000..8355598e --- /dev/null +++ b/is-nonsymmetric-matrix/docs/types/index.d.ts @@ -0,0 +1,47 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a non-symmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} \neq M_{ji}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param v - value to test +* @returns boolean indicating if a value is a non-symmetric matrix +* +* @example +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isNonSymmetricMatrix( arr ); +* // returns true +* +* bool = isNonSymmetricMatrix( [] ); +* // returns false +*/ +declare function isNonSymmetricMatrix( v: any ): boolean; + + +// EXPORTS // + +export = isNonSymmetricMatrix; diff --git a/is-nonsymmetric-matrix/docs/types/test.ts b/is-nonsymmetric-matrix/docs/types/test.ts new file mode 100644 index 00000000..ea298949 --- /dev/null +++ b/is-nonsymmetric-matrix/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* tslint:disable:no-invalid-this */ + +import isNonSymmetricMatrix = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const matrix = { + 'data': [ 1, 2, 3, 4 ], + 'ndims': 2, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': function get( i: number, j: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + return this.data[ idx ]; + }, + 'set': function set( i: number, j: number, v: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + this.data[ idx ] = v; + return v; + } + }; + isNonSymmetricMatrix( matrix ); // $ExpectType boolean + isNonSymmetricMatrix( [] ); // $ExpectType boolean + isNonSymmetricMatrix( false ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNonSymmetricMatrix(); // $ExpectError +} diff --git a/is-nonsymmetric-matrix/examples/index.js b/is-nonsymmetric-matrix/examples/index.js new file mode 100644 index 00000000..25e8fa83 --- /dev/null +++ b/is-nonsymmetric-matrix/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isNonSymmetricMatrix = require( './../lib' ); + +var arr = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +console.log( isNonSymmetricMatrix( arr ) ); +// => true + +console.log( isNonSymmetricMatrix( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isNonSymmetricMatrix( {} ) ); +// => false + +console.log( isNonSymmetricMatrix( null ) ); +// => false diff --git a/is-nonsymmetric-matrix/lib/index.js b/is-nonsymmetric-matrix/lib/index.js new file mode 100644 index 00000000..4fe2c559 --- /dev/null +++ b/is-nonsymmetric-matrix/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a non-symmetric matrix. +* +* @module @stdlib/assert/is-nonsymmetric-matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isNonSymmetricMatrix = require( '@stdlib/assert/is-nonsymmetric-matrix' ); +* +* var arr = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isNonSymmetricMatrix( arr ); +* // returns true +* +* bool = isNonSymmetricMatrix( [] ); +* // returns false +*/ + +// MODULES // + +var isNonSymmetricMatrix = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNonSymmetricMatrix; diff --git a/is-nonsymmetric-matrix/lib/main.js b/is-nonsymmetric-matrix/lib/main.js new file mode 100644 index 00000000..60721f42 --- /dev/null +++ b/is-nonsymmetric-matrix/lib/main.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isMatrixLike = require( '@stdlib/assert/is-matrix-like' ); + + +// MAIN // + +/** +* Tests if a value is a non-symmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} \neq M_{ji}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a non-symmetric matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var arr = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isNonSymmetricMatrix( arr ); +* // returns true +* +* bool = isNonSymmetricMatrix( [] ); +* // returns false +*/ +function isNonSymmetricMatrix( v ) { + var M; + var i; + var j; + if ( !isMatrixLike( v ) ) { + return false; + } + M = v.shape[ 0 ]; + if ( M !== v.shape[ 1 ] ) { + return true; + } + for ( i = 0; i < M; i++ ) { + for ( j = 0; j < i; j++ ) { + if ( v.get( i, j ) !== v.get( j, i ) ) { + return true; + } + } + } + return false; +} + + +// EXPORTS // + +module.exports = isNonSymmetricMatrix; diff --git a/is-nonsymmetric-matrix/package.json b/is-nonsymmetric-matrix/package.json new file mode 100644 index 00000000..5d59b416 --- /dev/null +++ b/is-nonsymmetric-matrix/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-nonsymmetric-matrix", + "version": "0.0.0", + "description": "Test if a value is a non-symmetric matrix.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "matrix", + "ismatrix", + "2d", + "square", + "nonsymmetric", + "asymmetric", + "symmetric", + "symm", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-nonsymmetric-matrix/test/test.js b/is-nonsymmetric-matrix/test/test.js new file mode 100644 index 00000000..36de474f --- /dev/null +++ b/is-nonsymmetric-matrix/test/test.js @@ -0,0 +1,147 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var isNonSymmetricMatrix = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNonSymmetricMatrix, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a non-symmetric matrix', function test( t ) { + var arr = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isNonSymmetricMatrix( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray-like object having equal dimensions and which does not equal its transpose', function test( t ) { + var arr; + + arr = { + 'data': [ 1, 2, 3, 4 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isNonSymmetricMatrix( arr ), true, 'returns true' ); + t.end(); + + function get( i, j ) { + return arr.data[ (2*i)+j ]; + } +}); + +tape( 'the function returns `true` if not provided a square matrix', function test( t ) { + var arr = ndarray( 'generic', [ 0, 0, 0, 0, 0, 0 ], [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isNonSymmetricMatrix( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a non-symmetric matrix', function test( t ) { + var arr = ndarray( 'generic', [ 1, 2, 2, 3 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isNonSymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray-like object not having equal dimensions', function test( t ) { + var arr = { + 'data': [ 0, 0, 0, 0, 0, 0 ], + 'shape': [ 3, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 6, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isNonSymmetricMatrix( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray-like object which equals its transpose', function test( t ) { + var arr = { + 'data': [ 1, 2, 2, 3 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isNonSymmetricMatrix( arr ), false, 'returns false' ); + t.end(); + + function get( i, j ) { + return arr.data[ (2*i) + j ]; + } +}); + +tape( 'the function returns `false` if not provided a 2-dimensional ndarray-like object', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 0, 0, 0 ], [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + values = [ + arr, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNonSymmetricMatrix( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-null-array/README.md b/is-null-array/README.md new file mode 100644 index 00000000..b37f7614 --- /dev/null +++ b/is-null-array/README.md @@ -0,0 +1,85 @@ + + +# isNullArray + +> Test if a value is an array-like object containing only null values. + +
+ +## Usage + +```javascript +var isNullArray = require( '@stdlib/assert/is-null-array' ); +``` + +#### isNullArray( value ) + +Tests if a `value` is an array-like object containing only `null` values. + +```javascript +var bool = isNullArray( [ null, null, null ] ); +// returns true + +bool = isNullArray( [ null, NaN, null ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isNullArray = require( '@stdlib/assert/is-null-array' ); + +var bool = isNullArray( [ null ] ); +// returns true + +bool = isNullArray( [ null, null, null ] ); +// returns true + +bool = isNullArray( null ); +// returns false + +bool = isNullArray( 'beep' ); +// returns false + +bool = isNullArray( [ null, 'boop' ] ); +// returns false + +bool = isNullArray( [] ); +// returns false +``` + +
+ + + + + + diff --git a/is-null-array/benchmark/benchmark.js b/is-null-array/benchmark/benchmark.js new file mode 100644 index 00000000..5dc55256 --- /dev/null +++ b/is-null-array/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNullArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( null ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = isNullArray( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/is-null-array/docs/repl.txt b/is-null-array/docs/repl.txt new file mode 100644 index 00000000..82cd02b3 --- /dev/null +++ b/is-null-array/docs/repl.txt @@ -0,0 +1,25 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only null values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + null values. + + Examples + -------- + > var bool = {{alias}}( [ null, null, null ] ) + true + > bool = {{alias}}( [ NaN, 2, null ] ) + false + + See Also + -------- + diff --git a/is-null-array/docs/types/index.d.ts b/is-null-array/docs/types/index.d.ts new file mode 100644 index 00000000..2a6c13f0 --- /dev/null +++ b/is-null-array/docs/types/index.d.ts @@ -0,0 +1,39 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an array-like object containing only `null` values. +* +* @param value - value to test +* @returns boolean indicating whether an input value is an array-like object containing only `null` values +* +* @example +* var bool = isNullArray( [null,null,null] ); +* // returns true +* +* bool = isNullArray( [NaN,2,null] ); +* // returns false +*/ +declare function isNullArray( value: any ): boolean; + + +// EXPORTS // + +export = isNullArray; diff --git a/is-null-array/docs/types/test.ts b/is-null-array/docs/types/test.ts new file mode 100644 index 00000000..7792ad05 --- /dev/null +++ b/is-null-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNullArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNullArray( [ null, null ] ); // $ExpectType boolean + isNullArray( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNullArray(); // $ExpectError + isNullArray( [ null, NaN, null ], 123 ); // $ExpectError +} diff --git a/is-null-array/examples/index.js b/is-null-array/examples/index.js new file mode 100644 index 00000000..2fccbee0 --- /dev/null +++ b/is-null-array/examples/index.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isNullArray = require( './../lib' ); + +console.log( isNullArray( [ null ] ) ); +// => true + +console.log( isNullArray( [ null, null, null ] ) ); +// => true + +console.log( isNullArray( null ) ); +// => false + +console.log( isNullArray( 'beep' ) ); +// => false + +console.log( isNullArray( [ null, 'boop' ] ) ); +// => false + +console.log( isNullArray( [] ) ); +// => false diff --git a/is-null-array/lib/index.js b/is-null-array/lib/index.js new file mode 100644 index 00000000..8c1a4d7c --- /dev/null +++ b/is-null-array/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only `null` values. +* +* @module @stdlib/assert/is-null-array +* +* @example +* var isNullArray = require( '@stdlib/assert/is-null-array' ); +* +* var bool = isNullArray( [null,null,null] ); +* // returns true +* +* bool = isNullArray( [NaN,2,null] ); +* // returns false +*/ + +// MODULES // + +var isNullArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNullArray; diff --git a/is-null-array/lib/main.js b/is-null-array/lib/main.js new file mode 100644 index 00000000..18a07e05 --- /dev/null +++ b/is-null-array/lib/main.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); +var isNull = require( '@stdlib/assert/is-null' ); + + +// MAIN // + +/** +* Tests if a value is an array-like object containing only `null` values. +* +* @name isNullArray +* @type {Function} +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether an input value is an array-like object containing only `null` values +* +* @example +* var bool = isNullArray( [null,null,null] ); +* // returns true +* +* bool = isNullArray( [NaN,2,null] ); +* // returns false +*/ +var isNullArray = arrayfun( isNull ); + + +// EXPORTS // + +module.exports = isNullArray; diff --git a/is-null-array/package.json b/is-null-array/package.json new file mode 100644 index 00000000..b0724ee0 --- /dev/null +++ b/is-null-array/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-null-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only null values.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "array", + "is", + "isarray", + "null", + "isnull", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-null-array/test/test.js b/is-null-array/test/test.js new file mode 100644 index 00000000..d23e75e9 --- /dev/null +++ b/is-null-array/test/test.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNullArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNullArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object having only `null` values', function test( t ) { + var arr; + + arr = [ null, null ]; + t.equal( isNullArray( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': null, + '1': null + }; + t.equal( isNullArray( arr ), true, 'returns true' ); + + arr = [ null, 3, null ]; + t.equal( isNullArray( arr ), false, 'returns false' ); + + arr = [ null, undefined, false, null ]; + t.equal( isNullArray( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-null/README.md b/is-null/README.md new file mode 100644 index 00000000..95f10c8b --- /dev/null +++ b/is-null/README.md @@ -0,0 +1,90 @@ + + +# isNull + +> Test if a value is null. + +
+ +## Usage + +```javascript +var isNull = require( '@stdlib/assert/is-null' ); +``` + +#### isNull( value ) + +Tests if a `value` is `null`. + +```javascript +var bool = isNull( null ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isNull = require( '@stdlib/assert/is-null' ); + +var bool = isNull( null ); +// returns true + +bool = isNull( 'beep' ); +// returns false + +bool = isNull( 5 ); +// returns false + +bool = isNull( void 0 ); +// returns false + +bool = isNull( true ); +// returns false + +bool = isNull( {} ); +// returns false + +bool = isNull( [] ); +// returns false + +bool = isNull( function foo() {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-null/benchmark/benchmark.js b/is-null/benchmark/benchmark.js new file mode 100644 index 00000000..b6c3999d --- /dev/null +++ b/is-null/benchmark/benchmark.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isNull = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNull( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-null/docs/repl.txt b/is-null/docs/repl.txt new file mode 100644 index 00000000..e30889f8 --- /dev/null +++ b/is-null/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is null. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is null. + + Examples + -------- + > var bool = {{alias}}( null ) + true + > bool = {{alias}}( true ) + false + + See Also + -------- + diff --git a/is-null/docs/types/index.d.ts b/is-null/docs/types/index.d.ts new file mode 100644 index 00000000..bac23d1a --- /dev/null +++ b/is-null/docs/types/index.d.ts @@ -0,0 +1,39 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is `null`. +* +* @param value - value to test +* @returns boolean indicating whether value is null +* +* @example +* var bool = isNull( null ); +* // returns true +* +* bool = isNull( true ); +* // returns false +*/ +declare function isNull( value: any ): boolean; + + +// EXPORTS // + +export = isNull; diff --git a/is-null/docs/types/test.ts b/is-null/docs/types/test.ts new file mode 100644 index 00000000..95fee711 --- /dev/null +++ b/is-null/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNull = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNull( null ); // $ExpectType boolean + isNull( NaN ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNull(); // $ExpectError + isNull( null, 123 ); // $ExpectError +} diff --git a/is-null/examples/index.js b/is-null/examples/index.js new file mode 100644 index 00000000..93a4cae1 --- /dev/null +++ b/is-null/examples/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function, no-restricted-syntax */ + +'use strict'; + +var isNull = require( './../lib' ); + +console.log( isNull( null ) ); +// => true + +console.log( isNull( 'beep' ) ); +// => false + +console.log( isNull( 5 ) ); +// => false + +console.log( isNull( void 0 ) ); +// => false + +console.log( isNull( true ) ); +// => false + +console.log( isNull( {} ) ); +// => false + +console.log( isNull( [] ) ); +// => false + +console.log( isNull( function foo() {} ) ); +// => false diff --git a/is-null/lib/index.js b/is-null/lib/index.js new file mode 100644 index 00000000..cdbe3b82 --- /dev/null +++ b/is-null/lib/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is `null`. +* +* @module @stdlib/assert/is-null +* +* @example +* var isNull = require( '@stdlib/assert/is-null' ); +* +* var value = null; +* +* var bool = isNull( value ); +* // returns true +*/ + +// MODULES // + +var isNull = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNull; diff --git a/is-null/lib/main.js b/is-null/lib/main.js new file mode 100644 index 00000000..021bd0c2 --- /dev/null +++ b/is-null/lib/main.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Tests if a value is `null`. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is null +* +* @example +* var bool = isNull( null ); +* // returns true +* +* bool = isNull( true ); +* // returns false +*/ +function isNull( value ) { + return value === null; +} + + +// EXPORTS // + +module.exports = isNull; diff --git a/is-null/package.json b/is-null/package.json new file mode 100644 index 00000000..a121adde --- /dev/null +++ b/is-null/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/assert/is-null", + "version": "0.0.0", + "description": "Test if a value is null.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "null", + "is", + "isnull", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-null/test/test.js b/is-null/test/test.js new file mode 100644 index 00000000..317acb26 --- /dev/null +++ b/is-null/test/test.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNull = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNull, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided `null`', function test( t ) { + t.equal( isNull( null ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided `null`', function test( t ) { + var values; + var i; + + values = [ + 0, + [], + '', + function noop() {}, + false, + {}, + NaN, + undefined + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNull( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-number-array/README.md b/is-number-array/README.md new file mode 100644 index 00000000..daca4cbf --- /dev/null +++ b/is-number-array/README.md @@ -0,0 +1,120 @@ + + +# isNumberArray + +> Test if a value is an array-like object of numbers. + +
+ +## Usage + +```javascript +var isNumberArray = require( '@stdlib/assert/is-number-array' ); +``` + +#### isNumberArray( value ) + +Tests if a `value` is an array-like object of `numbers`. + +```javascript +var bool = isNumberArray( [ 1, 2, 3, 4 ] ); +// returns true +``` + +#### isNumberArray.primitives( value ) + +Tests if a `value` is an array-like object containing **only** `number` primitives. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNumberArray.primitives( [ 1, 2, 3 ] ); +// returns true + +bool = isNumberArray.primitives( [ 1, new Number( 2 ), 3 ] ); +// returns false +``` + +#### isNumberArray.objects( value ) + +Tests if a `value` is an array-like object containing **only** `Number` objects. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNumberArray.objects( [ new Number( 1 ), new Number( 2 ) ] ); +// returns true + +bool = isNumberArray.objects( [ new Number( 1 ), 2 ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNumberArray = require( '@stdlib/assert/is-number-array' ); + +var bool = isNumberArray( [ 3.14 ] ); +// returns true + +bool = isNumberArray( [ NaN ] ); +// returns true + +bool = isNumberArray( [ 1, 2, 3 ] ); +// returns true + +bool = isNumberArray( [ new Number( 3.14 ), 2, 3 ] ); +// returns true + +bool = isNumberArray( 3.14 ); +// returns false + +bool = isNumberArray( [] ); +// returns false + +bool = isNumberArray( [ '1', 2 ] ); +// returns false +``` + +
+ + + + + + diff --git a/is-number-array/benchmark/benchmark.js b/is-number-array/benchmark/benchmark.js new file mode 100644 index 00000000..19d39f38 --- /dev/null +++ b/is-number-array/benchmark/benchmark.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isNumberArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( 1.0 ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( 1.0 ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = fromCodePoint( i % 126 ); + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isNumberArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isNumberArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isNumberArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-number-array/docs/repl.txt b/is-number-array/docs/repl.txt new file mode 100644 index 00000000..ff8c79a3 --- /dev/null +++ b/is-number-array/docs/repl.txt @@ -0,0 +1,73 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + numbers. + + Examples + -------- + > var bool = {{alias}}( [ 1, 2, 3 ] ) + true + > bool = {{alias}}( [ '1', 2, 3 ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only number primitives. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + number primitives. + + Examples + -------- + > var arr = [ 1, 2, 3 ]; + > var bool = {{alias}}.primitives( arr ) + true + > arr = [ 1, new Number( 2 ) ]; + > bool = {{alias}}.primitives( arr ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only `Number` objects. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + `Number` objects. + + Examples + -------- + > var arr = [ new Number( 1 ), new Number( 2 ) ]; + > var bool = {{alias}}.objects( arr ) + true + > arr = [ new Number( 1 ), 2 ]; + > bool = {{alias}}.objects( arr ) + false + + See Also + -------- + diff --git a/is-number-array/docs/types/index.d.ts b/is-number-array/docs/types/index.d.ts new file mode 100644 index 00000000..eaa3271c --- /dev/null +++ b/is-number-array/docs/types/index.d.ts @@ -0,0 +1,108 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNumberArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsNumberArray { + /** + * Tests if a value is an array-like object containing only numbers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only numbers + * + * @example + * var bool = isNumberArray( [ -3.0, new Number(0.0), 2.0 ] ); + * // returns true + * + * @example + * var bool = isNumberArray( [ -3.0, '3.0' ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number primitives. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number primitives + * + * @example + * var bool = isNumberArray.primitives( [ -1.0, 10.0 ] ); + * // returns true + * + * @example + * var bool = isNumberArray.primitives( [ -1.0, 0.0, 5.0 ] ); + * // returns true + * + * @example + * var bool = isNumberArray.primitives( [ -3.0, new Number(-1.0) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number objects + * + * @example + * var bool = isNumberArray.objects( [ new Number(1.0), new Number(3.0) ] ); + * // returns true + * + * @example + * var bool = isNumberArray.objects( [ -1.0, 0.0, 3.0 ] ); + * // returns false + * + * @example + * var bool = isNumberArray.objects( [ 3.0, new Number(-1.0) ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object containing only numbers. +* +* @param value - value to test +* @returns boolean indicating whether value is an array-like object containing only numbers +* +* @example +* var bool = isNumberArray( [ -3.0, new Number(0.0), 2.0 ] ); +* // returns true +* +* @example +* var bool = isNumberArray( [ -3.0, '3.0' ] ); +* // returns false +* @example +* var bool = isNumberArray.primitives( [ -1.0, 0.0, 5.0 ] ); +* // returns true +* +* @example +* var bool = isNumberArray.objects( [ new Number(1.0), new Number(3.0) ] ); +* // returns true +*/ +declare var isNumberArray: IsNumberArray; + + +// EXPORTS // + +export = isNumberArray; diff --git a/is-number-array/docs/types/test.ts b/is-number-array/docs/types/test.ts new file mode 100644 index 00000000..dffdc28c --- /dev/null +++ b/is-number-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNumberArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNumberArray( [ 4 ] ); // $ExpectType boolean + isNumberArray( [ 2.8 ] ); // $ExpectType boolean + isNumberArray( [ '2.8' ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNumberArray(); // $ExpectError + isNumberArray( [ -3 ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNumberArray.primitives( [ new Number( 3 ) ] ); // $ExpectType boolean + isNumberArray.primitives( [ 3 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isNumberArray.primitives(); // $ExpectError + isNumberArray.primitives( [ 2 ], 123 ); // $ExpectError +} + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNumberArray.objects( [ new Number( -2 ) ] ); // $ExpectType boolean + isNumberArray.objects( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isNumberArray.objects(); // $ExpectError + isNumberArray.objects( [ 2 ], 123 ); // $ExpectError +} diff --git a/is-number-array/examples/index.js b/is-number-array/examples/index.js new file mode 100644 index 00000000..dde220b3 --- /dev/null +++ b/is-number-array/examples/index.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNumberArray = require( './../lib' ); + +var bool = isNumberArray( [ 3.14 ] ); +console.log( bool ); +// => true + +bool = isNumberArray( [ NaN ] ); +console.log( bool ); +// => true + +bool = isNumberArray( [ 1, 2, 3 ] ); +console.log( bool ); +// => true + +bool = isNumberArray( [ new Number( 3.14 ), 2, 3 ] ); +console.log( bool ); +// => true + +bool = isNumberArray( 3.14 ); +console.log( bool ); +// => false + +bool = isNumberArray( [] ); +console.log( bool ); +// => false + +bool = isNumberArray( [ '1', 2 ] ); +console.log( bool ); +// => false diff --git a/is-number-array/lib/index.js b/is-number-array/lib/index.js new file mode 100644 index 00000000..55898021 --- /dev/null +++ b/is-number-array/lib/index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object of numbers. +* +* @module @stdlib/assert/is-number-array +* +* @example +* var isNumberArray = require( '@stdlib/assert/is-number-array' ); +* +* var bool = isNumberArray( [ 1, 2, 3 ] ); +* // returns true +* +* bool = isNumberArray( [ '1', 2, 3 ] ); +* // returns false +* +* @example +* var isNumberArray = require( '@stdlib/assert/is-number-array' ).primitives; +* +* var bool = isNumberArray( [ 1, 2, 3 ] ); +* // returns true +* +* bool = isNumberArray( [ 1, new Number( 2 ) ] ); +* // returns false +* +* @example +* var isNumberArray = require( '@stdlib/assert/is-number-array' ).objects; +* +* var bool = isNumberArray( [ new Number( 1 ), new Number( 2 ) ] ); +* // returns true +* +* bool = isNumberArray( [ new Number( 1 ), 2 ] ); +* // returns false +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); +var isNumber = require( '@stdlib/assert/is-number' ); + + +// MAIN // + +var isNumberArray = arrayfun( isNumber ); +setReadOnly( isNumberArray, 'primitives', arrayfun( isNumber.isPrimitive ) ); +setReadOnly( isNumberArray, 'objects', arrayfun( isNumber.isObject ) ); + + +// EXPORTS // + +module.exports = isNumberArray; diff --git a/is-number-array/package.json b/is-number-array/package.json new file mode 100644 index 00000000..36c4f312 --- /dev/null +++ b/is-number-array/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-number-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object of numbers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "array", + "array-like", + "is", + "number", + "numeric", + "isnumber", + "isarray", + "isarraylike", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-number-array/test/test.js b/is-number-array/test/test.js new file mode 100644 index 00000000..adf2a32a --- /dev/null +++ b/is-number-array/test/test.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNumberArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNumberArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only numbers', function test( t ) { + var arr; + + arr = [ 1, new Number( 2 ), 3, new Number( 4 ) ]; + t.strictEqual( isNumberArray( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': 1, + '1': 2 + }; + t.strictEqual( isNumberArray( arr ), true, 'returns true' ); + + arr = [ 1, '2', 3 ]; + t.strictEqual( isNumberArray( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only number primitives', function test( t ) { + var arr; + + arr = [ 1, 2, 3, 4 ]; + t.strictEqual( isNumberArray.primitives( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': 1, + '1': 2 + }; + t.strictEqual( isNumberArray.primitives( arr ), true, 'returns true' ); + + arr = [ new Number( 1 ), 2, 3 ]; + t.strictEqual( isNumberArray.primitives( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only `Number` objects', function test( t ) { + var arr; + + arr = [ new Number( 1 ), new Number( 2 ), new Number( 3 ) ]; + t.strictEqual( isNumberArray.objects( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': new Number( 1 ), + '1': new Number( 2 ) + }; + t.strictEqual( isNumberArray.objects( arr ), true, 'returns true' ); + + arr = [ new Number( 1 ), 2, 3 ]; + t.strictEqual( isNumberArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-number/README.md b/is-number/README.md new file mode 100644 index 00000000..b2190935 --- /dev/null +++ b/is-number/README.md @@ -0,0 +1,130 @@ + + +# isNumber + +> Test if a value is a number. + +
+ +## Usage + +```javascript +var isNumber = require( '@stdlib/assert/is-number' ); +``` + +#### isNumber( value ) + +Tests if a `value` is a `number`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNumber( 3.14 ); +// returns true + +bool = isNumber( new Number( 3.14 ) ); +// returns true + +bool = isNumber( NaN ); +// returns true + +bool = isNumber( null ); +// returns false +``` + +#### isNumber.isPrimitive( value ) + +Tests if a `value` is a primitive `number`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNumber.isPrimitive( 3.14 ); +// returns true + +bool = isNumber.isPrimitive( NaN ); +// returns true + +bool = isNumber.isPrimitive( new Number( 3.14 ) ); +// returns false +``` + +#### isNumber.isObject( value ) + +Tests if a `value` is a `Number` object. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isNumber.isObject( 3.14 ); +// returns false + +bool = isNumber.isObject( new Number( 3.14 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isNumber = require( '@stdlib/assert/is-number' ); + +var bool = isNumber( 5 ); +// returns true + +bool = isNumber( new Number( 5 ) ); +// returns true + +bool = isNumber( NaN ); +// returns true + +bool = isNumber( '5' ); +// returns false + +bool = isNumber( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-number/benchmark/benchmark.js b/is-number/benchmark/benchmark.js new file mode 100644 index 00000000..419b31cb --- /dev/null +++ b/is-number/benchmark/benchmark.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isNumber = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-number/docs/repl.txt b/is-number/docs/repl.txt new file mode 100644 index 00000000..5aca992b --- /dev/null +++ b/is-number/docs/repl.txt @@ -0,0 +1,72 @@ + +{{alias}}( value ) + Tests if a value is a number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number. + + Examples + -------- + > var bool = {{alias}}( 3.14 ) + true + > bool = {{alias}}( new Number( 3.14 ) ) + true + > bool = {{alias}}( NaN ) + true + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 3.14 ) + true + > bool = {{alias}}.isPrimitive( NaN ) + true + > bool = {{alias}}.isPrimitive( new Number( 3.14 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a `Number` object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a `Number` object. + + Examples + -------- + > var bool = {{alias}}.isObject( 3.14 ) + false + > bool = {{alias}}.isObject( new Number( 3.14 ) ) + true + + See Also + -------- + diff --git a/is-number/docs/types/index.d.ts b/is-number/docs/types/index.d.ts new file mode 100644 index 00000000..83b2e741 --- /dev/null +++ b/is-number/docs/types/index.d.ts @@ -0,0 +1,121 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isNumber` with methods for testing for primitives and objects, respectively. +*/ +interface IsNumber { + /** + * Tests if a value is a number. + * + * @param value - value to test + * @returns boolean indicating whether value is a number + * + * @example + * var bool = isNumber( 3.14 ); + * // returns true + * + * @example + * var bool = isNumber( new Number( 3.14 ) ); + * // returns true + * + * @example + * var bool = isNumber( NaN ); + * // returns true + * + * @example + * var bool = isNumber( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive + * + * @example + * var bool = isNumber.isPrimitive( 3.14 ); + * // returns true + * + * @example + * var bool = isNumber.isPrimitive( NaN ); + * // returns true + * + * @example + * var bool = isNumber.isPrimitive( new Number( 3.14 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object + * + * @example + * var bool = isNumber.isObject( 3.14 ); + * // returns false + * + * @example + * var bool = isNumber.isObject( new Number( 3.14 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a number. +* +* @param value - value to test +* @returns boolean indicating whether value is a number +* +* @example +* var bool = isNumber( 3.14 ); +* // returns true +* +* @example +* var bool = isNumber( new Number( 3.14 ) ); +* // returns true +* +* @example +* var bool = isNumber( NaN ); +* // returns true +* +* @example +* var bool = isNumber( null ); +* // returns false +* +* @example +* var bool = isNumber.isPrimitive( 3.14 ); +* // returns true +* +* @example +* var bool = isNumber.isObject( new Number( 3.14 ) ); +* // returns true +*/ +declare var isNumber: IsNumber; + + +// EXPORTS // + +export = isNumber; diff --git a/is-number/docs/types/test.ts b/is-number/docs/types/test.ts new file mode 100644 index 00000000..5879bcff --- /dev/null +++ b/is-number/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNumber = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNumber( 3 ); // $ExpectType boolean + isNumber( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNumber(); // $ExpectError + isNumber( 2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNumber.isPrimitive( new Number( 2 ) ); // $ExpectType boolean + isNumber.isPrimitive( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isNumber.isPrimitive(); // $ExpectError + isNumber.isPrimitive( 2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isNumber.isObject( new Number( 2 ) ); // $ExpectType boolean + isNumber.isObject( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isNumber.isObject(); // $ExpectError + isNumber.isObject( 2, 123 ); // $ExpectError +} diff --git a/is-number/examples/index.js b/is-number/examples/index.js new file mode 100644 index 00000000..37aad180 --- /dev/null +++ b/is-number/examples/index.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isNumber = require( './../lib' ); + +console.log( isNumber( 5 ) ); +// => true + +console.log( isNumber( new Number( 5 ) ) ); +// => true + +console.log( isNumber( NaN ) ); +// => true + +console.log( isNumber( '5' ) ); +// => false + +console.log( isNumber( null ) ); +// => false diff --git a/is-number/lib/index.js b/is-number/lib/index.js new file mode 100644 index 00000000..bbea7253 --- /dev/null +++ b/is-number/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a number. +* +* @module @stdlib/assert/is-number +* +* @example +* var isNumber = require( '@stdlib/assert/is-number' ); +* +* var bool = isNumber( 3.14 ); +* // returns true +* +* bool = isNumber( new Number( 3.14 ) ); +* // returns true +* +* bool = isNumber( NaN ); +* // returns true +* +* bool = isNumber( null ); +* // returns false +* +* @example +* var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +* +* var bool = isNumber( 3.14 ); +* // returns true +* +* bool = isNumber( NaN ); +* // returns true +* +* bool = isNumber( new Number( 3.14 ) ); +* // returns false +* +* @example +* var isNumber = require( '@stdlib/assert/is-number' ).isObject; +* +* var bool = isNumber( 3.14 ); +* // returns false +* +* bool = isNumber( new Number( 3.14 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isNumber = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isNumber, 'isPrimitive', isPrimitive ); +setReadOnly( isNumber, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isNumber; diff --git a/is-number/lib/main.js b/is-number/lib/main.js new file mode 100644 index 00000000..5a6ff2ea --- /dev/null +++ b/is-number/lib/main.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a number +* +* @example +* var bool = isNumber( 3.14 ); +* // returns true +* +* @example +* var bool = isNumber( new Number( 3.14 ) ); +* // returns true +* +* @example +* var bool = isNumber( NaN ); +* // returns true +* +* @example +* var bool = isNumber( null ); +* // returns false +*/ +function isNumber( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isNumber; diff --git a/is-number/lib/object.js b/is-number/lib/object.js new file mode 100644 index 00000000..bd7e9ded --- /dev/null +++ b/is-number/lib/object.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); +var nativeClass = require( '@stdlib/utils/native-class' ); +var Number = require( '@stdlib/number/ctor' ); +var test = require( './try2serialize.js' ); + + +// VARIABLES // + +var FLG = hasToStringTag(); + + +// MAIN // + +/** +* Tests if a value is a number object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object +* +* @example +* var bool = isNumber( 3.14 ); +* // returns false +* +* @example +* var bool = isNumber( new Number( 3.14 ) ); +* // returns true +*/ +function isNumber( value ) { + if ( typeof value === 'object' ) { + if ( value instanceof Number ) { + return true; + } + if ( FLG ) { + return test( value ); + } + return ( nativeClass( value ) === '[object Number]' ); + } + return false; +} + + +// EXPORTS // + +module.exports = isNumber; diff --git a/is-number/lib/primitive.js b/is-number/lib/primitive.js new file mode 100644 index 00000000..967ac4cc --- /dev/null +++ b/is-number/lib/primitive.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Tests if a value is a number primitive. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive +* +* @example +* var bool = isNumber( 3.14 ); +* // returns true +* +* @example +* var bool = isNumber( NaN ); +* // returns true +* +* @example +* var bool = isNumber( new Number( 3.14 ) ); +* // returns false +*/ +function isNumber( value ) { + return ( typeof value === 'number' ); +} + + +// EXPORTS // + +module.exports = isNumber; diff --git a/is-number/lib/tostring.js b/is-number/lib/tostring.js new file mode 100644 index 00000000..81c5e661 --- /dev/null +++ b/is-number/lib/tostring.js @@ -0,0 +1,34 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Number = require( '@stdlib/number/ctor' ); + + +// MAIN // + +// eslint-disable-next-line stdlib/no-redeclare +var toString = Number.prototype.toString; // non-generic + + +// EXPORTS // + +module.exports = toString; diff --git a/is-number/lib/try2serialize.js b/is-number/lib/try2serialize.js new file mode 100644 index 00000000..556559c4 --- /dev/null +++ b/is-number/lib/try2serialize.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var toString = require( './tostring.js' ); // eslint-disable-line stdlib/no-redeclare + + +// MAIN // + +/** +* Attempts to serialize a value to a string. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value can be serialized +*/ +function test( value ) { + try { + toString.call( value ); + return true; + } catch ( err ) { // eslint-disable-line no-unused-vars + return false; + } +} + + +// EXPORTS // + +module.exports = test; diff --git a/is-number/package.json b/is-number/package.json new file mode 100644 index 00000000..496cb2b9 --- /dev/null +++ b/is-number/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-number", + "version": "0.0.0", + "description": "Test if a value is a number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "is", + "type", + "check", + "isnumber", + "isnumeric", + "primitive", + "object", + "validate", + "valid", + "test" + ] +} diff --git a/is-number/test/test.js b/is-number/test/test.js new file mode 100644 index 00000000..fd1b73df --- /dev/null +++ b/is-number/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNumber = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number', function test( t ) { + t.strictEqual( typeof isNumber.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object', function test( t ) { + t.strictEqual( typeof isNumber.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-number/test/test.main.js b/is-number/test/test.main.js new file mode 100644 index 00000000..830e78cc --- /dev/null +++ b/is-number/test/test.main.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNumber = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number', function test( t ) { + t.strictEqual( isNumber( 5 ), true, 'returns true' ); + t.strictEqual( isNumber( new Number( 5 ) ), true, 'returns true' ); + t.strictEqual( isNumber( NaN ), true, 'returns true' ); + t.strictEqual( isNumber( new Number( NaN ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + false, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-number/test/test.object.js b/is-number/test/test.object.js new file mode 100644 index 00000000..6875b224 --- /dev/null +++ b/is-number/test/test.object.js @@ -0,0 +1,125 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); +var Number = require( '@stdlib/number/ctor' ); +var isNumber = require( './../lib/object.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object', function test( t ) { + t.strictEqual( isNumber( new Number( 5 ) ), true, 'returns true' ); + t.strictEqual( isNumber( new Number( NaN ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number', function test( t ) { + t.strictEqual( isNumber( 3.14 ), false, 'returns false' ); + t.strictEqual( isNumber( NaN ), false, 'returns false' ); + t.end(); +}); + +tape( 'if `Symbol.toStringTag` is supported, the function guards against objects masquerading as `Number` objects', opts, function test( t ) { + var isNumber; + var mock; + + isNumber = proxyquire( './../lib/object.js', { + '@stdlib/assert/has-tostringtag-support': detect + }); + + mock = { + 'toString': toString, + 'valueOf': valueOf + }; + if ( hasToStringTag() ) { + mock[ Symbol.toStringTag ] = 'Number'; + } + t.strictEqual( isNumber( mock ), false, 'returns false' ); + t.end(); + + function detect() { + return true; + } + function toString() { + return '999'; + } + function valueOf() { + return 999; + } +}); + +tape( 'if `Symbol.toStringTag` is not supported, the function attempts to determine the native class', function test( t ) { + var isNumber = proxyquire( './../lib/object.js', { + '@stdlib/assert/has-tostringtag-support': detect + }); + + t.strictEqual( isNumber( new Number( 5 ) ), true, 'returns true' ); + t.strictEqual( isNumber( {} ), false, 'returns false' ); + + t.end(); + + function detect() { + return false; + } +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-number/test/test.primitive.js b/is-number/test/test.primitive.js new file mode 100644 index 00000000..2c48a912 --- /dev/null +++ b/is-number/test/test.primitive.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isNumber = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number', function test( t ) { + t.strictEqual( isNumber( 3.14 ), true, 'returns true' ); + t.strictEqual( isNumber( NaN ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object', function test( t ) { + t.strictEqual( isNumber( new Number( 5 ) ), false, 'returns false' ); + t.strictEqual( isNumber( new Number( NaN ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + false, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-number/test/test.try2serialize.js b/is-number/test/test.try2serialize.js new file mode 100644 index 00000000..e6650665 --- /dev/null +++ b/is-number/test/test.try2serialize.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var serialize = require( './../lib/try2serialize.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof serialize, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if able to serialize', function test( t ) { + t.strictEqual( serialize( 5 ), true, 'returns true' ); + t.strictEqual( serialize( new Number( 5 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if unable to serialize', function test( t ) { + t.strictEqual( serialize( {} ), false, 'returns false' ); + t.end(); +}); diff --git a/is-numeric-array/README.md b/is-numeric-array/README.md new file mode 100644 index 00000000..d2a61f4b --- /dev/null +++ b/is-numeric-array/README.md @@ -0,0 +1,138 @@ + + +# isNumericArray + +> Test if a value is a numeric array. + +
+ +## Usage + +```javascript +var isNumericArray = require( '@stdlib/assert/is-numeric-array' ); +``` + +#### isNumericArray( value ) + +Tests if a value is a numeric array. + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); + +var bool = isNumericArray( new Int8Array( 10 ) ); +// returns true + +bool = isNumericArray( [ 1, 2, 3 ] ); +// returns true + +bool = isNumericArray( [ '1', '2', '3' ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + + + +```javascript +var Buffer = require( '@stdlib/buffer/ctor' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isNumericArray = require( '@stdlib/assert/is-numeric-array' ); + +var arr = new Int8Array( 10 ); +var bool = isNumericArray( arr ); +// returns true + +arr = new Uint8Array( 10 ); +bool = isNumericArray( arr ); +// returns true + +arr = new Uint8ClampedArray( 10 ); +bool = isNumericArray( arr ); +// returns true + +arr = new Int16Array( 10 ); +bool = isNumericArray( arr ); +// returns true + +arr = new Uint16Array( 10 ); +bool = isNumericArray( arr ); +// returns true + +arr = new Int32Array( 10 ); +bool = isNumericArray( arr ); +// returns true + +arr = new Uint32Array( 10 ); +bool = isNumericArray( arr ); +// returns true + +arr = new Float32Array( 10 ); +bool = isNumericArray( arr ); +// returns true + +arr = new Float64Array( 10 ); +bool = isNumericArray( arr ); +// returns true + +arr = [ 1, 2, 3 ]; +bool = isNumericArray( arr ); +// returns true + +bool = isNumericArray( [] ); +// returns false + +bool = isNumericArray( {} ); +// returns false + +bool = isNumericArray( null ); +// returns false + +bool = isNumericArray( new Buffer( 10 ) ); +// returns false +``` + +
+ + + + + + diff --git a/is-numeric-array/benchmark/benchmark.js b/is-numeric-array/benchmark/benchmark.js new file mode 100644 index 00000000..3ff09e73 --- /dev/null +++ b/is-numeric-array/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var isNumericArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( 1.0 ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = fromCodePoint( i % 126 ); + bool = isNumericArray( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/is-numeric-array/docs/repl.txt b/is-numeric-array/docs/repl.txt new file mode 100644 index 00000000..67be70aa --- /dev/null +++ b/is-numeric-array/docs/repl.txt @@ -0,0 +1,27 @@ + +{{alias}}( value ) + Tests if a value is a numeric array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating if a value is a numeric array. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/int8}}( 10 ) ) + true + > bool = {{alias}}( [ 1, 2, 3 ] ) + true + > bool = {{alias}}( [ '1', '2', '3' ] ) + false + + See Also + -------- + + diff --git a/is-numeric-array/docs/types/index.d.ts b/is-numeric-array/docs/types/index.d.ts new file mode 100644 index 00000000..e8cf53b3 --- /dev/null +++ b/is-numeric-array/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a numeric array. +* +* @param v - value to test +* @returns boolean indicating if a value is a numeric array +* +* @example +* var bool = isNumericArray( new Int8Array( 10 ) ); +* // returns true +* +* bool = isNumericArray( [ 1, 2, 3 ] ); +* // returns true +* +* bool = isNumericArray( [ '1', '2', '3' ] ); +* // returns false +*/ +declare function isNumericArray( v: any ): boolean; + + +// EXPORTS // + +export = isNumericArray; diff --git a/is-numeric-array/docs/types/test.ts b/is-numeric-array/docs/types/test.ts new file mode 100644 index 00000000..0d5a3d86 --- /dev/null +++ b/is-numeric-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isNumericArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isNumericArray( [ 1, 2, 3 ] ); // $ExpectType boolean + isNumericArray( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isNumericArray(); // $ExpectError + isNumericArray( [ 1, 2, 3 ], 123 ); // $ExpectError +} diff --git a/is-numeric-array/examples/index.js b/is-numeric-array/examples/index.js new file mode 100644 index 00000000..2b63ebab --- /dev/null +++ b/is-numeric-array/examples/index.js @@ -0,0 +1,83 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' ); +var isNumericArray = require( './../lib' ); + +var arr = new Int8Array( 10 ); +console.log( isNumericArray( arr ) ); +// => true + +arr = new Uint8Array( 10 ); +console.log( isNumericArray( arr ) ); +// => true + +arr = new Uint8ClampedArray( 10 ); +console.log( isNumericArray( arr ) ); +// => true + +arr = new Int16Array( 10 ); +console.log( isNumericArray( arr ) ); +// => true + +arr = new Uint16Array( 10 ); +console.log( isNumericArray( arr ) ); +// => true + +arr = new Int32Array( 10 ); +console.log( isNumericArray( arr ) ); +// => true + +arr = new Uint32Array( 10 ); +console.log( isNumericArray( arr ) ); +// => true + +arr = new Float32Array( 10 ); +console.log( isNumericArray( arr ) ); +// => true + +arr = new Float64Array( 10 ); +console.log( isNumericArray( arr ) ); +// => true + +arr = [ 1, 2, 3 ]; +console.log( isNumericArray( arr ) ); +// => true + +console.log( isNumericArray( [] ) ); +// => false + +console.log( isNumericArray( {} ) ); +// => false + +console.log( isNumericArray( null ) ); +// => false + +console.log( isNumericArray( allocUnsafe( 10 ) ) ); +// => false diff --git a/is-numeric-array/lib/index.js b/is-numeric-array/lib/index.js new file mode 100644 index 00000000..96085efb --- /dev/null +++ b/is-numeric-array/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a numeric array. +* +* @module @stdlib/assert/is-numeric-array +* +* @example +* var isNumericArray = require( '@stdlib/assert/is-numeric-array' ); +* +* var bool = isNumericArray( new Int8Array( 10 ) ); +* // returns true +* +* bool = isNumericArray( [ 1, 2, 3 ] ); +* // returns true +* +* bool = isNumericArray( [ '1', '2', '3' ] ); +* // returns false +*/ + +// MODULES // + +var isNumericArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isNumericArray; diff --git a/is-numeric-array/lib/main.js b/is-numeric-array/lib/main.js new file mode 100644 index 00000000..6940327f --- /dev/null +++ b/is-numeric-array/lib/main.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isTypedArray = require( '@stdlib/assert/is-typed-array' ); +var isNumberArray = require( '@stdlib/assert/is-number-array' ).primitives; +var isBuffer = require( '@stdlib/assert/is-buffer' ); + + +// MAIN // + +/** +* Tests if a value is a numeric array. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a numeric array +* +* @example +* var bool = isNumericArray( new Int8Array( 10 ) ); +* // returns true +* +* bool = isNumericArray( [ 1, 2, 3 ] ); +* // returns true +* +* bool = isNumericArray( [ '1', '2', '3' ] ); +* // returns false +*/ +function isNumericArray( v ) { + return ( + !isBuffer( v ) && + (isTypedArray( v ) || isNumberArray( v )) + ); +} + + +// EXPORTS // + +module.exports = isNumericArray; diff --git a/is-numeric-array/package.json b/is-numeric-array/package.json new file mode 100644 index 00000000..635c09f4 --- /dev/null +++ b/is-numeric-array/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-numeric-array", + "version": "0.0.0", + "description": "Test if a value is a numeric array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "numeric", + "number", + "type", + "check", + "valid", + "validate", + "isvalid" + ] +} diff --git a/is-numeric-array/test/test.js b/is-numeric-array/test/test.js new file mode 100644 index 00000000..334de609 --- /dev/null +++ b/is-numeric-array/test/test.js @@ -0,0 +1,91 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' ); +var isNumericArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isNumericArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a numeric array', function test( t ) { + var values; + var i; + + values = [ + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Float32Array( 10 ), + new Float64Array( 10 ), + [ 1, 2, 3 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNumericArray( values[i] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a numeric array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + undefined, + true, + [], + [5, null], + [5, '5' ], + {}, + function noop() {}, + allocUnsafe( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isNumericArray( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-object-array/README.md b/is-object-array/README.md new file mode 100644 index 00000000..c6d8e4e0 --- /dev/null +++ b/is-object-array/README.md @@ -0,0 +1,101 @@ + + +# isObjectArray + +> Test if a value is an array-like object containing only objects. + +
+ +## Usage + +```javascript +var isObjectArray = require( '@stdlib/assert/is-object-array' ); +``` + +#### isObjectArray( value ) + +Tests if a `value` is an array-like object containing **only** `objects`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isObjectArray( [ {}, new Number(3.0) ] ); +// returns true + +bool = isObjectArray( [ {}, { 'beep': 'boop' } ] ); +// returns true + +bool = isObjectArray( [ {}, '3.0' ] ); +// returns false + +bool = isObjectArray( [] ); +// returns false + +bool = isObjectArray( [ null, {} ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isObjectArray = require( '@stdlib/assert/is-object-array' ); + +var bool = isObjectArray( [ { 'beep': 'boop' }, {}, {} ] ); +// returns true + +bool = isObjectArray( [ new Date(), new Number( 3 ) ] ); +// returns true + +bool = isObjectArray( [ {}, new String( 'abc' ), {} ] ); +// returns true + +bool = isObjectArray( [ [], {} ] ); +// returns false + +bool = isObjectArray( [ 'a', 'b' ] ); +// returns false + +bool = isObjectArray( [] ); +// returns false +``` + +
+ + + + + + diff --git a/is-object-array/benchmark/benchmark.js b/is-object-array/benchmark/benchmark.js new file mode 100644 index 00000000..7998169f --- /dev/null +++ b/is-object-array/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isObjectArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( {} ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = isObjectArray( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/is-object-array/docs/repl.txt b/is-object-array/docs/repl.txt new file mode 100644 index 00000000..b78b4565 --- /dev/null +++ b/is-object-array/docs/repl.txt @@ -0,0 +1,27 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only objects. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + objects. + + Examples + -------- + > var bool = {{alias}}( [ {}, new Number(3.0) ] ) + true + > bool = {{alias}}( [ {}, { 'beep': 'boop' } ] ) + true + > bool = {{alias}}( [ {}, '3.0' ] ) + false + + See Also + -------- + diff --git a/is-object-array/docs/types/index.d.ts b/is-object-array/docs/types/index.d.ts new file mode 100644 index 00000000..346703c9 --- /dev/null +++ b/is-object-array/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Test if a value is an array-like object containing only objects. +* +* @param value - value to test +* @returns boolean indicating whether an input value is an array-like object containing only objects +* +* @example +* var bool = isObjectArray( [ {}, new Number(3.0) ] ); +* // returns true +* +* bool = isObjectArray( [ {}, { 'beep': 'boop' } ] ); +* // returns true +* +* bool = isObjectArray( [ {}, '3.0' ] ); +* // returns false +*/ +declare function isObjectArray( value: any ): boolean; + + +// EXPORTS // + +export = isObjectArray; diff --git a/is-object-array/docs/types/test.ts b/is-object-array/docs/types/test.ts new file mode 100644 index 00000000..eda2b100 --- /dev/null +++ b/is-object-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isObjectArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isObjectArray( [ {}, {} ] ); // $ExpectType boolean + isObjectArray( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isObjectArray(); // $ExpectError + isObjectArray( [ {} ], 123 ); // $ExpectError +} diff --git a/is-object-array/examples/index.js b/is-object-array/examples/index.js new file mode 100644 index 00000000..78467ed6 --- /dev/null +++ b/is-object-array/examples/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, object-curly-newline */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isObjectArray = require( './../lib' ); + +console.log( isObjectArray( [ { 'beep': 'boop' }, {}, {} ] ) ); +// => true + +console.log( isObjectArray( [ new Date(), new Number( 3 ) ] ) ); +// => true + +console.log( isObjectArray( [ {}, new String( 'abc' ), {} ] ) ); +// => true + +console.log( isObjectArray( [ [], {} ] ) ); +// => false + +console.log( isObjectArray( [ 'a', 'b' ] ) ); +// => false + +console.log( isObjectArray( [] ) ); +// => false diff --git a/is-object-array/lib/index.js b/is-object-array/lib/index.js new file mode 100644 index 00000000..b2f97038 --- /dev/null +++ b/is-object-array/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only objects. +* +* @module @stdlib/assert/is-object-array +* +* @example +* var isObjectArray = require( '@stdlib/assert/is-object-array' ); +* +* var bool = isObjectArray( [ {}, new Number(3.0) ] ); +* // returns true +* +* bool = isObjectArray( [ {}, { 'beep': 'boop' } ] ); +* // returns true +* +* bool = isObjectArray( [ {}, '3.0' ] ); +* // returns false +*/ + +// MODULES // + +var isObjectArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isObjectArray; diff --git a/is-object-array/lib/main.js b/is-object-array/lib/main.js new file mode 100644 index 00000000..411a7c42 --- /dev/null +++ b/is-object-array/lib/main.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); +var isObject = require( '@stdlib/assert/is-object' ); + + +// MAIN // + +/** +* Test if a value is an array-like object containing only objects. +* +* @name isObjectArray +* @type {Function} +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether an input value is an array-like object containing only objects +* +* @example +* var bool = isObjectArray( [ {}, new Number(3.0) ] ); +* // returns true +* +* bool = isObjectArray( [ {}, { 'beep': 'boop' } ] ); +* // returns true +* +* bool = isObjectArray( [ {}, '3.0' ] ); +* // returns false +*/ +var isObjectArray = arrayfun( isObject ); + + +// EXPORTS // + +module.exports = isObjectArray; diff --git a/is-object-array/package.json b/is-object-array/package.json new file mode 100644 index 00000000..5bf00674 --- /dev/null +++ b/is-object-array/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-object-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only objects.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "array", + "is", + "isarray", + "object", + "obj", + "isobject", + "type", + "check", + "validate", + "test" + ] +} diff --git a/is-object-array/test/test.js b/is-object-array/test/test.js new file mode 100644 index 00000000..679b6935 --- /dev/null +++ b/is-object-array/test/test.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isObjectArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isObjectArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only objects', function test( t ) { + var arr; + var obj; + + obj = { + 'type': 'object' + }; + arr = [ obj, /.*/, {} ]; + t.equal( isObjectArray( arr ), true, 'returns true' ); + + arr = [ 5.0, {}, new Date() ]; + t.equal( isObjectArray( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': {}, + '1': new Date() + }; + t.equal( isObjectArray( arr ), true, 'returns true' ); + + arr = []; + t.equal( isObjectArray( arr ), false, 'returns false when provided an empty array' ); + + t.end(); +}); diff --git a/is-object-like/README.md b/is-object-like/README.md new file mode 100644 index 00000000..328460ec --- /dev/null +++ b/is-object-like/README.md @@ -0,0 +1,150 @@ + + +# isObjectLike + +> Test if a value is object-like. + +
+ +## Usage + +```javascript +var isObjectLike = require( '@stdlib/assert/is-object-like' ); +``` + +#### isObjectLike( value ) + +Tests if a `value` is object-like. + +```javascript +var bool = isObjectLike( {} ); +// returns true + +bool = isObjectLike( [] ); +// returns true + +bool = isObjectLike( true ); +// returns false +``` + +#### isObjectLike.isObjectLikeArray( value ) + +Tests if a `value` is an `array` of object-like values. + +```javascript +var bool = isObjectLike.isObjectLikeArray( [ {}, [] ] ); +// returns true + +bool = isObjectLike.isObjectLikeArray( [ {}, '3.0' ] ); +// returns false + +bool = isObjectLike.isObjectLikeArray( [] ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Return values are the same as would be obtained using the built-in [`typeof`][type-of] operator **except** that `null` is [**not**][harmony-proposal] considered an `object`. + + ```javascript + var bool = ( typeof null === 'object' ); + // returns true + + bool = isObjectLike( null ); + // returns false + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var isObjectLike = require( '@stdlib/assert/is-object-like' ); + +var bool = isObjectLike( {} ); +// returns true + +bool = isObjectLike( [] ); +// returns true + +bool = isObjectLike( /./ ); +// returns true + +bool = isObjectLike( new Date() ); +// returns true + +bool = isObjectLike( Math ); +// returns true + +bool = isObjectLike( JSON ); +// returns true + +bool = isObjectLike( new Int8Array() ); +// returns true + +bool = isObjectLike( new ArrayBuffer() ); +// returns true + +bool = isObjectLike( 'a' ); +// returns false + +bool = isObjectLike( 5 ); +// returns false + +bool = isObjectLike( null ); +// returns false + +bool = isObjectLike( void 0 ); +// returns false + +bool = isObjectLike( function foo() {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-object-like/benchmark/benchmark.js b/is-object-like/benchmark/benchmark.js new file mode 100644 index 00000000..9adae6b7 --- /dev/null +++ b/is-object-like/benchmark/benchmark.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isObjectLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + new Date() + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isObjectLike( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-object-like/docs/repl.txt b/is-object-like/docs/repl.txt new file mode 100644 index 00000000..bf42774c --- /dev/null +++ b/is-object-like/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( value ) + Tests if a value is object-like. + + Return values are the same as would be obtained using the built-in `typeof` + operator except that `null` is not considered an object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is object-like. + + Examples + -------- + > var bool = {{alias}}( {} ) + true + > bool = {{alias}}( [] ) + true + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-object-like/docs/types/index.d.ts b/is-object-like/docs/types/index.d.ts new file mode 100644 index 00000000..d435a9bd --- /dev/null +++ b/is-object-like/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is object-like. +* +* ## Notes +* +* - Return values are the same as would be obtained using the built-in `typeof` operator except that `null` is not considered an object. +* +* @param value - value to test +* @returns boolean indicating whether a value is object-like +* +* @example +* var bool = isObjectLike( {} ); +* // returns true +* +* @example +* var bool = isObjectLike( [] ); +* // returns true +* +* @example +* var bool = isObjectLike( null ); +* // returns false +*/ +declare function isObjectLike( value: any ): boolean; + + +// EXPORTS // + +export = isObjectLike; diff --git a/is-object-like/docs/types/test.ts b/is-object-like/docs/types/test.ts new file mode 100644 index 00000000..8bf66d66 --- /dev/null +++ b/is-object-like/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isObjectLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isObjectLike( {} ); // $ExpectType boolean + isObjectLike( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isObjectLike(); // $ExpectError + isObjectLike( {}, 123 ); // $ExpectError +} diff --git a/is-object-like/examples/index.js b/is-object-like/examples/index.js new file mode 100644 index 00000000..c62dee69 --- /dev/null +++ b/is-object-like/examples/index.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-restricted-syntax, no-empty-function */ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var isObjectLike = require( './../lib' ); + +console.log( isObjectLike( {} ) ); +// => true + +console.log( isObjectLike( [] ) ); +// => true + +console.log( isObjectLike( /./ ) ); +// => true + +console.log( isObjectLike( new Date() ) ); +// => true + +console.log( isObjectLike( Math ) ); +// => true + +console.log( isObjectLike( JSON ) ); +// => true + +console.log( isObjectLike( new Int8Array() ) ); +// => true + +console.log( isObjectLike( new ArrayBuffer() ) ); +// => true + +console.log( isObjectLike( 'a' ) ); +// => false + +console.log( isObjectLike( 5 ) ); +// => false + +console.log( isObjectLike( null ) ); +// => false + +console.log( isObjectLike( void 0 ) ); +// => false + +console.log( isObjectLike( function foo() {} ) ); +// => false diff --git a/is-object-like/lib/index.js b/is-object-like/lib/index.js new file mode 100644 index 00000000..347a6c7f --- /dev/null +++ b/is-object-like/lib/index.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is object-like. +* +* @module @stdlib/assert/is-object-like +* +* @example +* var isObjectLike = require( '@stdlib/assert/is-object-like' ); +* +* var bool = isObjectLike( {} ); +* // returns true +* +* bool = isObjectLike( [] ); +* // returns true +* +* bool = isObjectLike( null ); +* // returns false +* +* @example +* var isObjectLike = require( '@stdlib/assert/is-object-like' ).isObjectLikeArray; +* +* var bool = isObjectLike( [ {}, [] ] ); +* // returns true +* +* bool = isObjectLike( [ {}, '3.0' ] ); +* // returns false +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-function' ); +var isObjectLike = require( './main.js' ); + + +// MAIN // + +setReadOnly( isObjectLike, 'isObjectLikeArray', arrayfun( isObjectLike ) ); + + +// EXPORTS // + +module.exports = isObjectLike; diff --git a/is-object-like/lib/main.js b/is-object-like/lib/main.js new file mode 100644 index 00000000..afd44755 --- /dev/null +++ b/is-object-like/lib/main.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Tests if a value is object-like. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is object-like +* +* @example +* var bool = isObjectLike( {} ); +* // returns true +* +* @example +* var bool = isObjectLike( [] ); +* // returns true +* +* @example +* var bool = isObjectLike( null ); +* // returns false +*/ +function isObjectLike( value ) { + return ( + value !== null && + typeof value === 'object' + ); +} + + +// EXPORTS // + +module.exports = isObjectLike; diff --git a/is-object-like/package.json b/is-object-like/package.json new file mode 100644 index 00000000..c056cf02 --- /dev/null +++ b/is-object-like/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-object-like", + "version": "0.0.0", + "description": "Test if a value is object-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "type", + "typeof", + "object", + "obj", + "object-like", + "is", + "isobjectlike", + "check", + "validate", + "valid", + "test", + "isvalid" + ] +} diff --git a/is-object-like/test/test.array.js b/is-object-like/test/test.array.js new file mode 100644 index 00000000..7a1ff182 --- /dev/null +++ b/is-object-like/test/test.array.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isObjectLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isObjectLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function provides a method to test for an array of object-like elements', function test( t ) { + var arr; + var obj; + + obj = { + 'type': 'object' + }; + + arr = [ obj, [], {} ]; + t.equal( isObjectLike.isObjectLikeArray( arr ), true, 'returns true' ); + + arr = [ 2, {}, [] ]; + t.equal( isObjectLike.isObjectLikeArray( arr ), false, 'returns false' ); + + arr = []; + t.equal( isObjectLike.isObjectLikeArray( arr ), false, 'returns false when provided an empty array' ); + + t.end(); +}); diff --git a/is-object-like/test/test.js b/is-object-like/test/test.js new file mode 100644 index 00000000..2572b7f5 --- /dev/null +++ b/is-object-like/test/test.js @@ -0,0 +1,204 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var hasMap = require( '@stdlib/assert/has-map-support' ); +var hasSet = require( '@stdlib/assert/has-set-support' ); +var hasWeakMap = require( '@stdlib/assert/has-weakmap-support' ); +var hasWeakSet = require( '@stdlib/assert/has-weakset-support' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var Number = require( '@stdlib/number/ctor' ); +var isObjectLike = require( './../lib' ); + + +// VARIABLES // + +var opts; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isObjectLike, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a function to test if a value is an array of object-like elements', function test( t ) { + t.equal( typeof isObjectLike.isObjectLikeArray, 'function', 'has method' ); + t.end(); +}); + +tape( 'the function returns a boolean indicating if a value is object-like', function test( t ) { + var expected; + var values; + var bool; + var i; + + values = [ + 'a', + new String( 'a' ), // eslint-disable-line no-new-wrappers + 5, + new Number( 5 ), // eslint-disable-line no-new-wrappers + NaN, + true, + new Boolean( true ), // eslint-disable-line no-new-wrappers + false, + new Boolean( false ), // eslint-disable-line no-new-wrappers + void 0, + null, + [], + {}, + function noop() {}, + /./, + new Date(), + Math, + JSON, + new Error(), + new TypeError(), + new SyntaxError(), + new URIError(), + new EvalError(), + new ReferenceError(), + new RangeError(), + new Int8Array(), + new Uint8Array(), + new Uint8ClampedArray(), + new Int16Array(), + new Uint16Array(), + new Int32Array(), + new Uint32Array(), + new Float32Array(), + new Float64Array(), + new ArrayBuffer() + ]; + + expected = [ + false, // 'a' + true, // String + false, // 5 + true, // Number + false, // NaN + false, // true + true, // Boolean + false, // false + true, // Boolean + false, // void 0 + false, // null + true, // [] + true, // {} + false, // function + true, // RegExp + true, // Date + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true, + true + ]; + + for ( i = 0; i < values.length; i++ ) { + bool = isObjectLike( values[i] ); + t.equal( bool, expected[i], 'returns '+expected[i]+' when provided '+values[i] ); + } + t.end(); +}); + +opts = { + 'skip': !hasMap() +}; +tape( 'the function supports Map objects', opts, function test( t ) { + var bool = isObjectLike( new Map() ); + t.equal( bool, true, 'returns true' ); + t.end(); +}); + +opts = { + 'skip': !hasWeakMap() +}; +tape( 'the function supports WeakMap objects', opts, function test( t ) { + var bool = isObjectLike( new WeakMap() ); + t.equal( bool, true, 'returns true' ); + t.end(); +}); + +opts = { + 'skip': !hasSet() +}; +tape( 'the function supports Set objects', opts, function test( t ) { + var bool = isObjectLike( new Set() ); + t.equal( bool, true, 'returns true' ); + t.end(); +}); + +opts = { + 'skip': !hasWeakSet() +}; +tape( 'the function supports WeakSet objects', opts, function test( t ) { + var bool = isObjectLike( new WeakSet() ); + t.equal( bool, true, 'returns true' ); + t.end(); +}); + +opts = { + 'skip': !hasSymbols() +}; +tape( 'the function supports Symbol objects', opts, function test( t ) { + var bool = isObjectLike( Symbol( 'beep' ) ); + t.equal( bool, false, 'returns false' ); + t.end(); +}); + +tape( 'the function supports custom objects', function test( t ) { + var bool; + function Person() {} + bool = isObjectLike( new Person() ); + t.equal( bool, true, 'returns true' ); + t.end(); +}); + +// TODO: add generator function test diff --git a/is-object/README.md b/is-object/README.md new file mode 100644 index 00000000..55253cc7 --- /dev/null +++ b/is-object/README.md @@ -0,0 +1,100 @@ + + +# isObject + +> Test if a value is an object. + +
+ +## Usage + +```javascript +var isObject = require( '@stdlib/assert/is-object' ); +``` + +#### isObject( value ) + +Tests if a `value` is an `object`. + +```javascript +var bool = isObject( {} ); +// returns true + +bool = isObject( true ); +// returns false +``` + +
+ + + +
+ +## Notes + +- The function returns `false` if provided an `array` or `null`. + + ```javascript + var bool = isObject( [] ); + // returns false + + bool = isObject( null ); + // returns false + ``` + +
+ + + +
+ +## Examples + + + +```javascript +var isObject = require( '@stdlib/assert/is-object' ); + +var bool = isObject( {} ); +// returns true + +bool = isObject( new Date() ); +// returns true + +bool = isObject( /.*/ ); +// returns true + +bool = isObject( null ); +// returns false + +bool = isObject( [] ); +// returns false +``` + +
+ + + + + + diff --git a/is-object/benchmark/benchmark.js b/is-object/benchmark/benchmark.js new file mode 100644 index 00000000..b1a38914 --- /dev/null +++ b/is-object/benchmark/benchmark.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isObject = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + new Date() + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isObject( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-object/docs/repl.txt b/is-object/docs/repl.txt new file mode 100644 index 00000000..91df9b35 --- /dev/null +++ b/is-object/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is an object; e.g., `{}`. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an object. + + Examples + -------- + > var bool = {{alias}}( {} ) + true + > bool = {{alias}}( true ) + false + + See Also + -------- + diff --git a/is-object/docs/types/index.d.ts b/is-object/docs/types/index.d.ts new file mode 100644 index 00000000..fa0ee4a5 --- /dev/null +++ b/is-object/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an object; e.g., `{}`. +* +* @param value - value to test +* @returns boolean indicating whether value is an object +* +* @example +* var bool = isObject( {} ); +* // returns true +* +* @example +* var bool = isObject( null ); +* // returns false +*/ +declare function isObject( value: any ): boolean; + + +// EXPORTS // + +export = isObject; diff --git a/is-object/docs/types/test.ts b/is-object/docs/types/test.ts new file mode 100644 index 00000000..c8b104f5 --- /dev/null +++ b/is-object/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isObject = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isObject( {} ); // $ExpectType boolean + isObject( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isObject(); // $ExpectError + isObject( {}, 123 ); // $ExpectError +} diff --git a/is-object/examples/index.js b/is-object/examples/index.js new file mode 100644 index 00000000..ebb2b96a --- /dev/null +++ b/is-object/examples/index.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isObject = require( './../lib' ); + +console.log( isObject( {} ) ); +// => true + +console.log( isObject( new Date() ) ); +// => true + +console.log( isObject( /.*/ ) ); +// => true + +console.log( isObject( null ) ); +// => false + +console.log( isObject( [] ) ); +// => false diff --git a/is-object/lib/index.js b/is-object/lib/index.js new file mode 100644 index 00000000..94b0d36f --- /dev/null +++ b/is-object/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an object. +* +* @module @stdlib/assert/is-object +* +* @example +* var isObject = require( '@stdlib/assert/is-object' ); +* +* var bool = isObject( {} ); +* // returns true +* +* bool = isObject( true ); +* // returns false +*/ + +// MODULES // + +var isObject = require( './main.js' ); + + +// EXPORTS // + +module.exports = isObject; diff --git a/is-object/lib/main.js b/is-object/lib/main.js new file mode 100644 index 00000000..1454bd08 --- /dev/null +++ b/is-object/lib/main.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isArray = require( '@stdlib/assert/is-array' ); + + +// MAIN // + +/** +* Tests if a value is an object; e.g., `{}`. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an object +* +* @example +* var bool = isObject( {} ); +* // returns true +* +* @example +* var bool = isObject( null ); +* // returns false +*/ +function isObject( value ) { + return ( + typeof value === 'object' && + value !== null && + !isArray( value ) + ); +} + + +// EXPORTS // + +module.exports = isObject; diff --git a/is-object/package.json b/is-object/package.json new file mode 100644 index 00000000..ae0e8585 --- /dev/null +++ b/is-object/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/assert/is-object", + "version": "0.0.0", + "description": "Test if a value is an object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "object", + "obj", + "is", + "isobject", + "type", + "check", + "validate", + "test" + ] +} diff --git a/is-object/test/test.js b/is-object/test/test.js new file mode 100644 index 00000000..4539f566 --- /dev/null +++ b/is-object/test/test.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isObject = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isObject, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an object', function test( t ) { + t.equal( isObject( {} ), true, 'returns true' ); + t.equal( isObject( Object.create( null ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` when provided object instances', function test( t ) { + t.equal( isObject( new Date() ), true, 'returns true when provided a Date object' ); + t.equal( isObject( /.*/ ), true, 'returns true when provided a regular expression' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + undefined, + null, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isObject( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-odd/README.md b/is-odd/README.md new file mode 100644 index 00000000..c7c74788 --- /dev/null +++ b/is-odd/README.md @@ -0,0 +1,139 @@ + + +# isOdd + +> Test if a value is an odd number. + +
+ +## Usage + +```javascript +var isOdd = require( '@stdlib/assert/is-odd' ); +``` + +#### isOdd( value ) + +Tests if a `value` is an odd `number`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isOdd( 5.0 ); +// returns true + +bool = isOdd( new Number( 5.0 ) ); +// returns true + +bool = isOdd( 6.0 ); +// returns false + +bool = isOdd( -3.14 ); +// returns false + +bool = isOdd( null ); +// returns false + +bool = isOdd( NaN ); +// returns false +``` + +#### isOdd.isPrimitive( value ) + +Tests if a `value` is an odd primitive number. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isOdd.isPrimitive( -5.0 ); +// returns true + +bool = isOdd.isPrimitive( new Number( -5.0 ) ); +// returns false +``` + +#### isOdd.isObject( value ) + +Tests if a `value` is a `Number` object having an odd number value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isOdd.isObject( 5.0 ); +// returns false + +bool = isOdd.isObject( new Number( 5.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isOdd = require( '@stdlib/assert/is-odd' ); + +var bool = isOdd( 5.0 ); +// returns true + +bool = isOdd( new Number( 5 ) ); +// returns true + +bool = isOdd( 0.0 ); +// returns false + +bool = isOdd( 4.0 ); +// returns false + +bool = isOdd( NaN ); +// returns false + +bool = isOdd( '5' ); +// returns false + +bool = isOdd( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-odd/benchmark/benchmark.js b/is-odd/benchmark/benchmark.js new file mode 100644 index 00000000..835ea5a6 --- /dev/null +++ b/is-odd/benchmark/benchmark.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isOdd = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isOdd( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isOdd( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isOdd.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isOdd.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isOdd.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isOdd.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-odd/docs/repl.txt b/is-odd/docs/repl.txt new file mode 100644 index 00000000..c22dcd51 --- /dev/null +++ b/is-odd/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is an odd number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an odd number. + + Examples + -------- + > var bool = {{alias}}( 5.0 ) + true + > bool = {{alias}}( new Number( 5.0 ) ) + true + > bool = {{alias}}( 4.0 ) + false + > bool = {{alias}}( new Number( 4.0 ) ) + false + > bool = {{alias}}( -3.14 ) + false + > bool = {{alias}}( null ) + false + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive that is an odd number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive that is an odd + number. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( -5.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( -5.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object that has an odd number value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object that has an odd + number value. + + Examples + -------- + > var bool = {{alias}}.isObject( 5.0 ) + false + > bool = {{alias}}.isObject( new Number( 5.0 ) ) + true + + See Also + -------- + diff --git a/is-odd/docs/types/index.d.ts b/is-odd/docs/types/index.d.ts new file mode 100644 index 00000000..112fcaf5 --- /dev/null +++ b/is-odd/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isOdd` with methods for testing for primitives and objects, respectively. +*/ +interface IsOdd { + /** + * Tests if a value is an odd number. + * + * @param value - value to test + * @returns boolean indicating whether value is an odd number + * + * @example + * var bool = isOdd( 3.0 ); + * // returns true + * + * @example + * var bool = isOdd( new Number( 3.0 ) ); + * // returns true + * + * @example + * var bool = isOdd( 4.0 ); + * // returns false + * + * @example + * var bool = isOdd( new Number( 6.0 ) ); + * // returns false + * + * @example + * var bool = isOdd( -3.14 ); + * // returns false + * + * @example + * var bool = isOdd( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive that is an odd number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive that is an odd number + * + * @example + * var bool = isOdd.isPrimitive( -5.0 ); + * // returns true + * + * @example + * var bool = isOdd.isPrimitive( new Number( -5.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object that is an odd number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object that is an odd number + * + * @example + * var bool = isOdd.isObject( 5.0 ); + * // returns false + * + * @example + * var bool = isOdd.isObject( new Number( 5.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is an odd number. +* +* @param value - value to test +* @returns boolean indicating whether value is an odd number +* +* @example +* var bool = isOdd( 5.0 ); +* // returns true +* +* @example +* var bool = isOdd( new Number( 6.0 ) ); +* // returns true +* +* @example +* var bool = isOdd( 3.0 ); +* // returns false +* +* @example +* var bool = isOdd.isPrimitive( new Number( 5.0 ) ); +* // returns false +* +* @example +* var bool = isOdd.isObject( 5.0 ); +* // returns false +* +* @example +* var bool = isOdd( null ); +* // returns false +*/ +declare var isOdd: IsOdd; + + +// EXPORTS // + +export = isOdd; diff --git a/is-odd/docs/types/test.ts b/is-odd/docs/types/test.ts new file mode 100644 index 00000000..813a5665 --- /dev/null +++ b/is-odd/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isOdd = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isOdd( 3 ); // $ExpectType boolean + isOdd( 4 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isOdd(); // $ExpectError + isOdd( 3, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isOdd.isPrimitive( new Number( 3 ) ); // $ExpectType boolean + isOdd.isPrimitive( 3 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isOdd.isPrimitive(); // $ExpectError + isOdd.isPrimitive( 3, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isOdd.isObject( new Number( 3 ) ); // $ExpectType boolean + isOdd.isObject( 3 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isOdd.isObject(); // $ExpectError + isOdd.isObject( 3, 123 ); // $ExpectError +} diff --git a/is-odd/examples/index.js b/is-odd/examples/index.js new file mode 100644 index 00000000..5fd33409 --- /dev/null +++ b/is-odd/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isOdd = require( './../lib' ); + +console.log( isOdd( 5.0 ) ); +// => true + +console.log( isOdd( new Number( 5 ) ) ); +// => true + +console.log( isOdd( 0.0 ) ); +// => false + +console.log( isOdd( 4.0 ) ); +// => false + +console.log( isOdd( NaN ) ); +// => false + +console.log( isOdd( '5' ) ); +// => false + +console.log( isOdd( null ) ); +// => false diff --git a/is-odd/lib/index.js b/is-odd/lib/index.js new file mode 100644 index 00000000..eb21b207 --- /dev/null +++ b/is-odd/lib/index.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an odd number. +* +* @module @stdlib/assert/is-odd +* +* @example +* var isOdd = require( '@stdlib/assert/is-odd' ); +* +* var bool = isOdd( 5.0 ); +* // returns true +* +* bool = isOdd( new Number( 5.0 ) ); +* // returns true +* +* bool = isOdd( 4.0 ); +* // returns false +* +* bool = isOdd( new Number( 4.0 ) ); +* // returns false +* +* bool = isOdd( -3.14 ); +* // returns false +* +* bool = isOdd( null ); +* // returns false +* +* @example +* var isOdd = require( '@stdlib/assert/is-odd' ).isPrimitive; +* +* var bool = isOdd( -5.0 ); +* // returns true +* +* bool = isOdd( new Number( -5.0 ) ); +* // returns false +* +* @example +* var isOdd = require( '@stdlib/assert/is-odd' ).isObject; +* +* var bool = isOdd( 5.0 ); +* // returns false +* +* bool = isOdd( new Number( 5.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isOdd = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isOdd, 'isPrimitive', isPrimitive ); +setReadOnly( isOdd, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isOdd; diff --git a/is-odd/lib/main.js b/is-odd/lib/main.js new file mode 100644 index 00000000..88544a0d --- /dev/null +++ b/is-odd/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is an odd number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an odd number. +* +* @example +* var bool = isOdd( 5.0 ); +* // returns true +* +* @example +* var bool = isOdd( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isOdd( 4.0 ); +* // returns false +* +* @example +* var bool = isOdd( new Number( 4.0 ) ); +* // returns false +* +* @example +* var bool = isOdd( -3.14 ); +* // returns false +* +* @example +* var bool = isOdd( null ); +* // returns false +*/ +function isOdd( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isOdd; diff --git a/is-odd/lib/object.js b/is-odd/lib/object.js new file mode 100644 index 00000000..87da114d --- /dev/null +++ b/is-odd/lib/object.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isObject; +var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a number object that has an odd number value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object that has an odd number value +* +* @example +* var bool = isOdd( 5.0 ); +* // returns false +* +* @example +* var bool = isOdd( new Number( 5.0 ) ); +* // returns true +*/ +function isOdd( value ) { + if ( !isInteger( value ) ) { + return false; + } + // Check sign to prevent overflow... + if ( value > 0.0 ) { + return isEven( value-1.0 ); + } + return isEven( value+1.0 ); +} + + +// EXPORTS // + +module.exports = isOdd; diff --git a/is-odd/lib/primitive.js b/is-odd/lib/primitive.js new file mode 100644 index 00000000..99f4749b --- /dev/null +++ b/is-odd/lib/primitive.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a number primitive that is an odd number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive that is an odd number +* +* @example +* var bool = isOdd( -5.0 ); +* // returns true +* +* @example +* var bool = isOdd( new Number( -5.0 ) ); +* // returns false +*/ +function isOdd( value ) { + if ( !isInteger( value ) ) { + return false; + } + // Check sign to prevent overflow... + if ( value > 0.0 ) { + return isEven( value-1.0 ); + } + return isEven( value+1.0 ); +} + + +// EXPORTS // + +module.exports = isOdd; diff --git a/is-odd/package.json b/is-odd/package.json new file mode 100644 index 00000000..ca3c2664 --- /dev/null +++ b/is-odd/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-odd", + "version": "0.0.0", + "description": "Test if a value is an odd number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "odd", + "is", + "isodd", + "type", + "check", + "primitive", + "object", + "validate", + "valid", + "test" + ] +} diff --git a/is-odd/test/test.js b/is-odd/test/test.js new file mode 100644 index 00000000..5f4f39de --- /dev/null +++ b/is-odd/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isOdd = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isOdd, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive odd number', function test( t ) { + t.equal( typeof isOdd.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a value which is an odd number', function test( t ) { + t.equal( typeof isOdd.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-odd/test/test.main.js b/is-odd/test/test.main.js new file mode 100644 index 00000000..bd1fbb07 --- /dev/null +++ b/is-odd/test/test.main.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isOdd = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isOdd, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an odd number', function test( t ) { + t.equal( isOdd( 5.0 ), true, 'returns true' ); + t.equal( isOdd( new Number( 5 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided an odd number', function test( t ) { + var values; + var i; + + values = [ + '5', + 4.0, + 3.14, + null, + true, + void 0, + [], + {}, + function noop() {}, + Number.POSITIVE_INFINITY, + Number.NEGATIVE_INFINITY, + NaN + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isOdd( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-odd/test/test.object.js b/is-odd/test/test.object.js new file mode 100644 index 00000000..514ac266 --- /dev/null +++ b/is-odd/test/test.object.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isOdd = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isOdd, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an odd number object', function test( t ) { + t.equal( isOdd( new Number( 5 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.equal( isOdd( new Number( -3 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is odd', function test( t ) { + t.equal( isOdd( 5.0 ), false, 'returns false' ); + t.equal( isOdd( -1.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an odd number', function test( t ) { + var values; + var i; + + values = [ + '5', + 4, + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isOdd( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-odd/test/test.primitive.js b/is-odd/test/test.primitive.js new file mode 100644 index 00000000..87a708e6 --- /dev/null +++ b/is-odd/test/test.primitive.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isOdd = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isOdd, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive odd number', function test( t ) { + t.equal( isOdd( -5.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number is odd', function test( t ) { + t.equal( isOdd( new Number( 5.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided an odd number', function test( t ) { + var values; + var i; + + values = [ + '4', + 4.0, + null, + true, + void 0, + [], + {}, + function noop() {}, + Number.POSITIVE_INFINITY, + Number.NEGATIVE_INFINITY, + NaN + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isOdd( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-persymmetric-matrix/README.md b/is-persymmetric-matrix/README.md new file mode 100644 index 00000000..d0d55e73 --- /dev/null +++ b/is-persymmetric-matrix/README.md @@ -0,0 +1,83 @@ + + +# isPersymmetricMatrix + +> Test if a value is a [persymmetric matrix][persymmetric-matrix]. + +
+ +## Usage + +```javascript +var isPersymmetricMatrix = require( '@stdlib/assert/is-persymmetric-matrix' ); +``` + +#### isPersymmetricMatrix( value ) + +Tests if a value is a [persymmetric matrix][persymmetric-matrix]. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'generic', [ 1, 2, 3, 1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var bool = isPersymmetricMatrix( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isPersymmetricMatrix = require( '@stdlib/assert/is-persymmetric-matrix' ); + +var arr = ndarray( 'generic', [ 1, 2, 3, 1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var out = isPersymmetricMatrix( arr ); +// returns true + +out = isPersymmetricMatrix( [ 1, 2, 3, 4 ] ); +// returns false + +out = isPersymmetricMatrix( {} ); +// returns false + +out = isPersymmetricMatrix( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-persymmetric-matrix/benchmark/benchmark.js b/is-persymmetric-matrix/benchmark/benchmark.js new file mode 100644 index 00000000..ea0bcf9a --- /dev/null +++ b/is-persymmetric-matrix/benchmark/benchmark.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var isPersymmetricMatrix = require( './../lib' ); + + +// MAIN // + +bench( pkg + '::true', function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = [ 0, 0, 0, 0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + + values = [ + arr, + arr, + arr, + arr, + arr + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPersymmetricMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg + '::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPersymmetricMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-persymmetric-matrix/benchmark/benchmark.size.js b/is-persymmetric-matrix/benchmark/benchmark.size.js new file mode 100644 index 00000000..b6de07d1 --- /dev/null +++ b/is-persymmetric-matrix/benchmark/benchmark.size.js @@ -0,0 +1,113 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isPersymmetricMatrix = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} size - matrix size +* @param {PositiveInteger} N - matrix order +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, size, N ) { + var strides; + var buffer; + var shape; + var x; + + buffer = new Float64Array( size ); + shape = [ N, N ]; + strides = [ N, 1 ]; + x = ndarray( 'float64', buffer, shape, strides, 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var j; + var i; + + j = ((N-1)*N) + 1; // index of last checked element + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire matrix before finding a failing value. + buffer[ j ] = i + 3.14; + bool = fcn( x ); + if ( bool !== false ) { + b.fail( 'should return false' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var sz; + var N; + var f; + var i; + + min = 4; // minimum number of elements + + for ( i = 0; i <= 16; i += 2 ) { + sz = min * pow( 2, i ); + N = sqrt( sz ); + f = createBenchmark( isPersymmetricMatrix, sz, N ); + bench( pkg+':size='+sz+',dims='+N+'x'+N, f ); + } +} + +main(); diff --git a/is-persymmetric-matrix/docs/repl.txt b/is-persymmetric-matrix/docs/repl.txt new file mode 100644 index 00000000..6faf24cf --- /dev/null +++ b/is-persymmetric-matrix/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( value ) + Tests if a value is a square matrix which is symmetric about its + antidiagonal. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a persymmetric matrix. + + Examples + -------- + > var buf = [ 1, 2, 3, 1 ]; + > var sh = [ 2, 2 ]; + > var st = [ 2, 1 ]; + > var M = {{alias:@stdlib/ndarray/ctor}}( 'generic', buf, sh, st, 0, 'row-major' ); + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-persymmetric-matrix/docs/types/index.d.ts b/is-persymmetric-matrix/docs/types/index.d.ts new file mode 100644 index 00000000..c8cac518 --- /dev/null +++ b/is-persymmetric-matrix/docs/types/index.d.ts @@ -0,0 +1,47 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a persymmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} = M_{N-j-1,N-i-1}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param v - value to test +* @returns boolean indicating if a value is a persymmetric matrix +* +* @example +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'generic', [ 1, 2, 3, 1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isPersymmetricMatrix( arr ); +* // returns true +* +* bool = isPersymmetricMatrix( [] ); +* // returns false +*/ +declare function isPersymmetricMatrix( v: any ): boolean; + + +// EXPORTS // + +export = isPersymmetricMatrix; diff --git a/is-persymmetric-matrix/docs/types/test.ts b/is-persymmetric-matrix/docs/types/test.ts new file mode 100644 index 00000000..515bb7ff --- /dev/null +++ b/is-persymmetric-matrix/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* tslint:disable:no-invalid-this */ + +import isPersymmetricMatrix = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const matrix = { + 'data': [ 2, 1, 1, 2 ], + 'ndims': 2, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': function get( i: number, j: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + return this.data[ idx ]; + }, + 'set': function set( i: number, j: number, v: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + this.data[ idx ] = v; + return v; + } + }; + isPersymmetricMatrix( matrix ); // $ExpectType boolean + isPersymmetricMatrix( [] ); // $ExpectType boolean + isPersymmetricMatrix( false ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isPersymmetricMatrix(); // $ExpectError +} diff --git a/is-persymmetric-matrix/examples/index.js b/is-persymmetric-matrix/examples/index.js new file mode 100644 index 00000000..d0665cfa --- /dev/null +++ b/is-persymmetric-matrix/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isPersymmetricMatrix = require( './../lib' ); + +var arr = ndarray( 'generic', [ 1, 2, 3, 1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +console.log( isPersymmetricMatrix( arr ) ); +// => true + +console.log( isPersymmetricMatrix( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isPersymmetricMatrix( {} ) ); +// => false + +console.log( isPersymmetricMatrix( null ) ); +// => false diff --git a/is-persymmetric-matrix/lib/index.js b/is-persymmetric-matrix/lib/index.js new file mode 100644 index 00000000..28f2574f --- /dev/null +++ b/is-persymmetric-matrix/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a persymmetric matrix. +* +* @module @stdlib/assert/is-persymmetric-matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isPersymmetricMatrix = require( '@stdlib/assert/is-persymmetric-matrix' ); +* +* var arr = ndarray( 'generic', [ 1, 2, 3, 1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isPersymmetricMatrix( arr ); +* // returns true +* +* bool = isPersymmetricMatrix( [] ); +* // returns false +*/ + +// MODULES // + +var isPersymmetricMatrix = require( './main.js' ); + + +// EXPORTS // + +module.exports = isPersymmetricMatrix; diff --git a/is-persymmetric-matrix/lib/main.js b/is-persymmetric-matrix/lib/main.js new file mode 100644 index 00000000..b293a686 --- /dev/null +++ b/is-persymmetric-matrix/lib/main.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isSquareMatrix = require( '@stdlib/assert/is-square-matrix' ); + + +// MAIN // + +/** +* Tests if a value is a persymmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} = M_{N-j-1,N-i-1}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a persymmetric matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var arr = ndarray( 'generic', [ 1, 2, 3, 1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isPersymmetricMatrix( arr ); +* // returns true +* +* bool = isPersymmetricMatrix( [] ); +* // returns false +*/ +function isPersymmetricMatrix( v ) { + var M; + var n; + var i; + var j; + if ( !isSquareMatrix( v ) ) { + return false; + } + M = v.shape[ 0 ] - 1; + for ( i = 0; i < M; i++ ) { + n = M - i; + for ( j = 0; j < n; j++ ) { + if ( v.get( i, j ) !== v.get( M-j, n ) ) { + return false; + } + } + } + return true; +} + + +// EXPORTS // + +module.exports = isPersymmetricMatrix; diff --git a/is-persymmetric-matrix/package.json b/is-persymmetric-matrix/package.json new file mode 100644 index 00000000..56409923 --- /dev/null +++ b/is-persymmetric-matrix/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-persymmetric-matrix", + "version": "0.0.0", + "description": "Test if a value is a persymmetric matrix.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "matrix", + "ismatrix", + "2d", + "square", + "symmetric", + "persymmetric", + "bisymmetric", + "symm", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-persymmetric-matrix/test/test.js b/is-persymmetric-matrix/test/test.js new file mode 100644 index 00000000..6ee21bcd --- /dev/null +++ b/is-persymmetric-matrix/test/test.js @@ -0,0 +1,157 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable array-element-newline */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var isPersymmetricMatrix = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPersymmetricMatrix, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a persymmetric matrix', function test( t ) { + var buffer; + var arr; + + buffer = [ + 1, 2, 3, 4, + 5, 6, 7, 3, + 8, 9, 6, 2, + 10, 8, 5, 1 + ]; + arr = ndarray( 'generic', buffer, [ 4, 4 ], [ 4, 1 ], 0, 'row-major' ); + + t.equal( isPersymmetricMatrix( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray-like object which is persymmetric', function test( t ) { + var arr = { + 'data': [ 1, 2, 3, 1 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isPersymmetricMatrix( arr ), true, 'returns true' ); + t.end(); + + function get( i, j ) { + return arr.data[ (2*i) + j ]; + } +}); + +tape( 'the function returns `false` if not provided a square matrix', function test( t ) { + var arr = ndarray( 'generic', [ 0, 0, 0, 0, 0, 0 ], [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isPersymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a persymmetric matrix', function test( t ) { + var arr = ndarray( 'generic', [ 1, 2, 2, 3 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isPersymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray-like object not having equal dimensions', function test( t ) { + var arr = { + 'data': [ 0, 0, 0, 0, 0, 0 ], + 'shape': [ 3, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 6, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isPersymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray-like object which is persymmetric', function test( t ) { + var arr = { + 'data': [ 1, 2, 2, 3 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isPersymmetricMatrix( arr ), false, 'returns false' ); + t.end(); + + function get( i, j ) { + return arr.data[ (2*i) + j ]; + } +}); + +tape( 'the function returns `false` if not provided a 2-dimensional ndarray-like object', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 0, 0, 0 ], [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + values = [ + arr, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPersymmetricMatrix( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-plain-object-array/README.md b/is-plain-object-array/README.md new file mode 100644 index 00000000..0efd4bcf --- /dev/null +++ b/is-plain-object-array/README.md @@ -0,0 +1,101 @@ + + +# isPlainObjectArray + +> Test if a value is an array-like object containing only plain objects. + +
+ +## Usage + +```javascript +var isPlainObjectArray = require( '@stdlib/assert/is-plain-object-array' ); +``` + +#### isPlainObjectArray( value ) + +Tests if a `value` is an array-like object containing **only** plain `objects`. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPlainObjectArray( [ {}, { 'beep': 'boop' } ] ); +// returns true + +bool = isPlainObjectArray( [ {}, new Number(3.0) ] ); +// returns false + +bool = isPlainObjectArray( [ {}, '3.0' ] ); +// returns false + +bool = isPlainObjectArray( [] ); +// returns false + +bool = isPlainObjectArray( [ null, {} ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isPlainObjectArray = require( '@stdlib/assert/is-plain-object-array' ); + +var bool = isPlainObjectArray( [ { 'beep': 'boop' }, {}, {} ] ); +// returns true + +bool = isPlainObjectArray( [ new Date(), new Number( 3 ) ] ); +// returns false + +bool = isPlainObjectArray( [ {}, new String( 'abc' ), {} ] ); +// returns false + +bool = isPlainObjectArray( [ [], {} ] ); +// returns false + +bool = isPlainObjectArray( [ 'a', 'b' ] ); +// returns false + +bool = isPlainObjectArray( [] ); +// returns false +``` + +
+ + + + + + diff --git a/is-plain-object-array/benchmark/benchmark.js b/is-plain-object-array/benchmark/benchmark.js new file mode 100644 index 00000000..14bbb764 --- /dev/null +++ b/is-plain-object-array/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isPlainObjectArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( {} ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = isPlainObjectArray( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/is-plain-object-array/docs/repl.txt b/is-plain-object-array/docs/repl.txt new file mode 100644 index 00000000..8ceebbee --- /dev/null +++ b/is-plain-object-array/docs/repl.txt @@ -0,0 +1,27 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only plain objects. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + plain objects. + + Examples + -------- + > var bool = {{alias}}( [ {}, { 'beep': 'boop' } ] ) + true + > bool = {{alias}}( [ {}, new Number(3.0) ] ) + false + > bool = {{alias}}( [ {}, '3.0' ] ) + false + + See Also + -------- + diff --git a/is-plain-object-array/docs/types/index.d.ts b/is-plain-object-array/docs/types/index.d.ts new file mode 100644 index 00000000..09db9859 --- /dev/null +++ b/is-plain-object-array/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an array-like object containing only plain objects. +* +* @param value - value to test +* @returns boolean indicating whether an input value is an array-like object containing only plain objects +* +* @example +* var bool = isPlainObjectArray( [ {}, { 'beep': 'boop' } ] ); +* // returns true +* +* bool = isPlainObjectArray( [ {}, new Number(3.0) ] ); +* // returns false +* +* bool = isPlainObjectArray( [ {}, '3.0' ] ); +* // returns false +*/ +declare function isPlainObjectArray( value: any ): boolean; + + +// EXPORTS // + +export = isPlainObjectArray; diff --git a/is-plain-object-array/docs/types/test.ts b/is-plain-object-array/docs/types/test.ts new file mode 100644 index 00000000..1bc022c5 --- /dev/null +++ b/is-plain-object-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isPlainObjectArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isPlainObjectArray( {} ); // $ExpectType boolean + isPlainObjectArray( [ {}, { 'beep': 'boop' } ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isPlainObjectArray(); // $ExpectError + isPlainObjectArray( [ {}, { 'beep': 'boop' } ], 123 ); // $ExpectError +} diff --git a/is-plain-object-array/examples/index.js b/is-plain-object-array/examples/index.js new file mode 100644 index 00000000..fbf7d349 --- /dev/null +++ b/is-plain-object-array/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, object-curly-newline */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isPlainObjectArray = require( './../lib' ); + +var bool = isPlainObjectArray( [ { 'beep': 'boop' }, {}, {} ] ); +console.log( bool ); +// => true + +bool = isPlainObjectArray( [ new Date(), new Number( 3 ) ] ); +console.log( bool ); +// => false + +bool = isPlainObjectArray( [ {}, new String( 'abc' ), {} ] ); +console.log( bool ); +// => false + +bool = isPlainObjectArray( [ [], {} ] ); +console.log( bool ); +// => false + +bool = isPlainObjectArray( [ 'a', 'b' ] ); +console.log( bool ); +// => false + +bool = isPlainObjectArray( [] ); +console.log( bool ); +// => false diff --git a/is-plain-object-array/lib/index.js b/is-plain-object-array/lib/index.js new file mode 100644 index 00000000..613decad --- /dev/null +++ b/is-plain-object-array/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only plain objects. +* +* @module @stdlib/assert/is-plain-object-array +* +* @example +* var isPlainObjectArray = require( '@stdlib/assert/is-plain-object-array' ); +* +* var bool = isPlainObjectArray( [ {}, { 'beep': 'boop' } ] ); +* // returns true +* +* bool = isPlainObjectArray( [ {}, new Number(3.0) ] ); +* // returns false +* +* bool = isPlainObjectArray( [ {}, '3.0' ] ); +* // returns false +*/ + +// MODULES // + +var isPlainObjectArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isPlainObjectArray; diff --git a/is-plain-object-array/lib/main.js b/is-plain-object-array/lib/main.js new file mode 100644 index 00000000..f8d06293 --- /dev/null +++ b/is-plain-object-array/lib/main.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); + + +// MAIN // + +/** +* Tests if a value is an array-like object containing only plain objects. +* +* @name isPlainObjectArray +* @type {Function} +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether an input value is an array-like object containing only plain objects +* +* @example +* var bool = isPlainObjectArray( [ {}, { 'beep': 'boop' } ] ); +* // returns true +* +* bool = isPlainObjectArray( [ {}, new Number(3.0) ] ); +* // returns false +* +* bool = isPlainObjectArray( [ {}, '3.0' ] ); +* // returns false +*/ +var isPlainObjectArray = arrayfun( isPlainObject ); + + +// EXPORTS // + +module.exports = isPlainObjectArray; diff --git a/is-plain-object-array/package.json b/is-plain-object-array/package.json new file mode 100644 index 00000000..d95f7314 --- /dev/null +++ b/is-plain-object-array/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-plain-object-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only plain objects.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "array", + "is", + "isarray", + "object", + "obj", + "isobject", + "plain", + "isplainobject", + "type", + "check", + "validate", + "isvalid", + "test" + ] +} diff --git a/is-plain-object-array/test/test.js b/is-plain-object-array/test/test.js new file mode 100644 index 00000000..5fa15bd2 --- /dev/null +++ b/is-plain-object-array/test/test.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isPlainObjectArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isPlainObjectArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only plain objects', function test( t ) { + var arr; + var obj; + + obj = { + 'type': 'object' + }; + + arr = [ obj, {}, {} ]; + t.strictEqual( isPlainObjectArray( arr ), true, 'returns true' ); + + arr = [ obj, /.*/, {} ]; + t.strictEqual( isPlainObjectArray( arr ), false, 'returns false' ); + + arr = [ 5.0, {}, new Date() ]; + t.strictEqual( isPlainObjectArray( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': {}, + '1': {} + }; + t.strictEqual( isPlainObjectArray( arr ), true, 'returns true' ); + + arr = []; + t.strictEqual( isPlainObjectArray( arr ), false, 'returns false when provided an empty array' ); + + t.end(); +}); diff --git a/is-plain-object/README.md b/is-plain-object/README.md new file mode 100644 index 00000000..08eb22f4 --- /dev/null +++ b/is-plain-object/README.md @@ -0,0 +1,79 @@ + + +# isPlainObject + +> Test if a value is a plain object. + +
+ +## Usage + +```javascript +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +``` + +#### isPlainObject( value ) + +Tests if a `value` is a plain `object`. + +```javascript +var bool = isPlainObject( {} ); +// returns true + +bool = isPlainObject( null ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); + +var bool = isPlainObject( {} ); +// returns true + +bool = isPlainObject( Object.create( null ) ); +// returns true + +bool = isPlainObject( [] ); +// returns false + +bool = isPlainObject( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-plain-object/benchmark/benchmark.js b/is-plain-object/benchmark/benchmark.js new file mode 100644 index 00000000..a44718e8 --- /dev/null +++ b/is-plain-object/benchmark/benchmark.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isPlainObject = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPlainObject( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-plain-object/docs/repl.txt b/is-plain-object/docs/repl.txt new file mode 100644 index 00000000..a508f83c --- /dev/null +++ b/is-plain-object/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a plain object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a plain object. + + Examples + -------- + > var bool = {{alias}}( {} ) + true + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-plain-object/docs/types/index.d.ts b/is-plain-object/docs/types/index.d.ts new file mode 100644 index 00000000..09329037 --- /dev/null +++ b/is-plain-object/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a plain object. +* +* @param value - value to test +* @returns boolean indicating whether value is a plain object +* +* @example +* var bool = isPlainObject( {} ); +* // returns true +* +* @example +* var bool = isPlainObject( null ); +* // returns false +*/ +declare function isPlainObject( value: any ): boolean; + + +// EXPORTS // + +export = isPlainObject; diff --git a/is-plain-object/docs/types/test.ts b/is-plain-object/docs/types/test.ts new file mode 100644 index 00000000..41e0aeea --- /dev/null +++ b/is-plain-object/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isPlainObject = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isPlainObject( {} ); // $ExpectType boolean + isPlainObject( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isPlainObject(); // $ExpectError + isPlainObject( {}, 123 ); // $ExpectError +} diff --git a/is-plain-object/examples/index.js b/is-plain-object/examples/index.js new file mode 100644 index 00000000..c82e9452 --- /dev/null +++ b/is-plain-object/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isPlainObject = require( './../lib' ); + +console.log( isPlainObject( {} ) ); +// => true + +console.log( isPlainObject( Object.create( null ) ) ); +// => true + +console.log( isPlainObject( [] ) ); +// => false + +console.log( isPlainObject( null ) ); +// => false diff --git a/is-plain-object/lib/index.js b/is-plain-object/lib/index.js new file mode 100644 index 00000000..2bfbf22e --- /dev/null +++ b/is-plain-object/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a plain object. +* +* @module @stdlib/assert/is-plain-object +* +* @example +* var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +* +* var bool = isPlainObject( {} ); +* // returns true +* +* bool = isPlainObject( null ); +* // returns false +*/ + +// MODULES // + +var isPlainObject = require( './main.js' ); + + +// EXPORTS // + +module.exports = isPlainObject; diff --git a/is-plain-object/lib/main.js b/is-plain-object/lib/main.js new file mode 100644 index 00000000..2f4ec883 --- /dev/null +++ b/is-plain-object/lib/main.js @@ -0,0 +1,112 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isObject = require( '@stdlib/assert/is-object' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var getPrototypeOf = require( '@stdlib/utils/get-prototype-of' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var objectPrototype = Object.prototype; + + +// FUNCTIONS // + +/** +* Tests that an object only has own properties. +* +* @private +* @param {Object} obj - value to test +* @returns {boolean} boolean indicating if an object only has own properties +*/ +function ownProps( obj ) { + var key; + + // NOTE: possibility of perf boost if key enumeration order is known (see http://stackoverflow.com/questions/18531624/isplainobject-thing). + for ( key in obj ) { + if ( !hasOwnProp( obj, key ) ) { + return false; + } + } + return true; +} + + +// MAIN // + +/** +* Tests if a value is a plain object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a plain object +* +* @example +* var bool = isPlainObject( {} ); +* // returns true +* +* @example +* var bool = isPlainObject( null ); +* // returns false +*/ +function isPlainObject( value ) { + var proto; + + // Screen for obvious non-objects... + if ( !isObject( value ) ) { + return false; + } + // Objects with no prototype (e.g., `Object.create( null )`) are plain... + proto = getPrototypeOf( value ); + if ( !proto ) { + return true; + } + // Objects having a prototype are plain if and only if they are constructed with a global `Object` function and the prototype points to the prototype of a plain object... + return ( + // Cannot have own `constructor` property: + !hasOwnProp( value, 'constructor' ) && + + // Prototype `constructor` property must be a function (see also https://bugs.jquery.com/ticket/9897 and http://stackoverflow.com/questions/18531624/isplainobject-thing): + hasOwnProp( proto, 'constructor' ) && + isFunction( proto.constructor ) && + nativeClass( proto.constructor ) === '[object Function]' && + + // Test for object-specific method: + hasOwnProp( proto, 'isPrototypeOf' ) && + isFunction( proto.isPrototypeOf ) && + + ( + // Test if the prototype matches the global `Object` prototype (same realm): + proto === objectPrototype || + + // Test that all properties are own properties (cross-realm; *most* likely a plain object): + ownProps( value ) + ) + ); +} + + +// EXPORTS // + +module.exports = isPlainObject; diff --git a/is-plain-object/package.json b/is-plain-object/package.json new file mode 100644 index 00000000..7e9d26e6 --- /dev/null +++ b/is-plain-object/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-plain-object", + "version": "0.0.0", + "description": "Test if a value is a plain object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "object", + "obj", + "is", + "isobject", + "plain", + "type", + "check", + "validate", + "test" + ] +} diff --git a/is-plain-object/test/test.js b/is-plain-object/test/test.js new file mode 100644 index 00000000..efa23f56 --- /dev/null +++ b/is-plain-object/test/test.js @@ -0,0 +1,193 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isPlainObject = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPlainObject, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a plain object', function test( t ) { + t.equal( isPlainObject( {} ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an object with no prototype', function test( t ) { + var bool; + + bool = isPlainObject( Object.create( null ) ); + t.equal( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an object', function test( t ) { + var values; + var i; + values = [ + '5', + 5, + true, + void 0, + null, + NaN, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPlainObject( values[i] ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided a value having an own `constructor` property', function test( t ) { + var obj = { + 'constructor': Object + }; + + t.equal( isPlainObject( obj ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a value whose prototype lacks a `isPrototypeOf` property', function test( t ) { + function Foo() { + return this; + } + Foo.prototype = Object.create( Object.prototype ); + Foo.prototype.constructor = Foo; + delete Foo.prototype.isPrototypeOf; + + t.equal( isPlainObject( new Foo() ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a value whose prototype has a `isPrototypeOf` property which is not a function', function test( t ) { + function Foo() { + return this; + } + Foo.prototype = Object.create( Object.prototype ); + Foo.prototype.constructor = Foo; + Foo.prototype.isPrototypeOf = 'beep'; + + t.equal( isPlainObject( new Foo() ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a value whose prototype lacks a `constructor` property', function test( t ) { + function Foo() { + return this; + } + Foo.prototype = {}; + + t.equal( isPlainObject( new Foo() ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a value whose prototype has a constructor property which is not a function', function test( t ) { + function Foo() { + return this; + } + Foo.prototype = Object.create( {} ); + Foo.prototype.constructor = 'beep'; + + t.equal( isPlainObject( new Foo() ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a value whose prototype has a constructor property whose native class does not match a function\'s native class', function test( t ) { + var isPlainObject; + + function Foo() { + return this; + } + Foo.prototype = Object.create( {} ); + Foo.prototype.constructor = Foo; + + isPlainObject = proxyquire( './../lib/main.js', { + '@stdlib/utils/native-class': nativeClass + }); + t.equal( isPlainObject( new Foo() ), false, 'returns false' ); + t.end(); + + function nativeClass() { + return '[object String]'; + } +}); + +tape( 'the function returns `true` if provided a value whose prototype equals the global `Object` prototype (same realm)', function test( t ) { + function Foo() { + return this; + } + Foo.prototype = Object.prototype; + Foo.prototype.constructor = Foo; + + t.equal( isPlainObject( new Foo() ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a value whose prototype does not equal the global `Object` prototype and has non-own properties (cross-realm)', function test( t ) { + function Foo() { + this.foo = 'bar'; + this.a = 'b'; + this.c = 'd'; + return this; + } + Foo.prototype = Object.create( Function.prototype ); + Foo.prototype.constructor = Foo; + Foo.prototype.isPrototypeOf = function beep() {}; + Foo.prototype.boop = 'boop'; + + t.equal( isPlainObject( new Foo() ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a value whose prototype does not equal the global `Object` prototype and has only own properties (cross-realm)', function test( t ) { + function Foo() { + this.foo = 'bar'; + this.a = 'b'; + this.c = 'd'; + return this; + } + Foo.prototype = Object.create( Object.prototype ); + + defineProperty( Foo.prototype, 'constructor', { + 'configurable': true, + 'enumerable': false, + 'value': Foo + }); + defineProperty( Foo.prototype, 'isPrototypeOf', { + 'configurable': true, + 'enumerable': false, + 'value': function foo() {} + }); + + t.equal( isPlainObject( new Foo() ), true, 'returns true' ); + t.end(); +}); diff --git a/is-positive-integer-array/README.md b/is-positive-integer-array/README.md new file mode 100644 index 00000000..d4bd36b4 --- /dev/null +++ b/is-positive-integer-array/README.md @@ -0,0 +1,124 @@ + + +# isPositiveIntegerArray + +> Test if a value is an array-like object containing only positive integers. + +
+ +## Usage + +```javascript +var isPositiveIntegerArray = require( '@stdlib/assert/is-positive-integer-array' ); +``` + +#### isPositiveIntegerArray( value ) + +Tests if a `value` is an array-like object containing **only** positive `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveIntegerArray( [ 3.0, new Number(3.0) ] ); +// returns true + +bool = isPositiveIntegerArray( [ 3.0, '3.0' ] ); +// returns false +``` + +#### isPositiveIntegerArray.primitives( value ) + +Tests if a `value` is an array-like object containing **only** positive primitive `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveIntegerArray.primitives( [ 1.0, 2.0, 10.0 ] ); +// returns true + +bool = isPositiveIntegerArray.primitives( [ 3.0, new Number(1.0) ] ); +// returns false +``` + +#### isPositiveIntegerArray.objects( value ) + +Tests if a `value` is an array-like object containing **only** positive object `integer` values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveIntegerArray.objects( [ new Number(3.0), new Number(1.0) ] ); +// returns true + +bool = isPositiveIntegerArray.objects( [ 1.0, 2.0, 10.0 ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isPositiveIntegerArray = require( '@stdlib/assert/is-positive-integer-array' ); + +var bool = isPositiveIntegerArray( [ 5, 2, 3 ] ); +// returns true + +bool = isPositiveIntegerArray( [ 1, new Number( 6 ), 3 ] ); +// returns true + +bool = isPositiveIntegerArray( [ 0, 1, 2, 3, 4 ] ); +// returns false + +bool = isPositiveIntegerArray( [ 1, 'abc', 3 ] ); +// returns false + +bool = isPositiveIntegerArray( [ 2.3, 1, 3 ] ); +// returns false + +bool = isPositiveIntegerArray( [] ); +// returns false +``` + +
+ + + + + + diff --git a/is-positive-integer-array/benchmark/benchmark.js b/is-positive-integer-array/benchmark/benchmark.js new file mode 100644 index 00000000..2f0e5ad9 --- /dev/null +++ b/is-positive-integer-array/benchmark/benchmark.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var isPositiveIntegerArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( 1.0 ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( 1.0 ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isPositiveIntegerArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isPositiveIntegerArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isPositiveIntegerArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-positive-integer-array/docs/repl.txt b/is-positive-integer-array/docs/repl.txt new file mode 100644 index 00000000..6e93f19c --- /dev/null +++ b/is-positive-integer-array/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only positive integers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + positive integers. + + Examples + -------- + > var bool = {{alias}}( [ 3.0, new Number(3.0) ] ) + true + > bool = {{alias}}( [ 3.0, '3.0' ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only positive primitive + integer values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + positive primitive integer values. + + Examples + -------- + > var bool = {{alias}}.primitives( [ 1.0, 10.0 ] ) + true + > bool = {{alias}}.primitives( [ 1.0, 0.0, 10.0 ] ) + false + > bool = {{alias}}.primitives( [ 3.0, new Number(1.0) ] ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only number objects + having positive integer values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + number objects having positive integer values. + + Examples + -------- + > var bool = {{alias}}.objects( [ new Number(1.0), new Number(10.0) ] ) + true + > bool = {{alias}}.objects( [ 1.0, 2.0, 10.0 ] ) + false + > bool = {{alias}}.objects( [ 3.0, new Number(1.0) ] ) + false + + See Also + -------- + diff --git a/is-positive-integer-array/docs/types/index.d.ts b/is-positive-integer-array/docs/types/index.d.ts new file mode 100644 index 00000000..5dcd9183 --- /dev/null +++ b/is-positive-integer-array/docs/types/index.d.ts @@ -0,0 +1,101 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isPositiveIntegerArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsPositiveIntegerArray { + /** + * Tests if a value is an array-like object containing only positive integers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only positive integers + * + * @example + * var bool = isPositiveIntegerArray( [ 3.0, new Number(3.0) ] ); + * // returns true + * + * @example + * var bool = isPositiveIntegerArray( [ 3.0, '3.0' ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only positive primitive integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only positive primitive integer values + * + * @example + * var bool = isPositiveIntegerArray.primitives( [ 1.0, 2.0, 10.0 ] ); + * // returns true + * + * @example + * var bool = isPositiveIntegerArray.primitives( [ 3.0, new Number(1.0) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects having positive integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number objects having positive integer values + * + * @example + * var bool = isPositiveIntegerArray.objects( [ new Number(3.0), new Number(1.0) ] ); + * // returns true + * + * @example + * var bool = isPositiveIntegerArray.objects( [ 1.0, 2.0, 10.0 ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object containing only positive integers. +* +* @param value - value to test +* @returns boolean indicating whether value is an array-like object containing only positive integers +* +* @example +* var bool = isPositiveIntegerArray( [ 3.0, new Number(3.0) ] ); +* // returns true +* +* @example +* var bool = isPositiveIntegerArray( [ 3.0, '3.0' ] ); +* // returns false +* +* @example +* var bool = isPositiveIntegerArray.primitives( [ 1.0, 2.0, 10.0 ] ); +* // returns true +* +* @example +* var bool = isPositiveIntegerArray.objects( [ new Number(3.0), new Number(1.0) ] ); +* // returns true +*/ +declare var isPositiveIntegerArray: IsPositiveIntegerArray; + + +// EXPORTS // + +export = isPositiveIntegerArray; diff --git a/is-positive-integer-array/docs/types/test.ts b/is-positive-integer-array/docs/types/test.ts new file mode 100644 index 00000000..6f860e1a --- /dev/null +++ b/is-positive-integer-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isPositiveIntegerArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isPositiveIntegerArray( [ 3 ] ); // $ExpectType boolean + isPositiveIntegerArray( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isPositiveIntegerArray(); // $ExpectError + isPositiveIntegerArray( [ 2 ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isPositiveIntegerArray.primitives( [ new Number( 2 ) ] ); // $ExpectType boolean + isPositiveIntegerArray.primitives( [ 2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isPositiveIntegerArray.primitives(); // $ExpectError + isPositiveIntegerArray.primitives( [ 2 ], 123 ); // $ExpectError +} + + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isPositiveIntegerArray.objects( [ new Number( 2 ) ] ); // $ExpectType boolean + isPositiveIntegerArray.objects( [ 2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isPositiveIntegerArray.objects(); // $ExpectError + isPositiveIntegerArray.objects( [ 2 ], 123 ); // $ExpectError +} diff --git a/is-positive-integer-array/examples/index.js b/is-positive-integer-array/examples/index.js new file mode 100644 index 00000000..d3dc9291 --- /dev/null +++ b/is-positive-integer-array/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isPositiveIntegerArray = require( './../lib' ); + +var bool = isPositiveIntegerArray( [ 5, 2, 3 ] ); +console.log( bool ); +// => true + +bool = isPositiveIntegerArray( [ 1, new Number( 6 ), 3 ] ); +console.log( bool ); +// => true + +bool = isPositiveIntegerArray( [ 0, 1, 2, 3, 4 ] ); +console.log( bool ); +// => false + +bool = isPositiveIntegerArray( [ 1, 'abc', 3 ] ); +console.log( bool ); +// => false + +bool = isPositiveIntegerArray( [ 2.3, 1, 3 ] ); +console.log( bool ); +// => false + +bool = isPositiveIntegerArray( [] ); +console.log( bool ); +// => false diff --git a/is-positive-integer-array/lib/index.js b/is-positive-integer-array/lib/index.js new file mode 100644 index 00000000..862de4f8 --- /dev/null +++ b/is-positive-integer-array/lib/index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only positive integers. +* +* @module @stdlib/assert/is-positive-integer-array +* +* @example +* var isPositiveIntegerArray = require( '@stdlib/assert/is-positive-integer-array' ); +* +* var bool = isPositiveIntegerArray( [ 3.0, new Number(3.0) ] ); +* // returns true +* +* bool = isPositiveIntegerArray( [ 3.0, '3.0' ] ); +* // returns false +* +* @example +* var isPositiveIntegerArray = require( '@stdlib/assert/is-positive-integer-array' ).primitives; +* +* var bool = isPositiveIntegerArray( [ 1.0, 2.0, 10.0 ] ); +* // returns true +* +* bool = isPositiveIntegerArray( [ 3.0, new Number(1.0) ] ); +* // returns false +* +* @example +* var isPositiveIntegerArray = require( '@stdlib/assert/is-positive-integer-array' ).objects; +* +* var bool = isPositiveIntegerArray( [ new Number(3.0), new Number(1.0) ] ); +* // returns true +* +* bool = isPositiveIntegerArray( [ 1.0, 2.0, 10.0 ] ); +* // returns false +*/ + +// MODULES // + +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); + + +// MAIN // + +var isPositiveIntegerArray = arrayfun( isPositiveInteger ); +setReadOnly( isPositiveIntegerArray, 'primitives', arrayfun( isPositiveInteger.isPrimitive ) ); +setReadOnly( isPositiveIntegerArray, 'objects', arrayfun( isPositiveInteger.isObject ) ); + + +// EXPORTS // + +module.exports = isPositiveIntegerArray; diff --git a/is-positive-integer-array/package.json b/is-positive-integer-array/package.json new file mode 100644 index 00000000..32584f7c --- /dev/null +++ b/is-positive-integer-array/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-positive-integer-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only positive integers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "number", + "numeric", + "positive", + "isnumber", + "isnumeric", + "integer", + "int", + "isinteger", + "type", + "check" + ] +} diff --git a/is-positive-integer-array/test/test.js b/is-positive-integer-array/test/test.js new file mode 100644 index 00000000..16bfddc4 --- /dev/null +++ b/is-positive-integer-array/test/test.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Number = require( '@stdlib/number/ctor' ); +var isPositiveIntegerArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPositiveIntegerArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for a generic number array having only positive integer values', function test( t ) { + var arr; + + arr = [ 5.0, new Number( 5 ), 1.0 ]; + t.equal( isPositiveIntegerArray( arr ), true, 'returns true' ); + + arr = [ 5.0, '3', null ]; + t.equal( isPositiveIntegerArray( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': 5, + '1': 1 + }; + t.equal( isPositiveIntegerArray( arr ), true, 'returns true' ); + + arr = new Int32Array( [ 5, 2, 0 ] ); + t.equal( isPositiveIntegerArray( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only positive integer primitives', function test( t ) { + var arr; + + arr = [ 5.0, 1.0 ]; + t.equal( isPositiveIntegerArray.primitives( arr ), true, 'returns true' ); + + arr = [ new Number( 5 ), 1.0, 1.0 ]; + t.equal( isPositiveIntegerArray.primitives( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': 5, + '1': 1 + }; + t.equal( isPositiveIntegerArray.primitives( arr ), true, 'returns true' ); + + arr = new Int16Array( [ 5, 2, 1 ] ); + t.equal( isPositiveIntegerArray.primitives( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only number objects with positive integer values', function test( t ) { + var arr; + + arr = [ new Number( 5 ), new Number( 2 ) ]; + t.equal( isPositiveIntegerArray.objects( arr ), true, 'returns true' ); + + arr = [ 5.0, 1.0, 1.0 ]; + t.equal( isPositiveIntegerArray.objects( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': new Number( 5 ), + '1': new Number( 1 ) + }; + t.equal( isPositiveIntegerArray.objects( arr ), true, 'returns true' ); + + arr = new Uint32Array( [ 5, 1 ] ); + t.equal( isPositiveIntegerArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-positive-integer/README.md b/is-positive-integer/README.md new file mode 100644 index 00000000..fd592614 --- /dev/null +++ b/is-positive-integer/README.md @@ -0,0 +1,139 @@ + + +# isPositiveInteger + +> Test if a value is a number having a positive integer value. + +
+ +## Usage + +```javascript +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ); +``` + +#### isPositiveInteger( value ) + +Tests if a `value` is a `number` having a positive `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveInteger( 5.0 ); +// returns true + +bool = isPositiveInteger( new Number( 5.0 ) ); +// returns true + +bool = isPositiveInteger( -5.0 ); +// returns false + +bool = isPositiveInteger( 0.0 ); +// returns false + +bool = isPositiveInteger( 3.14 ); +// returns false + +bool = isPositiveInteger( null ); +// returns false +``` + +#### isPositiveInteger.isPrimitive( value ) + +Tests if a `value` is a primitive `number` having a positive `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveInteger.isPrimitive( 3.0 ); +// returns true + +bool = isPositiveInteger.isPrimitive( new Number( 3.0 ) ); +// returns false +``` + +#### isPositiveInteger.isObject( value ) + +Tests if a `value` is a `Number` object having a positive `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveInteger.isObject( 3.0 ); +// returns false + +bool = isPositiveInteger.isObject( new Number( 3.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ); + +var bool = isPositiveInteger( 5.0 ); +// returns true + +bool = isPositiveInteger( new Number( 5.0 ) ); +// returns true + +bool = isPositiveInteger( 0.0 ); +// returns false + +bool = isPositiveInteger( 3.14 ); +// returns false + +bool = isPositiveInteger( -5.0 ); +// returns false + +bool = isPositiveInteger( '5' ); +// returns false + +bool = isPositiveInteger( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-positive-integer/benchmark/benchmark.js b/is-positive-integer/benchmark/benchmark.js new file mode 100644 index 00000000..6dfeba8d --- /dev/null +++ b/is-positive-integer/benchmark/benchmark.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isPositiveInteger = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveInteger( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveInteger( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveInteger.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveInteger.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveInteger.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveInteger.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-positive-integer/docs/repl.txt b/is-positive-integer/docs/repl.txt new file mode 100644 index 00000000..0d8761cb --- /dev/null +++ b/is-positive-integer/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is a positive integer. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a positive integer. + + Examples + -------- + > var bool = {{alias}}( 5.0 ) + true + > bool = {{alias}}( new Number( 5.0 ) ) + true + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( -5.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive having a positive integer value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive having a + positive integer value. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 3.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( 3.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a positive integer value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a positive + integer value. + + Examples + -------- + > var bool = {{alias}}.isObject( 3.0 ) + false + > bool = {{alias}}.isObject( new Number( 3.0 ) ) + true + + + See Also + -------- + diff --git a/is-positive-integer/docs/types/index.d.ts b/is-positive-integer/docs/types/index.d.ts new file mode 100644 index 00000000..9d9e747d --- /dev/null +++ b/is-positive-integer/docs/types/index.d.ts @@ -0,0 +1,133 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isPositiveInteger` with methods for testing for primitives and objects, respectively. +*/ +interface IsPositiveInteger { + /** + * Tests if a value is a positive integer. + * + * @param value - value to test + * @returns boolean indicating whether value is a positive integer + * + * @example + * var bool = isPositiveInteger( 5.0 ); + * // returns true + * + * @example + * var bool = isPositiveInteger( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = isPositiveInteger( 0.0 ); + * // returns false + * + * @example + * var bool = isPositiveInteger( -5.0 ); + * // returns false + * + * @example + * var bool = isPositiveInteger( 3.14 ); + * // returns false + * + * @example + * var bool = isPositiveInteger( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a positive integer value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a positive integer value + * + * @example + * var bool = isPositiveInteger.isPrimitive( 3.0 ); + * // returns true + * + * @example + * var bool = isPositiveInteger.isPrimitive( new Number( 3.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a positive integer value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a positive integer value + * + * @example + * var bool = isPositiveInteger.isObject( 3.0 ); + * // returns false + * + * @example + * var bool = isPositiveInteger.isObject( new Number( 3.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a positive integer. +* +* @param value - value to test +* @returns boolean indicating whether value is a positive integer +* +* @example +* var bool = isPositiveInteger( 5.0 ); +* // returns true +* +* @example +* var bool = isPositiveInteger( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isPositiveInteger( 0.0 ); +* // returns false +* +* @example +* var bool = isPositiveInteger( -5.0 ); +* // returns false +* +* @example +* var bool = isPositiveInteger( 3.14 ); +* // returns false +* +* @example +* var bool = isPositiveInteger( null ); +* // returns false +* +* @example +* var bool = isPositiveInteger.isPrimitive( 3.0 ); +* // returns true +* +* @example +* var bool = isPositiveInteger.isObject( 3.0 ); +* // returns false +*/ +declare var isPositiveInteger: IsPositiveInteger; + + +// EXPORTS // + +export = isPositiveInteger; diff --git a/is-positive-integer/docs/types/test.ts b/is-positive-integer/docs/types/test.ts new file mode 100644 index 00000000..d1517501 --- /dev/null +++ b/is-positive-integer/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isPositiveInteger = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isPositiveInteger( 3 ); // $ExpectType boolean + isPositiveInteger( -2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isPositiveInteger(); // $ExpectError + isPositiveInteger( 2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isPositiveInteger.isPrimitive( new Number( 2 ) ); // $ExpectType boolean + isPositiveInteger.isPrimitive( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isPositiveInteger.isPrimitive(); // $ExpectError + isPositiveInteger.isPrimitive( 2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isPositiveInteger.isObject( new Number( 2 ) ); // $ExpectType boolean + isPositiveInteger.isObject( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isPositiveInteger.isObject(); // $ExpectError + isPositiveInteger.isObject( 2, 123 ); // $ExpectError +} diff --git a/is-positive-integer/examples/index.js b/is-positive-integer/examples/index.js new file mode 100644 index 00000000..9726789e --- /dev/null +++ b/is-positive-integer/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isPositiveInteger = require( './../lib' ); + +console.log( isPositiveInteger( 5.0 ) ); +// => true + +console.log( isPositiveInteger( new Number( 5.0 ) ) ); +// => true + +console.log( isPositiveInteger( 0.0 ) ); +// => false + +console.log( isPositiveInteger( 3.14 ) ); +// => false + +console.log( isPositiveInteger( -5.0 ) ); +// => false + +console.log( isPositiveInteger( '5' ) ); +// => false + +console.log( isPositiveInteger( null ) ); +// => false diff --git a/is-positive-integer/lib/index.js b/is-positive-integer/lib/index.js new file mode 100644 index 00000000..68e9cb24 --- /dev/null +++ b/is-positive-integer/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a positive integer. +* +* @module @stdlib/assert/is-positive-integer +* +* @example +* var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ); +* +* var bool = isPositiveInteger( 5.0 ); +* // returns true +* +* bool = isPositiveInteger( new Number( 5.0 ) ); +* // returns true +* +* bool = isPositiveInteger( -5.0 ); +* // returns false +* +* bool = isPositiveInteger( 3.14 ); +* // returns false +* +* bool = isPositiveInteger( null ); +* // returns false +* +* @example +* var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +* +* var bool = isPositiveInteger( 3.0 ); +* // returns true +* +* bool = isPositiveInteger( new Number( 3.0 ) ); +* // returns false +* +* @example +* var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isObject; +* +* var bool = isPositiveInteger( 3.0 ); +* // returns false +* +* bool = isPositiveInteger( new Number( 3.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isPositiveInteger = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isPositiveInteger, 'isPrimitive', isPrimitive ); +setReadOnly( isPositiveInteger, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isPositiveInteger; diff --git a/is-positive-integer/lib/main.js b/is-positive-integer/lib/main.js new file mode 100644 index 00000000..16fab9f1 --- /dev/null +++ b/is-positive-integer/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a positive integer. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a positive integer +* +* @example +* var bool = isPositiveInteger( 5.0 ); +* // returns true +* +* @example +* var bool = isPositiveInteger( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isPositiveInteger( 0.0 ); +* // returns false +* +* @example +* var bool = isPositiveInteger( -5.0 ); +* // returns false +* +* @example +* var bool = isPositiveInteger( 3.14 ); +* // returns false +* +* @example +* var bool = isPositiveInteger( null ); +* // returns false +*/ +function isPositiveInteger( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isPositiveInteger; diff --git a/is-positive-integer/lib/object.js b/is-positive-integer/lib/object.js new file mode 100644 index 00000000..57062c8f --- /dev/null +++ b/is-positive-integer/lib/object.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isObject; + + +// MAIN // + +/** +* Tests if a value is a number object having a positive integer value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a positive integer value +* +* @example +* var bool = isPositiveInteger( 3.0 ); +* // returns false +* +* @example +* var bool = isPositiveInteger( new Number( 3.0 ) ); +* // returns true +*/ +function isPositiveInteger( value ) { + return ( + isInteger( value ) && + value.valueOf() > 0.0 + ); +} + + +// EXPORTS // + +module.exports = isPositiveInteger; diff --git a/is-positive-integer/lib/primitive.js b/is-positive-integer/lib/primitive.js new file mode 100644 index 00000000..028833c7 --- /dev/null +++ b/is-positive-integer/lib/primitive.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a number primitive having a positive integer value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a positive integer value +* +* @example +* var bool = isPositiveInteger( 3.0 ); +* // returns true +* +* @example +* var bool = isPositiveInteger( new Number( 3.0 ) ); +* // returns false +*/ +function isPositiveInteger( value ) { + return ( + isInteger( value ) && + value > 0.0 + ); +} + + +// EXPORTS // + +module.exports = isPositiveInteger; diff --git a/is-positive-integer/package.json b/is-positive-integer/package.json new file mode 100644 index 00000000..ecc25ffe --- /dev/null +++ b/is-positive-integer/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-positive-integer", + "version": "0.0.0", + "description": "Test if a value is a number having a positive integer value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "integer", + "int", + "positive", + "is", + "isinteger", + "type", + "check", + "primitive", + "object" + ] +} diff --git a/is-positive-integer/test/test.js b/is-positive-integer/test/test.js new file mode 100644 index 00000000..3e615c1d --- /dev/null +++ b/is-positive-integer/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isPositiveInteger = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPositiveInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number having a positive integer value', function test( t ) { + t.equal( typeof isPositiveInteger.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a positive integer value', function test( t ) { + t.equal( typeof isPositiveInteger.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-positive-integer/test/test.main.js b/is-positive-integer/test/test.main.js new file mode 100644 index 00000000..d2e855ae --- /dev/null +++ b/is-positive-integer/test/test.main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isPositiveInteger = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPositiveInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number having a positive integer value', function test( t ) { + t.equal( isPositiveInteger( 5.0 ), true, 'returns true' ); + t.equal( isPositiveInteger( new Number( 5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number having a positive integer value', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + -1.0, + 0.0, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPositiveInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-positive-integer/test/test.object.js b/is-positive-integer/test/test.object.js new file mode 100644 index 00000000..d16ebe63 --- /dev/null +++ b/is-positive-integer/test/test.object.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isPositiveInteger = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPositiveInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a positive integer value', function test( t ) { + t.equal( isPositiveInteger( new Number( 5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is a positive integer value', function test( t ) { + t.equal( isPositiveInteger( 3.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPositiveInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-positive-integer/test/test.primitive.js b/is-positive-integer/test/test.primitive.js new file mode 100644 index 00000000..cc7bd856 --- /dev/null +++ b/is-positive-integer/test/test.primitive.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isPositiveInteger = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPositiveInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number having a positive integer value', function test( t ) { + t.equal( isPositiveInteger( 3.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has a positive integer value', function test( t ) { + t.equal( isPositiveInteger( new Number( 5.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPositiveInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-positive-number-array/README.md b/is-positive-number-array/README.md new file mode 100644 index 00000000..a3601e89 --- /dev/null +++ b/is-positive-number-array/README.md @@ -0,0 +1,124 @@ + + +# isPositiveNumberArray + +> Test if a value is an array-like object containing only positive numbers. + +
+ +## Usage + +```javascript +var isPositiveNumberArray = require( '@stdlib/assert/is-positive-number-array' ); +``` + +#### isPositiveNumberArray( value ) + +Tests if a `value` is an array-like object containing **only** positive numbers. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveNumberArray( [ 3.0, new Number(3.0) ] ); +// returns true + +bool = isPositiveNumberArray( [ 3.0, '3.0' ] ); +// returns false +``` + +#### isPositiveNumberArray.primitives( value ) + +Tests if a `value` is an array-like object `array` containing **only** primitive positive numbers. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveNumberArray.primitives( [ 1.0, 5.0, 10.0 ] ); +// returns true + +bool = isPositiveNumberArray.primitives( [ 3.0, new Number(1.0) ] ); +// returns false +``` + +#### isPositiveNumberArray.objects( value ) + +Tests if a `value` is an array-like object `array` containing **only** object positive numbers. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveNumberArray.objects( [ new Number(1.0), new Number(1.0) ] ); +// returns true + +bool = isPositiveNumberArray.objects( [ 1.0, 5.0, 10.0 ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isPositiveNumberArray = require( '@stdlib/assert/is-positive-number-array' ); + +var bool = isPositiveNumberArray( [ 5.0, 0.2, 3.9 ] ); +// returns true + +bool = isPositiveNumberArray( [ 1, 2, 3 ] ); +// returns true + +bool = isPositiveNumberArray( [ 1, new Number( 6 ), 3 ] ); +// returns true + +bool = isPositiveNumberArray( [ 0, 2, 4 ] ); +// returns false + +bool = isPositiveNumberArray( [ 1, 'abc', 3 ] ); +// returns false + +bool = isPositiveNumberArray( 78.0 ); +// returns false +``` + +
+ + + + + + diff --git a/is-positive-number-array/benchmark/benchmark.js b/is-positive-number-array/benchmark/benchmark.js new file mode 100644 index 00000000..99ec6e3c --- /dev/null +++ b/is-positive-number-array/benchmark/benchmark.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var isPositiveNumberArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( 1.0 ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( 1.0 ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = -i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isPositiveNumberArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isPositiveNumberArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isPositiveNumberArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-positive-number-array/docs/repl.txt b/is-positive-number-array/docs/repl.txt new file mode 100644 index 00000000..3e71ab9b --- /dev/null +++ b/is-positive-number-array/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only positive numbers. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + positive numbers. + + Examples + -------- + > var bool = {{alias}}( [ 3.0, new Number(3.0) ] ) + true + > bool = {{alias}}( [ 3.0, '3.0' ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only positive primitive + number values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + positive primitive number values. + + Examples + -------- + > var bool = {{alias}}.primitives( [ 1.0, 10.0 ] ) + true + > bool = {{alias}}.primitives( [ 1.0, 0.0, 10.0 ] ) + false + > bool = {{alias}}.primitives( [ 3.0, new Number(1.0) ] ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only number objects + having positive values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + number objects having positive values. + + Examples + -------- + > var bool = {{alias}}.objects( [ new Number(1.0), new Number(10.0) ] ) + true + > bool = {{alias}}.objects( [ 1.0, 2.0, 10.0 ] ) + false + > bool = {{alias}}.objects( [ 3.0, new Number(1.0) ] ) + false + + See Also + -------- + diff --git a/is-positive-number-array/docs/types/index.d.ts b/is-positive-number-array/docs/types/index.d.ts new file mode 100644 index 00000000..f31dcea0 --- /dev/null +++ b/is-positive-number-array/docs/types/index.d.ts @@ -0,0 +1,101 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isPositiveNumberArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsPositiveNumberArray { + /** + * Tests if a value is an array-like object containing only positive numbers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only positive numbers + * + * @example + * var bool = isPositiveNumberArray( [ 2.7, new Number(3.0) ] ); + * // returns true + * + * @example + * var bool = isPositiveNumberArray( [ 3.0, '3.0' ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only positive primitive number values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only positive primitive number values + * + * @example + * var bool = isPositiveNumberArray.primitives( [ 1.0, 2.7, 10.0 ] ); + * // returns true + * + * @example + * var bool = isPositiveNumberArray.primitives( [ 3.0, new Number(1.0) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects having positive number values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number objects having positive number values + * + * @example + * var bool = isPositiveNumberArray.objects( [ new Number(3.0), new Number(1.7) ] ); + * // returns true + * + * @example + * var bool = isPositiveNumberArray.objects( [ 1.0, 2.7, 10.0 ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object containing only positive numbers. +* +* @param value - value to test +* @returns boolean indicating whether value is an array-like object containing only positive numbers +* +* @example +* var bool = isPositiveNumberArray( [ 2.7, new Number(3.0) ] ); +* // returns true +* +* @example +* var bool = isPositiveNumberArray( [ 2.7, '3.0' ] ); +* // returns false +* +* @example +* var bool = isPositiveNumberArray.primitives( [ 1.0, 2.0, 10.1 ] ); +* // returns true +* +* @example +* var bool = isPositiveNumberArray.objects( [ new Number(3.0), new Number(1.0) ] ); +* // returns true +*/ +declare var isPositiveNumberArray: IsPositiveNumberArray; + + +// EXPORTS // + +export = isPositiveNumberArray; diff --git a/is-positive-number-array/docs/types/test.ts b/is-positive-number-array/docs/types/test.ts new file mode 100644 index 00000000..3e768928 --- /dev/null +++ b/is-positive-number-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isPositiveNumberArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isPositiveNumberArray( [ 3.9 ] ); // $ExpectType boolean + isPositiveNumberArray( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isPositiveNumberArray(); // $ExpectError + isPositiveNumberArray( [ 2.8 ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isPositiveNumberArray.primitives( [ new Number( 2.8 ) ] ); // $ExpectType boolean + isPositiveNumberArray.primitives( [ 2.8 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isPositiveNumberArray.primitives(); // $ExpectError + isPositiveNumberArray.primitives( [ 2 ], 123 ); // $ExpectError +} + + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isPositiveNumberArray.objects( [ new Number( 2 ) ] ); // $ExpectType boolean + isPositiveNumberArray.objects( [ 2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isPositiveNumberArray.objects(); // $ExpectError + isPositiveNumberArray.objects( [ 2 ], 123 ); // $ExpectError +} diff --git a/is-positive-number-array/examples/index.js b/is-positive-number-array/examples/index.js new file mode 100644 index 00000000..f45cd76b --- /dev/null +++ b/is-positive-number-array/examples/index.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isPositiveNumberArray = require( './../lib' ); + +var bool = isPositiveNumberArray( [ 5.0, 0.2, 3.9 ] ); +console.log( bool ); +// => true + +bool = isPositiveNumberArray( [ 1, 2, 3 ] ); +console.log( bool ); +// => true + +bool = isPositiveNumberArray( [ 1, new Number( 6 ), 3 ] ); +console.log( bool ); +// => true + +bool = isPositiveNumberArray( [ 0, 2, 4 ] ); +console.log( bool ); +// => false + +bool = isPositiveNumberArray( [ 1, 'abc', 3 ] ); +console.log( bool ); +// => false + +bool = isPositiveNumberArray( 78.0 ); +console.log( bool ); +// => false + +bool = isPositiveNumberArray( [] ); +console.log( bool ); +// => false diff --git a/is-positive-number-array/lib/index.js b/is-positive-number-array/lib/index.js new file mode 100644 index 00000000..3254d882 --- /dev/null +++ b/is-positive-number-array/lib/index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only positive numbers. +* +* @module @stdlib/assert/is-positive-number-array +* +* @example +* var isPositiveNumberArray = require( '@stdlib/assert/is-positive-number-array' ); +* +* var bool = isPositiveNumberArray( [ 3.0, new Number(3.0) ] ); +* // returns true +* +* bool = isPositiveNumberArray( [ 3.0, '3.0' ] ); +* // returns false +* +* @example +* var isPositiveNumberArray = require( '@stdlib/assert/is-positive-number-array' ).primitives; +* +* var bool = isPositiveNumberArray( [ 1.0, 5.0, 10.0 ] ); +* // returns true +* +* bool = isPositiveNumberArray( [ 3.0, new Number(1.0) ] ); +* // returns false +* +* @example +* var isPositiveNumberArray = require( '@stdlib/assert/is-positive-number-array' ).objects; +* +* var bool = isPositiveNumberArray( [ new Number(3.0), new Number(1.0) ] ); +* // returns true +* +* bool = isPositiveNumberArray( [ 1.0, 5.0, 10.0 ] ); +* // returns false +*/ + +// MODULES // + +var isPositiveNumber = require( '@stdlib/assert/is-positive-number' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); + + +// MAIN // + +var isPositiveNumberArray = arrayfun( isPositiveNumber ); +setReadOnly( isPositiveNumberArray, 'primitives', arrayfun( isPositiveNumber.isPrimitive ) ); +setReadOnly( isPositiveNumberArray, 'objects', arrayfun( isPositiveNumber.isObject ) ); + + +// EXPORTS // + +module.exports = isPositiveNumberArray; diff --git a/is-positive-number-array/package.json b/is-positive-number-array/package.json new file mode 100644 index 00000000..74dc0d58 --- /dev/null +++ b/is-positive-number-array/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-positive-number-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only positive numbers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "number", + "numeric", + "positive", + "isnumber", + "isnumeric", + "type", + "check" + ] +} diff --git a/is-positive-number-array/test/test.js b/is-positive-number-array/test/test.js new file mode 100644 index 00000000..c6761e14 --- /dev/null +++ b/is-positive-number-array/test/test.js @@ -0,0 +1,101 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Number = require( '@stdlib/number/ctor' ); +var isPositiveNumberArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPositiveNumberArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only positive numbers', function test( t ) { + var arr; + + arr = [ 5.0, new Number( 5 ), 1.0 ]; + t.equal( isPositiveNumberArray( arr ), true, 'returns true' ); + + arr = [ 5.0, '3', null ]; + t.equal( isPositiveNumberArray( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': 3.14, + '1': 2.0 + }; + t.equal( isPositiveNumberArray( arr ), true, 'returns true' ); + + arr = new Float32Array( [ 5.0, 1.5 ] ); + t.equal( isPositiveNumberArray( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object having only positive number primitives', function test( t ) { + var arr; + + arr = [ 5.0, 2.0 ]; + t.equal( isPositiveNumberArray.primitives( arr ), true, 'returns true' ); + + arr = [ new Number( 5 ), 1.0, 1.0 ]; + t.equal( isPositiveNumberArray.primitives( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': 3.14, + '1': 1.0 + }; + t.equal( isPositiveNumberArray.primitives( arr ), true, 'returns true' ); + + arr = new Float32Array( [ 5.0, 1.5 ] ); + t.equal( isPositiveNumberArray.primitives( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object having only positive number objects', function test( t ) { + var arr; + + arr = [ new Number( 5 ), new Number( 5 ) ]; + t.equal( isPositiveNumberArray.objects( arr ), true, 'returns true' ); + + arr = [ 5.0, 1.0 ]; + t.equal( isPositiveNumberArray.objects( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': new Number( 3.14 ), + '1': new Number( 2.0 ) + }; + t.equal( isPositiveNumberArray.objects( arr ), true, 'returns true' ); + + arr = new Float32Array( [ 5.0, 1.5 ] ); + t.equal( isPositiveNumberArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-positive-number/README.md b/is-positive-number/README.md new file mode 100644 index 00000000..269290df --- /dev/null +++ b/is-positive-number/README.md @@ -0,0 +1,136 @@ + + +# isPositiveNumber + +> Test if a value is a number having a positive value. + +
+ +## Usage + +```javascript +var isPositiveNumber = require( '@stdlib/assert/is-positive-number' ); +``` + +#### isPositiveNumber( value ) + +Tests if a `value` is a `number` having a positive value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveNumber( 5.0 ); +// returns true + +bool = isPositiveNumber( new Number( 5.0 ) ); +// returns true + +bool = isPositiveNumber( 3.14 ); +// returns true + +bool = isPositiveNumber( -5.0 ); +// returns false + +bool = isPositiveNumber( null ); +// returns false +``` + +#### isPositiveNumber.isPrimitive( value ) + +Tests if a `value` is a primitive `number` having a positive value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveNumber.isPrimitive( 3.0 ); +// returns true + +bool = isPositiveNumber.isPrimitive( new Number( 3.0 ) ); +// returns false +``` + +#### isPositiveNumber.isObject( value ) + +Tests if a `value` is a `Number` object having a positive value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveNumber.isObject( 3.0 ); +// returns false + +bool = isPositiveNumber.isObject( new Number( 3.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isPositiveNumber = require( '@stdlib/assert/is-positive-number' ); + +var bool = isPositiveNumber( 5.0 ); +// returns true + +bool = isPositiveNumber( new Number( 5.0 ) ); +// returns true + +bool = isPositiveNumber( 3.14 ); +// returns true + +bool = isPositiveNumber( 0.0 ); +// returns false + +bool = isPositiveNumber( -5.0 ); +// returns false + +bool = isPositiveNumber( '5' ); +// returns false + +bool = isPositiveNumber( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-positive-number/benchmark/benchmark.js b/is-positive-number/benchmark/benchmark.js new file mode 100644 index 00000000..e0f48c36 --- /dev/null +++ b/is-positive-number/benchmark/benchmark.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isPositiveNumber = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( -3.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-positive-number/docs/repl.txt b/is-positive-number/docs/repl.txt new file mode 100644 index 00000000..76b35363 --- /dev/null +++ b/is-positive-number/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is a positive number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a positive number. + + Examples + -------- + > var bool = {{alias}}( 5.0 ) + true + > bool = {{alias}}( new Number( 5.0 ) ) + true + > bool = {{alias}}( 3.14 ) + true + > bool = {{alias}}( -5.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive having a positive value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive having a positive + value. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 3.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( 3.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a positive value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a positive + value. + + Examples + -------- + > var bool = {{alias}}.isObject( 3.0 ) + false + > bool = {{alias}}.isObject( new Number( 3.0 ) ) + true + + + See Also + -------- + diff --git a/is-positive-number/docs/types/index.d.ts b/is-positive-number/docs/types/index.d.ts new file mode 100644 index 00000000..6ab7ac21 --- /dev/null +++ b/is-positive-number/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isPositiveNumber` with methods for testing for primitives and objects, respectively. +*/ +interface IsPositiveNumber { + /** + * Tests if a value is a positive number. + * + * @param value - value to test + * @returns boolean indicating whether value is a positive number + * + * @example + * var bool = isPositiveNumber( 5.0 ); + * // returns true + * + * @example + * var bool = isPositiveNumber( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = isPositiveNumber( 3.14 ); + * // returns true + * + * @example + * var bool = isPositiveNumber( -5.0 ); + * // returns false + * + * @example + * var bool = isPositiveNumber( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a positive value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a positive value + * + * @example + * var bool = isPositiveNumber.isPrimitive( 3.0 ); + * // returns true + * + * @example + * var bool = isPositiveNumber.isPrimitive( new Number( 3.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a positive value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a positive value + * + * @example + * var bool = isPositiveNumber.isObject( 3.0 ); + * // returns false + * + * @example + * var bool = isPositiveNumber.isObject( new Number( 3.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a positive number. +* +* @param value - value to test +* @returns boolean indicating whether value is a positive number +* +* @example +* var bool = isPositiveNumber( 5.0 ); +* // returns true +* +* @example +* var bool = isPositiveNumber( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isPositiveNumber( 3.14 ); +* // returns true +* +* @example +* var bool = isPositiveNumber( -5.0 ); +* // returns false +* +* @example +* var bool = isPositiveNumber( null ); +* // returns false +* +* @example +* var bool = isPositiveNumber.isPrimitive( 3.0 ); +* // returns true +* +* @example +* var bool = isPositiveNumber.isObject( 3.0 ); +* // returns false +*/ +declare var isPositiveNumber: IsPositiveNumber; + + +// EXPORTS // + +export = isPositiveNumber; diff --git a/is-positive-number/docs/types/test.ts b/is-positive-number/docs/types/test.ts new file mode 100644 index 00000000..d30e573c --- /dev/null +++ b/is-positive-number/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isPositiveNumber = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isPositiveNumber( 3 ); // $ExpectType boolean + isPositiveNumber( -2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isPositiveNumber(); // $ExpectError + isPositiveNumber( 2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isPositiveNumber.isPrimitive( new Number( 2 ) ); // $ExpectType boolean + isPositiveNumber.isPrimitive( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isPositiveNumber.isPrimitive(); // $ExpectError + isPositiveNumber.isPrimitive( 2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isPositiveNumber.isObject( new Number( 2 ) ); // $ExpectType boolean + isPositiveNumber.isObject( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isPositiveNumber.isObject(); // $ExpectError + isPositiveNumber.isObject( 2, 123 ); // $ExpectError +} diff --git a/is-positive-number/examples/index.js b/is-positive-number/examples/index.js new file mode 100644 index 00000000..af1f8e98 --- /dev/null +++ b/is-positive-number/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isPositiveNumber = require( './../lib' ); + +console.log( isPositiveNumber( 5.0 ) ); +// => true + +console.log( isPositiveNumber( new Number( 5.0 ) ) ); +// => true + +console.log( isPositiveNumber( 3.14 ) ); +// => true + +console.log( isPositiveNumber( 0.0 ) ); +// => false + +console.log( isPositiveNumber( -5.0 ) ); +// => false + +console.log( isPositiveNumber( '5' ) ); +// => false + +console.log( isPositiveNumber( null ) ); +// => false diff --git a/is-positive-number/lib/index.js b/is-positive-number/lib/index.js new file mode 100644 index 00000000..9b2f5c65 --- /dev/null +++ b/is-positive-number/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a positive number. +* +* @module @stdlib/assert/is-positive-number +* +* @example +* var isPositiveNumber = require( '@stdlib/assert/is-positive-number' ); +* +* var bool = isPositiveNumber( 5.0 ); +* // returns true +* +* bool = isPositiveNumber( new Number( 5.0 ) ); +* // returns true +* +* bool = isPositiveNumber( 3.14 ); +* // returns true +* +* bool = isPositiveNumber( -5.0 ); +* // returns false +* +* bool = isPositiveNumber( null ); +* // returns false +* +* @example +* var isPositiveNumber = require( '@stdlib/assert/is-positive-number' ).isPrimitive; +* +* var bool = isPositiveNumber( 3.0 ); +* // returns true +* +* bool = isPositiveNumber( new Number( 3.0 ) ); +* // returns false +* +* @example +* var isPositiveNumber = require( '@stdlib/assert/is-positive-number' ).isObject; +* +* var bool = isPositiveNumber( 3.0 ); +* // returns false +* +* bool = isPositiveNumber( new Number( 3.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isPositiveNumber = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isPositiveNumber, 'isPrimitive', isPrimitive ); +setReadOnly( isPositiveNumber, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isPositiveNumber; diff --git a/is-positive-number/lib/main.js b/is-positive-number/lib/main.js new file mode 100644 index 00000000..f72c7b1d --- /dev/null +++ b/is-positive-number/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a positive number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a positive number +* +* @example +* var bool = isPositiveNumber( 5.0 ); +* // returns true +* +* @example +* var bool = isPositiveNumber( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isPositiveNumber( 3.14 ); +* // returns true +* +* @example +* var bool = isPositiveNumber( -5.0 ); +* // returns false +* +* @example +* var bool = isPositiveNumber( null ); +* // returns false +*/ +function isPositiveNumber( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isPositiveNumber; diff --git a/is-positive-number/lib/object.js b/is-positive-number/lib/object.js new file mode 100644 index 00000000..2208e5da --- /dev/null +++ b/is-positive-number/lib/object.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; + + +// MAIN // + +/** +* Tests if a value is a number object having a positive value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a positive value +* +* @example +* var bool = isPositiveNumber( 3.0 ); +* // returns false +* +* @example +* var bool = isPositiveNumber( new Number( 3.0 ) ); +* // returns true +*/ +function isPositiveNumber( value ) { + return ( + isNumber( value ) && + value.valueOf() > 0.0 + ); +} + + +// EXPORTS // + +module.exports = isPositiveNumber; diff --git a/is-positive-number/lib/primitive.js b/is-positive-number/lib/primitive.js new file mode 100644 index 00000000..7a660a8d --- /dev/null +++ b/is-positive-number/lib/primitive.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a number primitive having a positive value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a positive value +* +* @example +* var bool = isPositiveNumber( 3.0 ); +* // returns true +* +* @example +* var bool = isPositiveNumber( new Number( 3.0 ) ); +* // returns false +*/ +function isPositiveNumber( value ) { + return ( + isNumber( value ) && + value > 0.0 + ); +} + + +// EXPORTS // + +module.exports = isPositiveNumber; diff --git a/is-positive-number/package.json b/is-positive-number/package.json new file mode 100644 index 00000000..4895a871 --- /dev/null +++ b/is-positive-number/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-positive-number", + "version": "0.0.0", + "description": "Test if a value is a number having a positive value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "positive", + "is", + "isnumber", + "isnumeric", + "type", + "check", + "primitive", + "object" + ] +} diff --git a/is-positive-number/test/test.js b/is-positive-number/test/test.js new file mode 100644 index 00000000..67550846 --- /dev/null +++ b/is-positive-number/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isPositiveNumber = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPositiveNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number having a positive value', function test( t ) { + t.equal( typeof isPositiveNumber.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a positive value', function test( t ) { + t.equal( typeof isPositiveNumber.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-positive-number/test/test.main.js b/is-positive-number/test/test.main.js new file mode 100644 index 00000000..73cec703 --- /dev/null +++ b/is-positive-number/test/test.main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isPositiveNumber = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPositiveNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number having a positive number value', function test( t ) { + t.equal( isPositiveNumber( 5.0 ), true, 'returns true' ); + t.equal( isPositiveNumber( new Number( 5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number having a positive number value', function test( t ) { + var values; + var i; + + values = [ + '5', + -3.14, + -1.0, + 0.0, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPositiveNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-positive-number/test/test.object.js b/is-positive-number/test/test.object.js new file mode 100644 index 00000000..7bc9d0fd --- /dev/null +++ b/is-positive-number/test/test.object.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isPositiveNumber = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPositiveNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a positive value', function test( t ) { + t.equal( isPositiveNumber( new Number( 5.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is a positive value', function test( t ) { + t.equal( isPositiveNumber( 3.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPositiveNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-positive-number/test/test.primitive.js b/is-positive-number/test/test.primitive.js new file mode 100644 index 00000000..e7a7a96b --- /dev/null +++ b/is-positive-number/test/test.primitive.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isPositiveNumber = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPositiveNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number having a positive value', function test( t ) { + t.equal( isPositiveNumber( 3.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has a positive value', function test( t ) { + t.equal( isPositiveNumber( new Number( 5.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPositiveNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-positive-zero/README.md b/is-positive-zero/README.md new file mode 100644 index 00000000..3b481815 --- /dev/null +++ b/is-positive-zero/README.md @@ -0,0 +1,136 @@ + + +# isPositiveZero + +> Test if a value is a number equal to positive zero. + +
+ +## Usage + +```javascript +var isPositiveZero = require( '@stdlib/assert/is-positive-zero' ); +``` + +#### isPositiveZero( value ) + +Tests if a `value` is a `number` having a value equal to positive zero. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveZero( 0.0 ); +// returns true + +bool = isPositiveZero( new Number( 0.0 ) ); +// returns true + +bool = isPositiveZero( -3.14 ); +// returns false + +bool = isPositiveZero( -0.0 ); +// returns false + +bool = isPositiveZero( null ); +// returns false +``` + +#### isPositiveZero.isPrimitive( value ) + +Tests if a `value` is a primitive `number` equal to positive zero. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveZero.isPrimitive( 0.0 ); +// returns true + +bool = isPositiveZero.isPrimitive( new Number( 0.0 ) ); +// returns false +``` + +#### isPositiveZero.isObject( value ) + +Tests if a `value` is a `Number` object having a value equal to positive zero. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPositiveZero.isObject( 0.0 ); +// returns false + +bool = isPositiveZero.isObject( new Number( 0.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isPositiveZero = require( '@stdlib/assert/is-positive-zero' ); + +var bool = isPositiveZero( 0.0 ); +// returns true + +bool = isPositiveZero( new Number( 0.0 ) ); +// returns true + +bool = isPositiveZero( -3.14 ); +// returns false + +bool = isPositiveZero( -0.0 ); +// returns false + +bool = isPositiveZero( 5.0 ); +// returns false + +bool = isPositiveZero( '0' ); +// returns false + +bool = isPositiveZero( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-positive-zero/benchmark/benchmark.js b/is-positive-zero/benchmark/benchmark.js new file mode 100644 index 00000000..3e7f6d45 --- /dev/null +++ b/is-positive-zero/benchmark/benchmark.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isPositiveZero = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + 0.0, + -0.0, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveZero( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 0.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveZero( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + 0.0, + -0.0, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveZero.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 0.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveZero.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5.0, + 4.0, + 3.14, + 0.0, + -0.0, + -5.0, + -4.0, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveZero.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 0.0 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPositiveZero.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-positive-zero/docs/repl.txt b/is-positive-zero/docs/repl.txt new file mode 100644 index 00000000..24903aea --- /dev/null +++ b/is-positive-zero/docs/repl.txt @@ -0,0 +1,74 @@ + +{{alias}}( value ) + Tests if a value is positive zero. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is positive zero. + + Examples + -------- + > var bool = {{alias}}( 0.0 ) + true + > bool = {{alias}}( new Number( 0.0 ) ) + true + > bool = {{alias}}( -3.14 ) + false + > bool = {{alias}}( -0.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive equal to positive zero. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a number primitive equal to + positive zero. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 0.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( 0.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a value equal to positive zero. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a number object having a value + equal to positive zero. + + Examples + -------- + > var bool = {{alias}}.isObject( 0.0 ) + false + > bool = {{alias}}.isObject( new Number( 0.0 ) ) + true + + See Also + -------- + diff --git a/is-positive-zero/docs/types/index.d.ts b/is-positive-zero/docs/types/index.d.ts new file mode 100644 index 00000000..c6ff827d --- /dev/null +++ b/is-positive-zero/docs/types/index.d.ts @@ -0,0 +1,133 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isPositiveZero` with methods for testing for primitives and objects, respectively. +*/ +interface IsPositiveZero { + /** + * Tests if a value is equal to positive zero. + * + * @param value - value to test + * @returns boolean indicating whether value is equal to positive zero + * + * @example + * var bool = isPositiveZero( 0.0 ); + * // returns true + * + * @example + * var bool = isPositiveZero( new Number( 0.0 ) ); + * // returns true + * + * @example + * var bool = isPositiveZero( -3.14 ); + * // returns false + * + * @example + * var bool = isPositiveZero( 5.0 ); + * // returns false + * + * @example + * var bool = isPositiveZero( -0.0 ); + * // returns false + * + * @example + * var bool = isPositiveZero( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive equal to positive zero. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive equal to positive zero + * + * @example + * var bool = isPositiveZero.isPrimitive( 0.0 ); + * // returns true + * + * @example + * var bool = isPositiveZero.isPrimitive( new Number( 0.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a value equal to positive zero. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a value equal to positive zero + * + * @example + * var bool = isPositiveZero.isObject( 0.0 ); + * // returns false + * + * @example + * var bool = isPositiveZero.isObject( new Number( 0.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is equal to positive zero. +* +* @param value - value to test +* @returns boolean indicating whether value is equal to positive zero +* +* @example +* var bool = isPositiveZero( 0.0 ); +* // returns true +* +* @example +* var bool = isPositiveZero( new Number( 0.0 ) ); +* // returns true +* +* @example +* var bool = isPositiveZero( -3.14 ); +* // returns false +* +* @example +* var bool = isPositiveZero( 5.0 ); +* // returns false +* +* @example +* var bool = isPositiveZero( -0.0 ); +* // returns false +* +* @example +* var bool = isPositiveZero( null ); +* // returns false +* +* @example +* var bool = isPositiveZero.isPrimitive( 0.0 ); +* // returns true +* +* @example +* var bool = isPositiveZero.isObject( new Number( 0.0 ) ); +* // returns true +*/ +declare var isPositiveZero: IsPositiveZero; + + +// EXPORTS // + +export = isPositiveZero; diff --git a/is-positive-zero/docs/types/test.ts b/is-positive-zero/docs/types/test.ts new file mode 100644 index 00000000..5bc7d5e1 --- /dev/null +++ b/is-positive-zero/docs/types/test.ts @@ -0,0 +1,62 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isPositiveZero = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isPositiveZero( 3 ); // $ExpectType boolean + isPositiveZero( 0 ); // $ExpectType boolean + isPositiveZero( -0 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isPositiveZero(); // $ExpectError + isPositiveZero( 0, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isPositiveZero.isPrimitive( new Number( 0 ) ); // $ExpectType boolean + isPositiveZero.isPrimitive( 0 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isPositiveZero.isPrimitive(); // $ExpectError + isPositiveZero.isPrimitive( 0, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isPositiveZero.isObject( new Number( 0 ) ); // $ExpectType boolean + isPositiveZero.isObject( 0 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isPositiveZero.isObject(); // $ExpectError + isPositiveZero.isObject( 0, 123 ); // $ExpectError +} diff --git a/is-positive-zero/examples/index.js b/is-positive-zero/examples/index.js new file mode 100644 index 00000000..0c0770b5 --- /dev/null +++ b/is-positive-zero/examples/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isPositiveZero = require( './../lib' ); + +console.log( isPositiveZero( 0.0 ) ); +// => true + +console.log( isPositiveZero( new Number( 0.0 ) ) ); +// => true + +console.log( isPositiveZero( -3.14 ) ); +// => false + +console.log( isPositiveZero( -0.0 ) ); +// => false + +console.log( isPositiveZero( 5.0 ) ); +// => false + +console.log( isPositiveZero( '0' ) ); +// => false + +console.log( isPositiveZero( null ) ); +// => false diff --git a/is-positive-zero/lib/index.js b/is-positive-zero/lib/index.js new file mode 100644 index 00000000..bf70538f --- /dev/null +++ b/is-positive-zero/lib/index.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is equal to positive zero. +* +* @module @stdlib/assert/is-positive-zero +* +* @example +* var isPositiveZero = require( '@stdlib/assert/is-positive-zero' ); +* +* var bool = isPositiveZero( 0.0 ); +* // returns true +* +* bool = isPositiveZero( new Number( 0.0 ) ); +* // returns true +* +* bool = isPositiveZero( -3.14 ); +* // returns false +* +* bool = isPositiveZero( 5.0 ); +* // returns false +* +* bool = isPositiveZero( -0.0 ); +* // returns false +* +* bool = isPositiveZero( null ); +* // returns false +* +* @example +* var isPositiveZero = require( '@stdlib/assert/is-positive-zero' ).isPrimitive; +* +* var bool = isPositiveZero( 0.0 ); +* // returns true +* +* bool = isPositiveZero( new Number( 0.0 ) ); +* // returns false +* +* @example +* var isPositiveZero = require( '@stdlib/assert/is-positive-zero' ).isObject; +* +* var bool = isPositiveZero( 0.0 ); +* // returns false +* +* bool = isPositiveZero( new Number( 0.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isPositiveZero = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isPositiveZero, 'isPrimitive', isPrimitive ); +setReadOnly( isPositiveZero, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isPositiveZero; diff --git a/is-positive-zero/lib/main.js b/is-positive-zero/lib/main.js new file mode 100644 index 00000000..b3726c47 --- /dev/null +++ b/is-positive-zero/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is equal to positive zero. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is equal to positive zero +* +* @example +* var bool = isPositiveZero( 0.0 ); +* // returns true +* +* @example +* var bool = isPositiveZero( new Number( 0.0 ) ); +* // returns true +* +* @example +* var bool = isPositiveZero( -3.14 ); +* // returns false +* +* @example +* var bool = isPositiveZero( 5.0 ); +* // returns false +* +* @example +* var bool = isPositiveZero( -0.0 ); +* // returns false +* +* @example +* var bool = isPositiveZero( null ); +* // returns false +*/ +function isPositiveZero( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isPositiveZero; diff --git a/is-positive-zero/lib/object.js b/is-positive-zero/lib/object.js new file mode 100644 index 00000000..3588697a --- /dev/null +++ b/is-positive-zero/lib/object.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; +var PINF = require( '@stdlib/constants/math/float64-pinf' ); + + +// MAIN // + +/** +* Tests if a value is a number object having a value equal to positive zero. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a value equal to positive zero +* +* @example +* var bool = isPositiveZero( 0.0 ); +* // returns false +* +* @example +* var bool = isPositiveZero( new Number( 0.0 ) ); +* // returns true +*/ +function isPositiveZero( value ) { + return ( + isNumber( value ) && + ( 1.0 / value.valueOf() ) === PINF + ); +} + + +// EXPORTS // + +module.exports = isPositiveZero; diff --git a/is-positive-zero/lib/primitive.js b/is-positive-zero/lib/primitive.js new file mode 100644 index 00000000..5e1f2aac --- /dev/null +++ b/is-positive-zero/lib/primitive.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var PINF = require( '@stdlib/constants/math/float64-pinf' ); + + +// MAIN // + +/** +* Tests if a value is a number primitive equal to positive zero. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive equal to positive zero +* +* @example +* var bool = isPositiveZero( 0.0 ); +* // returns true +* +* @example +* var bool = isPositiveZero( new Number( 0.0 ) ); +* // returns false +*/ +function isPositiveZero( value ) { + return ( + isNumber( value ) && + ( 1.0 / value ) === PINF + ); +} + + +// EXPORTS // + +module.exports = isPositiveZero; diff --git a/is-positive-zero/package.json b/is-positive-zero/package.json new file mode 100644 index 00000000..b0a785f6 --- /dev/null +++ b/is-positive-zero/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-positive-zero", + "version": "0.0.0", + "description": "Test if a value is equal to positive zero.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "positive", + "zero", + "is", + "isnumber", + "isnumeric", + "type", + "check", + "primitive", + "object", + "validate", + "valid", + "test" + ] +} diff --git a/is-positive-zero/test/test.js b/is-positive-zero/test/test.js new file mode 100644 index 00000000..3f12af0d --- /dev/null +++ b/is-positive-zero/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isPositiveZero = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isPositiveZero, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number equal to positive zero', function test( t ) { + t.strictEqual( typeof isPositiveZero.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a value equal to positive zero', function test( t ) { + t.strictEqual( typeof isPositiveZero.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-positive-zero/test/test.main.js b/is-positive-zero/test/test.main.js new file mode 100644 index 00000000..ccc26de6 --- /dev/null +++ b/is-positive-zero/test/test.main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isPositiveZero = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isPositiveZero, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number equal to positive zero', function test( t ) { + t.strictEqual( isPositiveZero( 0.0 ), true, 'returns true' ); + t.strictEqual( isPositiveZero( new Number( 0.0 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number equal to positive zero', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + 1.0, + -0.0, + -3.14, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isPositiveZero( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-positive-zero/test/test.object.js b/is-positive-zero/test/test.object.js new file mode 100644 index 00000000..1800e074 --- /dev/null +++ b/is-positive-zero/test/test.object.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isPositiveZero = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isPositiveZero, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a value equal to positive zero', function test( t ) { + t.strictEqual( isPositiveZero( new Number( 0.0 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is equal to positive zero', function test( t ) { + t.strictEqual( isPositiveZero( 0.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided positive zero', function test( t ) { + var values; + var i; + + values = [ + '5', + new Number( -0.0 ), + new Number( 2.0 ), + new Number( -2.0 ), + 3.14, + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isPositiveZero( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-positive-zero/test/test.primitive.js b/is-positive-zero/test/test.primitive.js new file mode 100644 index 00000000..ad0a9bfb --- /dev/null +++ b/is-positive-zero/test/test.primitive.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isPositiveZero = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isPositiveZero, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number equal to positive zero', function test( t ) { + t.strictEqual( isPositiveZero( 0.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has a value equal to positive zero', function test( t ) { + t.strictEqual( isPositiveZero( new Number( 0.0 ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided positive zero', function test( t ) { + var values; + var i; + + values = [ + '5', + -0.0, + new Number( 2.0 ), + 3.14, + -1.0, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isPositiveZero( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-prime/README.md b/is-prime/README.md new file mode 100644 index 00000000..05489049 --- /dev/null +++ b/is-prime/README.md @@ -0,0 +1,153 @@ + + +# isPrime + +> Test if a value is a prime number. + +
+ +A **prime number** is defined as an integer value greater than `1` which is only divisible by `1` and itself. + +
+ + + +
+ +## Usage + +```javascript +var isPrime = require( '@stdlib/assert/is-prime' ); +``` + +#### isPrime( value ) + +Tests if a `value` is a prime number. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPrime( 5.0 ); +// returns true + +bool = isPrime( new Number( 5.0 ) ); +// returns true + +bool = isPrime( 3.14 ); +// returns false + +bool = isPrime( -5.0 ); +// returns false + +bool = isPrime( NaN ); +// returns false + +bool = isPrime( null ); +// returns false +``` + +#### isPrime.isPrimitive( value ) + +Tests if a `value` is a primitive prime number. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPrime.isPrimitive( 5.0 ); +// returns true + +bool = isPrime.isPrimitive( new Number( 5.0 ) ); +// returns false +``` + +#### isPrime.isObject( value ) + +Tests if a `value` is a `Number` object having a value which is a prime number. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isPrime.isObject( 5.0 ); +// returns false + +bool = isPrime.isObject( new Number( 5.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isPrime = require( '@stdlib/assert/is-prime' ); + +var bool = isPrime( 5.0 ); +// returns true + +bool = isPrime( new Number( 5.0 ) ); +// returns true + +bool = isPrime( 11.0 ); +// returns true + +bool = isPrime( 2.0 ); +// returns true + +bool = isPrime( 3.14 ); +// returns false + +bool = isPrime( -5.0 ); +// returns false + +bool = isPrime( NaN ); +// returns false + +bool = isPrime( '0.5' ); +// returns false + +bool = isPrime( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-prime/benchmark/benchmark.js b/is-prime/benchmark/benchmark.js new file mode 100644 index 00000000..d58e04c9 --- /dev/null +++ b/is-prime/benchmark/benchmark.js @@ -0,0 +1,233 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isPrime = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 0.5, + 1.0, + 0.0, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPrime( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 5.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPrime( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPrime.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 5.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPrime.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPrime.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 5.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPrime.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-prime/benchmark/benchmark.primes.js b/is-prime/benchmark/benchmark.primes.js new file mode 100644 index 00000000..747755ff --- /dev/null +++ b/is-prime/benchmark/benchmark.primes.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var log10 = require( '@stdlib/math/base/special/log10' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var PRIMES = require( './../test/fixtures/primes.js' ); +var isPrime = require( './../lib' ).isPrimitive; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} prime - prime number +* @returns {Function} benchmark function +*/ +function createBenchmark( prime ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPrime( prime ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var max; + var f; + var p; + var i; + var j; + + min = 1; // 10^min + max = floor( log10( PRIMES.length ) ); // 10^max + + for ( i = min; i <= max; i++ ) { + j = pow( 10, i ) - 1; + p = PRIMES[ j ]; + f = createBenchmark( p ); + bench( pkg+':prime='+p, f ); + } +} + +main(); diff --git a/is-prime/docs/repl.txt b/is-prime/docs/repl.txt new file mode 100644 index 00000000..285f4073 --- /dev/null +++ b/is-prime/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is a prime number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a prime number. + + Examples + -------- + > var bool = {{alias}}( 5.0 ) + true + > bool = {{alias}}( new Number( 5.0 ) ) + true + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( -5.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive which is a prime number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive which is a prime + number. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 5.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( 5.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a value which is a prime number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a value which + is a prime number. + + Examples + -------- + > var bool = {{alias}}.isObject( 5.0 ) + false + > bool = {{alias}}.isObject( new Number( 5.0 ) ) + true + + + See Also + -------- + diff --git a/is-prime/docs/types/index.d.ts b/is-prime/docs/types/index.d.ts new file mode 100644 index 00000000..39756051 --- /dev/null +++ b/is-prime/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface describing methods for testing for primitives and objects. +*/ +interface IsPrime { + /** + * Tests if a value is a prime number. + * + * @param value - value to test + * @returns boolean indicating whether value is a prime number + * + * @example + * var bool = isPrime( 5.0 ); + * // returns true + * + * @example + * var bool = isPrime( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = isPrime( 3.14 ); + * // returns false + * + * @example + * var bool = isPrime( -5.0 ); + * // returns false + * + * @example + * var bool = isPrime( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a value which is a prime number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a value which is a prime number + * + * @example + * var bool = isPrime.isPrimitive( 5.0 ); + * // returns true + * + * @example + * var bool = isPrime.isPrimitive( new Number( 5.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a value which is a prime number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a value which is a prime number + * + * @example + * var bool = isPrime.isObject( 5.0 ); + * // returns false + * + * @example + * var bool = isPrime.isObject( new Number( 5.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a prime number. +* +* @param value - value to test +* @returns boolean indicating whether value is a prime number +* +* @example +* var bool = isPrime( 5.0 ); +* // returns true +* +* @example +* var bool = isPrime( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isPrime( 3.14 ); +* // returns false +* +* @example +* var bool = isPrime( -5.0 ); +* // returns false +* +* @example +* var bool = isPrime( null ); +* // returns false +* +* @example +* var bool = isPrime.isPrimitive( 5.0 ); +* // returns true +* +* @example +* var bool = isPrime.isObject( new Number( 5.0 ) ); +* // returns true +*/ +declare var isPrime: IsPrime; + + +// EXPORTS // + +export = isPrime; diff --git a/is-prime/docs/types/test.ts b/is-prime/docs/types/test.ts new file mode 100644 index 00000000..96812dca --- /dev/null +++ b/is-prime/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isPrime = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isPrime( 3 ); // $ExpectType boolean + isPrime( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isPrime(); // $ExpectError + isPrime( 0.2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isPrime.isPrimitive( new Number( 0.2 ) ); // $ExpectType boolean + isPrime.isPrimitive( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isPrime.isPrimitive(); // $ExpectError + isPrime.isPrimitive( 0.2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isPrime.isObject( new Number( 0.2 ) ); // $ExpectType boolean + isPrime.isObject( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isPrime.isObject(); // $ExpectError + isPrime.isObject( 0.2, 123 ); // $ExpectError +} diff --git a/is-prime/examples/index.js b/is-prime/examples/index.js new file mode 100644 index 00000000..2b66a552 --- /dev/null +++ b/is-prime/examples/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isPrime = require( './../lib' ); + +console.log( isPrime( 5.0 ) ); +// => true + +console.log( isPrime( new Number( 5.0 ) ) ); +// => true + +console.log( isPrime( 11.0 ) ); +// => true + +console.log( isPrime( 2.0 ) ); +// => true + +console.log( isPrime( 3.14 ) ); +// => false + +console.log( isPrime( -5.0 ) ); +// => false + +console.log( isPrime( NaN ) ); +// => false + +console.log( isPrime( '0.5' ) ); +// => false + +console.log( isPrime( null ) ); +// => false diff --git a/is-prime/lib/index.js b/is-prime/lib/index.js new file mode 100644 index 00000000..db838011 --- /dev/null +++ b/is-prime/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a prime number. +* +* @module @stdlib/assert/is-prime +* +* @example +* var isPrime = require( '@stdlib/assert/is-prime' ); +* +* var bool = isPrime( 5.0 ); +* // returns true +* +* bool = isPrime( new Number( 5.0 ) ); +* // returns true +* +* bool = isPrime( 3.14 ); +* // returns false +* +* bool = isPrime( -5.0 ); +* // returns false +* +* bool = isPrime( null ); +* // returns false +* +* @example +* var isPrime = require( '@stdlib/assert/is-prime' ).isPrimitive; +* +* var bool = isPrime( 5.0 ); +* // returns true +* +* bool = isPrime( new Number( 5.0 ) ); +* // returns false +* +* @example +* var isPrime = require( '@stdlib/assert/is-prime' ).isObject; +* +* var bool = isPrime( 5.0 ); +* // returns false +* +* bool = isPrime( new Number( 5.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isPrime = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isPrime, 'isPrimitive', isPrimitive ); +setReadOnly( isPrime, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isPrime; diff --git a/is-prime/lib/main.js b/is-prime/lib/main.js new file mode 100644 index 00000000..8763c8c9 --- /dev/null +++ b/is-prime/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a prime number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a prime number +* +* @example +* var bool = isPrime( 5.0 ); +* // returns true +* +* @example +* var bool = isPrime( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isPrime( 3.14 ); +* // returns false +* +* @example +* var bool = isPrime( -5.0 ); +* // returns false +* +* @example +* var bool = isPrime( null ); +* // returns false +*/ +function isPrime( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isPrime; diff --git a/is-prime/lib/object.js b/is-prime/lib/object.js new file mode 100644 index 00000000..be132797 --- /dev/null +++ b/is-prime/lib/object.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; +var isprime = require( '@stdlib/math/base/assert/is-prime' ); + + +// MAIN // + +/** +* Tests if a value is a number object having a value which is a prime number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a value which is a prime number +* +* @example +* var bool = isPrime( 5.0 ); +* // returns false +* +* @example +* var bool = isPrime( new Number( 5.0 ) ); +* // returns true +*/ +function isPrime( value ) { + if ( !isNumber( value ) ) { + return false; + } + return isprime( value.valueOf() ); +} + + +// EXPORTS // + +module.exports = isPrime; diff --git a/is-prime/lib/primitive.js b/is-prime/lib/primitive.js new file mode 100644 index 00000000..c15ddf2a --- /dev/null +++ b/is-prime/lib/primitive.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isprime = require( '@stdlib/math/base/assert/is-prime' ); + + +// MAIN // + +/** +* Tests if a value is a number primitive having a value which is a prime number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a value which is a prime number +* +* @example +* var bool = isPrime( 5.0 ); +* // returns true +* +* @example +* var bool = isPrime( new Number( 5.0 ) ); +* // returns false +*/ +function isPrime( value ) { + if ( !isNumber( value ) ) { + return false; + } + return isprime( value ); +} + + +// EXPORTS // + +module.exports = isPrime; diff --git a/is-prime/package.json b/is-prime/package.json new file mode 100644 index 00000000..e8c419e4 --- /dev/null +++ b/is-prime/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-prime", + "version": "0.0.0", + "description": "Test if a value is a prime number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "integer", + "int", + "prime", + "primality", + "is", + "isprime", + "type", + "check", + "valid", + "validate", + "test", + "primitive", + "object" + ] +} diff --git a/is-prime/test/fixtures/primes.js b/is-prime/test/fixtures/primes.js new file mode 100644 index 00000000..8acba9a1 --- /dev/null +++ b/is-prime/test/fixtures/primes.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var primes = require( '@stdlib/datasets/primes-100k' ); + + +// MAIN // + +var PRIMES = primes(); + + +// EXPORTS // + +module.exports = PRIMES; diff --git a/is-prime/test/test.js b/is-prime/test/test.js new file mode 100644 index 00000000..c2627d30 --- /dev/null +++ b/is-prime/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isPrime = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPrime, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test primitives', function test( t ) { + t.equal( typeof isPrime.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test objects', function test( t ) { + t.equal( typeof isPrime.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-prime/test/test.main.js b/is-prime/test/test.main.js new file mode 100644 index 00000000..8524a4d2 --- /dev/null +++ b/is-prime/test/test.main.js @@ -0,0 +1,91 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var PRIMES = require( './fixtures/primes.js' ); +var isPrime = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPrime, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a prime number', function test( t ) { + var N; + var p; + var i; + var j; + + t.equal( isPrime( 2 ), true, 'returns expected value' ); + t.equal( isPrime( 3 ), true, 'returns expected value' ); + t.equal( isPrime( 5 ), true, 'returns expected value' ); + t.equal( isPrime( 7 ), true, 'returns expected value' ); + + t.equal( isPrime( new Number( 2 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isPrime( new Number( 3 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isPrime( new Number( 5 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isPrime( new Number( 7 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + + N = PRIMES.length - 1; + for ( i = 0; i < 1e3; i++ ) { + j = discreteUniform( 0, N ); + p = PRIMES[ j ]; + t.equal( isPrime( p ), true, 'returns expected value for '+p ); + t.equal( isPrime( new Number( p ) ), true, 'returns expected value for Number('+p+')' ); // eslint-disable-line no-new-wrappers + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a prime number', function test( t ) { + var values; + var i; + + values = [ + '5', + 1.0, + 4.0, + 21.0, + -5.0, + 6.0, + 100.0, + -3.14, + -1.0, + NaN, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPrime( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-prime/test/test.object.js b/is-prime/test/test.object.js new file mode 100644 index 00000000..8f90552d --- /dev/null +++ b/is-prime/test/test.object.js @@ -0,0 +1,90 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var PRIMES = require( './fixtures/primes.js' ); +var isPrime = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPrime, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a prime number', function test( t ) { + var N; + var p; + var i; + var j; + + t.equal( isPrime( new Number( 2 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isPrime( new Number( 3 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isPrime( new Number( 5 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + t.equal( isPrime( new Number( 7 ) ), true, 'returns expected value' ); // eslint-disable-line no-new-wrappers + + N = PRIMES.length - 1; + for ( i = 0; i < 1e3; i++ ) { + j = discreteUniform( 0, N ); + p = PRIMES[ j ]; + t.equal( isPrime( new Number( p ) ), true, 'returns expected value for Number('+p+')' ); // eslint-disable-line no-new-wrappers + } + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive prime number', function test( t ) { + t.equal( isPrime( 5.0 ), false, 'returns false' ); + t.equal( isPrime( 7.0 ), false, 'returns false' ); + t.equal( isPrime( 11.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a prime number', function test( t ) { + var values; + var i; + + values = [ + '5', + new Number( 4.0 ), // eslint-disable-line no-new-wrappers + new Number( 8.0 ), // eslint-disable-line no-new-wrappers + new Number( 21.0 ), // eslint-disable-line no-new-wrappers + null, + true, + NaN, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), // eslint-disable-line prefer-regex-literals + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPrime( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-prime/test/test.primitive.js b/is-prime/test/test.primitive.js new file mode 100644 index 00000000..dfec9d59 --- /dev/null +++ b/is-prime/test/test.primitive.js @@ -0,0 +1,90 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var PRIMES = require( './fixtures/primes.js' ); +var isPrime = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPrime, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive prime number', function test( t ) { + var N; + var p; + var i; + var j; + + t.equal( isPrime( 2 ), true, 'returns expected value' ); + t.equal( isPrime( 3 ), true, 'returns expected value' ); + t.equal( isPrime( 5 ), true, 'returns expected value' ); + t.equal( isPrime( 7 ), true, 'returns expected value' ); + + N = PRIMES.length - 1; + for ( i = 0; i < 1e3; i++ ) { + j = discreteUniform( 0, N ); + p = PRIMES[ j ]; + t.equal( isPrime( p ), true, 'returns expected value for '+p ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided a number object', function test( t ) { + t.equal( isPrime( new Number( 5.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.equal( isPrime( new Number( 7.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.equal( isPrime( new Number( 11.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a prime number', function test( t ) { + var values; + var i; + + values = [ + '5', + 4.0, + 21.0, + 35.0, + 55.0, + 100.0, + -5.0, + null, + true, + NaN, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPrime( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-primitive-array/README.md b/is-primitive-array/README.md new file mode 100644 index 00000000..7d86bf7f --- /dev/null +++ b/is-primitive-array/README.md @@ -0,0 +1,90 @@ + + +# isPrimitiveArray + +> Test if a value is an array-like object containing only JavaScript primitives. + +
+ +## Usage + +```javascript +var isPrimitiveArray = require( '@stdlib/assert/is-primitive-array' ); +``` + +#### isPrimitiveArray( value ) + +Tests if a `value` is an array-like object containing **only** JavaScript primitives. + + + +```javascript +var bool = isPrimitiveArray( [ '3', 2, null ] ); +// returns true + +bool = isPrimitiveArray( [ {}, 2, 1 ] ); +// returns false + +bool = isPrimitiveArray( [ new String( 'abc' ), '3.0' ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isPrimitiveArray = require( '@stdlib/assert/is-primitive-array' ); + +var bool = isPrimitiveArray( [ '3', 2, null ] ); +// returns true + +bool = isPrimitiveArray( [ void 0, true ] ); +// returns true + +bool = isPrimitiveArray( [ new String( 'abc' ), false ] ); +// returns false + +bool = isPrimitiveArray( [ new Number( 2 ), null ] ); +// returns false + +bool = isPrimitiveArray( [ function noop() {}, null ] ); +// returns false +``` + +
+ + + + + + diff --git a/is-primitive-array/benchmark/benchmark.js b/is-primitive-array/benchmark/benchmark.js new file mode 100644 index 00000000..74b37aca --- /dev/null +++ b/is-primitive-array/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isPrimitiveArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( i ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = [ i * 3.14 ]; + bool = isPrimitiveArray( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/is-primitive-array/docs/repl.txt b/is-primitive-array/docs/repl.txt new file mode 100644 index 00000000..168721fe --- /dev/null +++ b/is-primitive-array/docs/repl.txt @@ -0,0 +1,28 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only JavaScript + primitives. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + JavaScript primitives. + + Examples + -------- + > var bool = {{alias}}( [ '3', 2, null ] ) + true + > bool = {{alias}}( [ {}, 2, 1 ] ) + false + > bool = {{alias}}( [ new String('abc'), '3.0' ] ) + false + + See Also + -------- + diff --git a/is-primitive-array/docs/types/index.d.ts b/is-primitive-array/docs/types/index.d.ts new file mode 100644 index 00000000..9b363939 --- /dev/null +++ b/is-primitive-array/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an array-like object containing only JavaScript primitives. +* +* @param value - value to test +* @returns boolean indicating whether an input value is an array-like object containing only JavaScript primitives +* +* @example +* var bool = isPrimitiveArray( [ '3', 2, null ] ); +* // returns true +* +* bool = isPrimitiveArray( [ {}, 2, 1 ] ); +* // returns false +* +* bool = isPrimitiveArray( [ new String('abc'), '3.0' ] ); +* // returns false +*/ +declare function isPrimitiveArray( value: any ): boolean; + + +// EXPORTS // + +export = isPrimitiveArray; diff --git a/is-primitive-array/docs/types/test.ts b/is-primitive-array/docs/types/test.ts new file mode 100644 index 00000000..681c78cb --- /dev/null +++ b/is-primitive-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isPrimitiveArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isPrimitiveArray( [ 'abc', 'def' ] ); // $ExpectType boolean + isPrimitiveArray( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isPrimitiveArray(); // $ExpectError + isPrimitiveArray( [ 1, 2, 3 ], 123 ); // $ExpectError +} diff --git a/is-primitive-array/examples/index.js b/is-primitive-array/examples/index.js new file mode 100644 index 00000000..b2926d8d --- /dev/null +++ b/is-primitive-array/examples/index.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-restricted-syntax, no-new-wrappers, no-empty-function */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isPrimitiveArray = require( './../lib' ); + +console.log( isPrimitiveArray( [ '3', 2, null ] ) ); +// => true + +console.log( isPrimitiveArray( [ void 0, true ] ) ); +// => true + +console.log( isPrimitiveArray( [ new String( 'abc' ), false ] ) ); +// => false + +console.log( isPrimitiveArray( [ new Number( 2 ), null ] ) ); +// => false + +console.log( isPrimitiveArray( [ function noop() {}, null ] ) ); +// => false diff --git a/is-primitive-array/lib/index.js b/is-primitive-array/lib/index.js new file mode 100644 index 00000000..6fc693af --- /dev/null +++ b/is-primitive-array/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only JavaScript primitives. +* +* @module @stdlib/assert/is-primitive-array +* +* @example +* var isPrimitiveArray = require( '@stdlib/assert/is-primitive-array' ); +* +* var bool = isPrimitiveArray( [ '3', 2, null ] ); +* // returns true +* +* bool = isPrimitiveArray( [ {}, 2, 1 ] ); +* // returns false +* +* bool = isPrimitiveArray( [ new String('abc'), '3.0' ] ); +* // returns false +*/ + +// MODULES // + +var isPrimitiveArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isPrimitiveArray; diff --git a/is-primitive-array/lib/main.js b/is-primitive-array/lib/main.js new file mode 100644 index 00000000..2ae5d615 --- /dev/null +++ b/is-primitive-array/lib/main.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); +var isPrimitive = require( '@stdlib/assert/is-primitive' ); + + +// MAIN // + +/** +* Tests if a value is an array-like object containing only JavaScript primitives. +* +* @name isPrimitiveArray +* @type {Function} +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether an input value is an array-like object containing only JavaScript primitives +* +* @example +* var bool = isPrimitiveArray( [ '3', 2, null ] ); +* // returns true +* +* bool = isPrimitiveArray( [ {}, 2, 1 ] ); +* // returns false +* +* bool = isPrimitiveArray( [ new String('abc'), '3.0' ] ); +* // returns false +*/ +var isPrimitiveArray = arrayfun( isPrimitive ); + + +// EXPORTS // + +module.exports = isPrimitiveArray; diff --git a/is-primitive-array/package.json b/is-primitive-array/package.json new file mode 100644 index 00000000..693160c6 --- /dev/null +++ b/is-primitive-array/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-primitive-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only JavaScript primitives.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "array", + "is", + "isarray", + "primitive", + "string", + "number", + "null", + "undefined", + "boolean", + "symbol", + "isprimitive", + "type", + "check", + "validate", + "test" + ] +} diff --git a/is-primitive-array/test/test.js b/is-primitive-array/test/test.js new file mode 100644 index 00000000..327a140b --- /dev/null +++ b/is-primitive-array/test/test.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Number = require( '@stdlib/number/ctor' ); +var isPrimitiveArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPrimitiveArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only primitives', function test( t ) { + var arr; + + arr = [ 'abc', 123, null, NaN, undefined ]; + t.equal( isPrimitiveArray( arr ), true, 'returns true' ); + + arr = [ 5.0, '5.0', new Number( 2 ) ]; + t.equal( isPrimitiveArray( arr ), false, 'returns false' ); + + arr = { + 'length': 3, + '0': 5.0, + '1': '5.0', + '2': false + }; + t.equal( isPrimitiveArray( arr ), true, 'returns true' ); + + arr = new Uint8Array( [ 1, 2, 3, 4, 5 ] ); + t.equal( isPrimitiveArray( arr ), true, 'returns true' ); + + arr = [ {}, new String( 'abc' ) ]; + t.equal( isPrimitiveArray( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-primitive/README.md b/is-primitive/README.md new file mode 100644 index 00000000..678e3a53 --- /dev/null +++ b/is-primitive/README.md @@ -0,0 +1,120 @@ + + +# isPrimitive + +> Test if a value is a JavaScript primitive. + +
+ +## Usage + +```javascript +var isPrimitive = require( '@stdlib/assert/is-primitive' ); +``` + +#### isPrimitive( value ) + +Tests if a `value` is a JavaScript primitive. + +```javascript +var bool = isPrimitive( false ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Seven primitives: + + - `string` + - `number` + - `boolean` + - `null` + - `undefined` + - `symbol` (ES6/ES2015) + - `bigint` (ES11/ES2020) + +
+ + + +
+ +## Examples + + + + + +```javascript +var isPrimitive = require( '@stdlib/assert/is-primitive' ); + +var bool = isPrimitive( false ); +// returns true + +bool = isPrimitive( 0 ); +// returns true + +bool = isPrimitive( '' ); +// returns true + +bool = isPrimitive( null ); +// returns true + +bool = isPrimitive( void 0 ); +// returns true + +bool = isPrimitive( [] ); +// returns false + +bool = isPrimitive( {} ); +// returns false + +bool = isPrimitive( function noop() {} ); +// returns false + +bool = isPrimitive( new Boolean() ); +// returns false + +bool = isPrimitive( new String() ); +// returns false + +bool = isPrimitive( new Array() ); +// returns false + +bool = isPrimitive( new Object() ); +// returns false +``` + +
+ + + + + + diff --git a/is-primitive/benchmark/benchmark.js b/is-primitive/benchmark/benchmark.js new file mode 100644 index 00000000..8d5101a1 --- /dev/null +++ b/is-primitive/benchmark/benchmark.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isPrimitive = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPrimitive( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-primitive/docs/repl.txt b/is-primitive/docs/repl.txt new file mode 100644 index 00000000..59497b8a --- /dev/null +++ b/is-primitive/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a JavaScript primitive. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a JavaScript primitive. + + Examples + -------- + > var bool = {{alias}}( true ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-primitive/docs/types/index.d.ts b/is-primitive/docs/types/index.d.ts new file mode 100644 index 00000000..f07cc973 --- /dev/null +++ b/is-primitive/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a JavaScript primitive. +* +* @param value - value to test +* @returns boolean indicating if a value is a JavaScript primitive +* +* @example +* var bool = isPrimitive( true ); +* // returns true +* +* @example +* var bool = isPrimitive( {} ); +* // returns false +*/ +declare function isPrimitive( value: any ): boolean; + + +// EXPORTS // + +export = isPrimitive; diff --git a/is-primitive/docs/types/test.ts b/is-primitive/docs/types/test.ts new file mode 100644 index 00000000..312c323a --- /dev/null +++ b/is-primitive/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isPrimitive = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isPrimitive( {} ); // $ExpectType boolean + isPrimitive( 8 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isPrimitive(); // $ExpectError + isPrimitive( 8, 123 ); // $ExpectError +} diff --git a/is-primitive/examples/index.js b/is-primitive/examples/index.js new file mode 100644 index 00000000..7ac35615 --- /dev/null +++ b/is-primitive/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-restricted-syntax, no-new-object, no-new-wrappers, no-empty-function, no-array-constructor */ + +'use strict'; + +var isPrimitive = require( './../lib' ); + +console.log( isPrimitive( false ) ); +// => true + +console.log( isPrimitive( 0 ) ); +// => true + +console.log( isPrimitive( '' ) ); +// => true + +console.log( isPrimitive( null ) ); +// => true + +console.log( isPrimitive( void 0 ) ); +// => true + +console.log( isPrimitive( [] ) ); +// => false + +console.log( isPrimitive( {} ) ); +// => false + +console.log( isPrimitive( function noop() {} ) ); +// => false + +console.log( isPrimitive( new Boolean() ) ); +// => false + +console.log( isPrimitive( new String() ) ); +// => false + +console.log( isPrimitive( new Array() ) ); +// => false + +console.log( isPrimitive( new Object() ) ); +// => false diff --git a/is-primitive/lib/index.js b/is-primitive/lib/index.js new file mode 100644 index 00000000..378be2ff --- /dev/null +++ b/is-primitive/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a JavaScript primitive. +* +* @module @stdlib/assert/is-primitive +* +* @example +* var isPrimitive = require( '@stdlib/assert/is-primitive' ); +* +* var bool = isPrimitive( true ); +* // returns true +* +* bool = isPrimitive( {} ); +* // returns false +*/ + +// MODULES // + +var isPrimitive = require( './main.js' ); + + +// EXPORTS // + +module.exports = isPrimitive; diff --git a/is-primitive/lib/main.js b/is-primitive/lib/main.js new file mode 100644 index 00000000..e8f5115b --- /dev/null +++ b/is-primitive/lib/main.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Tests if a value is a JavaScript primitive. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a JavaScript primitive +* +* @example +* var bool = isPrimitive( true ); +* // returns true +* +* @example +* var bool = isPrimitive( {} ); +* // returns false +*/ +function isPrimitive( value ) { + var type = typeof value; + return ( + type === 'string' || + type === 'number' || + type === 'boolean' || + type === 'symbol' || // ES6/ES2015 + type === 'bigint' || // ES11/ES2020 + value === null || + value === void 0 + ); +} + + +// EXPORTS // + +module.exports = isPrimitive; diff --git a/is-primitive/package.json b/is-primitive/package.json new file mode 100644 index 00000000..f8e0c247 --- /dev/null +++ b/is-primitive/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-primitive", + "version": "0.0.0", + "description": "Test if a value is a JavaScript primitive.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "primitive", + "string", + "number", + "null", + "undefined", + "boolean", + "symbol", + "is", + "isprimitive", + "type", + "check", + "validate", + "test" + ] +} diff --git a/is-primitive/test/test.js b/is-primitive/test/test.js new file mode 100644 index 00000000..3542c17b --- /dev/null +++ b/is-primitive/test/test.js @@ -0,0 +1,100 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var Number = require( '@stdlib/number/ctor' ); +var BigInt = require( '@stdlib/bigint/ctor' ); +var isPrimitive = require( './../lib' ); + + +// VARIABLES // + +var hasSymbols = hasSymbolSupport(); +var hasBigInts = hasBigIntSupport(); +var opts; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPrimitive, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive (ES5)', function test( t ) { + var values; + var i; + + values = [ + '', + 0, + false, + void 0, + null + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPrimitive( values[i] ), true, 'returns true' ); + } + t.end(); +}); + +opts = { + 'skip': !hasSymbols +}; +tape( 'the function returns `true` if provided a `Symbol` (ES6/ES2015)', opts, function test( t ) { + t.equal( isPrimitive( Symbol( 'symbol' ) ), true, 'returns true' ); + t.end(); +}); + +opts = { + 'skip': !hasBigInts +}; +tape( 'the function returns `true` if provided a `BigInt` (ES2020)', opts, function test( t ) { + t.equal( isPrimitive( BigInt( '1' ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a primitive', function test( t ) { + var values; + var i; + + values = [ + new Boolean(), // eslint-disable-line no-new-wrappers + new String(), // eslint-disable-line no-new-wrappers + new Array(), // eslint-disable-line no-array-constructor + new Object(), // eslint-disable-line no-new-object + new Number(), // eslint-disable-line no-new-wrappers + function noop() {}, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPrimitive( values[i] ), false, 'returns false for value: '+values[i] ); + } + t.end(); +}); diff --git a/is-prng-like/README.md b/is-prng-like/README.md new file mode 100644 index 00000000..fcce5472 --- /dev/null +++ b/is-prng-like/README.md @@ -0,0 +1,89 @@ + + +# isPRNGLike + +> Test if a value is PRNG-like. + +
+ +## Usage + +```javascript +var isPRNGLike = require( '@stdlib/assert/is-prng-like' ); +``` + +#### isPRNGLike( value ) + +Tests if a value is PRNG-like. + +```javascript +var randu = require( '@stdlib/random/base/randu' ); + +var bool = isPRNGLike( randu ); +// returns true +``` + +
+ + + +
+ +## Notes + +- The function is **not** rigorous and only checks for the existence of particular properties which **should** be bound to a seedable pseudorandom number generator (PRNG) function. The function's main use case is for testing that a provided `value` (loosely) conforms to a particular interface. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var isPRNGLike = require( '@stdlib/assert/is-prng-like' ); + +var bool = isPRNGLike( randu ); +// returns true + +bool = isPRNGLike( [ 1, 2, 3, 4 ] ); +// returns false + +bool = isPRNGLike( {} ); +// returns false + +bool = isPRNGLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-prng-like/benchmark/benchmark.js b/is-prng-like/benchmark/benchmark.js new file mode 100644 index 00000000..df87ee00 --- /dev/null +++ b/is-prng-like/benchmark/benchmark.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var isPRNGLike = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + randu, + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPRNGLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-prng-like/docs/repl.txt b/is-prng-like/docs/repl.txt new file mode 100644 index 00000000..7962a369 --- /dev/null +++ b/is-prng-like/docs/repl.txt @@ -0,0 +1,28 @@ + +{{alias}}( value ) + Tests if a value is PRNG-like. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is PRNG-like. + + Examples + -------- + > var bool = {{alias}}( {{alias:@stdlib/random/base/randu}} ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-prng-like/docs/types/index.d.ts b/is-prng-like/docs/types/index.d.ts new file mode 100644 index 00000000..958e1a32 --- /dev/null +++ b/is-prng-like/docs/types/index.d.ts @@ -0,0 +1,41 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is PRNG-like. +* +* @param v - value to test +* @returns boolean indicating if a value is PRNG-like +* +* @example +* var randu = require( `@stdlib/random/base/randu` ); +* +* var bool = isPRNGLike( randu ); +* // returns true +* +* bool = isPRNGLike( [] ); +* // returns false +*/ +declare function isPRNGLike( v: any ): boolean; + + +// EXPORTS // + +export = isPRNGLike; diff --git a/is-prng-like/docs/types/test.ts b/is-prng-like/docs/types/test.ts new file mode 100644 index 00000000..e2322f8d --- /dev/null +++ b/is-prng-like/docs/types/test.ts @@ -0,0 +1,35 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isPRNGLike = require( './index' ); +import randu = require( '@stdlib/random/base/randu' ); + + +// TESTS // + +// The function returns a boolean... +{ + isPRNGLike( randu ); // $ExpectType boolean + isPRNGLike( {} ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isPRNGLike(); // $ExpectError + isPRNGLike( {}, 123 ); // $ExpectError +} diff --git a/is-prng-like/docs/types/tsconfig.json b/is-prng-like/docs/types/tsconfig.json new file mode 100644 index 00000000..a541d610 --- /dev/null +++ b/is-prng-like/docs/types/tsconfig.json @@ -0,0 +1,39 @@ +{ + "compilerOptions": { + "allowSyntheticDefaultImports": false, + "allowUnreachableCode": false, + "allowUnusedLabels": false, + "alwaysStrict": true, + "baseUrl": ".", + "charset": "utf8", + "forceConsistentCasingInFileNames": true, + "keyofStringsOnly": false, + "lib": [ + "es6" + ], + "locale": "en", + "module": "commonjs", + "moduleResolution": "node", + "newLine": "lf", + "noEmit": true, + "noFallthroughCasesInSwitch": true, + "noImplicitAny": true, + "noImplicitReturns": false, + "noImplicitThis": true, + "noStrictGenericChecks": false, + "noUnusedLocals": true, + "noUnusedParameters": true, + "paths": {}, + "pretty": true, + "strictBindCallApply": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "suppressExcessPropertyErrors": false, + "suppressImplicitAnyIndexErrors": false, + "typeRoots": [ "." ], + "types": [] + }, + "exclude": [ + "node_modules" + ] +} diff --git a/is-prng-like/docs/types/tslint.json b/is-prng-like/docs/types/tslint.json new file mode 100644 index 00000000..a0c360d8 --- /dev/null +++ b/is-prng-like/docs/types/tslint.json @@ -0,0 +1,252 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "expect": true, + "export-just-namespace": true, + "no-any-union": true, + "no-bad-reference": true, + "no-const-enum": true, + "no-dead-reference": true, + "no-declare-current-package": true, + "no-import-default-of-export-equals": true, + "no-outside-dependencies": true, + "no-padding": true, + "no-redundant-undefined": true, + "no-relative-import-in-test": false, + "no-self-import": true, + "no-single-declare-module": true, + "no-single-element-tuple-type": true, + "no-unnecessary-generics": true, + "no-useless-files": true, + "prefer-declare-function": true, + "strict-export-declare-modifiers": true, + "trim-file": true, + "unified-signatures": true, + "void-return": true, + + "adjacent-overload-signatures": true, + "ban-ts-ignore": false, + "ban-types": false, + "member-access": [ true, "no-public" ], + "member-ordering": [ true, "fields-first" ], + "no-any": false, + "no-empty-interface": true, + "no-import-side-effect": true, + "no-inferrable-types": true, + "no-internal-module": true, + "no-magic-numbers": false, + "no-namespace": true, + "no-non-null-assertion": true, + "no-parameter-reassignment": false, + "no-reference": true, + "no-unnecessary-type-assertion": true, + "no-var-requires": true, + "only-arrow-functions": false, + "prefer-for-of": false, + "promise-function-async": false, + "typedef": true, + "typedef-whitespace": [ true, + { + "call-signature": "nospace", + "index-signature": "nospace", + "parameter": "nospace", + "property-declaration": "nospace", + "variable-declaration": "nospace" + }, + { + "call-signature": "onespace", + "index-signature": "onespace", + "parameter": "onespace", + "property-declaration": "onespace", + "variable-declaration": "onespace" + } + ], + "unified-signatures": true, + + "await-promise": true, + "ban-comma-operator": true, + "ban": [ true, + { + "name": [ "*", "forEach" ], + "message": "Use a regular `for` loop instead." + } + ], + "curly": true, + "forin": true, + "function-constructor": true, + "import-blacklist": [ true, "lodash" ], + "label-position": true, + "no-arg": true, + "no-bitwise": false, + "no-conditional-assignment": true, + "no-console": true, + "no-construct": true, + "no-debugger": true, + "no-duplicate-super": true, + "no-duplicate-switch-case": true, + "no-duplicate-variable": [ true, "check-parameters" ], + "no-dynamic-delete": false, + "no-empty": true, + "no-eval": true, + "no-floating-promises": true, + "no-for-in-array": true, + "no-implicit-dependencies": false, + "no-inferred-empty-object-type": true, + "no-invalid-template-strings": true, + "no-invalid-this": true, + "no-misused-new": true, + "no-null-keyword": false, + "no-object-literal-type-assertion": true, + "no-return-await": true, + "no-shadowed-variable": false, + "no-sparse-arrays": true, + "no-string-literal": false, + "no-string-throw": true, + "no-submodule-imports": false, + "no-switch-case-fall-through": true, + "no-this-assignment": [ true, + { + "allowed-names": [ "^self$" ], + "allow-destructuring": true + } + ], + "no-unbound-method": [ true, "ignore-static" ], + "no-unnecessary-class": true, + "no-unsafe-any": true, + "no-unsafe-finally": true, + "no-unused-expression": true, + "no-use-before-declare": true, + "no-var-keyword": true, + "no-void-expression": true, + "prefer-conditional-expression": false, + "prefer-object-spread": false, + "radix": true, + "restrict-plus-operands": true, + "strict-boolean-expressions": false, + "strict-type-predicates": true, + "switch-default": true, + "triple-equals": true, + "unnecessary-constructor": true, + "use-default-type-parameter": true, + "use-isnan": true, + + "cyclomatic-complexity": false, + "deprecation": true, + "eofline": true, + "indent": [ true, "tabs", 4 ], + "linebreak-style": [ true, "LF" ], + "max-classes-per-file": [ true, 1 ], + "max-file-line-count": [ true, 1000 ], + "max-line-length": [ true, + { + "limit": 80, + "ignore-pattern": "^import |\\/\\/ |\\/?\\* " + } + ], + "no-default-export": false, + "no-default-import": false, + "no-duplicate-imports": true, + "no-mergeable-namespace": true, + "no-require-imports": false, + "object-literal-sort-keys": false, + "prefer-const": true, + "prefer-readonly": true, + "trailing-comma": [ false, + { + "esSpecCompliant": true + } + ], + + "align": false, + "array-type": [ true, "generic" ], + "arrow-parens": true, + "arrow-return-shorthand": true, + "binary-expression-operand-order": true, + "callable-types": true, + "class-name": true, + "comment-format": [ true, "check-space" ], + "comment-type": false, + "completed-docs": true, + "encoding": true, + "file-header": false, + "file-name-casing": [ true, "snake-case" ], + "import-spacing": true, + "increment-decrement": true, + "interface-name": [ true, "never-prefix" ], + "interface-over-type-literal": true, + "jsdoc-format": false, + "match-default-export-name": false, + "newline-before-return": false, + "newline-per-chained-call": false, + "new-parens": true, + "no-angle-bracket-type-assertion": true, + "no-boolean-literal-compare": false, + "no-consecutive-blank-lines": [ true, 2 ], + "no-irregular-whitespace": true, + "no-parameter-properties": true, + "no-redundant-jsdoc": true, + "no-reference-import": true, + "no-trailing-whitespace": true, + "no-unnecessary-callback-wrapper": true, + "no-unnecessary-initializer": true, + "no-unnecessary-qualifier": true, + "number-literal-format": false, + "object-literal-key-quotes": [ true, "always" ], + "object-literal-shorthand": [ true, "never" ], + "one-line": [ true, + "check-catch", + "check-finally", + "check-else", + "check-open-brace", + "check-whitespace" + ], + "one-variable-per-declaration": true, + "ordered-imports": [ true, + { + "grouped-imports": false, + "import-sources-order": "any", + "named-imports-order": "any", + "module-source-path": "full" + } + ], + "prefer-function-over-method": [ true, "allow-public", "allow-protected" ], + "prefer-method-signature": true, + "prefer-switch": [ true, + { + "min-cases": 5 + } + ], + "prefer-template": false, + "prefer-while": true, + "quotemark": [ true, "single", "avoid-escape", "avoid-template" ], + "return-undefined": false, + "semicolon": [ true, "always" ], + "space-before-function-paren": [ true, + { + "anonymous": "always", + "named": "never", + "asyncArrow": "always", + "method": "never", + "constructor": "never" + } + ], + "space-within-parens": [ true, 1 ], + "switch-final-break": [ true, "always" ], + "type-literal-delimiter": [ true, + { + "singleLine": "always" + } + ], + "unnecessary-bind": true, + "variable-name": [ true, "ban-keywords", "check-format" ], + "whitespace": [ true, + "check-branch", + "check-decl", + "check-operator", + "check-rest-spread", + "check-separator", + "check-type", + "check-preblock" + ] + } +} diff --git a/is-prng-like/examples/index.js b/is-prng-like/examples/index.js new file mode 100644 index 00000000..a31cf290 --- /dev/null +++ b/is-prng-like/examples/index.js @@ -0,0 +1,34 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var randu = require( '@stdlib/random/base/randu' ); +var isPRNGLike = require( './../lib' ); + +console.log( isPRNGLike( randu ) ); +// => true + +console.log( isPRNGLike( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isPRNGLike( {} ) ); +// => false + +console.log( isPRNGLike( null ) ); +// => false diff --git a/is-prng-like/lib/index.js b/is-prng-like/lib/index.js new file mode 100644 index 00000000..6cfb9087 --- /dev/null +++ b/is-prng-like/lib/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is PRNG-like. +* +* @module @stdlib/assert/is-prng-like +* +* @example +* var randu = require( '@stdlib/random/base/randu' ); +* var isPRNGLike = require( '@stdlib/assert/is-prng-like' ); +* +* var bool = isPRNGLike( randu ); +* // returns true +* +* bool = isPRNGLike( [] ); +* // returns false +*/ + +// MODULES // + +var isPRNGLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isPRNGLike; diff --git a/is-prng-like/lib/main.js b/is-prng-like/lib/main.js new file mode 100644 index 00000000..f3587fec --- /dev/null +++ b/is-prng-like/lib/main.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); + + +// MAIN // + +/** +* Tests if a value is PRNG-like. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is PRNG-like +* +* @example +* var randu = require( '@stdlib/random/base/randu' ); +* +* var bool = isPRNGLike( randu ); +* // returns true +* +* bool = isPRNGLike( [] ); +* // returns false +*/ +function isPRNGLike( v ) { + return ( + isFunction( v ) && + typeof v.NAME === 'string' && + hasOwnProp( v, 'state' ) && + hasOwnProp( v, 'seed' ) + ); +} + + +// EXPORTS // + +module.exports = isPRNGLike; diff --git a/is-prng-like/package.json b/is-prng-like/package.json new file mode 100644 index 00000000..a54ccc3d --- /dev/null +++ b/is-prng-like/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-prng-like", + "version": "0.0.0", + "description": "Test if a value is PRNG-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isprng", + "isprnglike", + "prng", + "pseudorandom", + "number", + "generator", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-prng-like/test/test.js b/is-prng-like/test/test.js new file mode 100644 index 00000000..64e25519 --- /dev/null +++ b/is-prng-like/test/test.js @@ -0,0 +1,75 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var randu = require( '@stdlib/random/base/randu' ); +var noop = require( '@stdlib/utils/noop' ); +var isPRNGLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPRNGLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a PRNG', function test( t ) { + t.equal( isPRNGLike( randu ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a PRNG-like function', function test( t ) { + var rand; + + rand = noop; + rand.NAME = 'super-random'; + rand.state = null; + rand.seed = null; + + t.equal( isPRNGLike( rand ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a PRNG-like value', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isPRNGLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-probability-array/README.md b/is-probability-array/README.md new file mode 100644 index 00000000..f709a9cb --- /dev/null +++ b/is-probability-array/README.md @@ -0,0 +1,141 @@ + + +# isProbabilityArray + +> Test if a value is an array-like object containing only probabilities. + +
+ +A **probability** is defined as a numeric value on the interval `[0,1]`. + +
+ + + +
+ +## Usage + +```javascript +var isProbabilityArray = require( '@stdlib/assert/is-probability-array' ); +``` + +#### isProbabilityArray( value ) + +Tests if a `value` is an array-like object containing **only** probabilities. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var Uint8Array = require( '@stdlib/array/uint8' ); + +var bool = isProbabilityArray( [ 0.6, 0.5, 0.25 ] ); +// returns true + +bool = isProbabilityArray( [ 0.6, 0.5, new Number( 0.25 ) ] ); +// returns true + +bool = isProbabilityArray( new Uint8Array( [ 0, 1 ] ) ); +// returns true + +bool = isProbabilityArray( [ 3.14, 0.0 ] ); +// returns false +``` + +#### isProbabilityArray.primitives( value ) + +Tests if a `value` is an array-like object `array` containing **only** primitive probabilities. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isProbabilityArray.primitives( [ 1.0, 0.0, 0.8 ] ); +// returns true + +bool = isProbabilityArray.primitives( [ 0.9, new Number(1.0) ] ); +// returns false +``` + +#### isProbabilityArray.objects( value ) + +Tests if a `value` is an array-like object `array` containing **only** `Number` objects whose values are probabilities. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isProbabilityArray.objects( [ new Number(1.0), new Number(1.0) ] ); +// returns true + +bool = isProbabilityArray.objects( [ 1.0, 0.0, 0.6 ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Uint8Array = require( '@stdlib/array/uint8' ); +var isProbabilityArray = require( '@stdlib/assert/is-probability-array' ); + +var arr = [ 0.0, 1.0, 0.5 ]; +var bool = isProbabilityArray( arr ); +// returns true + +arr = [ 0.5, 0.25, 0.25 ]; +bool = isProbabilityArray( arr ); +// returns true + +arr = new Uint8Array( [ 0, 0, 1, 0, 1 ] ); +bool = isProbabilityArray( arr ); +// returns true + +arr = [ 3.14, -1.0 ]; +bool = isProbabilityArray( arr ); +// returns false + +bool = isProbabilityArray( [] ); +// returns false + +bool = isProbabilityArray( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-probability-array/benchmark/benchmark.js b/is-probability-array/benchmark/benchmark.js new file mode 100644 index 00000000..ea461c94 --- /dev/null +++ b/is-probability-array/benchmark/benchmark.js @@ -0,0 +1,120 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var randu = require( '@stdlib/random/base/randu' ); +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isProbabilityArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( randu() ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( randu() ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isProbabilityArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isProbabilityArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isProbabilityArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-probability-array/docs/repl.txt b/is-probability-array/docs/repl.txt new file mode 100644 index 00000000..514ae5c0 --- /dev/null +++ b/is-probability-array/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only probabilities. + + A probability is defined as a numeric value on the interval [0,1]. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + probabilities. + + Examples + -------- + > var bool = {{alias}}( [ 0.5, new Number(0.8) ] ) + true + > bool = {{alias}}( [ 0.8, 1.2 ] ) + false + > bool = {{alias}}( [ 0.8, '0.2' ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only primitive + probabilities. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + primitive probabilities. + + Examples + -------- + > var bool = {{alias}}.primitives( [ 1.0, 0.0, 0.5 ] ) + true + > bool = {{alias}}.primitives( [ 0.3, new Number(0.4) ] ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only number objects + having probability values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + number objects having probability values. + + Examples + -------- + > var bool = {{alias}}.objects( [ new Number(0.7), new Number(1.0) ] ) + true + > bool = {{alias}}.objects( [ 1.0, 0.0, new Number(0.7) ] ) + false + + See Also + -------- + diff --git a/is-probability-array/docs/types/index.d.ts b/is-probability-array/docs/types/index.d.ts new file mode 100644 index 00000000..1124b65d --- /dev/null +++ b/is-probability-array/docs/types/index.d.ts @@ -0,0 +1,101 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isProbabilityArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsProbabilityArray { + /** + * Test if a value is an array-like object containing only probabilities. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only probabilities + * + * @example + * var bool = isProbabilityArray( [ 0.3, new Number(0.3) ] ); + * // returns true + * + * @example + * var bool = isProbabilityArray( [ 0.3, '0.3' ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only primitive probabilities. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only primitive probabilities + * + * @example + * var bool = isProbabilityArray.primitives( [ 0.1, 0.2, 0.1 ] ); + * // returns true + * + * @example + * var bool = isProbabilityArray.primitives( [ 0.3, new Number(0.1) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects having probability values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number objects having probability values + * + * @example + * var bool = isProbabilityArray.objects( [ new Number(0.3), new Number(0.1) ] ); + * // returns true + * + * @example + * var bool = isProbabilityArray.objects( [ 0.1, 0.2, 0.3 ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Test if a value is an array-like object containing only probabilities. +* +* @param value - value to test +* @returns boolean indicating whether value is a positive integer +* +* @example +* var bool = isProbabilityArray( [ 0.3, new Number(0.3) ] ); +* // returns true +* +* @example +* var bool = isProbabilityArray( [ 0.3, '0.3' ] ); +* // returns false +* +* @example +* var bool = isProbabilityArray.primitives( [ 0.1, 0.2, 0.3 ] ); +* // returns true +* +* @example +* var bool = isProbabilityArray.objects( [ new Number(0.3), new Number(0.9) ] ); +* // returns true +*/ +declare var isProbabilityArray: IsProbabilityArray; + + +// EXPORTS // + +export = isProbabilityArray; diff --git a/is-probability-array/docs/types/test.ts b/is-probability-array/docs/types/test.ts new file mode 100644 index 00000000..0253a44b --- /dev/null +++ b/is-probability-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isProbabilityArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isProbabilityArray( [ 0.5 ] ); // $ExpectType boolean + isProbabilityArray( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isProbabilityArray(); // $ExpectError + isProbabilityArray( [ 0.2 ], 10.23 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isProbabilityArray.primitives( [ new Number( 0.2 ) ] ); // $ExpectType boolean + isProbabilityArray.primitives( [ 0.2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isProbabilityArray.primitives(); // $ExpectError + isProbabilityArray.primitives( [ 0.2 ], 123 ); // $ExpectError +} + + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isProbabilityArray.objects( [ new Number( 0.2 ) ] ); // $ExpectType boolean + isProbabilityArray.objects( [ 0.2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isProbabilityArray.objects(); // $ExpectError + isProbabilityArray.objects( [ 0.2 ], 123 ); // $ExpectError +} diff --git a/is-probability-array/examples/index.js b/is-probability-array/examples/index.js new file mode 100644 index 00000000..ea1c5d2c --- /dev/null +++ b/is-probability-array/examples/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Uint8Array = require( '@stdlib/array/uint8' ); +var isProbabilityArray = require( './../lib' ); + +var arr = [ 0.0, 1.0, 0.5 ]; +console.log( isProbabilityArray( arr ) ); +// => true + +arr = [ 0.5, 0.25, 0.25 ]; +console.log( isProbabilityArray( arr ) ); +// => true + +arr = new Uint8Array( [ 0, 0, 1, 0, 1 ] ); +console.log( isProbabilityArray( arr ) ); +// => true + +arr = [ 3.14, -1.0 ]; +console.log( isProbabilityArray( arr ) ); +// => false + +console.log( isProbabilityArray( [] ) ); +// => false + +console.log( isProbabilityArray( null ) ); +// => false diff --git a/is-probability-array/lib/index.js b/is-probability-array/lib/index.js new file mode 100644 index 00000000..72b776f3 --- /dev/null +++ b/is-probability-array/lib/index.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only probabilities. +* +* @module @stdlib/assert/is-probability-array +* +* @example +* var isNonNegativeNumberArray = require( '@stdlib/assert/is-probability-array' ); +* +* var bool = isProbabilityArray( [ 0.5, new Number(0.8) ] ); +* // returns true +* +* bool = isProbabilityArray( [ 0.8, 1.2 ] ); +* // returns false +* +* bool = isProbabilityArray( [ 0.8, '0.2' ] ); +* // returns false +* +* @example +* var isProbabilityArray = require( '@stdlib/assert/is-probability-array' ).primitives; +* +* var bool = isProbabilityArray( [ 1.0, 0.0, 0.5 ] ); +* // returns true +* +* bool = isProbabilityArray( [ 0.3, new Number(0.4) ] ); +* // returns false +* +* @example +* var isProbabilityArray = require( '@stdlib/assert/is-probability-array' ).objects; +* +* var bool = isProbabilityArray( [ new Number(0.7), new Number(1.0) ] ); +* // returns true +* +* bool = isProbabilityArray( [ 1.0, 0.0, new Number(0.7) ] ); +* // returns false +*/ + +// MODULES // + +var isProbability = require( '@stdlib/assert/is-probability' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); + + +// MAIN // + +var isProbabilityArray = arrayfun( isProbability ); +setReadOnly( isProbabilityArray, 'primitives', arrayfun( isProbability.isPrimitive ) ); +setReadOnly( isProbabilityArray, 'objects', arrayfun( isProbability.isObject ) ); + + +// EXPORTS // + +module.exports = isProbabilityArray; diff --git a/is-probability-array/package.json b/is-probability-array/package.json new file mode 100644 index 00000000..d01d8908 --- /dev/null +++ b/is-probability-array/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-probability-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only probabilities.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "probability", + "probs", + "prob", + "is", + "isarray", + "numeric", + "number", + "type", + "check", + "valid", + "validate", + "isvalid", + "test" + ] +} diff --git a/is-probability-array/test/test.js b/is-probability-array/test/test.js new file mode 100644 index 00000000..fa5e86bb --- /dev/null +++ b/is-probability-array/test/test.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Number = require( '@stdlib/number/ctor' ); +var isProbabilityArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isProbabilityArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only probabilities', function test( t ) { + var arr; + + arr = [ 0.9, new Number( 0.8 ), 0.0 ]; // eslint-disable-line no-new-wrappers + t.equal( isProbabilityArray( arr ), true, 'returns true' ); + + arr = new Float64Array( [ 0.9, 0.5, 0.3 ] ); + t.equal( isProbabilityArray( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': 0.3, + '1': 0.8 + }; + t.equal( isProbabilityArray( arr ), true, 'returns true' ); + + arr = [ 0.9, '3', null ]; + t.equal( isProbabilityArray( arr ), false, 'returns false' ); + + arr = new Float64Array( [ 0.9, NaN, 0.3 ] ); + t.equal( isProbabilityArray( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only primitive probabilities', function test( t ) { + var arr; + + arr = [ 1.0, 0.0 ]; + t.equal( isProbabilityArray.primitives( arr ), true, 'returns true' ); + + arr = new Float64Array( [ 0.9, 0.5, 0.3 ] ); + t.equal( isProbabilityArray( arr ), true, 'returns true' ); + + arr = [ new Number( 5 ), 1.0, 1.0 ]; // eslint-disable-line no-new-wrappers + t.equal( isProbabilityArray.primitives( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only containing only Number objects whose values are probabilities', function test( t ) { + var arr; + + arr = [ new Number( 0.5 ), new Number( 0.5 ) ]; // eslint-disable-line no-new-wrappers + t.equal( isProbabilityArray.objects( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': new Number( 0.3 ), // eslint-disable-line no-new-wrappers + '1': new Number( 0.8 ) // eslint-disable-line no-new-wrappers + }; + t.equal( isProbabilityArray( arr ), true, 'returns true' ); + + arr = [ 0.5, 0.0 ]; + t.equal( isProbabilityArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-probability/README.md b/is-probability/README.md new file mode 100644 index 00000000..c5a53f32 --- /dev/null +++ b/is-probability/README.md @@ -0,0 +1,153 @@ + + +# isProbability + +> Test if a value is a probability. + +
+ +A **probability** is defined as a numeric value on the interval `[0,1]`. + +
+ + + +
+ +## Usage + +```javascript +var isProbability = require( '@stdlib/assert/is-probability' ); +``` + +#### isProbability( value ) + +Tests if a `value` is a probability. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isProbability( 0.5 ); +// returns true + +bool = isProbability( new Number( 0.5 ) ); +// returns true + +bool = isProbability( 3.14 ); +// returns false + +bool = isProbability( -5.0 ); +// returns false + +bool = isProbability( NaN ); +// returns false + +bool = isProbability( null ); +// returns false +``` + +#### isProbability.isPrimitive( value ) + +Tests if a `value` is a primitive probability. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isProbability.isPrimitive( 0.33 ); +// returns true + +bool = isProbability.isPrimitive( new Number( 0.33 ) ); +// returns false +``` + +#### isProbability.isObject( value ) + +Tests if a `value` is a `Number` object having a value which is a probability. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isProbability.isObject( 0.11 ); +// returns false + +bool = isProbability.isObject( new Number( 0.11 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isProbability = require( '@stdlib/assert/is-probability' ); + +var bool = isProbability( 0.5 ); +// returns true + +bool = isProbability( new Number( 0.5 ) ); +// returns true + +bool = isProbability( 0.0 ); +// returns true + +bool = isProbability( 1.0 ); +// returns true + +bool = isProbability( 3.14 ); +// returns false + +bool = isProbability( -5.0 ); +// returns false + +bool = isProbability( NaN ); +// returns false + +bool = isProbability( '0.5' ); +// returns false + +bool = isProbability( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-probability/benchmark/benchmark.js b/is-probability/benchmark/benchmark.js new file mode 100644 index 00000000..2c379959 --- /dev/null +++ b/is-probability/benchmark/benchmark.js @@ -0,0 +1,239 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isProbability = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 0.5, + 1.0, + 0.0, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isProbability( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isProbability( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isProbability.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isProbability.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isProbability.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isProbability.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-probability/docs/repl.txt b/is-probability/docs/repl.txt new file mode 100644 index 00000000..66b52b24 --- /dev/null +++ b/is-probability/docs/repl.txt @@ -0,0 +1,77 @@ + +{{alias}}( value ) + Tests if a value is a probability. + + A probability is defined as a numeric value on the interval [0,1]. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a probability. + + Examples + -------- + > var bool = {{alias}}( 0.5 ) + true + > bool = {{alias}}( new Number( 0.5 ) ) + true + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( -5.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive which is a probability. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive which is a + probability. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 0.3 ) + true + > bool = {{alias}}.isPrimitive( new Number( 0.3 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a value which is a probability. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a value which + is a probability. + + Examples + -------- + > var bool = {{alias}}.isObject( 0.77 ) + false + > bool = {{alias}}.isObject( new Number( 0.77 ) ) + true + + + See Also + -------- + diff --git a/is-probability/docs/types/index.d.ts b/is-probability/docs/types/index.d.ts new file mode 100644 index 00000000..254410c9 --- /dev/null +++ b/is-probability/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isProbability` with methods for testing for primitives and objects, respectively. +*/ +interface IsProbability { + /** + * Tests if a value is a probability. + * + * @param value - value to test + * @returns boolean indicating whether value is a probability + * + * @example + * var bool = isProbability( 0.5 ); + * // returns true + * + * @example + * var bool = isProbability( new Number( 0.5 ) ); + * // returns true + * + * @example + * var bool = isProbability( 3.14 ); + * // returns false + * + * @example + * var bool = isProbability( -5.0 ); + * // returns false + * + * @example + * var bool = isProbability( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a value which is a probability. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a value which is a probability + * + * @example + * var bool = isProbability.isPrimitive( 0.66 ); + * // returns true + * + * @example + * var bool = isProbability.isPrimitive( new Number( 0.66 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a value which is a probability. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a value which is a probability + * + * @example + * var bool = isProbability.isObject( 0.5 ); + * // returns false + * + * @example + * var bool = isProbability.isObject( new Number( 0.5 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a probability. +* +* @param value - value to test +* @returns boolean indicating whether value is a probability +* +* @example +* var bool = isProbability( 0.5 ); +* // returns true +* +* @example +* var bool = isProbability( new Number( 0.5 ) ); +* // returns true +* +* @example +* var bool = isProbability( 3.14 ); +* // returns false +* +* @example +* var bool = isProbability( -5.0 ); +* // returns false +* +* @example +* var bool = isProbability( null ); +* // returns false +* +* @example +* var bool = isProbability.isPrimitive( 0.66 ); +* // returns true +* +* @example +* var bool = isProbability.isObject( new Number( 0.5 ) ); +* // returns true +*/ +declare var isProbability: IsProbability; + + +// EXPORTS // + +export = isProbability; diff --git a/is-probability/docs/types/test.ts b/is-probability/docs/types/test.ts new file mode 100644 index 00000000..11865f49 --- /dev/null +++ b/is-probability/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isProbability = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isProbability( 3 ); // $ExpectType boolean + isProbability( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isProbability(); // $ExpectError + isProbability( 0.2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isProbability.isPrimitive( new Number( 0.2 ) ); // $ExpectType boolean + isProbability.isPrimitive( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isProbability.isPrimitive(); // $ExpectError + isProbability.isPrimitive( 0.2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isProbability.isObject( new Number( 0.2 ) ); // $ExpectType boolean + isProbability.isObject( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isProbability.isObject(); // $ExpectError + isProbability.isObject( 0.2, 123 ); // $ExpectError +} diff --git a/is-probability/examples/index.js b/is-probability/examples/index.js new file mode 100644 index 00000000..e29390d2 --- /dev/null +++ b/is-probability/examples/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isProbability = require( './../lib' ); + +console.log( isProbability( 0.5 ) ); +// => true + +console.log( isProbability( new Number( 0.5 ) ) ); +// => true + +console.log( isProbability( 0.0 ) ); +// => true + +console.log( isProbability( 1.0 ) ); +// => true + +console.log( isProbability( 3.14 ) ); +// => false + +console.log( isProbability( -5.0 ) ); +// => false + +console.log( isProbability( NaN ) ); +// => false + +console.log( isProbability( '0.5' ) ); +// => false + +console.log( isProbability( null ) ); +// => false diff --git a/is-probability/lib/index.js b/is-probability/lib/index.js new file mode 100644 index 00000000..687fa3fe --- /dev/null +++ b/is-probability/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a probability. +* +* @module @stdlib/assert/is-probability +* +* @example +* var isProbability = require( '@stdlib/assert/is-probability' ); +* +* var bool = isProbability( 0.5 ); +* // returns true +* +* bool = isProbability( new Number( 0.5 ) ); +* // returns true +* +* bool = isProbability( 3.14 ); +* // returns false +* +* bool = isProbability( -5.0 ); +* // returns false +* +* bool = isProbability( null ); +* // returns false +* +* @example +* var isProbability = require( '@stdlib/assert/is-probability' ).isPrimitive; +* +* var bool = isProbability( 0.3 ); +* // returns true +* +* bool = isProbability( new Number( 0.3 ) ); +* // returns false +* +* @example +* var isProbability = require( '@stdlib/assert/is-probability' ).isObject; +* +* var bool = isProbability( 0.77 ); +* // returns false +* +* bool = isProbability( new Number( 0.77 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isProbability = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isProbability, 'isPrimitive', isPrimitive ); +setReadOnly( isProbability, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isProbability; diff --git a/is-probability/lib/main.js b/is-probability/lib/main.js new file mode 100644 index 00000000..232c0c9b --- /dev/null +++ b/is-probability/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a probability. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a probability +* +* @example +* var bool = isProbability( 0.5 ); +* // returns true +* +* @example +* var bool = isProbability( new Number( 0.5 ) ); +* // returns true +* +* @example +* var bool = isProbability( 3.14 ); +* // returns false +* +* @example +* var bool = isProbability( -5.0 ); +* // returns false +* +* @example +* var bool = isProbability( null ); +* // returns false +*/ +function isProbability( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isProbability; diff --git a/is-probability/lib/object.js b/is-probability/lib/object.js new file mode 100644 index 00000000..221f97ff --- /dev/null +++ b/is-probability/lib/object.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; + + +// MAIN // + +/** +* Tests if a value is a number object having a value which is a probability. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a value which is a probability +* +* @example +* var bool = isProbability( 0.5 ); +* // returns false +* +* @example +* var bool = isProbability( new Number( 0.5 ) ); +* // returns true +*/ +function isProbability( value ) { + return ( + isNumber( value ) && + value.valueOf() >= 0.0 && + value.valueOf() <= 1.0 + ); +} + + +// EXPORTS // + +module.exports = isProbability; diff --git a/is-probability/lib/primitive.js b/is-probability/lib/primitive.js new file mode 100644 index 00000000..e579bf7e --- /dev/null +++ b/is-probability/lib/primitive.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is a number primitive having a value which is a probability. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a value which is a probability +* +* @example +* var bool = isProbability( 0.66 ); +* // returns true +* +* @example +* var bool = isProbability( new Number( 0.66 ) ); +* // returns false +*/ +function isProbability( value ) { + return ( + isNumber( value ) && + value >= 0.0 && + value <= 1.0 + ); +} + + +// EXPORTS // + +module.exports = isProbability; diff --git a/is-probability/package.json b/is-probability/package.json new file mode 100644 index 00000000..2bea5fd5 --- /dev/null +++ b/is-probability/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-probability", + "version": "0.0.0", + "description": "Test if a value is a probability.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "probability", + "prob", + "is", + "isprobability", + "isprob", + "type", + "check", + "valid", + "validate", + "test", + "primitive", + "object" + ] +} diff --git a/is-probability/test/test.js b/is-probability/test/test.js new file mode 100644 index 00000000..c7628b02 --- /dev/null +++ b/is-probability/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isProbability = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isProbability, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive probability', function test( t ) { + t.equal( typeof isProbability.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a probability value', function test( t ) { + t.equal( typeof isProbability.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-probability/test/test.main.js b/is-probability/test/test.main.js new file mode 100644 index 00000000..c03bad7f --- /dev/null +++ b/is-probability/test/test.main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isProbability = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isProbability, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a probability', function test( t ) { + t.equal( isProbability( 0.5 ), true, 'returns true' ); + t.equal( isProbability( new Number( 0.5 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a probability', function test( t ) { + var values; + var i; + + values = [ + '5', + -3.14, + -1.0, + NaN, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isProbability( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-probability/test/test.object.js b/is-probability/test/test.object.js new file mode 100644 index 00000000..910a1dbc --- /dev/null +++ b/is-probability/test/test.object.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isProbability = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isProbability, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a probability value', function test( t ) { + t.equal( isProbability( new Number( 0.33 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive probability', function test( t ) { + t.equal( isProbability( 0.33 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + NaN, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isProbability( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-probability/test/test.primitive.js b/is-probability/test/test.primitive.js new file mode 100644 index 00000000..5d5c7fb9 --- /dev/null +++ b/is-probability/test/test.primitive.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isProbability = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isProbability, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive probability', function test( t ) { + t.equal( isProbability( 0.11 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object', function test( t ) { + t.equal( isProbability( new Number( 0.11 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + NaN, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isProbability( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-prototype-of/README.md b/is-prototype-of/README.md new file mode 100644 index 00000000..a2642af9 --- /dev/null +++ b/is-prototype-of/README.md @@ -0,0 +1,218 @@ + + +# isPrototypeOf + +> Test if an object's prototype chain contains a provided prototype. + +
+ +
+ + + +
+ +## Usage + + + +```javascript +var isPrototypeOf = require( '@stdlib/assert/is-prototype-of' ); +``` + +#### isPrototypeOf( obj, prototype ) + +Tests if an `object`'s prototype chain contains a provided `prototype`. + + + +```javascript +var inherit = require( '@stdlib/utils/inherit' ); + +function Foo() { + return this; +} + +function Bar() { + return this; +} +inherit( Bar, Foo ); + +var bar = new Bar(); + +var bool = isPrototypeOf( bar, Foo.prototype ); +// returns true +``` + +
+ + + +
+ +## Notes + +- The function returns `false` if provided a primitive value. + + + + ```javascript + var Number = require( '@stdlib/number/ctor' ); + + var bool = isPrototypeOf( 5, Number.prototype ); + // returns false + + bool = isPrototypeOf( 'beep', String.prototype ); + // returns false + + bool = isPrototypeOf( true, Boolean.prototype ); + // returns false + ``` + +- The function throws a `TypeError` if provided a `prototype` value which is neither an `object` (except `null`) or a `function`. + + + + ```javascript + var bool = isPrototypeOf( Object.create( null ), null ); + // throws + ``` + +- `isPrototypeOf()` is generally more robust than the `instanceof` operator. Consider the following example which does not use constructors: + + + + ```javascript + // Functionally similar to `Object.create()`... + function createObject( proto ) { + function Ctor() { + // Empty constructor... + } + Ctor.prototype = proto; + return new Ctor(); + } + var superProto = { + 'beep': 'beep' + }; + + var subProto = createObject( superProto ); + subProto.boop = 'boop'; + + var v = createObject( subProto ); + + var bool; + try { + bool = ( v instanceof superProto ); + } catch ( error ) { + // Encountered a type error... + console.error( error.message ); + } + + bool = isPrototypeOf( v, superProto ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var inherit = require( '@stdlib/utils/inherit' ); +var isPrototypeOf = require( '@stdlib/assert/is-prototype-of' ); + +function A() { + return this; +} + +function B() { + return this; +} +inherit( B, A ); + +function C() { + return this; +} +inherit( C, B ); + +function D() { + return this; +} +inherit( D, C ); + +var a = new A(); +var b = new B(); +var c = new C(); +var d = new D(); + +var bool = isPrototypeOf( d, C.prototype ); +// returns true + +bool = isPrototypeOf( d, B.prototype ); +// returns true + +bool = isPrototypeOf( d, A.prototype ); +// returns true + +bool = isPrototypeOf( c, B.prototype ); +// returns true + +bool = isPrototypeOf( c, A.prototype ); +// returns true + +bool = isPrototypeOf( c, D.prototype ); +// returns false + +bool = isPrototypeOf( b, A.prototype ); +// returns true + +bool = isPrototypeOf( b, C.prototype ); +// returns false + +bool = isPrototypeOf( b, D.prototype ); +// returns false + +bool = isPrototypeOf( a, B.prototype ); +// returns false + +bool = isPrototypeOf( a, C.prototype ); +// returns false + +bool = isPrototypeOf( a, D.prototype ); +// returns false +``` + +
+ + + + + + diff --git a/is-prototype-of/benchmark/benchmark.js b/is-prototype-of/benchmark/benchmark.js new file mode 100644 index 00000000..16c1b39f --- /dev/null +++ b/is-prototype-of/benchmark/benchmark.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isPrototypeOf = require( './../lib' ); // eslint-disable-line stdlib/no-redeclare + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var vals; + var i; + + function Foo() { + return this; + } + + vals = [ + new Date(), + new RegExp( '.*' ), + new Array( 10 ), + new Foo() + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isPrototypeOf( vals[ i%vals.length ], Foo.prototype ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-prototype-of/docs/repl.txt b/is-prototype-of/docs/repl.txt new file mode 100644 index 00000000..806dd5e9 --- /dev/null +++ b/is-prototype-of/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, proto ) + Tests if an object's prototype chain contains a provided prototype. + + The function returns `false` if provided a primitive value. + + This function is generally more robust than the `instanceof` operator (e.g., + where inheritance is performed without using constructors). + + Parameters + ---------- + value: any + Input value. + + proto: Object|Function + Prototype. + + Returns + ------- + bool: boolean + Boolean indicating if a provided prototype exists in a prototype chain. + + Examples + -------- + > function Foo() { return this; }; + > function Bar() { return this; }; + > {{alias:@stdlib/utils/inherit}}( Bar, Foo ); + > var bar = new Bar(); + > var bool = {{alias}}( bar, Foo.prototype ) + true + + See Also + -------- + diff --git a/is-prototype-of/docs/types/index.d.ts b/is-prototype-of/docs/types/index.d.ts new file mode 100644 index 00000000..b75cd8f1 --- /dev/null +++ b/is-prototype-of/docs/types/index.d.ts @@ -0,0 +1,51 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's prototype chain contains a provided prototype. +* +* @param value - value to test +* @param proto - prototype +* @throws second argument must be an object and not null +* @returns boolean indicating if a provided prototype exists in a prototype chain +* +* @example +* var inherit = require( `@stdlib/utils/inherit` ); +* +* function Foo() { +* return this; +* } +* +* function Bar() { +* return this; +* } +* inherit( Bar, Foo ); +* +* var bar = new Bar(); +* +* var bool = isPrototypeOf( bar, Foo.prototype ); +* // returns true +*/ +declare function isPrototypeOf( value: any, proto: any ): boolean; + + +// EXPORTS // + +export = isPrototypeOf; diff --git a/is-prototype-of/docs/types/test.ts b/is-prototype-of/docs/types/test.ts new file mode 100644 index 00000000..b04653da --- /dev/null +++ b/is-prototype-of/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isPrototypeOf = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isPrototypeOf( 3.12, Number.prototype ); // $ExpectType boolean +} + +// The function does not compile if provided insufficient arguments... +{ + isPrototypeOf(); // $ExpectError + isPrototypeOf( 3.12 ); // $ExpectError +} diff --git a/is-prototype-of/examples/index.js b/is-prototype-of/examples/index.js new file mode 100644 index 00000000..f4314787 --- /dev/null +++ b/is-prototype-of/examples/index.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var inherit = require( '@stdlib/utils/inherit' ); +var isPrototypeOf = require( './../lib' ); // eslint-disable-line stdlib/no-redeclare + +function A() { + return this; +} + +function B() { + return this; +} +inherit( B, A ); + +function C() { + return this; +} +inherit( C, B ); + +function D() { + return this; +} +inherit( D, C ); + +var a = new A(); +var b = new B(); +var c = new C(); +var d = new D(); + +var bool = isPrototypeOf( d, C.prototype ); +console.log( 'd --> ... --> C? %s.', bool ); +// => 'd --> ... --> C? true.' + +bool = isPrototypeOf( d, B.prototype ); +console.log( 'd --> ... --> B? %s.', bool ); +// => 'd --> ... --> B? true.' + +bool = isPrototypeOf( d, A.prototype ); +console.log( 'd --> ... --> A? %s.', bool ); +// => 'd --> ... --> A? true.' + +bool = isPrototypeOf( c, B.prototype ); +console.log( 'c --> ... --> B? %s.', bool ); +// => 'c --> ... --> B? true.' + +bool = isPrototypeOf( c, A.prototype ); +console.log( 'c --> ... --> A? %s.', bool ); +// => 'c --> ... --> A? true.' + +bool = isPrototypeOf( c, D.prototype ); +console.log( 'c --> ... --> D? %s.', bool ); +// => 'c --> ... --> D? false.' + +bool = isPrototypeOf( b, A.prototype ); +console.log( 'b --> ... --> A? %s.', bool ); +// => 'b --> ... --> A? true.' + +bool = isPrototypeOf( b, C.prototype ); +console.log( 'b --> ... --> C? %s.', bool ); +// => 'b --> ... --> C? false.' + +bool = isPrototypeOf( b, D.prototype ); +console.log( 'b --> ... --> D? %s.', bool ); +// => 'b --> ... --> D? false.' + +bool = isPrototypeOf( a, B.prototype ); +console.log( 'a --> ... --> B? %s.', bool ); +// => 'a --> ... --> B? false.' + +bool = isPrototypeOf( a, C.prototype ); +console.log( 'a --> ... --> C? %s.', bool ); +// => 'a --> ... --> C? false.' + +bool = isPrototypeOf( a, D.prototype ); +console.log( 'a --> ... --> D? %s.', bool ); +// => 'a --> ... --> D? false.' diff --git a/is-prototype-of/lib/index.js b/is-prototype-of/lib/index.js new file mode 100644 index 00000000..aa730ed5 --- /dev/null +++ b/is-prototype-of/lib/index.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if an object's prototype chain contains a provided prototype. +* +* @module @stdlib/assert/is-prototype-of +* +* @example +* var inherit = require( '@stdlib/utils/inherit' ); +* var isPrototypeOf = require( '@stdlib/assert/is-prototype-of' ); +* +* function Foo() { +* return this; +* } +* +* function Bar() { +* return this; +* } +* inherit( Bar, Foo ); +* +* var bar = new Bar(); +* +* var bool = isPrototypeOf( bar, Foo.prototype ); +* // returns true +*/ + +// MODULES // + +var isPrototypeOf = require( './main.js' ); // eslint-disable-line stdlib/no-redeclare + + +// EXPORTS // + +module.exports = isPrototypeOf; diff --git a/is-prototype-of/lib/main.js b/is-prototype-of/lib/main.js new file mode 100644 index 00000000..781542f4 --- /dev/null +++ b/is-prototype-of/lib/main.js @@ -0,0 +1,74 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// VARIABLES // + +var isProtoOf = Object.prototype.isPrototypeOf; + + +// MAIN // + +/** +* Tests if an object's prototype chain contains a provided prototype. +* +* @param {*} value - value to test +* @param {(Object|Function)} proto - prototype +* @throws {TypeError} second argument must be an object and not null +* @returns {boolean} boolean indicating if a provided prototype exists in a prototype chain +* +* @example +* var inherit = require( '@stdlib/utils/inherit' ); +* +* function Foo() { +* return this; +* } +* +* function Bar() { +* return this; +* } +* inherit( Bar, Foo ); +* +* var bar = new Bar(); +* +* var bool = isPrototypeOf( bar, Foo.prototype ); +* // returns true +*/ +function isPrototypeOf( value, proto ) { // eslint-disable-line stdlib/no-redeclare + var type = typeof proto; + if ( + proto === null || + (type !== 'object' && type !== 'function') + ) { + throw new TypeError( 'invalid argument. Second argument must be either an object (except null) or a function. Value: `'+proto+'`.' ); + } + type = typeof value; + if ( + value === null || + (type !== 'object' && type !== 'function') + ) { + return false; + } + return isProtoOf.call( proto, value ); +} + + +// EXPORTS // + +module.exports = isPrototypeOf; diff --git a/is-prototype-of/package.json b/is-prototype-of/package.json new file mode 100644 index 00000000..963869db --- /dev/null +++ b/is-prototype-of/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/assert/is-prototype-of", + "version": "0.0.0", + "description": "Test if an object's prototype chain contains a provided prototype.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "prototype", + "proto", + "inherit", + "inherits", + "class", + "oop", + "object-oriented", + "inheritance", + "instanceof", + "instance", + "isprototypeof", + "super", + "parent", + "test", + "check", + "is", + "validate", + "valid", + "verify" + ] +} diff --git a/is-prototype-of/test/test.js b/is-prototype-of/test/test.js new file mode 100644 index 00000000..a837c502 --- /dev/null +++ b/is-prototype-of/test/test.js @@ -0,0 +1,172 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var inherit = require( '@stdlib/utils/inherit' ); +var Number = require( '@stdlib/number/ctor' ); +var isPrototypeOf = require( './../lib' ); // eslint-disable-line stdlib/no-redeclare + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isPrototypeOf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if a prototype argument which is neither an object (except null) or a function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + isPrototypeOf( {}, value ); + }; + } +}); + +tape( 'the function returns `false` if provided a test value which is neither an object (except null) or a function', function test( t ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isPrototypeOf( values[i], {} ); + t.strictEqual( bool, false, 'returns false when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if a provided prototype is part of a value\'s prototype chain', function test( t ) { + var bool; + var bar; + + function Foo() { + return this; + } + + function Bar() { + return this; + } + inherit( Bar, Foo ); + + bar = new Bar(); + bool = isPrototypeOf( bar, Foo.prototype ); + + t.strictEqual( bool, true, 'returns true' ); + t.strictEqual( bar instanceof Bar, true, 'is instance of Bar' ); + t.strictEqual( bar instanceof Foo, true, 'is instance of Foo' ); + t.end(); +}); + +tape( 'the function returns `false` if a provided prototype is not part of a value\'s prototype chain', function test( t ) { + var bool; + var bar; + + function Foo() { + return this; + } + + function Bar() { + return this; + } + + bar = new Bar(); + bool = isPrototypeOf( bar, Foo.prototype ); + + t.strictEqual( bool, false, 'returns false' ); + t.strictEqual( bar instanceof Bar, true, 'is instance of Bar' ); + t.strictEqual( bar instanceof Foo, false, 'is not instance of Foo' ); + t.end(); +}); + +tape( 'the function supports situations where constructors are never used', function test( t ) { + var superProto; + var subProto; + var bool; + var v; + + function createObject( proto ) { + function Ctor() {} + Ctor.prototype = proto; + return new Ctor(); + } + superProto = { + 'beep': 'beep' + }; + + subProto = createObject( superProto ); + subProto.boop = 'boop'; + + v = createObject( subProto ); + + t.throws( isInstance, TypeError, 'throws type error if using `instanceof`' ); + + bool = isPrototypeOf( v, superProto ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); + + function isInstance() { + var bool = ( v instanceof superProto ); + return bool; + } +}); + +tape( 'the function returns `false` if provided primitives and their corresponding object constructors', function test( t ) { + var bool; + + bool = isPrototypeOf( true, Boolean.prototype ); + t.strictEqual( bool, false, 'returns false' ); + + bool = isPrototypeOf( 'beep', String.prototype ); + t.strictEqual( bool, false, 'returns false' ); + + bool = isPrototypeOf( 5, Number.prototype ); + t.strictEqual( bool, false, 'returns false' ); + + t.end(); +}); diff --git a/is-range-error/README.md b/is-range-error/README.md new file mode 100644 index 00000000..c9a64c11 --- /dev/null +++ b/is-range-error/README.md @@ -0,0 +1,129 @@ + + +# isRangeError + +> Test if a value is a [RangeError][mdn-range-error] object. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var isRangeError = require( '@stdlib/assert/is-range-error' ); +``` + +#### isRangeError( value ) + +Tests if a `value` is a [`RangeError`][mdn-range-error] object. + +```javascript +var bool = isRangeError( new RangeError( 'beep' ) ); +// returns true +``` + +
+ + + + + +
+ +## Notes + +- This function should **not** be considered robust. While the function should **always** return `true` if provided a [`RangeError`][mdn-range-error] (or a descendant) object, false positives may occur due to the fact that the [`RangeError`][mdn-range-error] constructor inherits from [`Error`][mdn-error] and has no internal class of its own. Hence, [`RangeError`][mdn-range-error] impersonation is possible. + +
+ + + + + +
+ +## Examples + + + +```javascript +var isRangeError = require( '@stdlib/assert/is-range-error' ); + +var bool = isRangeError( new RangeError( 'range error' ) ); +// returns true + +bool = isRangeError( new Error( 'error' ) ); +// returns false + +bool = isRangeError( new EvalError( 'eval error' ) ); +// returns false + +bool = isRangeError( new ReferenceError( 'reference error' ) ); +// returns false + +bool = isRangeError( new SyntaxError( 'syntax error' ) ); +// returns false + +bool = isRangeError( new TypeError( 'type error' ) ); +// returns false + +bool = isRangeError( new URIError( 'URI error' ) ); +// returns false + +bool = isRangeError( {} ); +// returns false + +bool = isRangeError( null ); +// returns false +``` + +
+ + + + + +
+ +
+ + + + + + + + diff --git a/is-range-error/benchmark/benchmark.js b/is-range-error/benchmark/benchmark.js new file mode 100644 index 00000000..a5d14589 --- /dev/null +++ b/is-range-error/benchmark/benchmark.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isRangeError = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + {}, + [], + null, + false, + true, + new Error( 'beep' ), + new RangeError( 'bar' ), + new TypeError( 'boop' ), + new SyntaxError( 'foo' ) + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isRangeError( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-range-error/docs/repl.txt b/is-range-error/docs/repl.txt new file mode 100644 index 00000000..c8b6b208 --- /dev/null +++ b/is-range-error/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( value ) + Tests if a value is a RangeError object. + + This function should *not* be considered robust. While the function should + always return `true` if provided a RangeError (or a descendant) object, + false positives may occur due to the fact that the RangeError constructor + inherits from Error and has no internal class of its own. Hence, RangeError + impersonation is possible. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a RangeError object. + + Examples + -------- + > var bool = {{alias}}( new RangeError( 'beep' ) ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-range-error/docs/types/index.d.ts b/is-range-error/docs/types/index.d.ts new file mode 100644 index 00000000..8e3e39c9 --- /dev/null +++ b/is-range-error/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a `RangeError` object. +* +* ## Notes +* +* - This function should **not** be considered robust. While the function should always return `true` if provided a RangeError (or a descendant) object, false positives may occur due to the fact that the RangeError constructor inherits from Error and has no internal class of its own. Hence, RangeError impersonation is possible. +* +* @param value - value to test +* @returns boolean indicating whether a value is a `RangeError` object +* +* @example +* var bool = isRangeError( new RangeError( 'beep' ) ); +* // returns true +* +* @example +* var bool = isRangeError( {} ); +* // returns false +*/ +declare function isRangeError( value: any ): boolean; + + +// EXPORTS // + +export = isRangeError; diff --git a/is-range-error/docs/types/test.ts b/is-range-error/docs/types/test.ts new file mode 100644 index 00000000..8603b799 --- /dev/null +++ b/is-range-error/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isRangeError = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isRangeError( new RangeError( 'beep' ) ); // $ExpectType boolean + isRangeError( {} ); // $ExpectType boolean +} + +// The function does not compile if provided insufficient arguments... +{ + isRangeError(); // $ExpectError + isRangeError( new RangeError( 'beep' ), 123 ); // $ExpectError +} diff --git a/is-range-error/examples/index.js b/is-range-error/examples/index.js new file mode 100644 index 00000000..fdff0150 --- /dev/null +++ b/is-range-error/examples/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isRangeError = require( './../lib' ); + +var bool = isRangeError( new RangeError( 'range error' ) ); +console.log( bool ); +// => true + +bool = isRangeError( new Error( 'error' ) ); +console.log( bool ); +// => false + +bool = isRangeError( new EvalError( 'eval error' ) ); +console.log( bool ); +// => false + +bool = isRangeError( new ReferenceError( 'reference error' ) ); +console.log( bool ); +// => false + +bool = isRangeError( new SyntaxError( 'syntax error' ) ); +console.log( bool ); +// => false + +bool = isRangeError( new TypeError( 'type error' ) ); +console.log( bool ); +// => false + +bool = isRangeError( new URIError( 'URI error' ) ); +console.log( bool ); +// => false + +bool = isRangeError( {} ); +console.log( bool ); +// => false + +bool = isRangeError( null ); +console.log( bool ); +// => false diff --git a/is-range-error/lib/index.js b/is-range-error/lib/index.js new file mode 100644 index 00000000..896c6162 --- /dev/null +++ b/is-range-error/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a `RangeError` object. +* +* @module @stdlib/assert/is-range-error +* +* @example +* var isRangeError = require( '@stdlib/assert/is-range-error' ); +* +* var bool = isRangeError( new RangeError( 'beep' ) ); +* // returns true +* +* bool = isRangeError( {} ); +* // returns false +*/ + +// MODULES // + +var isRangeError = require( './main.js' ); + + +// EXPORTS // + +module.exports = isRangeError; diff --git a/is-range-error/lib/main.js b/is-range-error/lib/main.js new file mode 100644 index 00000000..2fb59e43 --- /dev/null +++ b/is-range-error/lib/main.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var getPrototypeOf = require( '@stdlib/utils/get-prototype-of' ); +var typeOf = require( '@stdlib/utils/type-of' ); +var isError = require( '@stdlib/assert/is-error' ); + + +// MAIN // + +/** +* Tests if a value is a `RangeError` object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is a `RangeError` object +* +* @example +* var bool = isRangeError( new RangeError( 'beep' ) ); +* // returns true +* +* @example +* var bool = isRangeError( {} ); +* // returns false +*/ +function isRangeError( value ) { + if ( typeof value !== 'object' || value === null ) { + return false; + } + // Check for `RangeError` objects from the same realm (same Node.js `vm` or same `Window` object)... + if ( value instanceof RangeError ) { + return true; + } + // All `RangeError` objects are `Error` objects... + if ( isError( value ) ) { + // Walk the prototype tree until we find the desired constructor... + while ( value ) { + if ( typeOf( value ) === 'rangeerror' ) { + return true; + } + value = getPrototypeOf( value ); + } + } + return false; +} + + +// EXPORTS // + +module.exports = isRangeError; diff --git a/is-range-error/package.json b/is-range-error/package.json new file mode 100644 index 00000000..8ffe4ea8 --- /dev/null +++ b/is-range-error/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-range-error", + "version": "0.0.0", + "description": "Test if a value is a RangeError object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "range", + "error", + "err", + "exception", + "is", + "iserr", + "iserror", + "valid", + "isvalid", + "type", + "check", + "validate", + "validation", + "test" + ] +} diff --git a/is-range-error/test/test.js b/is-range-error/test/test.js new file mode 100644 index 00000000..25f33378 --- /dev/null +++ b/is-range-error/test/test.js @@ -0,0 +1,115 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var vm = require( 'vm' ); // TODO: handle in-browser tests +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var inherit = require( '@stdlib/utils/inherit' ); +var isRangeError = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': IS_BROWSER +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isRangeError, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a `RangeError` object', function test( t ) { + var values; + var i; + + values = [ + new RangeError( 'range error' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isRangeError( values[ i ] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an object inheriting from `RangeError`', function test( t ) { + function CustomError( msg ) { + RangeError.call( this ); + this.name = 'CustomError'; + this.message = msg; + return this; + } + + inherit( CustomError, RangeError ); + + t.strictEqual( isRangeError( new CustomError( 'custom error' ) ), true, 'returns true when provided a value which inherits from RangeError' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a `RangeError` object from a different realm', opts, function test( t ) { + var error = vm.runInNewContext( 'new RangeError()' ); + t.strictEqual( isRangeError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an object from a different realm which inherits from a `RangeError` object', opts, function test( t ) { + var error = vm.runInNewContext( 'function Err() { return this; }; Err.prototype = new RangeError(); new Err();' ); + t.strictEqual( isRangeError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a `RangeError` object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined, + [], + {}, + function noop() {}, + function error() {}, + new Date(), + new RegExp( '.*' ), + new Error( 'error' ), + new EvalError( 'eval error' ), + new ReferenceError( 'reference error' ), + new SyntaxError( 'syntax error' ), + new TypeError( 'type error' ), + new URIError( 'URI error' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isRangeError( values[ i ] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-read-only-property-in/README.md b/is-read-only-property-in/README.md new file mode 100644 index 00000000..d56cc81d --- /dev/null +++ b/is-read-only-property-in/README.md @@ -0,0 +1,163 @@ + + +# isReadOnlyPropertyIn + +> Test if an object's own or inherited property is [read-only][@stdlib/utils/define-read-only-property]. + +
+ +## Usage + +```javascript +var isReadOnlyPropertyIn = require( '@stdlib/assert/is-read-only-property-in' ); +``` + +#### isReadOnlyPropertyIn( value, property ) + +Returns a `boolean` indicating if a `value` has a [read-only][@stdlib/utils/define-read-only-property] `property`. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var bool; +var obj; + +function Foo() { + this.foo = 'bar'; + return this; +} + +defineProperty( Foo.prototype, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'boop' +}); + +defineProperty( Foo.prototype, 'accessor', { + 'configurable': false, + 'enumerable': true, + 'get': function getter() { + return obj.foo; + } +}); + +obj = new Foo(); + +bool = isReadOnlyPropertyIn( obj, 'foo' ); +// returns false + +bool = isReadOnlyPropertyIn( obj, 'beep' ); +// returns true + +bool = isReadOnlyPropertyIn( obj, 'accessor' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isReadOnlyPropertyIn( 'beep', 'length' ); + // returns true + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var defineProperty = require( '@stdlib/utils/define-property' ); + + var obj = {}; + + defineProperty( obj, 'null', { + 'configurable': false, + 'enumerable': true, + 'writable': false, + 'value': true + }); + + var bool = isReadOnlyPropertyIn( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isReadOnlyPropertyIn = require( '@stdlib/assert/is-read-only-property-in' ); + +var bool = isReadOnlyPropertyIn( 'a', 'length' ); +// returns true + +bool = isReadOnlyPropertyIn( { 'a': 'b' }, 'a' ); +// returns false + +bool = isReadOnlyPropertyIn( [ 'a' ], 0 ); +// returns false + +bool = isReadOnlyPropertyIn( { 'null': false }, null ); +// returns false + +bool = isReadOnlyPropertyIn( { '[object Object]': false }, {} ); +// returns false + +bool = isReadOnlyPropertyIn( {}, 'toString' ); +// returns false + +bool = isReadOnlyPropertyIn( {}, 'hasOwnProperty' ); +// returns false + +bool = isReadOnlyPropertyIn( null, 'a' ); +// returns false + +bool = isReadOnlyPropertyIn( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-read-only-property-in/benchmark/benchmark.js b/is-read-only-property-in/benchmark/benchmark.js new file mode 100644 index 00000000..af888602 --- /dev/null +++ b/is-read-only-property-in/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isReadOnlyPropertyIn = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isReadOnlyPropertyIn( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-read-only-property-in/docs/repl.txt b/is-read-only-property-in/docs/repl.txt new file mode 100644 index 00000000..6e0c29a9 --- /dev/null +++ b/is-read-only-property-in/docs/repl.txt @@ -0,0 +1,35 @@ + +{{alias}}( value, property ) + Tests if an object's own or inherited property is read-only. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own or inherited property is read- + only. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = true; + > desc.writable = false; + > desc.value = true; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + true + > bool = {{alias}}( obj, 'beep' ) + false + + See Also + -------- + diff --git a/is-read-only-property-in/docs/types/index.d.ts b/is-read-only-property-in/docs/types/index.d.ts new file mode 100644 index 00000000..1e3df01d --- /dev/null +++ b/is-read-only-property-in/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own or inherited property is read-only. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is read-only +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': false, +* 'value': true +* }); +* +* var bool = isReadOnlyPropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isReadOnlyPropertyIn( obj, 'beep' ); +* // returns true +*/ +declare function isReadOnlyPropertyIn( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isReadOnlyPropertyIn; diff --git a/is-read-only-property-in/docs/types/test.ts b/is-read-only-property-in/docs/types/test.ts new file mode 100644 index 00000000..e0453872 --- /dev/null +++ b/is-read-only-property-in/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isReadOnlyPropertyIn = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + Object.defineProperty( obj, 'beep', { + 'configurable': true, + 'enumerable': true, + 'writable': false, + 'value': 'beep' + } ); + isReadOnlyPropertyIn( obj, 'beep' ); // $ExpectType boolean + isReadOnlyPropertyIn( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isReadOnlyPropertyIn(); // $ExpectError + isReadOnlyPropertyIn( {} ); // $ExpectError + isReadOnlyPropertyIn( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-read-only-property-in/examples/index.js b/is-read-only-property-in/examples/index.js new file mode 100644 index 00000000..d87fb95a --- /dev/null +++ b/is-read-only-property-in/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isReadOnlyPropertyIn = require( './../lib' ); + +var bool = isReadOnlyPropertyIn( 'a', 'length' ); +console.log( bool ); +// => true + +bool = isReadOnlyPropertyIn( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => false + +bool = isReadOnlyPropertyIn( [ 'a' ], 0 ); +console.log( bool ); +// => false + +bool = isReadOnlyPropertyIn( { 'null': false }, null ); +console.log( bool ); +// => false + +bool = isReadOnlyPropertyIn( { '[object Object]': false }, {} ); +console.log( bool ); +// => false + +bool = isReadOnlyPropertyIn( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isReadOnlyPropertyIn( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isReadOnlyPropertyIn( null, 'a' ); +console.log( bool ); +// => false + +bool = isReadOnlyPropertyIn( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-read-only-property-in/lib/index.js b/is-read-only-property-in/lib/index.js new file mode 100644 index 00000000..a4252217 --- /dev/null +++ b/is-read-only-property-in/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own or inherited property is read-only. +* +* @module @stdlib/assert/is-read-only-property-in +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isReadOnlyPropertyIn = require( '@stdlib/assert/is-read-only-property-in' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': false, +* 'value': true +* }); +* +* var bool = isReadOnlyPropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isReadOnlyPropertyIn( obj, 'beep' ); +* // returns true +*/ + +// MODULES // + +var isReadOnlyPropertyIn = require( './main.js' ); + + +// EXPORTS // + +module.exports = isReadOnlyPropertyIn; diff --git a/is-read-only-property-in/lib/main.js b/is-read-only-property-in/lib/main.js new file mode 100644 index 00000000..9ceb32e4 --- /dev/null +++ b/is-read-only-property-in/lib/main.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptorIn = require( '@stdlib/utils/property-descriptor-in' ); + + +// MAIN // + +/** +* Tests if an object's own or inherited property is read-only. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is read-only +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': false, +* 'value': true +* }); +* +* var bool = isReadOnlyPropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isReadOnlyPropertyIn( obj, 'beep' ); +* // returns true +*/ +function isReadOnlyPropertyIn( value, property ) { + var desc = propertyDescriptorIn( value, property ); + return ( + desc !== null && + desc.configurable === false && + ( + // Data descriptor: + desc.writable === false || + + // Accessor descriptor: + ( + typeof desc.get === 'function' && + desc.set === void 0 + ) + ) + ); +} + + +// EXPORTS // + +module.exports = isReadOnlyPropertyIn; diff --git a/is-read-only-property-in/package.json b/is-read-only-property-in/package.json new file mode 100644 index 00000000..5422d637 --- /dev/null +++ b/is-read-only-property-in/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-read-only-property-in", + "version": "0.0.0", + "description": "Test if an object's own or inherited property is read-only.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "inherited", + "in", + "prototype", + "proto", + "readable", + "writable", + "read-only", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-read-only-property-in/test/test.js b/is-read-only-property-in/test/test.js new file mode 100644 index 00000000..341ee44f --- /dev/null +++ b/is-read-only-property-in/test/test.js @@ -0,0 +1,278 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isReadOnlyPropertyIn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isReadOnlyPropertyIn, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property is read-only', function test( t ) { + var bool; + var obj; + + obj = {}; + + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': true, + 'writable': false, + 'value': true + }); + + defineProperty( obj, 'b', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': true + }); + + defineProperty( obj, 'c', { + 'configurable': false, + 'enumerable': true, + 'get': getter + }); + + defineProperty( obj, 'd', { + 'configurable': false, + 'enumerable': false, + 'get': getter + }); + + bool = isReadOnlyPropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadOnlyPropertyIn( obj, 'b' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadOnlyPropertyIn( obj, 'c' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadOnlyPropertyIn( obj, 'd' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function getter() { + // No-op... + } +}); + +tape( 'the function returns `true` if provided an inherited property which is read-only', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + defineProperty( Foo.prototype, 'bar', { + 'configurable': false, + 'enumerable': true, + 'writable': false, + 'value': true + }); + + defineProperty( Foo.prototype, 'beep', { + 'configurable': false, + 'enumerable': false, + 'get': getter + }); + + obj = new Foo(); + + bool = isReadOnlyPropertyIn( obj, 'bar' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadOnlyPropertyIn( obj, 'beep' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function getter() { + // No-op... + } +}); + +tape( 'the function returns `false` if an object property is not read-only', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isReadOnlyPropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyPropertyIn( [ 1, 2, 3 ], '1' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyPropertyIn( [ 1, 2, 3 ], 1 ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyPropertyIn( new Foo(), 'bar' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyPropertyIn( [ 'a' ], 'length' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'b' + }); + + bool = isReadOnlyPropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'get': getter, + 'set': setter + }); + + defineProperty( obj, 'b', { + 'configurable': false, + 'enumerable': false, + 'set': setter + }); + + bool = isReadOnlyPropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyPropertyIn( obj, 'b' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function getter() { + // No-op... + } + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isReadOnlyPropertyIn( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isReadOnlyPropertyIn( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a read-only property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + defineProperty( obj, 'd', { + 'configurable': true, + 'enumerable': true, + 'set': setter + }); + + defineProperty( obj, 'e', { + 'configurable': true, + 'enumerable': true, + 'set': setter, + 'get': getter + }); + + defineProperty( obj, 'f', { + 'configurable': true, + 'enumerable': false, + 'writable': false, + 'value': 'g' + }); + + bool = isReadOnlyPropertyIn( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyPropertyIn( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyPropertyIn( obj, 'e' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyPropertyIn( obj, 'f' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function setter() { + // No-op... + } + + function getter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided an inherited property which is not read-only', function test( t ) { + var bool; + + bool = isReadOnlyPropertyIn( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyPropertyIn( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyPropertyIn( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isReadOnlyPropertyIn( 'beep', 'length' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/is-read-only-property/README.md b/is-read-only-property/README.md new file mode 100644 index 00000000..374bc12e --- /dev/null +++ b/is-read-only-property/README.md @@ -0,0 +1,157 @@ + + +# isReadOnlyProperty + +> Test if an object's own property is [read-only][@stdlib/utils/define-read-only-property]. + +
+ +## Usage + +```javascript +var isReadOnlyProperty = require( '@stdlib/assert/is-read-only-property' ); +``` + +#### isReadOnlyProperty( value, property ) + +Returns a `boolean` indicating if a `value` has a [read-only][@stdlib/utils/define-read-only-property] `property`. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var obj = { + 'foo': 'bar' +}; + +defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'boop' +}); + +defineProperty( obj, 'accessor', { + 'configurable': false, + 'enumerable': true, + 'get': function getter() { + return obj.foo; + } +}); + +var bool = isReadOnlyProperty( obj, 'foo' ); +// returns false + +bool = isReadOnlyProperty( obj, 'beep' ); +// returns true + +bool = isReadOnlyProperty( obj, 'accessor' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isReadOnlyProperty( 'beep', 'length' ); + // returns true + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var defineProperty = require( '@stdlib/utils/define-property' ); + + var obj = {}; + + defineProperty( obj, 'null', { + 'configurable': false, + 'enumerable': true, + 'writable': false, + 'value': true + }); + + var bool = isReadOnlyProperty( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isReadOnlyProperty = require( '@stdlib/assert/is-read-only-property' ); + +var bool = isReadOnlyProperty( 'a', 'length' ); +// returns true + +bool = isReadOnlyProperty( { 'a': 'b' }, 'a' ); +// returns false + +bool = isReadOnlyProperty( [ 'a' ], 0 ); +// returns false + +bool = isReadOnlyProperty( { 'null': false }, null ); +// returns false + +bool = isReadOnlyProperty( { '[object Object]': false }, {} ); +// returns false + +bool = isReadOnlyProperty( {}, 'toString' ); +// returns false + +bool = isReadOnlyProperty( {}, 'hasOwnProperty' ); +// returns false + +bool = isReadOnlyProperty( null, 'a' ); +// returns false + +bool = isReadOnlyProperty( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-read-only-property/benchmark/benchmark.js b/is-read-only-property/benchmark/benchmark.js new file mode 100644 index 00000000..3a49a1f4 --- /dev/null +++ b/is-read-only-property/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isReadOnlyProperty = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isReadOnlyProperty( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-read-only-property/docs/repl.txt b/is-read-only-property/docs/repl.txt new file mode 100644 index 00000000..efb38aae --- /dev/null +++ b/is-read-only-property/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, property ) + Tests if an object's own property is read-only. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own property is read-only. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = true; + > desc.writable = false; + > desc.value = true; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + true + > bool = {{alias}}( obj, 'beep' ) + false + + See Also + -------- + diff --git a/is-read-only-property/docs/types/index.d.ts b/is-read-only-property/docs/types/index.d.ts new file mode 100644 index 00000000..bb0a464d --- /dev/null +++ b/is-read-only-property/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own property is read-only. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is read-only +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': false, +* 'value': true +* }); +* +* var bool = isReadOnlyProperty( obj, 'boop' ); +* // returns false +* +* bool = isReadOnlyProperty( obj, 'beep' ); +* // returns true +*/ +declare function isReadOnlyProperty( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isReadOnlyProperty; diff --git a/is-read-only-property/docs/types/test.ts b/is-read-only-property/docs/types/test.ts new file mode 100644 index 00000000..c1952199 --- /dev/null +++ b/is-read-only-property/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isReadOnlyProperty = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + Object.defineProperty( obj, 'beep', { + 'configurable': true, + 'enumerable': true, + 'writable': false, + 'value': 'beep' + } ); + isReadOnlyProperty( obj, 'beep' ); // $ExpectType boolean + isReadOnlyProperty( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isReadOnlyProperty(); // $ExpectError + isReadOnlyProperty( {} ); // $ExpectError + isReadOnlyProperty( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-read-only-property/examples/index.js b/is-read-only-property/examples/index.js new file mode 100644 index 00000000..cea915e6 --- /dev/null +++ b/is-read-only-property/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isReadOnlyProperty = require( './../lib' ); + +var bool = isReadOnlyProperty( 'a', 'length' ); +console.log( bool ); +// => true + +bool = isReadOnlyProperty( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => false + +bool = isReadOnlyProperty( [ 'a' ], 0 ); +console.log( bool ); +// => false + +bool = isReadOnlyProperty( { 'null': false }, null ); +console.log( bool ); +// => false + +bool = isReadOnlyProperty( { '[object Object]': false }, {} ); +console.log( bool ); +// => false + +bool = isReadOnlyProperty( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isReadOnlyProperty( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isReadOnlyProperty( null, 'a' ); +console.log( bool ); +// => false + +bool = isReadOnlyProperty( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-read-only-property/lib/index.js b/is-read-only-property/lib/index.js new file mode 100644 index 00000000..8fadd375 --- /dev/null +++ b/is-read-only-property/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own property is read-only. +* +* @module @stdlib/assert/is-read-only-property +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isReadOnlyProperty = require( '@stdlib/assert/is-read-only-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': false, +* 'value': true +* }); +* +* var bool = isReadOnlyProperty( obj, 'boop' ); +* // returns false +* +* bool = isReadOnlyProperty( obj, 'beep' ); +* // returns true +*/ + +// MODULES // + +var isReadOnlyProperty = require( './main.js' ); + + +// EXPORTS // + +module.exports = isReadOnlyProperty; diff --git a/is-read-only-property/lib/main.js b/is-read-only-property/lib/main.js new file mode 100644 index 00000000..34c9a7b3 --- /dev/null +++ b/is-read-only-property/lib/main.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptor = require( '@stdlib/utils/property-descriptor' ); + + +// MAIN // + +/** +* Tests if an object's own property is read-only. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is read-only +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'writable': false, +* 'value': true +* }); +* +* var bool = isReadOnlyProperty( obj, 'boop' ); +* // returns false +* +* bool = isReadOnlyProperty( obj, 'beep' ); +* // returns true +*/ +function isReadOnlyProperty( value, property ) { + var desc = propertyDescriptor( value, property ); + return ( + desc !== null && + desc.configurable === false && + ( + // Data descriptor: + desc.writable === false || + + // Accessor descriptor: + ( + typeof desc.get === 'function' && + desc.set === void 0 + ) + ) + ); +} + + +// EXPORTS // + +module.exports = isReadOnlyProperty; diff --git a/is-read-only-property/package.json b/is-read-only-property/package.json new file mode 100644 index 00000000..bb70557a --- /dev/null +++ b/is-read-only-property/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-read-only-property", + "version": "0.0.0", + "description": "Test if an object's own property is read-only.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "readable", + "writable", + "read-only", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-read-only-property/test/test.js b/is-read-only-property/test/test.js new file mode 100644 index 00000000..37705ab4 --- /dev/null +++ b/is-read-only-property/test/test.js @@ -0,0 +1,232 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isReadOnlyProperty = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isReadOnlyProperty, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property is read-only', function test( t ) { + var bool; + var obj; + + obj = {}; + + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': true, + 'writable': false, + 'value': true + }); + + defineProperty( obj, 'b', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': true + }); + + defineProperty( obj, 'c', { + 'configurable': false, + 'enumerable': true, + 'get': getter + }); + + defineProperty( obj, 'd', { + 'configurable': false, + 'enumerable': false, + 'get': getter + }); + + bool = isReadOnlyProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadOnlyProperty( obj, 'b' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadOnlyProperty( obj, 'c' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadOnlyProperty( obj, 'd' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function getter() { + // No-op... + } +}); + +tape( 'the function returns `false` if an object property is not read-only', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isReadOnlyProperty( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyProperty( [ 1, 2, 3 ], '1' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyProperty( [ 1, 2, 3 ], 1 ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyProperty( new Foo(), 'bar' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyProperty( [ 'a' ], 'length' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'b' + }); + + bool = isReadOnlyProperty( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'get': getter, + 'set': setter + }); + + bool = isReadOnlyProperty( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function getter() { + // No-op... + } + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isReadOnlyProperty( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isReadOnlyProperty( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a read-only property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + defineProperty( obj, 'd', { + 'configurable': true, + 'enumerable': true, + 'set': setter + }); + + defineProperty( obj, 'e', { + 'configurable': true, + 'enumerable': true, + 'set': setter, + 'get': getter + }); + + defineProperty( obj, 'f', { + 'configurable': true, + 'enumerable': false, + 'writable': false, + 'value': 'g' + }); + + bool = isReadOnlyProperty( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyProperty( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyProperty( obj, 'e' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyProperty( obj, 'f' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function setter() { + // No-op... + } + + function getter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided an inherited property', function test( t ) { + var bool; + + bool = isReadOnlyProperty( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyProperty( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadOnlyProperty( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isReadOnlyProperty( 'beep', 'length' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/is-read-write-property-in/README.md b/is-read-write-property-in/README.md new file mode 100644 index 00000000..70b5d5e0 --- /dev/null +++ b/is-read-write-property-in/README.md @@ -0,0 +1,151 @@ + + +# isReadWritePropertyIn + +> Test if an object's own or inherited property is readable and writable. + +
+ +## Usage + +```javascript +var isReadWritePropertyIn = require( '@stdlib/assert/is-read-write-property-in' ); +``` + +#### isReadWritePropertyIn( value, property ) + +Returns a `boolean` indicating if a `value` has a readable **and** writable `property`. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var obj = { + 'foo': 'bar' +}; + +defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'boop' +}); + +defineProperty( obj, 'accessor', { + 'configurable': false, + 'enumerable': false, + 'get': function getter() { + return obj.foo; + }, + 'set': function setter( v ) { + obj.foo = v; + } +}); + +var bool = isReadWritePropertyIn( obj, 'foo' ); +// returns true + +bool = isReadWritePropertyIn( obj, 'beep' ); +// returns true + +bool = isReadWritePropertyIn( obj, 'accessor' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isReadWritePropertyIn( 'beep', 'length' ); + // returns false + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var obj = { + 'null': 'foo' + }; + + var bool = isReadWritePropertyIn( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isReadWritePropertyIn = require( '@stdlib/assert/is-read-write-property-in' ); + +var bool = isReadWritePropertyIn( [ 'a' ], 'length' ); +// returns true + +bool = isReadWritePropertyIn( { 'a': 'b' }, 'a' ); +// returns true + +bool = isReadWritePropertyIn( [ 'a' ], 0 ); +// returns true + +bool = isReadWritePropertyIn( { 'null': false }, null ); +// returns true + +bool = isReadWritePropertyIn( { '[object Object]': false }, {} ); +// returns true + +bool = isReadWritePropertyIn( {}, 'toString' ); +// returns true + +bool = isReadWritePropertyIn( {}, 'hasOwnProperty' ); +// returns true + +bool = isReadWritePropertyIn( null, 'a' ); +// returns false + +bool = isReadWritePropertyIn( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-read-write-property-in/benchmark/benchmark.js b/is-read-write-property-in/benchmark/benchmark.js new file mode 100644 index 00000000..15aad3c0 --- /dev/null +++ b/is-read-write-property-in/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isReadWritePropertyIn = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isReadWritePropertyIn( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-read-write-property-in/docs/repl.txt b/is-read-write-property-in/docs/repl.txt new file mode 100644 index 00000000..685b0d9e --- /dev/null +++ b/is-read-write-property-in/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, property ) + Tests if an object's own or inherited property is readable and writable. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own or inherited property is readable + and writable. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = false; + > desc.set = function setter( v ) { obj.boop = v; }; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + true + > bool = {{alias}}( obj, 'beep' ) + false + + See Also + -------- + diff --git a/is-read-write-property-in/docs/types/index.d.ts b/is-read-write-property-in/docs/types/index.d.ts new file mode 100644 index 00000000..f8fdff3f --- /dev/null +++ b/is-read-write-property-in/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own or inherited property is readable and writable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is readable and writable +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'set': setter +* }); +* +* var bool = isReadWritePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isReadWritePropertyIn( obj, 'beep' ); +* // returns false +*/ +declare function isReadWritePropertyIn( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isReadWritePropertyIn; diff --git a/is-read-write-property-in/docs/types/test.ts b/is-read-write-property-in/docs/types/test.ts new file mode 100644 index 00000000..f2bc0670 --- /dev/null +++ b/is-read-write-property-in/docs/types/test.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isReadWritePropertyIn = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + + const setter = ( v: boolean ) => { + obj.boop = v; + }; + + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'set': setter + } ); + isReadWritePropertyIn( obj, 'beep' ); // $ExpectType boolean + isReadWritePropertyIn( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isReadWritePropertyIn(); // $ExpectError + isReadWritePropertyIn( {} ); // $ExpectError + isReadWritePropertyIn( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-read-write-property-in/examples/index.js b/is-read-write-property-in/examples/index.js new file mode 100644 index 00000000..1b4cbf61 --- /dev/null +++ b/is-read-write-property-in/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isReadWritePropertyIn = require( './../lib' ); + +var bool = isReadWritePropertyIn( [ 'a' ], 'length' ); +console.log( bool ); +// => true + +bool = isReadWritePropertyIn( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = isReadWritePropertyIn( [ 'a' ], 0 ); +console.log( bool ); +// => true + +bool = isReadWritePropertyIn( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = isReadWritePropertyIn( { '[object Object]': false }, {} ); +console.log( bool ); +// => true + +bool = isReadWritePropertyIn( {}, 'toString' ); +console.log( bool ); +// => true + +bool = isReadWritePropertyIn( {}, 'hasOwnProperty' ); +console.log( bool ); +// => true + +bool = isReadWritePropertyIn( null, 'a' ); +console.log( bool ); +// => false + +bool = isReadWritePropertyIn( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-read-write-property-in/lib/index.js b/is-read-write-property-in/lib/index.js new file mode 100644 index 00000000..931d691d --- /dev/null +++ b/is-read-write-property-in/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own or inherited property is readable and writable. +* +* @module @stdlib/assert/is-read-write-property-in +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isReadWritePropertyIn = require( '@stdlib/assert/is-read-write-property-in' ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'set': setter +* }); +* +* var bool = isReadWritePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isReadWritePropertyIn( obj, 'beep' ); +* // returns false +*/ + +// MODULES // + +var isReadWritePropertyIn = require( './main.js' ); + + +// EXPORTS // + +module.exports = isReadWritePropertyIn; diff --git a/is-read-write-property-in/lib/main.js b/is-read-write-property-in/lib/main.js new file mode 100644 index 00000000..e4779734 --- /dev/null +++ b/is-read-write-property-in/lib/main.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptorIn = require( '@stdlib/utils/property-descriptor-in' ); + + +// MAIN // + +/** +* Tests if an object's own or inherited property is readable and writable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is readable and writable +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'set': setter +* }); +* +* var bool = isReadWritePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isReadWritePropertyIn( obj, 'beep' ); +* // returns false +*/ +function isReadWritePropertyIn( value, property ) { + var desc = propertyDescriptorIn( value, property ); + return ( + desc !== null && + ( + // Data descriptor: + desc.writable === true || + + // Accessor descriptor: + ( + typeof desc.get === 'function' && + typeof desc.set === 'function' + ) + ) + ); +} + + +// EXPORTS // + +module.exports = isReadWritePropertyIn; diff --git a/is-read-write-property-in/package.json b/is-read-write-property-in/package.json new file mode 100644 index 00000000..f7682079 --- /dev/null +++ b/is-read-write-property-in/package.json @@ -0,0 +1,79 @@ +{ + "name": "@stdlib/assert/is-read-write-property-in", + "version": "0.0.0", + "description": "Test if an object's own and inherited property is readable and writable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "inherited", + "in", + "prototype", + "proto", + "readable", + "writable", + "read-write", + "rw", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-read-write-property-in/test/test.js b/is-read-write-property-in/test/test.js new file mode 100644 index 00000000..127d0354 --- /dev/null +++ b/is-read-write-property-in/test/test.js @@ -0,0 +1,180 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isReadWritePropertyIn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isReadWritePropertyIn, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property is readable and writable', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isReadWritePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadWritePropertyIn( [ 1, 2, 3 ], '1' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadWritePropertyIn( [ 1, 2, 3 ], 1 ); + t.equal( bool, true, 'returns true' ); + + bool = isReadWritePropertyIn( new Foo(), 'bar' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadWritePropertyIn( [ 'a' ], 'length' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'b' + }); + + bool = isReadWritePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'get': getter, + 'set': setter + }); + + bool = isReadWritePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function getter() { + // No-op... + } + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `true` if provided an inherited readable and writable property', function test( t ) { + var bool; + + bool = isReadWritePropertyIn( {}, 'hasOwnProperty' ); + t.equal( bool, true, 'returns true ' ); + + bool = isReadWritePropertyIn( {}, 'toString' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadWritePropertyIn( {}, 'constructor' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isReadWritePropertyIn( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isReadWritePropertyIn( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a readable and writable property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + defineProperty( obj, 'd', { + 'configurable': true, + 'enumerable': true, + 'set': setter + }); + + defineProperty( obj, 'e', { + 'configurable': true, + 'enumerable': true, + 'get': getter + }); + + defineProperty( obj, 'f', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'g' + }); + + bool = isReadWritePropertyIn( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadWritePropertyIn( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadWritePropertyIn( obj, 'e' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadWritePropertyIn( obj, 'f' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function setter() { + // No-op... + } + + function getter() { + // No-op... + } +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isReadWritePropertyIn( 'beep', 'length' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); diff --git a/is-read-write-property/README.md b/is-read-write-property/README.md new file mode 100644 index 00000000..fc47c0d0 --- /dev/null +++ b/is-read-write-property/README.md @@ -0,0 +1,151 @@ + + +# isReadWriteProperty + +> Test if an object's own property is readable and writable. + +
+ +## Usage + +```javascript +var isReadWriteProperty = require( '@stdlib/assert/is-read-write-property' ); +``` + +#### isReadWriteProperty( value, property ) + +Returns a `boolean` indicating if a `value` has a readable **and** writable `property`. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var obj = { + 'foo': 'bar' +}; + +defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'boop' +}); + +defineProperty( obj, 'accessor', { + 'configurable': false, + 'enumerable': false, + 'get': function getter() { + return obj.foo; + }, + 'set': function setter( v ) { + obj.foo = v; + } +}); + +var bool = isReadWriteProperty( obj, 'foo' ); +// returns true + +bool = isReadWriteProperty( obj, 'beep' ); +// returns true + +bool = isReadWriteProperty( obj, 'accessor' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isReadWriteProperty( 'beep', 'length' ); + // returns false + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var obj = { + 'null': 'foo' + }; + + var bool = isReadWriteProperty( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isReadWriteProperty = require( '@stdlib/assert/is-read-write-property' ); + +var bool = isReadWriteProperty( [ 'a' ], 'length' ); +// returns true + +bool = isReadWriteProperty( { 'a': 'b' }, 'a' ); +// returns true + +bool = isReadWriteProperty( [ 'a' ], 0 ); +// returns true + +bool = isReadWriteProperty( { 'null': false }, null ); +// returns true + +bool = isReadWriteProperty( { '[object Object]': false }, {} ); +// returns true + +bool = isReadWriteProperty( {}, 'toString' ); +// returns false + +bool = isReadWriteProperty( {}, 'hasOwnProperty' ); +// returns false + +bool = isReadWriteProperty( null, 'a' ); +// returns false + +bool = isReadWriteProperty( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-read-write-property/benchmark/benchmark.js b/is-read-write-property/benchmark/benchmark.js new file mode 100644 index 00000000..7841943b --- /dev/null +++ b/is-read-write-property/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isReadWriteProperty = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isReadWriteProperty( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-read-write-property/docs/repl.txt b/is-read-write-property/docs/repl.txt new file mode 100644 index 00000000..f9576d96 --- /dev/null +++ b/is-read-write-property/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( value, property ) + Tests if an object's own property is readable and writable. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own property is readable and writable. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = false; + > desc.set = function setter( v ) { obj.boop = v; }; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + true + > bool = {{alias}}( obj, 'beep' ) + false + + See Also + -------- + diff --git a/is-read-write-property/docs/types/index.d.ts b/is-read-write-property/docs/types/index.d.ts new file mode 100644 index 00000000..f28bf45a --- /dev/null +++ b/is-read-write-property/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own property is readable and writable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is readable and writable +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'set': setter +* }); +* +* var bool = isReadWriteProperty( obj, 'boop' ); +* // returns true +* +* bool = isReadWriteProperty( obj, 'beep' ); +* // returns false +*/ +declare function isReadWriteProperty( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isReadWriteProperty; diff --git a/is-read-write-property/docs/types/test.ts b/is-read-write-property/docs/types/test.ts new file mode 100644 index 00000000..9f1555c5 --- /dev/null +++ b/is-read-write-property/docs/types/test.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isReadWriteProperty = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + + const setter = ( v: boolean ) => { + obj.boop = v; + }; + + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'set': setter + } ); + isReadWriteProperty( obj, 'beep' ); // $ExpectType boolean + isReadWriteProperty( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isReadWriteProperty(); // $ExpectError + isReadWriteProperty( {} ); // $ExpectError + isReadWriteProperty( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-read-write-property/examples/index.js b/is-read-write-property/examples/index.js new file mode 100644 index 00000000..3b59d3af --- /dev/null +++ b/is-read-write-property/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isReadWriteProperty = require( './../lib' ); + +var bool = isReadWriteProperty( [ 'a' ], 'length' ); +console.log( bool ); +// => true + +bool = isReadWriteProperty( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = isReadWriteProperty( [ 'a' ], 0 ); +console.log( bool ); +// => true + +bool = isReadWriteProperty( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = isReadWriteProperty( { '[object Object]': false }, {} ); +console.log( bool ); +// => true + +bool = isReadWriteProperty( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isReadWriteProperty( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isReadWriteProperty( null, 'a' ); +console.log( bool ); +// => false + +bool = isReadWriteProperty( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-read-write-property/lib/index.js b/is-read-write-property/lib/index.js new file mode 100644 index 00000000..57b5c855 --- /dev/null +++ b/is-read-write-property/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own property is readable and writable. +* +* @module @stdlib/assert/is-read-write-property +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isReadWriteProperty = require( '@stdlib/assert/is-read-write-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'set': setter +* }); +* +* var bool = isReadWriteProperty( obj, 'boop' ); +* // returns true +* +* bool = isReadWriteProperty( obj, 'beep' ); +* // returns false +*/ + +// MODULES // + +var isReadWriteProperty = require( './main.js' ); + + +// EXPORTS // + +module.exports = isReadWriteProperty; diff --git a/is-read-write-property/lib/main.js b/is-read-write-property/lib/main.js new file mode 100644 index 00000000..415d1480 --- /dev/null +++ b/is-read-write-property/lib/main.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptor = require( '@stdlib/utils/property-descriptor' ); + + +// MAIN // + +/** +* Tests if an object's own property is readable and writable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is readable and writable +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'set': setter +* }); +* +* var bool = isReadWriteProperty( obj, 'boop' ); +* // returns true +* +* bool = isReadWriteProperty( obj, 'beep' ); +* // returns false +*/ +function isReadWriteProperty( value, property ) { + var desc = propertyDescriptor( value, property ); + return ( + desc !== null && + ( + // Data descriptor: + desc.writable === true || + + // Accessor descriptor: + ( + typeof desc.get === 'function' && + typeof desc.set === 'function' + ) + ) + ); +} + + +// EXPORTS // + +module.exports = isReadWriteProperty; diff --git a/is-read-write-property/package.json b/is-read-write-property/package.json new file mode 100644 index 00000000..4dbb10de --- /dev/null +++ b/is-read-write-property/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-read-write-property", + "version": "0.0.0", + "description": "Test if an object's own property is readable and writable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "readable", + "writable", + "read-write", + "rw", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-read-write-property/test/test.js b/is-read-write-property/test/test.js new file mode 100644 index 00000000..3151b6e2 --- /dev/null +++ b/is-read-write-property/test/test.js @@ -0,0 +1,180 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isReadWriteProperty = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isReadWriteProperty, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property is readable and writable', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isReadWriteProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadWriteProperty( [ 1, 2, 3 ], '1' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadWriteProperty( [ 1, 2, 3 ], 1 ); + t.equal( bool, true, 'returns true' ); + + bool = isReadWriteProperty( new Foo(), 'bar' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadWriteProperty( [ 'a' ], 'length' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'b' + }); + + bool = isReadWriteProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'get': getter, + 'set': setter + }); + + bool = isReadWriteProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function getter() { + // No-op... + } + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isReadWriteProperty( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isReadWriteProperty( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a readable and writable property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + defineProperty( obj, 'd', { + 'configurable': true, + 'enumerable': true, + 'set': setter + }); + + defineProperty( obj, 'e', { + 'configurable': true, + 'enumerable': true, + 'get': getter + }); + + defineProperty( obj, 'f', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'g' + }); + + bool = isReadWriteProperty( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadWriteProperty( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadWriteProperty( obj, 'e' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadWriteProperty( obj, 'f' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function setter() { + // No-op... + } + + function getter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided an inherited property', function test( t ) { + var bool; + + bool = isReadWriteProperty( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadWriteProperty( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadWriteProperty( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isReadWriteProperty( 'beep', 'length' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); diff --git a/is-readable-property-in/README.md b/is-readable-property-in/README.md new file mode 100644 index 00000000..27ce6b3a --- /dev/null +++ b/is-readable-property-in/README.md @@ -0,0 +1,148 @@ + + +# isReadablePropertyIn + +> Test if an object's own or inherited property is readable. + +
+ +## Usage + +```javascript +var isReadablePropertyIn = require( '@stdlib/assert/is-readable-property-in' ); +``` + +#### isReadablePropertyIn( value, property ) + +Returns a `boolean` indicating if a `value` has a readable `property`. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var obj = { + 'foo': 'bar' +}; + +defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'boop' +}); + +defineProperty( obj, 'setter', { + 'configurable': false, + 'enumerable': false, + 'set': function setter( v ) { + obj.foo = v; + } +}); + +var bool = isReadablePropertyIn( obj, 'foo' ); +// returns true + +bool = isReadablePropertyIn( obj, 'beep' ); +// returns true + +bool = isReadablePropertyIn( obj, 'setter' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isReadablePropertyIn( 'beep', 'toString' ); + // returns true + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var obj = { + 'null': 'foo' + }; + + var bool = isReadablePropertyIn( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isReadablePropertyIn = require( '@stdlib/assert/is-readable-property-in' ); + +var bool = isReadablePropertyIn( [ 'a' ], 'length' ); +// returns true + +bool = isReadablePropertyIn( { 'a': 'b' }, 'a' ); +// returns true + +bool = isReadablePropertyIn( [ 'a' ], 0 ); +// returns true + +bool = isReadablePropertyIn( { 'null': false }, null ); +// returns true + +bool = isReadablePropertyIn( { '[object Object]': false }, {} ); +// returns true + +bool = isReadablePropertyIn( {}, 'toString' ); +// returns true + +bool = isReadablePropertyIn( {}, 'hasOwnProperty' ); +// returns true + +bool = isReadablePropertyIn( null, 'a' ); +// returns false + +bool = isReadablePropertyIn( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-readable-property-in/benchmark/benchmark.js b/is-readable-property-in/benchmark/benchmark.js new file mode 100644 index 00000000..d05d02dc --- /dev/null +++ b/is-readable-property-in/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isReadablePropertyIn = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isReadablePropertyIn( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-readable-property-in/docs/repl.txt b/is-readable-property-in/docs/repl.txt new file mode 100644 index 00000000..655ebf82 --- /dev/null +++ b/is-readable-property-in/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( value, property ) + Tests if an object's own or inherited property is readable. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own or inherited property is readable. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = false; + > desc.set = function setter( v ) { obj.boop = v; }; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + true + > bool = {{alias}}( obj, 'beep' ) + false + + See Also + -------- + diff --git a/is-readable-property-in/docs/types/index.d.ts b/is-readable-property-in/docs/types/index.d.ts new file mode 100644 index 00000000..5d2b1466 --- /dev/null +++ b/is-readable-property-in/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own or inherited property is readable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is readable +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'set': setter +* }); +* +* var bool = isReadablePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isReadablePropertyIn( obj, 'beep' ); +* // returns false +*/ +declare function isReadablePropertyIn( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isReadablePropertyIn; diff --git a/is-readable-property-in/docs/types/test.ts b/is-readable-property-in/docs/types/test.ts new file mode 100644 index 00000000..812efb93 --- /dev/null +++ b/is-readable-property-in/docs/types/test.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isReadablePropertyIn = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + + const setter = ( v: boolean ) => { + obj.boop = v; + }; + + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'set': setter + } ); + isReadablePropertyIn( obj, 'beep' ); // $ExpectType boolean + isReadablePropertyIn( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isReadablePropertyIn(); // $ExpectError + isReadablePropertyIn( {} ); // $ExpectError + isReadablePropertyIn( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-readable-property-in/examples/index.js b/is-readable-property-in/examples/index.js new file mode 100644 index 00000000..cb0e7201 --- /dev/null +++ b/is-readable-property-in/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isReadablePropertyIn = require( './../lib' ); + +var bool = isReadablePropertyIn( [ 'a' ], 'length' ); +console.log( bool ); +// => true + +bool = isReadablePropertyIn( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = isReadablePropertyIn( [ 'a' ], 0 ); +console.log( bool ); +// => true + +bool = isReadablePropertyIn( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = isReadablePropertyIn( { '[object Object]': false }, {} ); +console.log( bool ); +// => true + +bool = isReadablePropertyIn( {}, 'toString' ); +console.log( bool ); +// => true + +bool = isReadablePropertyIn( {}, 'hasOwnProperty' ); +console.log( bool ); +// => true + +bool = isReadablePropertyIn( null, 'a' ); +console.log( bool ); +// => false + +bool = isReadablePropertyIn( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-readable-property-in/lib/index.js b/is-readable-property-in/lib/index.js new file mode 100644 index 00000000..198f7d12 --- /dev/null +++ b/is-readable-property-in/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own or inherited property is readable. +* +* @module @stdlib/assert/is-readable-property-in +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isReadablePropertyIn = require( '@stdlib/assert/is-readable-property-in' ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'set': setter +* }); +* +* var bool = isReadablePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isReadablePropertyIn( obj, 'beep' ); +* // returns false +*/ + +// MODULES // + +var isReadablePropertyIn = require( './main.js' ); + + +// EXPORTS // + +module.exports = isReadablePropertyIn; diff --git a/is-readable-property-in/lib/main.js b/is-readable-property-in/lib/main.js new file mode 100644 index 00000000..c4ec928c --- /dev/null +++ b/is-readable-property-in/lib/main.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptorIn = require( '@stdlib/utils/property-descriptor-in' ); + + +// MAIN // + +/** +* Tests if an object's own or inherited property is readable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is readable +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'set': setter +* }); +* +* var bool = isReadablePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isReadablePropertyIn( obj, 'beep' ); +* // returns false +*/ +function isReadablePropertyIn( value, property ) { + var desc = propertyDescriptorIn( value, property ); + return ( + desc !== null && + ( + desc.writable !== void 0 || // data descriptor + typeof desc.get === 'function' // accessor descriptor + ) + ); +} + + +// EXPORTS // + +module.exports = isReadablePropertyIn; diff --git a/is-readable-property-in/package.json b/is-readable-property-in/package.json new file mode 100644 index 00000000..79be4e71 --- /dev/null +++ b/is-readable-property-in/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-readable-property-in", + "version": "0.0.0", + "description": "Test if an object's own or inherited property is readable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "inherited", + "in", + "prototype", + "proto", + "readable", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-readable-property-in/test/test.js b/is-readable-property-in/test/test.js new file mode 100644 index 00000000..43317266 --- /dev/null +++ b/is-readable-property-in/test/test.js @@ -0,0 +1,151 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isReadablePropertyIn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isReadablePropertyIn, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property is readable', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isReadablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadablePropertyIn( [ 1, 2, 3 ], '1' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadablePropertyIn( [ 1, 2, 3 ], 1 ); + t.equal( bool, true, 'returns true' ); + + bool = isReadablePropertyIn( new Foo(), 'bar' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadablePropertyIn( [ 'a' ], 'length' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'b' + }); + + bool = isReadablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'get': getter + }); + + bool = isReadablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function getter() { + // No-op... + } +}); + +tape( 'the function returns `true` if provided a readable inherited property', function test( t ) { + var bool; + + bool = isReadablePropertyIn( {}, 'hasOwnProperty' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadablePropertyIn( {}, 'toString' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadablePropertyIn( {}, 'constructor' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isReadablePropertyIn( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isReadablePropertyIn( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a readable property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + defineProperty( obj, 'd', { + 'configurable': true, + 'enumerable': true, + 'set': setter + }); + + bool = isReadablePropertyIn( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadablePropertyIn( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + t.end(); + + function setter() { + // No-op... + } +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isReadablePropertyIn( 'beep', 'toString' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/is-readable-property/README.md b/is-readable-property/README.md new file mode 100644 index 00000000..45335ab7 --- /dev/null +++ b/is-readable-property/README.md @@ -0,0 +1,148 @@ + + +# isReadableProperty + +> Test if an object's own property is readable. + +
+ +## Usage + +```javascript +var isReadableProperty = require( '@stdlib/assert/is-readable-property' ); +``` + +#### isReadableProperty( value, property ) + +Returns a `boolean` indicating if a `value` has a readable `property`. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var obj = { + 'foo': 'bar' +}; + +defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'boop' +}); + +defineProperty( obj, 'setter', { + 'configurable': false, + 'enumerable': false, + 'set': function setter( v ) { + obj.foo = v; + } +}); + +var bool = isReadableProperty( obj, 'foo' ); +// returns true + +bool = isReadableProperty( obj, 'beep' ); +// returns true + +bool = isReadableProperty( obj, 'setter' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isReadableProperty( 'beep', 'length' ); + // returns true + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var obj = { + 'null': 'foo' + }; + + var bool = isReadableProperty( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isReadableProperty = require( '@stdlib/assert/is-readable-property' ); + +var bool = isReadableProperty( [ 'a' ], 'length' ); +// returns true + +bool = isReadableProperty( { 'a': 'b' }, 'a' ); +// returns true + +bool = isReadableProperty( [ 'a' ], 0 ); +// returns true + +bool = isReadableProperty( { 'null': false }, null ); +// returns true + +bool = isReadableProperty( { '[object Object]': false }, {} ); +// returns true + +bool = isReadableProperty( {}, 'toString' ); +// returns false + +bool = isReadableProperty( {}, 'hasOwnProperty' ); +// returns false + +bool = isReadableProperty( null, 'a' ); +// returns false + +bool = isReadableProperty( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-readable-property/benchmark/benchmark.js b/is-readable-property/benchmark/benchmark.js new file mode 100644 index 00000000..249cdcd7 --- /dev/null +++ b/is-readable-property/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isReadableProperty = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isReadableProperty( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-readable-property/docs/repl.txt b/is-readable-property/docs/repl.txt new file mode 100644 index 00000000..81bb0bd2 --- /dev/null +++ b/is-readable-property/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( value, property ) + Tests if an object's own property is readable. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own property is readable. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = false; + > desc.set = function setter( v ) { obj.boop = v; }; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + true + > bool = {{alias}}( obj, 'beep' ) + false + + See Also + -------- + diff --git a/is-readable-property/docs/types/index.d.ts b/is-readable-property/docs/types/index.d.ts new file mode 100644 index 00000000..c4129181 --- /dev/null +++ b/is-readable-property/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own property is readable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is readable +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'set': setter +* }); +* +* var bool = isReadableProperty( obj, 'boop' ); +* // returns true +* +* bool = isReadableProperty( obj, 'beep' ); +* // returns false +*/ +declare function isReadableProperty( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isReadableProperty; diff --git a/is-readable-property/docs/types/test.ts b/is-readable-property/docs/types/test.ts new file mode 100644 index 00000000..3cb23261 --- /dev/null +++ b/is-readable-property/docs/types/test.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isReadableProperty = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + + const setter = ( v: boolean ) => { + obj.boop = v; + }; + + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'set': setter + } ); + isReadableProperty( obj, 'beep' ); // $ExpectType boolean + isReadableProperty( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isReadableProperty(); // $ExpectError + isReadableProperty( {} ); // $ExpectError + isReadableProperty( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-readable-property/examples/index.js b/is-readable-property/examples/index.js new file mode 100644 index 00000000..cbeabf46 --- /dev/null +++ b/is-readable-property/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isReadableProperty = require( './../lib' ); + +var bool = isReadableProperty( [ 'a' ], 'length' ); +console.log( bool ); +// => true + +bool = isReadableProperty( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = isReadableProperty( [ 'a' ], 0 ); +console.log( bool ); +// => true + +bool = isReadableProperty( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = isReadableProperty( { '[object Object]': false }, {} ); +console.log( bool ); +// => true + +bool = isReadableProperty( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isReadableProperty( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isReadableProperty( null, 'a' ); +console.log( bool ); +// => false + +bool = isReadableProperty( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-readable-property/lib/index.js b/is-readable-property/lib/index.js new file mode 100644 index 00000000..e12e54ba --- /dev/null +++ b/is-readable-property/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own property is readable. +* +* @module @stdlib/assert/is-readable-property +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isReadableProperty = require( '@stdlib/assert/is-readable-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'set': setter +* }); +* +* var bool = isReadableProperty( obj, 'boop' ); +* // returns true +* +* bool = isReadableProperty( obj, 'beep' ); +* // returns false +*/ + +// MODULES // + +var isReadableProperty = require( './main.js' ); + + +// EXPORTS // + +module.exports = isReadableProperty; diff --git a/is-readable-property/lib/main.js b/is-readable-property/lib/main.js new file mode 100644 index 00000000..840e3ea4 --- /dev/null +++ b/is-readable-property/lib/main.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptor = require( '@stdlib/utils/property-descriptor' ); + + +// MAIN // + +/** +* Tests if an object's own property is readable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is readable +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'set': setter +* }); +* +* var bool = isReadableProperty( obj, 'boop' ); +* // returns true +* +* bool = isReadableProperty( obj, 'beep' ); +* // returns false +*/ +function isReadableProperty( value, property ) { + var desc = propertyDescriptor( value, property ); + return ( + desc !== null && + ( + desc.writable !== void 0 || // data descriptor + typeof desc.get === 'function' // accessor descriptor + ) + ); +} + + +// EXPORTS // + +module.exports = isReadableProperty; diff --git a/is-readable-property/package.json b/is-readable-property/package.json new file mode 100644 index 00000000..df7e3597 --- /dev/null +++ b/is-readable-property/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-readable-property", + "version": "0.0.0", + "description": "Test if an object's own property is readable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "readable", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-readable-property/test/test.js b/is-readable-property/test/test.js new file mode 100644 index 00000000..eef76068 --- /dev/null +++ b/is-readable-property/test/test.js @@ -0,0 +1,151 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isReadableProperty = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isReadableProperty, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property is readable', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isReadableProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadableProperty( [ 1, 2, 3 ], '1' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadableProperty( [ 1, 2, 3 ], 1 ); + t.equal( bool, true, 'returns true' ); + + bool = isReadableProperty( new Foo(), 'bar' ); + t.equal( bool, true, 'returns true' ); + + bool = isReadableProperty( [ 'a' ], 'length' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'b' + }); + + bool = isReadableProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'get': getter + }); + + bool = isReadableProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function getter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isReadableProperty( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isReadableProperty( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a readable property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + defineProperty( obj, 'd', { + 'configurable': true, + 'enumerable': true, + 'set': setter + }); + + bool = isReadableProperty( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadableProperty( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + t.end(); + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided an inherited property', function test( t ) { + var bool; + + bool = isReadableProperty( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadableProperty( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isReadableProperty( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isReadableProperty( 'beep', 'length' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/is-reference-error/README.md b/is-reference-error/README.md new file mode 100644 index 00000000..3a40b114 --- /dev/null +++ b/is-reference-error/README.md @@ -0,0 +1,129 @@ + + +# isReferenceError + +> Test if a value is a [ReferenceError][mdn-reference-error] object. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var isReferenceError = require( '@stdlib/assert/is-reference-error' ); +``` + +#### isReferenceError( value ) + +Tests if a `value` is a [`ReferenceError`][mdn-reference-error] object. + +```javascript +var bool = isReferenceError( new ReferenceError( 'beep' ) ); +// returns true +``` + +
+ + + + + +
+ +## Notes + +- This function should **not** be considered robust. While the function should **always** return `true` if provided a [`ReferenceError`][mdn-reference-error] (or a descendant) object, false positives may occur due to the fact that the [`ReferenceError`][mdn-reference-error] constructor inherits from [`Error`][mdn-error] and has no internal class of its own. Hence, [`ReferenceError`][mdn-reference-error] impersonation is possible. + +
+ + + + + +
+ +## Examples + + + +```javascript +var isReferenceError = require( '@stdlib/assert/is-reference-error' ); + +var bool = isReferenceError( new ReferenceError( 'reference error' ) ); +// returns true + +bool = isReferenceError( new Error( 'error' ) ); +// returns false + +bool = isReferenceError( new EvalError( 'eval error' ) ); +// returns false + +bool = isReferenceError( new RangeError( 'range error' ) ); +// returns false + +bool = isReferenceError( new SyntaxError( 'syntax error' ) ); +// returns false + +bool = isReferenceError( new TypeError( 'type error' ) ); +// returns false + +bool = isReferenceError( new URIError( 'URI error' ) ); +// returns false + +bool = isReferenceError( {} ); +// returns false + +bool = isReferenceError( null ); +// returns false +``` + +
+ + + + + +
+ +
+ + + + + + + + diff --git a/is-reference-error/benchmark/benchmark.js b/is-reference-error/benchmark/benchmark.js new file mode 100644 index 00000000..cacfaac2 --- /dev/null +++ b/is-reference-error/benchmark/benchmark.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isReferenceError = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + {}, + [], + null, + false, + true, + new Error( 'beep' ), + new ReferenceError( 'bar' ), + new TypeError( 'boop' ), + new SyntaxError( 'foo' ) + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isReferenceError( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-reference-error/docs/repl.txt b/is-reference-error/docs/repl.txt new file mode 100644 index 00000000..d2ff266f --- /dev/null +++ b/is-reference-error/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( value ) + Tests if a value is a ReferenceError object. + + This function should *not* be considered robust. While the function should + always return `true` if provided a ReferenceError (or a descendant) + object, false positives may occur due to the fact that the ReferenceError + constructor inherits from Error and has no internal class of its own. + Hence, ReferenceError impersonation is possible. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a ReferenceError object. + + Examples + -------- + > var bool = {{alias}}( new ReferenceError( 'beep' ) ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-reference-error/docs/types/index.d.ts b/is-reference-error/docs/types/index.d.ts new file mode 100644 index 00000000..1e893206 --- /dev/null +++ b/is-reference-error/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a `ReferenceError` object. +* +* ## Notes +* +* - This function should **not** be considered robust. While the function should always return `true` if provided a ReferenceError (or a descendant) object, false positives may occur due to the fact that the ReferenceError constructor inherits from Error and has no internal class of its own. Hence, ReferenceError impersonation is possible. +* +* @param value - value to test +* @returns boolean indicating whether a value is a `ReferenceError` object +* +* @example +* var bool = isReferenceError( new ReferenceError( 'beep' ) ); +* // returns true +* +* @example +* var bool = isReferenceError( {} ); +* // returns false +*/ +declare function isReferenceError( value: any ): boolean; + + +// EXPORTS // + +export = isReferenceError; diff --git a/is-reference-error/docs/types/test.ts b/is-reference-error/docs/types/test.ts new file mode 100644 index 00000000..2358c84f --- /dev/null +++ b/is-reference-error/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isReferenceError = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isReferenceError( new ReferenceError( 'beep' ) ); // $ExpectType boolean + isReferenceError( {} ); // $ExpectType boolean +} + +// The function does not compile if provided insufficient arguments... +{ + isReferenceError(); // $ExpectError + isReferenceError( new ReferenceError( 'beep' ), 123 ); // $ExpectError +} diff --git a/is-reference-error/examples/index.js b/is-reference-error/examples/index.js new file mode 100644 index 00000000..f7f401d6 --- /dev/null +++ b/is-reference-error/examples/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isReferenceError = require( './../lib' ); + +var bool = isReferenceError( new ReferenceError( 'reference error' ) ); +console.log( bool ); +// => true + +bool = isReferenceError( new Error( 'error' ) ); +console.log( bool ); +// => false + +bool = isReferenceError( new EvalError( 'eval error' ) ); +console.log( bool ); +// => false + +bool = isReferenceError( new RangeError( 'range error' ) ); +console.log( bool ); +// => false + +bool = isReferenceError( new SyntaxError( 'syntax error' ) ); +console.log( bool ); +// => false + +bool = isReferenceError( new TypeError( 'type error' ) ); +console.log( bool ); +// => false + +bool = isReferenceError( new URIError( 'URI error' ) ); +console.log( bool ); +// => false + +bool = isReferenceError( {} ); +console.log( bool ); +// => false + +bool = isReferenceError( null ); +console.log( bool ); +// => false diff --git a/is-reference-error/lib/index.js b/is-reference-error/lib/index.js new file mode 100644 index 00000000..0d72dd6a --- /dev/null +++ b/is-reference-error/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a `ReferenceError` object. +* +* @module @stdlib/assert/is-reference-error +* +* @example +* var isReferenceError = require( '@stdlib/assert/is-reference-error' ); +* +* var bool = isReferenceError( new ReferenceError( 'beep' ) ); +* // returns true +* +* bool = isReferenceError( {} ); +* // returns false +*/ + +// MODULES // + +var isReferenceError = require( './main.js' ); + + +// EXPORTS // + +module.exports = isReferenceError; diff --git a/is-reference-error/lib/main.js b/is-reference-error/lib/main.js new file mode 100644 index 00000000..4f06e4ab --- /dev/null +++ b/is-reference-error/lib/main.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var getPrototypeOf = require( '@stdlib/utils/get-prototype-of' ); +var typeOf = require( '@stdlib/utils/type-of' ); +var isError = require( '@stdlib/assert/is-error' ); + + +// MAIN // + +/** +* Tests if a value is a `ReferenceError` object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is a `ReferenceError` object +* +* @example +* var bool = isReferenceError( new ReferenceError( 'beep' ) ); +* // returns true +* +* @example +* var bool = isReferenceError( {} ); +* // returns false +*/ +function isReferenceError( value ) { + if ( typeof value !== 'object' || value === null ) { + return false; + } + // Check for `ReferenceError` objects from the same realm (same Node.js `vm` or same `Window` object)... + if ( value instanceof ReferenceError ) { + return true; + } + // All `ReferenceError` objects are `Error` objects... + if ( isError( value ) ) { + // Walk the prototype tree until we find the desired constructor... + while ( value ) { + if ( typeOf( value ) === 'referenceerror' ) { + return true; + } + value = getPrototypeOf( value ); + } + } + return false; +} + + +// EXPORTS // + +module.exports = isReferenceError; diff --git a/is-reference-error/package.json b/is-reference-error/package.json new file mode 100644 index 00000000..a4f43adf --- /dev/null +++ b/is-reference-error/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-reference-error", + "version": "0.0.0", + "description": "Test if a value is a ReferenceError object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "reference", + "error", + "err", + "exception", + "is", + "iserr", + "iserror", + "valid", + "isvalid", + "type", + "check", + "validate", + "validation", + "test" + ] +} diff --git a/is-reference-error/test/test.js b/is-reference-error/test/test.js new file mode 100644 index 00000000..2d6f4328 --- /dev/null +++ b/is-reference-error/test/test.js @@ -0,0 +1,115 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var vm = require( 'vm' ); // TODO: handle in-browser tests +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var inherit = require( '@stdlib/utils/inherit' ); +var isReferenceError = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': IS_BROWSER +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isReferenceError, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a `ReferenceError` object', function test( t ) { + var values; + var i; + + values = [ + new ReferenceError( 'reference error' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isReferenceError( values[ i ] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an object inheriting from `ReferenceError`', function test( t ) { + function CustomError( msg ) { + ReferenceError.call( this ); + this.name = 'CustomError'; + this.message = msg; + return this; + } + + inherit( CustomError, ReferenceError ); + + t.strictEqual( isReferenceError( new CustomError( 'custom error' ) ), true, 'returns true when provided a value which inherits from ReferenceError' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a `ReferenceError` object from a different realm', opts, function test( t ) { + var error = vm.runInNewContext( 'new ReferenceError()' ); + t.strictEqual( isReferenceError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an object from a different realm which inherits from a `ReferenceError` object', opts, function test( t ) { + var error = vm.runInNewContext( 'function Err() { return this; }; Err.prototype = new ReferenceError(); new Err();' ); + t.strictEqual( isReferenceError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a `ReferenceError` object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined, + [], + {}, + function noop() {}, + function error() {}, + new Date(), + new RegExp( '.*' ), + new Error( 'error' ), + new EvalError( 'eval error' ), + new RangeError( 'range error' ), + new SyntaxError( 'syntax error' ), + new TypeError( 'type error' ), + new URIError( 'URI error' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isReferenceError( values[ i ] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-regexp-string/README.md b/is-regexp-string/README.md new file mode 100644 index 00000000..ea71bc0f --- /dev/null +++ b/is-regexp-string/README.md @@ -0,0 +1,136 @@ + + +# isRegExpString + +> Test if a value is a regular expression string. + +
+ +
+ + + +
+ +## Usage + +```javascript +var isRegExpString = require( '@stdlib/assert/is-regexp-string' ); +``` + +#### isRegExpString( value ) + +Tests if a `value` is a regular expression `string`. + +```javascript +var bool = isRegExpString( '/^beep$/' ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isRegExpString = require( '@stdlib/assert/is-regexp-string' ); + +var bool; + +bool = isRegExpString( '/beep/' ); +// returns true + +bool = isRegExpString( '/beep/gim' ); +// returns true + +bool = isRegExpString( 'beep' ); +// returns false + +bool = isRegExpString( '' ); +// returns false + +bool = isRegExpString( null ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-regexp-string [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-regexp-string '/beep/' +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n '/beep/' | is-regexp-string +true +``` + +
+ + + +
+ + + + + + diff --git a/is-regexp-string/benchmark/benchmark.js b/is-regexp-string/benchmark/benchmark.js new file mode 100644 index 00000000..b429647f --- /dev/null +++ b/is-regexp-string/benchmark/benchmark.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isRegExpString = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 'beep', + 'boop', + '/.*/', + '/[A-F]/ig', + '/^[0-9]$/g', + '' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isRegExpString( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-regexp-string/bin/cli b/is-regexp-string/bin/cli new file mode 100644 index 00000000..303b0cc8 --- /dev/null +++ b/is-regexp-string/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isRegExpString = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isRegExpString( args[ 0 ] ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isRegExpString( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-regexp-string/docs/repl.txt b/is-regexp-string/docs/repl.txt new file mode 100644 index 00000000..b7217004 --- /dev/null +++ b/is-regexp-string/docs/repl.txt @@ -0,0 +1,28 @@ + +{{alias}}( value ) + Tests if a value is a regular expression string. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a regular expression string. + + Examples + -------- + > var bool = {{alias}}( '/beep/' ) + true + > bool = {{alias}}( 'beep' ) + false + > bool = {{alias}}( '' ) + false + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-regexp-string/docs/types/index.d.ts b/is-regexp-string/docs/types/index.d.ts new file mode 100644 index 00000000..26fda4ef --- /dev/null +++ b/is-regexp-string/docs/types/index.d.ts @@ -0,0 +1,52 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a regular expression string. +* +* @param value - value to test +* @returns boolean indicating whether a value is a regular expression string +* +* @example +* var bool = isRegExpString( '/beep/' ); +* // returns true +* +* @example +* var bool = isRegExpString( '/beep/gim' ); +* // returns true +* +* @example +* var bool = isRegExpString( 'beep' ); +* // returns false +* +* @example +* var bool = isRegExpString( '' ); +* // returns false +* +* @example +* var bool = isRegExpString( null ); +* // returns false +*/ +declare function isRegExpString( value: any ): boolean; + + +// EXPORTS // + +export = isRegExpString; diff --git a/is-regexp-string/docs/types/test.ts b/is-regexp-string/docs/types/test.ts new file mode 100644 index 00000000..2bbe18ee --- /dev/null +++ b/is-regexp-string/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isRegExpString = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isRegExpString( /\.+/ ); // $ExpectType boolean + isRegExpString( '/beep/' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isRegExpString(); // $ExpectError + isRegExpString( '/beep/', 123 ); // $ExpectError +} diff --git a/is-regexp-string/docs/usage.txt b/is-regexp-string/docs/usage.txt new file mode 100644 index 00000000..0f84b25d --- /dev/null +++ b/is-regexp-string/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-regexp-string [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-regexp-string/etc/cli_opts.json b/is-regexp-string/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-regexp-string/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-regexp-string/examples/index.js b/is-regexp-string/examples/index.js new file mode 100644 index 00000000..85096c5f --- /dev/null +++ b/is-regexp-string/examples/index.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isRegExpString = require( './../lib' ); + +console.log( isRegExpString( '/beep/' ) ); +// => true + +console.log( isRegExpString( '/beep/gim' ) ); +// => true + +console.log( isRegExpString( 'beep' ) ); +// => false + +console.log( isRegExpString( '' ) ); +// => false + +console.log( isRegExpString( null ) ); +// => false diff --git a/is-regexp-string/lib/index.js b/is-regexp-string/lib/index.js new file mode 100644 index 00000000..7f28f010 --- /dev/null +++ b/is-regexp-string/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a regular expression string. +* +* @module @stdlib/assert/is-regexp-string +* +* @example +* var isRegExpString = require( '@stdlib/assert/is-regexp-string' ); +* +* var bool = isRegExpString( '/beep/' ); +* // returns true +* +* bool = isRegExpString( 'beep' ); +* // returns false +* +* bool = isRegExpString( '' ); +* // returns false +* +* bool = isRegExpString( null ); +* // returns false +*/ + +// MODULES // + +var isRegExpString = require( './main.js' ); + + +// EXPORTS // + +module.exports = isRegExpString; diff --git a/is-regexp-string/lib/main.js b/is-regexp-string/lib/main.js new file mode 100644 index 00000000..97541380 --- /dev/null +++ b/is-regexp-string/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var RE = require( '@stdlib/regexp/regexp' ).REGEXP; + + +// MAIN // + +/** +* Tests if a value is a regular expression string. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is a regular expression string +* +* @example +* var bool = isRegExpString( '/beep/' ); +* // returns true +* +* @example +* var bool = isRegExpString( '/beep/gim' ); +* // returns true +* +* @example +* var bool = isRegExpString( 'beep' ); +* // returns false +* +* @example +* var bool = isRegExpString( '' ); +* // returns false +* +* @example +* var bool = isRegExpString( null ); +* // returns false +*/ +function isRegExpString( value ) { + return isString( value ) && RE.test( value ); +} + + +// EXPORTS // + +module.exports = isRegExpString; diff --git a/is-regexp-string/package.json b/is-regexp-string/package.json new file mode 100644 index 00000000..fc9829a1 --- /dev/null +++ b/is-regexp-string/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-regexp-string", + "version": "0.0.0", + "description": "Test if a value is a regular expression string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-regexp-string": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utils", + "util", + "utilities", + "utility", + "isregexp", + "regular expression", + "regexp", + "regex", + "regular", + "expression", + "string", + "str", + "test", + "validate", + "valid", + "is", + "check" + ] +} diff --git a/is-regexp-string/test/fixtures/stdin_error.js.txt b/is-regexp-string/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-regexp-string/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-regexp-string/test/test.cli.js b/is-regexp-string/test/test.cli.js new file mode 100644 index 00000000..120a6c88 --- /dev/null +++ b/is-regexp-string/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument is a regular expression', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'/beep/gim\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "/beep\n/boop/"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'false\ntrue\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-regexp-string/test/test.js b/is-regexp-string/test/test.js new file mode 100644 index 00000000..9f4d44c5 --- /dev/null +++ b/is-regexp-string/test/test.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isRegExpString = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isRegExpString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a regular expression string', function test( t ) { + t.equal( isRegExpString( '/beep/' ), true, 'returns true' ); + t.equal( isRegExpString( '/beep/gim' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a regular expression string', function test( t ) { + var values; + var i; + + values = [ + 5, + [], + '5', + '/beep', + 'beep/', + 'beep/gim', + /\.+/, + new RegExp( 'beep', 'gim' ), + function noop() {}, + null, + NaN, + {}, + undefined + ]; + for ( i = 0; i < values.length; i++ ) { + t.equal( isRegExpString( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-regexp/README.md b/is-regexp/README.md new file mode 100644 index 00000000..ce27d148 --- /dev/null +++ b/is-regexp/README.md @@ -0,0 +1,82 @@ + + +# isRegExp + +> Test if a value is a regular expression. + +
+ +## Usage + +```javascript +var isRegExp = require( '@stdlib/assert/is-regexp' ); +``` + +#### isRegExp( value ) + +Tests if a `value` is a regular expression. + +```javascript +var bool = isRegExp( /.+/ ); +// returns true + +bool = isRegExp( {} ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isRegExp = require( '@stdlib/assert/is-regexp' ); + +var bool = isRegExp( /.+/ ); +// returns true + +bool = isRegExp( new RegExp( '.+' ) ); +// returns true + +bool = isRegExp( '/.+/' ); +// returns false + +bool = isRegExp( {} ); +// returns false + +bool = isRegExp( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-regexp/benchmark/benchmark.js b/is-regexp/benchmark/benchmark.js new file mode 100644 index 00000000..a6c94c43 --- /dev/null +++ b/is-regexp/benchmark/benchmark.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isRegExp = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + /.*/, + new RegExp( '.*' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isRegExp( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-regexp/docs/repl.txt b/is-regexp/docs/repl.txt new file mode 100644 index 00000000..94983ef2 --- /dev/null +++ b/is-regexp/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a regular expression. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a regular expression. + + Examples + -------- + > var bool = {{alias}}( /\.+/ ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-regexp/docs/types/index.d.ts b/is-regexp/docs/types/index.d.ts new file mode 100644 index 00000000..26853f6c --- /dev/null +++ b/is-regexp/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a regular expression. +* +* @param value - value to test +* @returns boolean indicating whether value is a regular expression +* +* @example +* var bool = isRegExp( /\.+/ ); +* // returns true +* +* @example +* var bool = isRegExp( {} ); +* // returns false +*/ +declare function isRegExp( value: any ): boolean; + + +// EXPORTS // + +export = isRegExp; diff --git a/is-regexp/docs/types/test.ts b/is-regexp/docs/types/test.ts new file mode 100644 index 00000000..56bd124a --- /dev/null +++ b/is-regexp/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isRegExp = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isRegExp( /\.+/ ); // $ExpectType boolean + isRegExp( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isRegExp(); // $ExpectError + isRegExp( ( x: number ) => x, 123 ); // $ExpectError +} diff --git a/is-regexp/examples/index.js b/is-regexp/examples/index.js new file mode 100644 index 00000000..b121762f --- /dev/null +++ b/is-regexp/examples/index.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isRegExp = require( './../lib' ); + +console.log( isRegExp( /.+/ ) ); +// => true + +console.log( isRegExp( new RegExp( '.+' ) ) ); +// => true + +console.log( isRegExp( '/.+/' ) ); +// => false + +console.log( isRegExp( {} ) ); +// => false + +console.log( isRegExp( null ) ); +// => false diff --git a/is-regexp/lib/exec.js b/is-regexp/lib/exec.js new file mode 100644 index 00000000..42064ed2 --- /dev/null +++ b/is-regexp/lib/exec.js @@ -0,0 +1,26 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var exec = RegExp.prototype.exec; // non-generic + + +// EXPORTS // + +module.exports = exec; diff --git a/is-regexp/lib/index.js b/is-regexp/lib/index.js new file mode 100644 index 00000000..895473c0 --- /dev/null +++ b/is-regexp/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a regular expression. +* +* @module @stdlib/assert/is-regexp +* +* @example +* var isRegExp = require( '@stdlib/assert/is-regexp' ); +* +* var bool = isRegExp( /\.+/ ); +* // returns true +* +* bool = isRegExp( {} ); +* // returns false +*/ + +// MODULES // + +var isRegExp = require( './main.js' ); + + +// EXPORTS // + +module.exports = isRegExp; diff --git a/is-regexp/lib/main.js b/is-regexp/lib/main.js new file mode 100644 index 00000000..a891150a --- /dev/null +++ b/is-regexp/lib/main.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); +var nativeClass = require( '@stdlib/utils/native-class' ); +var test = require( './try2exec.js' ); + + +// VARIABLES // + +var FLG = hasToStringTag(); + + +// MAIN // + +/** +* Tests if a value is a regular expression. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a regular expression +* +* @example +* var bool = isRegExp( /\.+/ ); +* // returns true +* +* @example +* var bool = isRegExp( {} ); +* // returns false +*/ +function isRegExp( value ) { + if ( typeof value === 'object' ) { + if ( value instanceof RegExp ) { + return true; + } + if ( FLG ) { + return test( value ); + } + return ( nativeClass( value ) === '[object RegExp]' ); + } + return false; +} + + +// EXPORTS // + +module.exports = isRegExp; diff --git a/is-regexp/lib/try2exec.js b/is-regexp/lib/try2exec.js new file mode 100644 index 00000000..8a9b41c7 --- /dev/null +++ b/is-regexp/lib/try2exec.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var exec = require( './exec.js' ); + + +// MAIN // + +/** +* Attempts to call a `RegExp` method. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating if able to call a `RegExp` method +*/ +function test( value ) { + try { + exec.call( value ); + return true; + } catch ( err ) { // eslint-disable-line no-unused-vars + return false; + } +} + + +// EXPORTS // + +module.exports = test; diff --git a/is-regexp/package.json b/is-regexp/package.json new file mode 100644 index 00000000..62612167 --- /dev/null +++ b/is-regexp/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-regexp", + "version": "0.0.0", + "description": "Test if a value is a regular expression.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "valid", + "validate", + "regular expression", + "regexp", + "regex", + "is", + "type", + "check", + "instanceof", + "isregexp", + "isregex" + ] +} diff --git a/is-regexp/test/test.js b/is-regexp/test/test.js new file mode 100644 index 00000000..aea620d3 --- /dev/null +++ b/is-regexp/test/test.js @@ -0,0 +1,131 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); +var isRegExp = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isRegExp, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a regular expression', function test( t ) { + t.strictEqual( isRegExp( /\.+/ ), true, 'returns true' ); + t.strictEqual( isRegExp( new RegExp( '\\.+' ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'if `Symbol.toStringTag` is supported, the function guards against objects masquerading as regular expressions', opts, function test( t ) { + var isRegExp; + var mock; + + isRegExp = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-tostringtag-support': detect + }); + + mock = { + 'toString': toString, + 'exec': exec + }; + if ( hasToStringTag() ) { + mock[ Symbol.toStringTag ] = 'RegExp'; + } + t.strictEqual( isRegExp( mock ), false, 'returns false' ); + + t.end(); + + function detect() { + return true; + } + function toString() { + return '/beep/'; + } + function exec() { + return null; + } +}); + +tape( 'if `Symbol.toStringTag` is supported, the function returns `true` if provided a regular expression', opts, function test( t ) { + var isRegExp = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-tostringtag-support': detect + }); + + t.strictEqual( isRegExp( /beep/ ), true, 'returns true' ); + + t.end(); + + function detect() { + return true; + } +}); + +tape( 'if `Symbol.toStringTag` is not supported, the function attempts to determine the native class', function test( t ) { + var isRegExp = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-tostringtag-support': detect + }); + + t.strictEqual( isRegExp( /beep/ ), true, 'returns true' ); + t.strictEqual( isRegExp( {} ), false, 'returns false' ); + + t.end(); + + function detect() { + return false; + } +}); + +tape( 'the function returns `false` if not provided a regular expression', function test( t ) { + var values; + var i; + + values = [ + '5', + '/.+/', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isRegExp( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-relative-path/README.md b/is-relative-path/README.md new file mode 100644 index 00000000..5bc9af89 --- /dev/null +++ b/is-relative-path/README.md @@ -0,0 +1,216 @@ + + +# isRelativePath + +> Test if a value is a relative path. + +
+ +
+ + + +
+ +## Usage + +```javascript +var isRelativePath = require( '@stdlib/assert/is-relative-path' ); +``` + +#### isRelativePath( value ) + +Tests if a `value` is a relative path. + +```javascript +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); + +var bool; +if ( IS_WINDOWS ) { + bool = isRelativePath( 'foo\\bar\\baz' ); + // returns true + + bool = isRelativePath( 'C:\\foo\\..\\bar\\baz' ); + // returns false +} else { + bool = isRelativePath( './foo/bar/baz' ); + // returns true + + bool = isRelativePath( '/foo/../bar/baz' ); + // returns false +} +``` + +#### isRelativePath.posix( value ) + +Tests if a `value` is a POSIX relative path. + +```javascript +var bool = isRelativePath.posix( './foo/bar/baz' ); +// returns true + +bool = isRelativePath.posix( '/foo/../bar/baz' ); +// returns false +``` + +#### isRelativePath.win32( value ) + +Tests if a `value` is a Windows relative path. + +```javascript +var bool = isRelativePath.win32( 'foo\\bar\\baz' ); +// returns true + +bool = isRelativePath.win32( 'C:\\foo\\..\\bar\\baz' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- `isRelativePath()` is platform-specific. On Windows platforms, the function is equal to `isRelativePath.win32()`. On POSIX platforms, the function is equal to `isRelativePath.posix()`. + +
+ + + +
+ +## Examples + + + +```javascript +var isRelativePath = require( '@stdlib/assert/is-relative-path' ); + +var bool = isRelativePath.posix( 'foo/' ); +// returns true + +bool = isRelativePath.posix( 'foo' ); +// returns true + +bool = isRelativePath.posix( '.' ); +// returns true + +bool = isRelativePath.posix( '' ); +// returns true + +bool = isRelativePath.posix( '/foo/bar/baz' ); +// returns false + +bool = isRelativePath.posix( '/foo/..' ); +// returns false + +bool = isRelativePath.win32( 'foo\\bar\\baz' ); +// returns true + +bool = isRelativePath.win32( 'foo/bar/baz' ); +// returns true + +bool = isRelativePath.win32( 'foo/..' ); +// returns true + +bool = isRelativePath.win32( '.' ); +// returns true + +bool = isRelativePath.win32( '' ); +// returns true + +bool = isRelativePath.win32( 'C:\\foo\\bar\\baz' ); +// returns false + +bool = isRelativePath.win32( '//server' ); +// returns false + +bool = isRelativePath.win32( '\\\\server' ); +// returns false + +bool = isRelativePath.win32( 'C:/foo/bar/baz' ); +// returns false + +bool = isRelativePath.win32( '/foo/..' ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-relative-path [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --platform name Platform: 'win32' or 'posix'. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-relative-path ./foo/bar/baz --platform=posix +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n './docs/repl.txt' | is-relative-path +true +``` + +
+ + + +
+ + + + + + diff --git a/is-relative-path/benchmark/benchmark.js b/is-relative-path/benchmark/benchmark.js new file mode 100644 index 00000000..6062a493 --- /dev/null +++ b/is-relative-path/benchmark/benchmark.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isRelativePath = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = './foo/bar/beep/boop/'+fromCodePoint( 97 + (i%26) ); + bool = isRelativePath( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':posix', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = './foo/bar/beep/boop/'+fromCodePoint( 97 + (i%26) ); + bool = isRelativePath.posix( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':win32', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = 'C:\\foo\\bar\\beep\\boop\\'+fromCodePoint( 97 + (i%26) ); + bool = isRelativePath.win32( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-relative-path/bin/cli b/is-relative-path/bin/cli new file mode 100644 index 00000000..c49460ae --- /dev/null +++ b/is-relative-path/bin/cli @@ -0,0 +1,100 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isRelativePath = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + var fun; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + if ( flags.platform === 'win32' ) { + fun = isRelativePath.win32; + } else if ( flags.platform === 'posix' ) { + fun = isRelativePath.posix; + } else { + fun = isRelativePath; + } + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( fun( args[ 0 ] ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( fun( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-relative-path/docs/repl.txt b/is-relative-path/docs/repl.txt new file mode 100644 index 00000000..df242f1e --- /dev/null +++ b/is-relative-path/docs/repl.txt @@ -0,0 +1,73 @@ + +{{alias}}( value ) + Tests if a value is a relative path. + + Function behavior is platform-specific. On Windows platforms, the function + is equal to `.win32()`. On POSIX platforms, the function is equal to + `.posix()`. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a relative path. + + Examples + -------- + // Windows environments: + > var bool = {{alias}}( 'foo\\bar\\baz' ) + true + + // POSIX environments: + > bool = {{alias}}( './foo/bar/baz' ) + true + + +{{alias}}.posix( value ) + Tests if a value is a POSIX relative path. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a POSIX relative path. + + Examples + -------- + > var bool = {{alias}}.posix( './foo/bar/baz' ) + true + > bool = {{alias}}.posix( '/foo/../bar/baz' ) + false + + +{{alias}}.win32( value ) + Tests if a value is a Windows relative path. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a Windows relative path. + + Examples + -------- + > var bool = {{alias}}( 'foo\\bar\\baz' ) + true + > bool = {{alias}}( 'C:\\foo\\..\\bar\\baz' ) + false + + See Also + -------- + diff --git a/is-relative-path/docs/types/index.d.ts b/is-relative-path/docs/types/index.d.ts new file mode 100644 index 00000000..3b8f31b3 --- /dev/null +++ b/is-relative-path/docs/types/index.d.ts @@ -0,0 +1,131 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isRelativePath` with methods for testing POSIX and Windows paths, respectively. +*/ +interface IsRelativePath { + /** + * Tests if a value is a relative path. + * + * @param value - value to test + * @returns boolean indicating whether value is a relative path + * + * @example + * var IS_WINDOWS = require( `@stdlib/assert/is-windows` ); + * var bool; + * if ( IS_WINDOWS ) { + * bool = isRelativePath( 'foo\\bar\\baz' ); + * // returns true + * + * bool = isRelativePath( 'C:\\foo\\..\\bar\\baz' ); + * // returns false + * } else { + * bool = isRelativePath( './foo/bar/baz' ); + * // returns true + * + * bool = isRelativePath( '/foo/../bar/baz' ); + * // returns false + * } + */ + ( value: any ): boolean; + + /** + * Tests if a value is a POSIX relative path. + * + * @param value - value to test + * @returns boolean indicating whether value is a POSIX relative path + * + * @example + * var bool = isRelativePath.posix( './foo/bar/baz' ); + * // returns true + * + * @example + * var bool = isRelativePath.posix( '/foo/../bar/baz' ); + * // returns false + */ + posix( value: any ): boolean; + + /** + * Tests if a value is a Windows relative path. + * + * @param value - value to test + * @returns boolean indicating whether value is a Windows relative path + * + * @example + * var bool = isRelativePath.win32( 'foo\\bar\\baz' ); + * // returns true + * + * @example + * var bool = isRelativePath.win32( 'C:\\foo\\..\\bar\\baz' ); + * // returns false + */ + win32( value: any ): boolean; +} + +/** +* Tests if a value is a relative path. +* +* ## Notes +* +* - Function behavior is platform-specific. On Windows platforms, the function is equal to `.win32()`. On POSIX platforms, the function is equal to `.posix()`. +* +* @param value - value to test +* @returns boolean indicating whether value is a relative path +* +* @example +* var IS_WINDOWS = require( `@stdlib/assert/is-windows` ); +* var bool; +* if ( IS_WINDOWS ) { +* bool = isRelativePath( 'foo\\bar\\baz' ); +* // returns true +* +* bool = isRelativePath( 'C:\\foo\\..\\bar\\baz' ); +* // returns false +* } else { +* bool = isRelativePath( './foo/bar/baz' ); +* // returns true +* +* bool = isRelativePath( '/foo/../bar/baz' ); +* // returns false +* } +* +* @example +* var bool = isRelativePath.posix( './foo/bar/baz' ); +* // returns true +* +* @example +* var bool = isRelativePath.posix( '/foo/../bar/baz' ); +* // returns false +* +* @example +* var bool = isRelativePath.win32( 'foo\\bar\\baz' ); +* // returns true +* +* @example +* var bool = isRelativePath.win32( 'C:\\foo\\..\\bar\\baz' ); +* // returns false +*/ +declare var isRelativePath: IsRelativePath; + + +// EXPORTS // + +export = isRelativePath; diff --git a/is-relative-path/docs/types/test.ts b/is-relative-path/docs/types/test.ts new file mode 100644 index 00000000..072038e5 --- /dev/null +++ b/is-relative-path/docs/types/test.ts @@ -0,0 +1,58 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isRelativePath = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isRelativePath( 'foo\\bar\\baz' ); // $ExpectType boolean + isRelativePath( './foo/bar/baz' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isRelativePath(); // $ExpectError + isRelativePath( 'foo\\bar\\baz', 123 ); // $ExpectError +} + +// Attached to main export is a `posix` method which returns a boolean... +{ + isRelativePath.posix( './foo/bar/baz' ); // $ExpectType boolean + isRelativePath.posix( '/foo/../bar/baz' ); // $ExpectType boolean +} + +// The compiler throws an error if the `posix` method is provided an unsupported number of arguments... +{ + isRelativePath.posix(); // $ExpectError + isRelativePath.posix( '/foo/../bar/baz', 123 ); // $ExpectError +} + +// Attached to main export is a `win32` method which returns a boolean... +{ + isRelativePath.win32( 'foo\\bar\\baz' ); // $ExpectType boolean + isRelativePath.win32( 'C:\\foo\\..\\bar\\baz' ); // $ExpectType boolean +} + +// The compiler throws an error if the `win32` method is provided an unsupported number of arguments... +{ + isRelativePath.win32(); // $ExpectError + isRelativePath.win32( 'C:\\foo\\..\\bar\\baz', 123 ); // $ExpectError +} diff --git a/is-relative-path/docs/usage.txt b/is-relative-path/docs/usage.txt new file mode 100644 index 00000000..e9fc4704 --- /dev/null +++ b/is-relative-path/docs/usage.txt @@ -0,0 +1,9 @@ + +Usage: is-relative-path [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --platform name Platform: 'win32' or 'posix'. + diff --git a/is-relative-path/etc/cli_opts.json b/is-relative-path/etc/cli_opts.json new file mode 100644 index 00000000..cd0c304c --- /dev/null +++ b/is-relative-path/etc/cli_opts.json @@ -0,0 +1,17 @@ +{ + "string": [ + "platform" + ], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-relative-path/examples/index.js b/is-relative-path/examples/index.js new file mode 100644 index 00000000..a393664d --- /dev/null +++ b/is-relative-path/examples/index.js @@ -0,0 +1,85 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isRelativePath = require( './../lib' ); + +var bool = isRelativePath.posix( 'foo/' ); +console.log( bool ); +// => true + +bool = isRelativePath.posix( 'foo' ); +console.log( bool ); +// => true + +bool = isRelativePath.posix( '.' ); +console.log( bool ); +// => true + +bool = isRelativePath.posix( '' ); +console.log( bool ); +// => true + +bool = isRelativePath.posix( '/foo/bar/baz' ); +console.log( bool ); +// => false + +bool = isRelativePath.posix( '/foo/..' ); +console.log( bool ); +// => false + +bool = isRelativePath.win32( 'foo\\bar\\baz' ); +console.log( bool ); +// => true + +bool = isRelativePath.win32( 'foo/bar/baz' ); +console.log( bool ); +// => true + +bool = isRelativePath.win32( 'foo/..' ); +console.log( bool ); +// => true + +bool = isRelativePath.win32( '.' ); +console.log( bool ); +// => true + +bool = isRelativePath.win32( '' ); +console.log( bool ); +// => true + +bool = isRelativePath.win32( 'C:\\foo\\bar\\baz' ); +console.log( bool ); +// => false + +bool = isRelativePath.win32( '//server' ); +console.log( bool ); +// => false + +bool = isRelativePath.win32( '\\\\server' ); +console.log( bool ); +// => false + +bool = isRelativePath.win32( 'C:/foo/bar/baz' ); +console.log( bool ); +// => false + +bool = isRelativePath.win32( '/foo/..' ); +console.log( bool ); +// => false diff --git a/is-relative-path/lib/index.js b/is-relative-path/lib/index.js new file mode 100644 index 00000000..a0ab86d3 --- /dev/null +++ b/is-relative-path/lib/index.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a relative path. +* +* @module @stdlib/assert/is-relative-path +* +* @example +* var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +* var isRelativePath = require( '@stdlib/assert/is-relative-path' ); +* +* var bool; +* if ( IS_WINDOWS ) { +* bool = isRelativePath( 'foo\\bar\\baz' ); +* // returns true +* +* bool = isRelativePath( 'C:\\foo\\..\\bar\\baz' ); +* // returns false +* } else { +* bool = isRelativePath( './foo/bar/baz' ); +* // returns true +* +* bool = isRelativePath( '/foo/../bar/baz' ); +* // returns false +* } +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var isRelativePathPosix = require( './posix.js' ); +var isRelativePathWin32 = require( './win32.js' ); + + +// MAIN // + +var isRelativePath; +if ( IS_WINDOWS ) { + isRelativePath = isRelativePathWin32; +} else { + isRelativePath = isRelativePathPosix; +} +setReadOnly( isRelativePath, 'posix', isRelativePathPosix ); +setReadOnly( isRelativePath, 'win32', isRelativePathWin32 ); + + +// EXPORTS // + +module.exports = isRelativePath; diff --git a/is-relative-path/lib/posix.js b/is-relative-path/lib/posix.js new file mode 100644 index 00000000..42385c18 --- /dev/null +++ b/is-relative-path/lib/posix.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isAbsolutePath = require( '@stdlib/assert/is-absolute-path' ).posix; + + +// MAIN // + +/** +* Tests if a value is a POSIX relative path. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a POSIX relative path +* +* @example +* var bool = isRelativePath( './foo/bar/baz' ); +* // returns true +* +* @example +* var bool = isRelativePath( '/foo/../bar/baz' ); +* // returns false +*/ +function isRelativePath( value ) { + return ( + isString( value ) && + !isAbsolutePath( value ) + ); +} + + +// EXPORTS // + +module.exports = isRelativePath; diff --git a/is-relative-path/lib/win32.js b/is-relative-path/lib/win32.js new file mode 100644 index 00000000..ae7fdbeb --- /dev/null +++ b/is-relative-path/lib/win32.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isAbsolutePath = require( '@stdlib/assert/is-absolute-path' ).win32; + + +// MAIN // + +/** +* Tests if a value is a Windows relative path. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a Windows relative path +* +* @example +* var bool = isRelativePath( 'foo\\bar\\baz' ); +* // returns true +* +* @example +* var bool = isRelativePath( 'C:\\foo\\..\\bar\\baz' ); +* // returns false +*/ +function isRelativePath( value ) { + return ( + isString( value ) && + !isAbsolutePath( value ) + ); +} + + +// EXPORTS // + +module.exports = isRelativePath; diff --git a/is-relative-path/package.json b/is-relative-path/package.json new file mode 100644 index 00000000..6ab17686 --- /dev/null +++ b/is-relative-path/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/assert/is-relative-path", + "version": "0.0.0", + "description": "Test if a value is a relative path.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "is-relative", + "is-path", + "path", + "relative", + "filesystem", + "file", + "filepath", + "filename", + "dir", + "directory", + "dirpath", + "check", + "validate", + "valid", + "isvalid", + "is-valid", + "test" + ] +} diff --git a/is-relative-path/test/fixtures/stdin_error.js.txt b/is-relative-path/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-relative-path/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-relative-path/test/test.cli.js b/is-relative-path/test/test.cli.js new file mode 100644 index 00000000..ea659759 --- /dev/null +++ b/is-relative-path/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument is a relative path', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'/var/www/\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'false\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "./index.js\n/var/www/"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\nfalse\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-relative-path/test/test.js b/is-relative-path/test/test.js new file mode 100644 index 00000000..b677c539 --- /dev/null +++ b/is-relative-path/test/test.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var isRelativePath = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isRelativePath, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if platform is POSIX, main export is method to test if a string is a POSIX relative path', function test( t ) { + var isRelativePath = proxyquire( './../lib', { + '@stdlib/assert/is-windows': false + }); + t.equal( isRelativePath, isRelativePath.posix, 'equals posix method' ); + t.end(); +}); + +tape( 'if platform is Windows, main export is method to test if a string is a Windows relative path', function test( t ) { + var isRelativePath = proxyquire( './../lib', { + '@stdlib/assert/is-windows': true + }); + t.equal( isRelativePath, isRelativePath.win32, 'equals win32 method' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test if a string is a POSIX relative path', function test( t ) { + t.equal( typeof isRelativePath.posix, 'function', 'has a posix method' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test if a string is a Windows relative path', function test( t ) { + t.equal( typeof isRelativePath.win32, 'function', 'has a win32 method' ); + t.end(); +}); diff --git a/is-relative-path/test/test.posix.js b/is-relative-path/test/test.posix.js new file mode 100644 index 00000000..3d2bbfd5 --- /dev/null +++ b/is-relative-path/test/test.posix.js @@ -0,0 +1,100 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isRelativePath = require( './../lib/posix.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isRelativePath, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a string', function test( t ) { + var values; + var bool; + var i; + + values = [ + 5, + NaN, + null, + void 0, + true, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isRelativePath( values[ i ] ); + t.equal( bool, false, 'returns `false` when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an empty string', function test( t ) { + var bool = isRelativePath( '' ); + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a POSIX relative path', function test( t ) { + var values; + var bool; + var i; + + values = [ + '.', + 'foo', + 'foo/bar', + './foo/bar', + '..', + '../foo/bar', + 'foo/../bar' + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isRelativePath( values[ i ] ); + t.strictEqual( bool, true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided an absolute POSIX path', function test( t ) { + var values; + var bool; + var i; + + values = [ + '/foo/bar/baz', + '/foo/..', + '/beep boop', + '/' + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isRelativePath( values[ i ] ); + t.strictEqual( bool, false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-relative-path/test/test.win32.js b/is-relative-path/test/test.win32.js new file mode 100644 index 00000000..c76522a6 --- /dev/null +++ b/is-relative-path/test/test.win32.js @@ -0,0 +1,114 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isRelativePath = require( './../lib/win32.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isRelativePath, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a string', function test( t ) { + var values; + var bool; + var i; + + values = [ + 5, + NaN, + null, + void 0, + true, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isRelativePath( values[ i ] ); + t.equal( bool, false, 'returns `false` when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an empty string', function test( t ) { + var bool = isRelativePath( '' ); + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a Windows relative path', function test( t ) { + var values; + var bool; + var i; + + values = [ + '.', + 'foo', + 'foo/bar', + './foo/bar', + '..', + '../foo/bar', + 'foo/../bar', + 'foo\\bar', + '.\\foo\\bar', + '..\\foo\\bar', + 'foo\\..\\bar' + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isRelativePath( values[ i ] ); + t.strictEqual( bool, true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided a Windows absolute path', function test( t ) { + var values; + var bool; + var i; + + values = [ + '/foo/bar/baz', + '/foo/..', + '/beep boop', + '/', + '\\', + '\\foo\\bar\\baz', + '\\foo\\..', + '\\beep boop', + 'C:\\foo\\bar\\baz', + 'C:/foo/bar/baz', + 'c:\\foo\\bar\\baz', + 'c:/foo/bar/baz', + '//server', + '\\\\server' + ]; + for ( i = 0; i < values.length; i++ ) { + bool = isRelativePath( values[ i ] ); + t.strictEqual( bool, false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-safe-integer-array/README.md b/is-safe-integer-array/README.md new file mode 100644 index 00000000..158f0aaa --- /dev/null +++ b/is-safe-integer-array/README.md @@ -0,0 +1,138 @@ + + +# isSafeIntegerArray + +> Test if a value is an array-like object containing only [safe integers][@stdlib/assert/is-safe-integer]. + +
+ +## Usage + +```javascript +var isSafeIntegerArray = require( '@stdlib/assert/is-safe-integer-array' ); +``` + +#### isSafeIntegerArray( value ) + +Tests if a `value` is an array-like object containing **only** [safe `integer`][@stdlib/assert/is-safe-integer] values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isSafeIntegerArray( [ -3, new Number(3) ] ); +// returns true + +bool = isSafeIntegerArray( [ -1e100, 2e200 ] ); +// returns false + +bool = isSafeIntegerArray( [ -3, 'abc' ] ); +// returns false +``` + +#### isSafeIntegerArray.primitives( value ) + +Tests if a `value` is an array-like object containing **only** primitive [safe `integer`][@stdlib/assert/is-safe-integer] values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isSafeIntegerArray.primitives( [ -1.0, 0.0, 4.0 ] ); +// returns true + +bool = isSafeIntegerArray.primitives( [ -1.0, 2.2 ] ); +// returns false + +bool = isSafeIntegerArray.primitives( [ -3.0, new Number(2.0) ] ); +// returns false +``` + +#### isSafeIntegerArray.objects( value ) + +Tests if a `value` is an array-like object containing **only** `Number` objects holding [safe `integer`][@stdlib/assert/is-safe-integer] values. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isSafeIntegerArray.objects( [ new Number(-1.0), new Number(2.0) ] ); +// returns true + +bool = isSafeIntegerArray.objects( [ -1.0, 0.0, 1.0 ] ); +// returns false + +bool = isSafeIntegerArray.objects( [ -3.0, new Number(1.0) ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isSafeIntegerArray = require( '@stdlib/assert/is-safe-integer-array' ); + +var bool = isSafeIntegerArray( [ -5, 0, 2, 5 ] ); +// returns true + +bool = isSafeIntegerArray( [ -4, -3, 1, 3 ] ); +// returns true + +bool = isSafeIntegerArray( [ -1, new Number( -6 ), 2 ] ); +// returns true + +bool = isSafeIntegerArray( [ 1e100, 2e200, 3e300 ] ); +// returns false + +bool = isSafeIntegerArray( [ -1, 'abc', 3 ] ); +// returns false + +bool = isSafeIntegerArray( [ -2.3, 0, 3 ] ); +// returns false + +bool = isSafeIntegerArray( [] ); +// returns false +``` + +
+ + + + + + diff --git a/is-safe-integer-array/benchmark/benchmark.js b/is-safe-integer-array/benchmark/benchmark.js new file mode 100644 index 00000000..3da807fa --- /dev/null +++ b/is-safe-integer-array/benchmark/benchmark.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var isSafeIntegerArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( 1.0 ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new Number( 1.0 ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isSafeIntegerArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isSafeIntegerArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isSafeIntegerArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-safe-integer-array/docs/repl.txt b/is-safe-integer-array/docs/repl.txt new file mode 100644 index 00000000..acb69830 --- /dev/null +++ b/is-safe-integer-array/docs/repl.txt @@ -0,0 +1,86 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only safe integers. + + An integer valued number is "safe" when the number can be exactly + represented as a double-precision floating-point number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing + only safe integers. + + Examples + -------- + > var arr = [ -3.0, new Number(0.0), 2.0 ]; + > var bool = {{alias}}( arr ) + true + > arr = [ -3.0, '3.0' ]; + > bool = {{alias}}( arr ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only primitive safe + integer values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + primitive safe integer values. + + Examples + -------- + > var arr = [ -1.0, 10.0 ]; + > var bool = {{alias}}.primitives( arr ) + true + > arr = [ -1.0, 0.0, 5.0 ]; + > bool = {{alias}}.primitives( arr ) + true + > arr = [ -3.0, new Number(-1.0) ]; + > bool = {{alias}}.primitives( arr ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only `Number` objects + having safe integer values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array-like object containing only + `Number` objects having safe integer values. + + Examples + -------- + > var arr = [ new Number(1.0), new Number(3.0) ]; + > var bool = {{alias}}.objects( arr ) + true + > arr = [ -1.0, 0.0, 3.0 ]; + > bool = {{alias}}.objects( arr ) + false + > arr = [ 3.0, new Number(-1.0) ]; + > bool = {{alias}}.objects( arr ) + false + + See Also + -------- + diff --git a/is-safe-integer-array/docs/types/index.d.ts b/is-safe-integer-array/docs/types/index.d.ts new file mode 100644 index 00000000..081d0c36 --- /dev/null +++ b/is-safe-integer-array/docs/types/index.d.ts @@ -0,0 +1,129 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isSafeIntegerArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsSafeIntegerArray { + /** + * Tests if a value is an array-like object containing only safe integers. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only safe integers + * + * @example + * var bool = isSafeIntegerArray( [ -3.0, new Number(0.0), 2.0 ] ); + * // returns true + * + * @example + * var bool = isSafeIntegerArray( [ -3.0, '3.0' ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only primitive safe integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only primitive safe integer values + * + * @example + * var bool = isSafeIntegerArray.primitives( [ -1.0, 10.0 ] ); + * // returns true + * + * @example + * var bool = isSafeIntegerArray.primitives( [ -1.0, 0.0, 5.0 ] ); + * // returns true + * + * @example + * var bool = isSafeIntegerArray.primitives( [ -3.0, new Number(-1.0) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only number objects having safe integer values. + * + * @param value - value to test + * @returns boolean indicating whether value is an array-like object containing only number objects having safe integer values + * + * @example + * var bool = isSafeIntegerArray.objects( [ new Number(1.0), new Number(3.0) ] ); + * // returns true + * + * @example + * var bool = isSafeIntegerArray.objects( [ -1.0, 0.0, 3.0 ] ); + * // returns false + * + * @example + * var bool = isSafeIntegerArray.objects( [ 3.0, new Number(-1.0) ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object containing only safe integers. +* +* ## Notes +* +* - An integer valued number is "safe" when the number can be exactly represented as a double-precision floating-point number. +* +* @param value - value to test +* @returns boolean indicating whether value is an array-like object containing only safe integers +* +* @example +* var bool = isSafeIntegerArray( [ -3.0, new Number(0.0), 2.0 ] ); +* // returns true +* +* @example +* var bool = isSafeIntegerArray( [ -3.0, '3.0' ] ); +* // returns false +* +* @example +* var bool = isSafeIntegerArray.primitives( [ -1.0, 10.0 ] ); +* // returns true +* +* @example +* var bool = isSafeIntegerArray.primitives( [ -1.0, 0.0, 5.0 ] ); +* // returns true +* +* @example +* var bool = isSafeIntegerArray.primitives( [ -3.0, new Number(-1.0) ] ); +* // returns false +* +* @example +* var bool = isSafeIntegerArray.objects( [ new Number(1.0), new Number(3.0) ] ); +* // returns true +* +* @example +* var bool = isSafeIntegerArray.objects( [ -1.0, 0.0, 3.0 ] ); +* // returns false +* +* @example +* var bool = isSafeIntegerArray.objects( [ 3.0, new Number(-1.0) ] ); +* // returns false +*/ +declare var isSafeIntegerArray: IsSafeIntegerArray; + + +// EXPORTS // + +export = isSafeIntegerArray; diff --git a/is-safe-integer-array/docs/types/test.ts b/is-safe-integer-array/docs/types/test.ts new file mode 100644 index 00000000..30437361 --- /dev/null +++ b/is-safe-integer-array/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isSafeIntegerArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isSafeIntegerArray( [ 4 ] ); // $ExpectType boolean + isSafeIntegerArray( [ 2.8 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSafeIntegerArray(); // $ExpectError + isSafeIntegerArray( [ -3 ], 123 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isSafeIntegerArray.primitives( [ new Number( 3 ) ] ); // $ExpectType boolean + isSafeIntegerArray.primitives( [ 3 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isSafeIntegerArray.primitives(); // $ExpectError + isSafeIntegerArray.primitives( [ 2 ], 123 ); // $ExpectError +} + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isSafeIntegerArray.objects( [ new Number( -2 ) ] ); // $ExpectType boolean + isSafeIntegerArray.objects( [ -2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isSafeIntegerArray.objects(); // $ExpectError + isSafeIntegerArray.objects( [ 2 ], 123 ); // $ExpectError +} diff --git a/is-safe-integer-array/examples/index.js b/is-safe-integer-array/examples/index.js new file mode 100644 index 00000000..9fce1e2f --- /dev/null +++ b/is-safe-integer-array/examples/index.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isSafeIntegerArray = require( './../lib' ); + +var bool = isSafeIntegerArray( [ -5, 0, 2, 5 ] ); +console.log( bool ); +// => true + +bool = isSafeIntegerArray( [ -4, -3, 1, 3 ] ); +console.log( bool ); +// => true + +bool = isSafeIntegerArray( [ -1, new Number( -6 ), 2 ] ); +console.log( bool ); +// => true + +bool = isSafeIntegerArray( [ 1e100, 2e200, 3e300 ] ); +console.log( bool ); +// => false + +bool = isSafeIntegerArray( [ -1, 'abc', 3 ] ); +console.log( bool ); +// => false + +bool = isSafeIntegerArray( [ -2.3, 0, 3 ] ); +console.log( bool ); +// => false + +bool = isSafeIntegerArray( [] ); +console.log( bool ); +// => false diff --git a/is-safe-integer-array/lib/index.js b/is-safe-integer-array/lib/index.js new file mode 100644 index 00000000..ba3ad45a --- /dev/null +++ b/is-safe-integer-array/lib/index.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only safe integers. +* +* @module @stdlib/assert/is-safe-integer-array +* +* @example +* var isSafeIntegerArray = require( '@stdlib/assert/is-safe-integer-array' ); +* +* var bool = isSafeIntegerArray( [ -3.0, new Number(0.0), 2.0 ] ); +* // returns true +* +* bool = isSafeIntegerArray( [ -3.0, '3.0' ] ); +* // returns false +* +* @example +* var isSafeIntegerArray = require( '@stdlib/assert/is-safe-integer-array' ).primitives; +* +* var bool = isSafeIntegerArray( [ -1.0, 10.0 ] ); +* // returns true +* +* bool = isSafeIntegerArray( [ -1.0, 0.0, 5.0 ] ); +* // returns true +* +* bool = isSafeIntegerArray( [ -3.0, new Number(-1.0) ] ); +* // returns false +* +* @example +* var isSafeIntegerArray = require( '@stdlib/assert/is-safe-integer-array' ).objects; +* +* var bool = isSafeIntegerArray( [ new Number(1.0), new Number(3.0) ] ); +* // returns true +* +* bool = isSafeIntegerArray( [ -1.0, 0.0, 3.0 ] ); +* // returns false +* +* bool = isSafeIntegerArray( [ 3.0, new Number(-1.0) ] ); +* // returns false +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); +var isSafeInteger = require( '@stdlib/assert/is-safe-integer' ); + + +// MAIN // + +var isSafeIntegerArray = arrayfun( isSafeInteger ); +setReadOnly( isSafeIntegerArray, 'primitives', arrayfun( isSafeInteger.isPrimitive ) ); +setReadOnly( isSafeIntegerArray, 'objects', arrayfun( isSafeInteger.isObject ) ); + + +// EXPORTS // + +module.exports = isSafeIntegerArray; diff --git a/is-safe-integer-array/package.json b/is-safe-integer-array/package.json new file mode 100644 index 00000000..bcde2dfc --- /dev/null +++ b/is-safe-integer-array/package.json @@ -0,0 +1,83 @@ +{ + "name": "@stdlib/assert/is-safe-integer-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only safe integers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "number", + "numeric", + "isnumber", + "isnumeric", + "integer", + "int", + "isinteger", + "safe", + "issafeinteger", + "number.issafeinteger", + "type", + "check", + "validate", + "valid", + "test" + ] +} diff --git a/is-safe-integer-array/test/test.js b/is-safe-integer-array/test/test.js new file mode 100644 index 00000000..3877a3f9 --- /dev/null +++ b/is-safe-integer-array/test/test.js @@ -0,0 +1,106 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isSafeIntegerArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSafeIntegerArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only safe integer values', function test( t ) { + var arr; + + arr = [ 5, new Number( 5 ), -3 ]; // eslint-disable-line no-new-wrappers + t.equal( isSafeIntegerArray( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': 2, + '1': 1 + }; + t.equal( isSafeIntegerArray( arr ), true, 'returns true' ); + + arr = [ 1e100, 2e200, 3e300 ]; + t.equal( isSafeIntegerArray( arr ), false, 'returns false' ); + + arr = [ 5.0, 2.3, 11.1 ]; + t.equal( isSafeIntegerArray( arr ), false, 'returns false' ); + + arr = [ 5.0, '3', null ]; + t.equal( isSafeIntegerArray( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only primitive safe integer values', function test( t ) { + var arr; + + arr = [ 5, -0, 0 ]; + t.equal( isSafeIntegerArray.primitives( arr ), true, 'returns true' ); + + arr = [ 1e100, 2e200, 3e300 ]; + t.equal( isSafeIntegerArray.primitives( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': 1, + '1': -3 + }; + t.equal( isSafeIntegerArray.primitives( arr ), true, 'returns true' ); + + arr = [ new Number( 5 ), 1, 1 ]; // eslint-disable-line no-new-wrappers + t.equal( isSafeIntegerArray.primitives( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only `Number` objects having safe integer values', function test( t ) { + var arr; + + arr = [ new Number( 5 ), new Number( -0 ), new Number( 0 ) ]; // eslint-disable-line no-new-wrappers + t.equal( isSafeIntegerArray.objects( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': new Number( 2 ), // eslint-disable-line no-new-wrappers + '1': new Number( -3 ) // eslint-disable-line no-new-wrappers + }; + t.equal( isSafeIntegerArray.objects( arr ), true, 'returns true' ); + + arr = [ new Number( 1e100 ), new Number( 2e200 ), new Number( 3e300 ) ]; // eslint-disable-line no-new-wrappers + t.equal( isSafeIntegerArray.objects( arr ), false, 'returns false' ); + + arr = [ new Number( 5 ), 1, 1 ]; // eslint-disable-line no-new-wrappers + t.equal( isSafeIntegerArray.objects( arr ), false, 'returns false' ); + + arr = [ -5, 1, 1 ]; + t.equal( isSafeIntegerArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-safe-integer/README.md b/is-safe-integer/README.md new file mode 100644 index 00000000..236f0655 --- /dev/null +++ b/is-safe-integer/README.md @@ -0,0 +1,173 @@ + + +# isSafeInteger + +> Test if a value is a number having a safe integer value. + +
+ +## Usage + +```javascript +var isSafeInteger = require( '@stdlib/assert/is-safe-integer' ); +``` + +#### isSafeInteger( value ) + +Tests if a value is a `number` having a safe `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isSafeInteger( 5.0 ); +// returns true + +bool = isSafeInteger( new Number( 5.0 ) ); +// returns true + +bool = isSafeInteger( 2.0e200 ); +// returns false + +bool = isSafeInteger( -3.14 ); +// returns false + +bool = isSafeInteger( null ); +// returns false +``` + +#### isSafeInteger.isPrimitive( value ) + +Tests if a `value` is a primitive `number` having a safe `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isSafeInteger.isPrimitive( -3.0 ); +// returns true + +bool = isSafeInteger.isPrimitive( new Number( -3.0 ) ); +// returns false +``` + +#### isSafeInteger.isObject( value ) + +Tests if a `value` is a `Number` object having a safe `integer` value. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isSafeInteger.isObject( 3.0 ); +// returns false + +bool = isSafeInteger.isObject( new Number( 3.0 ) ); +// returns true +``` + +
+ + + +
+ +## Notes + +- An integer valued number is "safe" when the number can be exactly represented as a [double-precision floating-point number][ieee754]. For example, + + ```javascript + var MAX_SAFE_INTEGER = require( '@stdlib/constants/math/float64-max-safe-integer' ); + // returns 9007199254740991 + + var x = 9007199254740992; + // returns 9007199254740992 + + var y = 9007199254740993; + // returns 9007199254740992 + + var bool = ( x === y ); + // returns true + ``` + + In this example, `x` and `y` should be distinct, but they are not due to constraints imposed by storing numeric values in [double-precision floating-point format][ieee754]. "Safe" integers are thus [double-precision floating-point numbers][ieee754] having integer values with unequivocal binary representations. + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isSafeInteger = require( '@stdlib/assert/is-safe-integer' ); + +var bool = isSafeInteger( -5.0 ); +// returns true + +bool = isSafeInteger( 0.0 ); +// returns true + +bool = isSafeInteger( new Number( 5.0 ) ); +// returns true + +bool = isSafeInteger( 2.0e200 ); +// returns false + +bool = isSafeInteger( 5.256 ); +// returns false + +bool = isSafeInteger( 1.0/0.0 ); +// returns false + +bool = isSafeInteger( -1.0/0.0 ); +// returns false + +bool = isSafeInteger( NaN ); +// returns false + +bool = isSafeInteger( '5' ); +// returns false + +bool = isSafeInteger( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-safe-integer/benchmark/benchmark.js b/is-safe-integer/benchmark/benchmark.js new file mode 100644 index 00000000..4a9aac83 --- /dev/null +++ b/is-safe-integer/benchmark/benchmark.js @@ -0,0 +1,233 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Number = require( '@stdlib/number/ctor' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isSafeInteger = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 2e200, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSafeInteger( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 2e200 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSafeInteger( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 2e200, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSafeInteger.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 2e200 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSafeInteger.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 2e200, + -5, + -4, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSafeInteger.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 2.0 ), + new Number( 3.0 ), + new Number( 2e200 ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSafeInteger.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-safe-integer/docs/repl.txt b/is-safe-integer/docs/repl.txt new file mode 100644 index 00000000..1ebb5003 --- /dev/null +++ b/is-safe-integer/docs/repl.txt @@ -0,0 +1,77 @@ + +{{alias}}( value ) + Tests if a value is a safe integer. + + An integer valued number is "safe" when the number can be exactly + represented as a double-precision floating-point number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a safe integer. + + Examples + -------- + > var bool = {{alias}}( 5.0 ) + true + > bool = {{alias}}( new Number( 5.0 ) ) + true + > bool = {{alias}}( 2.0e200 ) + false + > bool = {{alias}}( -3.14 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive having a safe integer value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive having a safe + integer value. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( -3.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( -3.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a `Number` object having a safe integer value. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a `Number` object having a safe + integer value. + + Examples + -------- + > var bool = {{alias}}.isObject( 3.0 ) + false + > bool = {{alias}}.isObject( new Number( 3.0 ) ) + true + + See Also + -------- + diff --git a/is-safe-integer/docs/types/index.d.ts b/is-safe-integer/docs/types/index.d.ts new file mode 100644 index 00000000..db95a75c --- /dev/null +++ b/is-safe-integer/docs/types/index.d.ts @@ -0,0 +1,130 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isSafeInteger` with methods for testing for primitives and objects, respectively. +*/ +interface IsSafeInteger { + /** + * Tests if a value is a safe integer. + * + * @param value - value to test + * @returns boolean indicating whether value is a safe integer + * + * @example + * var bool = isSafeInteger( 5.0 ); + * // returns true + * + * @example + * var bool = isSafeInteger( new Number( 5.0 ) ); + * // returns true + * + * @example + * var bool = isSafeInteger( 2.0e300 ); + * // returns false + * + * @example + * var bool = isSafeInteger( -3.14 ); + * // returns false + * + * @example + * var bool = isSafeInteger( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a safe integer value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a safe integer value + * + * @example + * var bool = isSafeInteger.isPrimitive( -3.0 ); + * // returns true + * + * @example + * var bool = isSafeInteger.isPrimitive( new Number( -3.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + + /** + * Tests if a value is a number object having a safe integer value. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a safe integer value + * + * @example + * var bool = isSafeInteger.isObject( 3.0 ); + * // returns false + * + * @example + * var bool = isSafeInteger.isObject( new Number( 3.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a safe integer. +* +* ## Notes +* +* - An integer valued number is "safe" when the number can be exactly represented as a double-precision floating-point number. +* +* @param value - value to test +* @returns boolean indicating whether value is a safe integer +* +* @example +* var bool = isSafeInteger( 5.0 ); +* // returns true +* +* @example +* var bool = isSafeInteger( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isSafeInteger( 2.0e300 ); +* // returns false +* +* @example +* var bool = isSafeInteger( -3.14 ); +* // returns false +* +* @example +* var bool = isSafeInteger( null ); +* // returns false +* +* @example +* var bool = isSafeInteger.isPrimitive( -3.0 ); +* // returns true +* +* @example +* var bool = isSafeInteger.isObject( new Number( 3.0 ) ); +* // returns true +*/ +declare var isSafeInteger: IsSafeInteger; + + +// EXPORTS // + +export = isSafeInteger; diff --git a/is-safe-integer/docs/types/test.ts b/is-safe-integer/docs/types/test.ts new file mode 100644 index 00000000..85c10b3d --- /dev/null +++ b/is-safe-integer/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isSafeInteger = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isSafeInteger( 1.2 ); // $ExpectType boolean + isSafeInteger( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSafeInteger(); // $ExpectError + isSafeInteger( 2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isSafeInteger.isPrimitive( new Number( 2 ) ); // $ExpectType boolean + isSafeInteger.isPrimitive( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isSafeInteger.isPrimitive(); // $ExpectError + isSafeInteger.isPrimitive( 2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isSafeInteger.isObject( new Number( 2 ) ); // $ExpectType boolean + isSafeInteger.isObject( 2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isSafeInteger.isObject(); // $ExpectError + isSafeInteger.isObject( 2, 123 ); // $ExpectError +} diff --git a/is-safe-integer/examples/index.js b/is-safe-integer/examples/index.js new file mode 100644 index 00000000..f0b2652f --- /dev/null +++ b/is-safe-integer/examples/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isSafeInteger = require( './../lib' ); + +console.log( isSafeInteger( -5.0 ) ); +// => true + +console.log( isSafeInteger( 0.0 ) ); +// => true + +console.log( isSafeInteger( new Number( 5.0 ) ) ); +// => true + +console.log( isSafeInteger( 3.0e200 ) ); +// => false + +console.log( isSafeInteger( 5.256 ) ); +// => false + +console.log( isSafeInteger( 1.0/0.0 ) ); +// => false + +console.log( isSafeInteger( -1.0/0.0 ) ); +// => false + +console.log( isSafeInteger( NaN ) ); +// => false + +console.log( isSafeInteger( '5' ) ); +// => false + +console.log( isSafeInteger( null ) ); +// => false diff --git a/is-safe-integer/lib/index.js b/is-safe-integer/lib/index.js new file mode 100644 index 00000000..cef72ce8 --- /dev/null +++ b/is-safe-integer/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a safe integer. +* +* @module @stdlib/assert/is-safe-integer +* +* @example +* var isSafeInteger = require( '@stdlib/assert/is-safe-integer' ); +* +* var bool = isSafeInteger( 5.0 ); +* // returns true +* +* bool = isSafeInteger( new Number( 5.0 ) ); +* // returns true +* +* bool = isSafeInteger( 2.0e200 ); +* // returns false +* +* bool = isSafeInteger( -3.14 ); +* // returns false +* +* bool = isSafeInteger( null ); +* // returns false +* +* @example +* var isSafeInteger = require( '@stdlib/assert/is-safe-integer' ).isPrimitive; +* +* var bool = isSafeInteger( -3.0 ); +* // returns true +* +* bool = isSafeInteger( new Number( -3.0 ) ); +* // returns false +* +* @example +* var isSafeInteger = require( '@stdlib/assert/is-safe-integer' ).isObject; +* +* var bool = isSafeInteger( 3.0 ); +* // returns false +* +* bool = isSafeInteger( new Number( 3.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isSafeInteger = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isSafeInteger, 'isPrimitive', isPrimitive ); +setReadOnly( isSafeInteger, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isSafeInteger; diff --git a/is-safe-integer/lib/main.js b/is-safe-integer/lib/main.js new file mode 100644 index 00000000..d7c98466 --- /dev/null +++ b/is-safe-integer/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a safe integer. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a safe integer +* +* @example +* var bool = isSafeInteger( 5.0 ); +* // returns true +* +* @example +* var bool = isSafeInteger( new Number( 5.0 ) ); +* // returns true +* +* @example +* var bool = isSafeInteger( 2.0e300 ); +* // returns false +* +* @example +* var bool = isSafeInteger( -3.14 ); +* // returns false +* +* @example +* var bool = isSafeInteger( null ); +* // returns false +*/ +function isSafeInteger( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isSafeInteger; diff --git a/is-safe-integer/lib/object.js b/is-safe-integer/lib/object.js new file mode 100644 index 00000000..b657b00e --- /dev/null +++ b/is-safe-integer/lib/object.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isObject; +var isSafeInt = require( '@stdlib/math/base/assert/is-safe-integer' ); + + +// MAIN // + +/** +* Tests if a value is a number object having a safe integer value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a safe integer value +* +* @example +* var bool = isSafeInteger( 3.0 ); +* // returns false +* +* @example +* var bool = isSafeInteger( new Number( 3.0 ) ); +* // returns true +*/ +function isSafeInteger( value ) { + return ( + isNumber( value ) && + isSafeInt( value.valueOf() ) + ); +} + + +// EXPORTS // + +module.exports = isSafeInteger; diff --git a/is-safe-integer/lib/primitive.js b/is-safe-integer/lib/primitive.js new file mode 100644 index 00000000..9d906598 --- /dev/null +++ b/is-safe-integer/lib/primitive.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isSafeInt = require( '@stdlib/math/base/assert/is-safe-integer' ); + + +// MAIN // + +/** +* Tests if a value is a number primitive having a safe integer value. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a safe integer value +* +* @example +* var bool = isSafeInteger( -3.0 ); +* // returns true +* +* @example +* var bool = isSafeInteger( new Number( -3.0 ) ); +* // returns false +*/ +function isSafeInteger( value ) { + return ( + isNumber( value ) && + isSafeInt( value ) + ); +} + + +// EXPORTS // + +module.exports = isSafeInteger; diff --git a/is-safe-integer/package.json b/is-safe-integer/package.json new file mode 100644 index 00000000..4b6c1359 --- /dev/null +++ b/is-safe-integer/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-safe-integer", + "version": "0.0.0", + "description": "Test if a value is a number having a safe integer value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "integer", + "int", + "is", + "isinteger", + "safe", + "issafeinteger", + "number.issafeinteger", + "type", + "check", + "primitive", + "object", + "validate", + "isvalid", + "valid" + ] +} diff --git a/is-safe-integer/test/test.js b/is-safe-integer/test/test.js new file mode 100644 index 00000000..927458d5 --- /dev/null +++ b/is-safe-integer/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isSafeInteger = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSafeInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive number having a safe integer value', function test( t ) { + t.strictEqual( typeof isSafeInteger.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a number object having a safe integer value', function test( t ) { + t.strictEqual( typeof isSafeInteger.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-safe-integer/test/test.main.js b/is-safe-integer/test/test.main.js new file mode 100644 index 00000000..85e98e3a --- /dev/null +++ b/is-safe-integer/test/test.main.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isSafeInteger = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSafeInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number having a safe integer value', function test( t ) { + t.strictEqual( isSafeInteger( 5 ), true, 'returns true' ); + t.strictEqual( isSafeInteger( -5 ), true, 'returns true' ); + t.strictEqual( isSafeInteger( new Number( 5 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.strictEqual( isSafeInteger( new Number( -5 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number having a safe integer value', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + 2.0e200, + -2.0e200, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSafeInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-safe-integer/test/test.object.js b/is-safe-integer/test/test.object.js new file mode 100644 index 00000000..73030365 --- /dev/null +++ b/is-safe-integer/test/test.object.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isSafeInteger = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSafeInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a safe integer value', function test( t ) { + t.strictEqual( isSafeInteger( new Number( 5 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a number object not having a safe integer value', function test( t ) { + t.strictEqual( isSafeInteger( new Number( 2.0e200 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number, even if the number is a safe integer value', function test( t ) { + t.strictEqual( isSafeInteger( 3.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSafeInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-safe-integer/test/test.primitive.js b/is-safe-integer/test/test.primitive.js new file mode 100644 index 00000000..405feed5 --- /dev/null +++ b/is-safe-integer/test/test.primitive.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isSafeInteger = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSafeInteger, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive number having a safe integer value', function test( t ) { + t.strictEqual( isSafeInteger( -3.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive number not having a safe integer value', function test( t ) { + t.strictEqual( isSafeInteger( 2.0e200 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object, even if the number has a safe integer value', function test( t ) { + t.strictEqual( isSafeInteger( new Number( 5.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSafeInteger( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-same-value-zero/README.md b/is-same-value-zero/README.md new file mode 100644 index 00000000..54efaf0f --- /dev/null +++ b/is-same-value-zero/README.md @@ -0,0 +1,138 @@ + + +# isSameValueZero + +> Test if two arguments are the same value. + +
+ +## Usage + +```javascript +var isSameValueZero = require( '@stdlib/assert/is-same-value-zero' ); +``` + +#### isSameValueZero( a, b ) + +Tests if two arguments `a` and `b` are the same value. + +```javascript +var bool = isSameValueZero( false, false ); +// returns true + +bool = isSameValueZero( '', '' ); +// returns true + +bool = isSameValueZero( {}, {} ); +// returns false +``` + +In contrast to the strict equality operator `===`, the function treats `NaNs` as the same value. + + + +```javascript +var bool = ( NaN === NaN ); +// returns false + +bool = isSameValueZero( NaN, NaN ); +// returns true +``` + +In contrast to the [SameValue Algorithm][@stdlib/assert/is-same-value], the function does **not** distinguish between `+0` and `-0`. + + + +```javascript +var bool = ( 0.0 === -0.0 ); +// returns true + +bool = isSameValueZero( 0.0, -0.0 ); +// returns true + +bool = isSameValueZero( -0.0, 0.0 ); +// returns true +``` + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var isSameValueZero = require( '@stdlib/assert/is-same-value-zero' ); + +var bool = isSameValueZero( true, true ); +// returns true + +bool = isSameValueZero( true, false ); +// returns false + +bool = isSameValueZero( 'beep', 'beep' ); +// returns true + +bool = isSameValueZero( 3.14, 3.14 ); +// returns true + +bool = isSameValueZero( null, null ); +// returns true + +bool = isSameValueZero( 0.0, 0.0 ); +// returns true + +bool = isSameValueZero( -0.0, 0.0 ); +// returns true + +bool = isSameValueZero( NaN, NaN ); +// returns true + +bool = isSameValueZero( {}, {} ); +// returns false + +bool = isSameValueZero( [], [] ); +// returns false + +bool = isSameValueZero( isSameValueZero, isSameValueZero ); +// returns true +``` + +
+ + + + + + diff --git a/is-same-value-zero/benchmark/benchmark.js b/is-same-value-zero/benchmark/benchmark.js new file mode 100644 index 00000000..37bc5f7f --- /dev/null +++ b/is-same-value-zero/benchmark/benchmark.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isSameValueZero = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var v; + var i; + + values = [ + '', + '5', + 0, + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + new RegExp( '.*' ), + new Date(), + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = values[ i%values.length ]; + bool = isSameValueZero( v, v ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-same-value-zero/docs/repl.txt b/is-same-value-zero/docs/repl.txt new file mode 100644 index 00000000..bf908dd8 --- /dev/null +++ b/is-same-value-zero/docs/repl.txt @@ -0,0 +1,36 @@ + +{{alias}}( a, b ) + Tests if two arguments are the same value. + + The function differs from the `===` operator in that the function treats + `NaNs` as the same. + + Parameters + ---------- + a: any + First input value. + + b: any + Second input value. + + Returns + ------- + bool: boolean + Boolean indicating whether two arguments are the same value. + + Examples + -------- + > var bool = {{alias}}( true, true ) + true + > bool = {{alias}}( {}, {} ) + false + > bool = {{alias}}( -0.0, -0.0 ) + true + > bool = {{alias}}( -0.0, 0.0 ) + true + > bool = {{alias}}( NaN, NaN ) + true + + See Also + -------- + diff --git a/is-same-value-zero/docs/types/index.d.ts b/is-same-value-zero/docs/types/index.d.ts new file mode 100644 index 00000000..3d7479b4 --- /dev/null +++ b/is-same-value-zero/docs/types/index.d.ts @@ -0,0 +1,65 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if two arguments are the same value. +* +* ## Notes +* +* - The function differs from the `===` operator in that the function treats `NaNs` as the same. +* +* @param a - first input value +* @param b - second input value +* @returns boolean indicating whether two arguments are the same value +* +* @example +* var bool = isSameValueZero( true, true ); +* // returns true +* +* @example +* var bool = isSameValueZero( 3.14, 3.14 ); +* // returns true +* +* @example +* var bool = isSameValueZero( {}, {} ); +* // returns false +* +* @example +* var bool = isSameValueZero( -0.0, -0.0 ); +* // returns true +* +* @example +* var bool = isSameValueZero( -0.0, 0.0 ); +* // returns true +* +* @example +* var bool = isSameValueZero( NaN, NaN ); +* // returns true +* +* @example +* var bool = isSameValueZero( [], [] ); +* // returns false +*/ +declare function isSameValueZero( a: any, b: any ): boolean; + + +// EXPORTS // + +export = isSameValueZero; diff --git a/is-same-value-zero/docs/types/test.ts b/is-same-value-zero/docs/types/test.ts new file mode 100644 index 00000000..ba02ddd8 --- /dev/null +++ b/is-same-value-zero/docs/types/test.ts @@ -0,0 +1,36 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isSameValueZero = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isSameValueZero( 3.14, 3.14 ); // $ExpectType boolean + isSameValueZero( null, null ); // $ExpectType boolean + isSameValueZero( 'beep', 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSameValueZero(); // $ExpectError + isSameValueZero( 3.14 ); // $ExpectError + isSameValueZero( 'beep', 'beep', 3.14 ); // $ExpectError +} diff --git a/is-same-value-zero/examples/index.js b/is-same-value-zero/examples/index.js new file mode 100644 index 00000000..9a1c56f7 --- /dev/null +++ b/is-same-value-zero/examples/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isSameValueZero = require( './../lib' ); + +console.log( isSameValueZero( true, true ) ); +// => true + +console.log( isSameValueZero( true, false ) ); +// => false + +console.log( isSameValueZero( 'beep', 'beep' ) ); +// => true + +console.log( isSameValueZero( 3.14, 3.14 ) ); +// => true + +console.log( isSameValueZero( null, null ) ); +// => true + +console.log( isSameValueZero( 0.0, 0.0 ) ); +// => true + +console.log( isSameValueZero( -0.0, 0.0 ) ); +// => true + +console.log( isSameValueZero( NaN, NaN ) ); +// => true + +console.log( isSameValueZero( {}, {} ) ); +// => false + +console.log( isSameValueZero( [], [] ) ); +// => false + +console.log( isSameValueZero( isSameValueZero, isSameValueZero ) ); +// => true diff --git a/is-same-value-zero/lib/index.js b/is-same-value-zero/lib/index.js new file mode 100644 index 00000000..e3b61bcc --- /dev/null +++ b/is-same-value-zero/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if two arguments are the same value. +* +* @module @stdlib/assert/is-same-value-zero +* +* @example +* var isSameValueZero = require( '@stdlib/assert/is-same-value-zero' ); +* +* var bool = isSameValueZero( true, true ); +* // returns true +* +* bool = isSameValueZero( 3.14, 3.14 ); +* // returns true +* +* bool = isSameValueZero( {}, {} ); +* // returns false +* +* bool = isSameValueZero( -0.0, -0.0 ); +* // returns true +* +* bool = isSameValueZero( -0.0, 0.0 ); +* // returns true +* +* bool = isSameValueZero( NaN, NaN ); +* // returns true +* +* bool = isSameValueZero( [], [] ); +* // returns false +*/ + +// MODULES // + +var isSameValueZero = require( './main.js' ); + + +// EXPORTS // + +module.exports = isSameValueZero; diff --git a/is-same-value-zero/lib/main.js b/is-same-value-zero/lib/main.js new file mode 100644 index 00000000..c303a066 --- /dev/null +++ b/is-same-value-zero/lib/main.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests if two arguments are the same value. +* +* ## Notes +* +* - The function implements the SameValueZero Algorithm used by `TypedArray` and `ArrayBuffer` constructors, `Map` and `Set` operations, `String.prototype.includes`, and `Array.prototype.includes` since ES2016. +* - In contrast to the strict equality operator `===`, `NaNs` are considered the same value. +* +* @param {*} a - first input value +* @param {*} b - second input value +* @returns {boolean} boolean indicating whether two arguments are the same value +* +* @example +* var bool = isSameValueZero( true, true ); +* // returns true +* +* @example +* var bool = isSameValueZero( 3.14, 3.14 ); +* // returns true +* +* @example +* var bool = isSameValueZero( {}, {} ); +* // returns false +* +* @example +* var bool = isSameValueZero( -0.0, -0.0 ); +* // returns true +* +* @example +* var bool = isSameValueZero( -0.0, 0.0 ); +* // returns true +* +* @example +* var bool = isSameValueZero( NaN, NaN ); +* // returns true +* +* @example +* var bool = isSameValueZero( [], [] ); +* // returns false +*/ +function isSameValueZero( a, b ) { + return ( a === b ) || ( a !== a && b !== b ); // handles NaNs +} + + +// EXPORTS // + +module.exports = isSameValueZero; diff --git a/is-same-value-zero/package.json b/is-same-value-zero/package.json new file mode 100644 index 00000000..199357a0 --- /dev/null +++ b/is-same-value-zero/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-same-value-zero", + "version": "0.0.0", + "description": "Test if two arguments are the same value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "equal", + "same", + "strict", + "is", + "issame", + "issamevalue", + "isequal", + "isstrictequal", + "samevalue", + "samevaluezero", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-same-value-zero/test/test.js b/is-same-value-zero/test/test.js new file mode 100644 index 00000000..0840f6cc --- /dev/null +++ b/is-same-value-zero/test/test.js @@ -0,0 +1,120 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isSameValueZero = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSameValueZero, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided two arguments which are the same value', function test( t ) { + var values; + var i; + + values = [ + '', + 'beep', + 5, + 3.14, + -3.14, + 0.0, + -0.0, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + new Date(), + /.*/ + ]; + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSameValueZero( values[ i ], values[ i ] ), true, 'returns true when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function does not distinguish between positive and negative zero', function test( t ) { + t.strictEqual( isSameValueZero( -0.0, +0.0 ), true, 'returns expected value' ); + t.strictEqual( isSameValueZero( +0.0, -0.0 ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided two arguments which are the same value', function test( t ) { + var a; + var b; + var i; + + a = [ + '', + 'beep', + new String( 'beep' ), + 5, + 3.14, + -3.14, + new Number( 5 ), + true, + false, + new Boolean( true ), + null, + void 0, + [], + {}, + function noop() {}, + new Date(), + /.*/ + ]; + b = [ + 'abc', + 'boop', + new String( 'beep' ), + -5, + -3.14, + 3.14, + new Number( 5 ), + false, + true, + new Boolean( true ), + void 0, + null, + [], + {}, + function noop() {}, + new Date(), + /.*/ + ]; + for ( i = 0; i < a.length; i++ ) { + t.strictEqual( isSameValueZero( a[ i ], b[ i ] ), false, 'returns false when provided '+a[ i ]+' and '+b[ i ] ); + } + t.end(); +}); diff --git a/is-same-value/README.md b/is-same-value/README.md new file mode 100644 index 00000000..e69178b0 --- /dev/null +++ b/is-same-value/README.md @@ -0,0 +1,136 @@ + + +# isSameValue + +> Test if two arguments are the same value. + +
+ +## Usage + +```javascript +var isSameValue = require( '@stdlib/assert/is-same-value' ); +``` + +#### isSameValue( a, b ) + +Tests if two arguments `a` and `b` are the same value. + +```javascript +var bool = isSameValue( false, false ); +// returns true + +bool = isSameValue( '', '' ); +// returns true + +bool = isSameValue( {}, {} ); +// returns false +``` + +In contrast to the strict equality operator `===`, the function distinguishes between `+0` and `-0` and treats `NaNs` as the same value. + + + +```javascript +var bool = ( 0.0 === -0.0 ); +// returns true + +bool = isSameValue( 0.0, -0.0 ); +// returns false + +bool = isSameValue( -0.0, -0.0 ); +// returns true + +bool = ( NaN === NaN ); +// returns false + +bool = isSameValue( NaN, NaN ); +// returns true +``` + +
+ + + +
+ +## Notes + +- The function implements the [SameValue Algorithm][ecma-262-same-value-algorithm] as specified in ECMAScript 5. + +
+ + + +
+ +## Examples + + + +```javascript +var isSameValue = require( '@stdlib/assert/is-same-value' ); + +var bool = isSameValue( true, true ); +// returns true + +bool = isSameValue( true, false ); +// returns false + +bool = isSameValue( 'beep', 'beep' ); +// returns true + +bool = isSameValue( 3.14, 3.14 ); +// returns true + +bool = isSameValue( null, null ); +// returns true + +bool = isSameValue( 0.0, 0.0 ); +// returns true + +bool = isSameValue( -0.0, 0.0 ); +// returns false + +bool = isSameValue( NaN, NaN ); +// returns true + +bool = isSameValue( {}, {} ); +// returns false + +bool = isSameValue( [], [] ); +// returns false + +bool = isSameValue( isSameValue, isSameValue ); +// returns true +``` + +
+ + + + + + diff --git a/is-same-value/benchmark/benchmark.js b/is-same-value/benchmark/benchmark.js new file mode 100644 index 00000000..b0273325 --- /dev/null +++ b/is-same-value/benchmark/benchmark.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isSameValue = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var v; + var i; + + values = [ + '', + '5', + 0, + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + new RegExp( '.*' ), + new Date(), + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = values[ i%values.length ]; + bool = isSameValue( v, v ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-same-value/docs/repl.txt b/is-same-value/docs/repl.txt new file mode 100644 index 00000000..d2277397 --- /dev/null +++ b/is-same-value/docs/repl.txt @@ -0,0 +1,36 @@ + +{{alias}}( a, b ) + Tests if two arguments are the same value. + + The function differs from the `===` operator in that the function treats + `-0` and `+0` as distinct and `NaNs` as the same. + + Parameters + ---------- + a: any + First input value. + + b: any + Second input value. + + Returns + ------- + bool: boolean + Boolean indicating whether two arguments are the same value. + + Examples + -------- + > var bool = {{alias}}( true, true ) + true + > bool = {{alias}}( {}, {} ) + false + > bool = {{alias}}( -0.0, -0.0 ) + true + > bool = {{alias}}( -0.0, 0.0 ) + false + > bool = {{alias}}( NaN, NaN ) + true + + See Also + -------- + diff --git a/is-same-value/docs/types/index.d.ts b/is-same-value/docs/types/index.d.ts new file mode 100644 index 00000000..06dfc487 --- /dev/null +++ b/is-same-value/docs/types/index.d.ts @@ -0,0 +1,65 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if two arguments are the same value. +* +* ## Notes +* +* - The function differs from the `===` operator in that the function treats `-0` and `+0` as distinct and `NaNs` as the same. +* +* @param a - first input value +* @param b - second input value +* @returns boolean indicating whether two arguments are the same value +* +* @example +* var bool = isSameValue( true, true ); +* // returns true +* +* @example +* var bool = isSameValue( 3.14, 3.14 ); +* // returns true +* +* @example +* var bool = isSameValue( {}, {} ); +* // returns false +* +* @example +* var bool = isSameValue( -0.0, -0.0 ); +* // returns true +* +* @example +* var bool = isSameValue( -0.0, 0.0 ); +* // returns false +* +* @example +* var bool = isSameValue( NaN, NaN ); +* // returns true +* +* @example +* var bool = isSameValue( [], [] ); +* // returns false +*/ +declare function isSameValue( a: any, b: any ): boolean; + + +// EXPORTS // + +export = isSameValue; diff --git a/is-same-value/docs/types/test.ts b/is-same-value/docs/types/test.ts new file mode 100644 index 00000000..43ff600e --- /dev/null +++ b/is-same-value/docs/types/test.ts @@ -0,0 +1,36 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isSameValue = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isSameValue( 3.14, 3.14 ); // $ExpectType boolean + isSameValue( null, null ); // $ExpectType boolean + isSameValue( 'beep', 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSameValue(); // $ExpectError + isSameValue( 3.14 ); // $ExpectError + isSameValue( 'beep', 'beep', 3.14 ); // $ExpectError +} diff --git a/is-same-value/examples/index.js b/is-same-value/examples/index.js new file mode 100644 index 00000000..0c2598fa --- /dev/null +++ b/is-same-value/examples/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isSameValue = require( './../lib' ); + +console.log( isSameValue( true, true ) ); +// => true + +console.log( isSameValue( true, false ) ); +// => false + +console.log( isSameValue( 'beep', 'beep' ) ); +// => true + +console.log( isSameValue( 3.14, 3.14 ) ); +// => true + +console.log( isSameValue( null, null ) ); +// => true + +console.log( isSameValue( 0.0, 0.0 ) ); +// => true + +console.log( isSameValue( -0.0, 0.0 ) ); +// => false + +console.log( isSameValue( NaN, NaN ) ); +// => true + +console.log( isSameValue( {}, {} ) ); +// => false + +console.log( isSameValue( [], [] ) ); +// => false + +console.log( isSameValue( isSameValue, isSameValue ) ); +// => true diff --git a/is-same-value/lib/index.js b/is-same-value/lib/index.js new file mode 100644 index 00000000..9956bfb6 --- /dev/null +++ b/is-same-value/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if two arguments are the same value. +* +* @module @stdlib/assert/is-same-value +* +* @example +* var isSameValue = require( '@stdlib/assert/is-same-value' ); +* +* var bool = isSameValue( true, true ); +* // returns true +* +* bool = isSameValue( 3.14, 3.14 ); +* // returns true +* +* bool = isSameValue( {}, {} ); +* // returns false +* +* bool = isSameValue( -0.0, -0.0 ); +* // returns true +* +* bool = isSameValue( -0.0, 0.0 ); +* // returns false +* +* bool = isSameValue( NaN, NaN ); +* // returns true +* +* bool = isSameValue( [], [] ); +* // returns false +*/ + +// MODULES // + +var isSameValue = require( './main.js' ); + + +// EXPORTS // + +module.exports = isSameValue; diff --git a/is-same-value/lib/main.js b/is-same-value/lib/main.js new file mode 100644 index 00000000..4a381ee6 --- /dev/null +++ b/is-same-value/lib/main.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests if two arguments are the same value. +* +* ## Notes +* +* - The function implements the [SameValue Algorithm][ecma-262-same-value-algorithm], as specified in ECMAScript 5. +* - In contrast to the strict equality operator `===`, `-0` and `+0` are distinguishable and `NaNs` are the same. +* +* [ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12 +* +* @param {*} a - first input value +* @param {*} b - second input value +* @returns {boolean} boolean indicating whether two arguments are the same value +* +* @example +* var bool = isSameValue( true, true ); +* // returns true +* +* @example +* var bool = isSameValue( 3.14, 3.14 ); +* // returns true +* +* @example +* var bool = isSameValue( {}, {} ); +* // returns false +* +* @example +* var bool = isSameValue( -0.0, -0.0 ); +* // returns true +* +* @example +* var bool = isSameValue( -0.0, 0.0 ); +* // returns false +* +* @example +* var bool = isSameValue( NaN, NaN ); +* // returns true +* +* @example +* var bool = isSameValue( [], [] ); +* // returns false +*/ +function isSameValue( a, b ) { + if ( a === b ) { + if ( a === 0.0 ) { + return 1.0 / a === 1.0 / b; // handles +-0 + } + return true; + } + return ( a !== a && b !== b ); // handles NaNs +} + + +// EXPORTS // + +module.exports = isSameValue; diff --git a/is-same-value/package.json b/is-same-value/package.json new file mode 100644 index 00000000..e4bc25d1 --- /dev/null +++ b/is-same-value/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-same-value", + "version": "0.0.0", + "description": "Test if two arguments are the same value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "equal", + "same", + "strict", + "is", + "issame", + "issamevalue", + "isequal", + "isstrictequal", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-same-value/test/test.js b/is-same-value/test/test.js new file mode 100644 index 00000000..aee7762a --- /dev/null +++ b/is-same-value/test/test.js @@ -0,0 +1,118 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isSameValue = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSameValue, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided two arguments which are the same value', function test( t ) { + var values; + var i; + + values = [ + '', + 'beep', + 5, + 3.14, + -3.14, + 0.0, + -0.0, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + new Date(), + /.*/ + ]; + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSameValue( values[ i ], values[ i ] ), true, 'returns true when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided two arguments which are the same value', function test( t ) { + var a; + var b; + var i; + + a = [ + '', + 'beep', + new String( 'beep' ), + 5, + 3.14, + -3.14, + 0.0, + -0.0, + new Number( 5 ), + true, + false, + new Boolean( true ), + null, + void 0, + [], + {}, + function noop() {}, + new Date(), + /.*/ + ]; + b = [ + 'abc', + 'boop', + new String( 'beep' ), + -5, + -3.14, + 3.14, + -0.0, + 0.0, + new Number( 5 ), + false, + true, + new Boolean( true ), + void 0, + null, + [], + {}, + function noop() {}, + new Date(), + /.*/ + ]; + for ( i = 0; i < a.length; i++ ) { + t.strictEqual( isSameValue( a[ i ], b[ i ] ), false, 'returns false when provided '+a[ i ]+' and '+b[ i ] ); + } + t.end(); +}); diff --git a/is-sharedarraybuffer/README.md b/is-sharedarraybuffer/README.md new file mode 100644 index 00000000..e36016e7 --- /dev/null +++ b/is-sharedarraybuffer/README.md @@ -0,0 +1,137 @@ + + +# isSharedArrayBuffer + +> Test if a value is a [SharedArrayBuffer][mdn-sharedarraybuffer]. + +
+ +## Usage + +```javascript +var isSharedArrayBuffer = require( '@stdlib/assert/is-sharedarraybuffer' ); +``` + +#### isSharedArrayBuffer( value ) + +Tests if a value is a [`SharedArrayBuffer`][mdn-sharedarraybuffer]. + + + +```javascript +var SharedArrayBuffer = require( '@stdlib/array/shared-buffer' ); + +var bool; +try { + bool = isSharedArrayBuffer( new SharedArrayBuffer( 10 ) ); + // returns true +} catch ( err ) { + console.log( 'Environment does not support SharedArrayBuffers.' ); +} + +bool = isSharedArrayBuffer( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var SharedArrayBuffer = require( '@stdlib/array/shared-buffer' ); +var isSharedArrayBuffer = require( '@stdlib/assert/is-sharedarraybuffer' ); + +var bool; +try { + bool = isSharedArrayBuffer( new SharedArrayBuffer( 10 ) ); + // returns true +} catch ( err ) { + console.log( 'Environment does not support SharedArrayBuffers.' ); +} +bool = isSharedArrayBuffer( new ArrayBuffer( 10 ) ); +// returns false + +bool = isSharedArrayBuffer( new Float32Array( 10 ) ); +// returns false + +bool = isSharedArrayBuffer( new Int8Array( 10 ) ); +// returns false + +bool = isSharedArrayBuffer( new Uint8Array( 10 ) ); +// returns false + +bool = isSharedArrayBuffer( new Uint8ClampedArray( 10 ) ); +// returns false + +bool = isSharedArrayBuffer( new Int16Array( 10 ) ); +// returns false + +bool = isSharedArrayBuffer( new Uint16Array( 10 ) ); +// returns false + +bool = isSharedArrayBuffer( new Int32Array( 10 ) ); +// returns false + +bool = isSharedArrayBuffer( new Uint32Array( 10 ) ); +// returns false + +bool = isSharedArrayBuffer( new Float64Array( 10 ) ); +// returns false + +bool = isSharedArrayBuffer( new Array( 10 ) ); +// returns false + +bool = isSharedArrayBuffer( {} ); +// returns false + +bool = isSharedArrayBuffer( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-sharedarraybuffer/benchmark/benchmark.js b/is-sharedarraybuffer/benchmark/benchmark.js new file mode 100644 index 00000000..85780ef4 --- /dev/null +++ b/is-sharedarraybuffer/benchmark/benchmark.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var hasSharedArrayBufferSupport = require( '@stdlib/assert/has-sharedarraybuffer-support' ); // eslint-disable-line id-length +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var SharedArrayBuffer = require( '@stdlib/array/shared-buffer' ); +var pkg = require( './../package.json' ).name; +var isSharedArrayBuffer = require( './../lib' ); + + +// VARIABLES // + +var FLG = hasSharedArrayBufferSupport(); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ), + new ArrayBuffer( 10 ) + ]; + if ( FLG ) { + values.push( new SharedArrayBuffer( 10 ) ); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSharedArrayBuffer( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-sharedarraybuffer/docs/repl.txt b/is-sharedarraybuffer/docs/repl.txt new file mode 100644 index 00000000..bc93d736 --- /dev/null +++ b/is-sharedarraybuffer/docs/repl.txt @@ -0,0 +1,25 @@ + +{{alias}}( value ) + Tests if a value is a SharedArrayBuffer. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a SharedArrayBuffer. + + Examples + -------- + // Assuming an environment supports SharedArrayBuffer... + > var bool = {{alias}}( new {{alias:@stdlib/array/shared-buffer}}( 10 ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-sharedarraybuffer/docs/types/index.d.ts b/is-sharedarraybuffer/docs/types/index.d.ts new file mode 100644 index 00000000..5400b1af --- /dev/null +++ b/is-sharedarraybuffer/docs/types/index.d.ts @@ -0,0 +1,46 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a `SharedArrayBuffer`. +* +* @param value - value to test +* @returns boolean indicating whether value is a `SharedArrayBuffer` +* +* @example +* var SharedArrayBuffer = require( `@stdlib/array/shared-buffer` ); +* +* try { +* var bool = isSharedArrayBuffer( new SharedArrayBuffer( 10 ) ); +* // returns true +* } catch ( err ) { +* console.log( 'Environment does not support SharedArrayBuffers.' ); +* } +* +* @example +* var bool = isSharedArrayBuffer( [] ); +* // returns false +*/ +declare function isSharedArrayBuffer( value: any ): boolean; + + +// EXPORTS // + +export = isSharedArrayBuffer; diff --git a/is-sharedarraybuffer/docs/types/test.ts b/is-sharedarraybuffer/docs/types/test.ts new file mode 100644 index 00000000..411b49fe --- /dev/null +++ b/is-sharedarraybuffer/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isSharedArrayBuffer = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isSharedArrayBuffer( [] ); // $ExpectType boolean + isSharedArrayBuffer( null ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSharedArrayBuffer(); // $ExpectError + isSharedArrayBuffer( 'beep', 3.14 ); // $ExpectError +} diff --git a/is-sharedarraybuffer/examples/index.js b/is-sharedarraybuffer/examples/index.js new file mode 100644 index 00000000..94955719 --- /dev/null +++ b/is-sharedarraybuffer/examples/index.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var SharedArrayBuffer = require( '@stdlib/array/shared-buffer' ); +var isSharedArrayBuffer = require( './../lib' ); + +var bool; +try { + bool = isSharedArrayBuffer( new SharedArrayBuffer( 10 ) ); + console.log( bool ); + // => true +} catch ( err ) { // eslint-disable-line no-unused-vars + console.log( 'Environment does not support SharedArrayBuffers.' ); +} + +bool = isSharedArrayBuffer( new ArrayBuffer( 10 ) ); +console.log( bool ); +// => false + +bool = isSharedArrayBuffer( new Float32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isSharedArrayBuffer( new Int8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isSharedArrayBuffer( new Uint8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isSharedArrayBuffer( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => false + +bool = isSharedArrayBuffer( new Int16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isSharedArrayBuffer( new Uint16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isSharedArrayBuffer( new Int32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isSharedArrayBuffer( new Uint32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isSharedArrayBuffer( new Float64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isSharedArrayBuffer( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isSharedArrayBuffer( {} ); +console.log( bool ); +// => false + +bool = isSharedArrayBuffer( null ); +console.log( bool ); +// => false diff --git a/is-sharedarraybuffer/lib/index.js b/is-sharedarraybuffer/lib/index.js new file mode 100644 index 00000000..48d0956a --- /dev/null +++ b/is-sharedarraybuffer/lib/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a `SharedArrayBuffer`. +* +* @module @stdlib/assert/is-sharedarraybuffer +* +* @example +* var SharedArrayBuffer = require( '@stdlib/array/shared-buffer' ); +* var isSharedArrayBuffer = require( '@stdlib/assert/is-sharedarraybuffer' ); +* +* var bool; +* try { +* bool = isSharedArrayBuffer( new SharedArrayBuffer( 10 ) ); +* // returns true +* } catch ( err ) { +* console.log( 'Environment does not support SharedArrayBuffers.' ); +* } +* bool = isSharedArrayBuffer( [] ); +* // returns false +*/ + +// MODULES // + +var isSharedArrayBuffer = require( './main.js' ); + + +// EXPORTS // + +module.exports = isSharedArrayBuffer; diff --git a/is-sharedarraybuffer/lib/main.js b/is-sharedarraybuffer/lib/main.js new file mode 100644 index 00000000..b4706f55 --- /dev/null +++ b/is-sharedarraybuffer/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var hasSharedArrayBuffer = ( typeof SharedArrayBuffer === 'function' ); // eslint-disable-line stdlib/require-globals + + +// MAIN // + +/** +* Tests if a value is a `SharedArrayBuffer`. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a `SharedArrayBuffer` +* +* @example +* var SharedArrayBuffer = require( '@stdlib/array/shared-buffer' ); +* +* try { +* var bool = isSharedArrayBuffer( new SharedArrayBuffer( 10 ) ); +* // returns true +* } catch ( err ) { +* console.log( 'Environment does not support SharedArrayBuffers.' ); +* } +* +* @example +* var bool = isSharedArrayBuffer( [] ); +* // returns false +*/ +function isSharedArrayBuffer( value ) { + return ( + ( hasSharedArrayBuffer && value instanceof SharedArrayBuffer ) || // eslint-disable-line stdlib/require-globals, no-undef + nativeClass( value ) === '[object SharedArrayBuffer]' + ); +} + + +// EXPORTS // + +module.exports = isSharedArrayBuffer; diff --git a/is-sharedarraybuffer/package.json b/is-sharedarraybuffer/package.json new file mode 100644 index 00000000..75ab6e72 --- /dev/null +++ b/is-sharedarraybuffer/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-sharedarraybuffer", + "version": "0.0.0", + "description": "Test if a value is a SharedArrayBuffer.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "arraybuffer", + "shared", + "array", + "buffer", + "typed", + "typed array", + "typed-array", + "is", + "isbuffer", + "issharedarraybuffer", + "isarraybuffer", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-sharedarraybuffer/test/test.js b/is-sharedarraybuffer/test/test.js new file mode 100644 index 00000000..b964a213 --- /dev/null +++ b/is-sharedarraybuffer/test/test.js @@ -0,0 +1,86 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var ArrayBuffer = require( '@stdlib/array/buffer' ); +var SharedArrayBuffer = require( '@stdlib/array/shared-buffer' ); +var isSharedArrayBuffer = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSharedArrayBuffer, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a SharedArrayBuffer', function test( t ) { + try { + t.strictEqual( isSharedArrayBuffer( new SharedArrayBuffer( 10 ) ), true, 'returns true' ); + } catch ( err ) { // eslint-disable-line no-unused-vars + t.pass( 'environment does not support SharedArrayBuffer' ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a SharedArrayBuffer', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + void 0, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Uint32Array( 10 ), + new Int32Array( 10 ), + new Uint16Array( 10 ), + new Int16Array( 10 ), + new Uint8Array( 10 ), + new Int8Array( 10 ), + new Uint8ClampedArray( 10 ), + new ArrayBuffer( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSharedArrayBuffer( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-skew-centrosymmetric-matrix/README.md b/is-skew-centrosymmetric-matrix/README.md new file mode 100644 index 00000000..b2b6dda7 --- /dev/null +++ b/is-skew-centrosymmetric-matrix/README.md @@ -0,0 +1,97 @@ + + +# isSkewCentrosymmetricMatrix + +> Test if a value is a [skew-centrosymmetric matrix][centrosymmetric-matrix]. + +
+ +## Usage + + + +```javascript +var isSkewCentrosymmetricMatrix = require( '@stdlib/assert/is-skew-centrosymmetric-matrix' ); +``` + +#### isSkewCentrosymmetricMatrix( value ) + +Tests if a value is a [skew-centrosymmetric matrix][centrosymmetric-matrix]. + + + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var buffer = [ + 1, 2, 3, + 4, 0, -4, + -3, -2, -1 +]; +var arr = ndarray( 'generic', buffer, [ 3, 3 ], [ 3, 1 ], 0, 'row-major' ); + +var bool = isSkewCentrosymmetricMatrix( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isSkewCentrosymmetricMatrix = require( '@stdlib/assert/is-skew-centrosymmetric-matrix' ); + +var arr = ndarray( 'generic', [ 2, 1, -1, -2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var out = isSkewCentrosymmetricMatrix( arr ); +// returns true + +out = isSkewCentrosymmetricMatrix( [ 1, 2, 3, 4 ] ); +// returns false + +out = isSkewCentrosymmetricMatrix( {} ); +// returns false + +out = isSkewCentrosymmetricMatrix( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-skew-centrosymmetric-matrix/benchmark/benchmark.js b/is-skew-centrosymmetric-matrix/benchmark/benchmark.js new file mode 100644 index 00000000..4e287164 --- /dev/null +++ b/is-skew-centrosymmetric-matrix/benchmark/benchmark.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var isSkewCentrosymmetricMatrix = require( './../lib' ); // eslint-disable-line id-length + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = [ 0, 0, 0, 0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + + values = [ + arr, + arr, + arr, + arr, + arr + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSkewCentrosymmetricMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg + '::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSkewCentrosymmetricMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-skew-centrosymmetric-matrix/benchmark/benchmark.size.js b/is-skew-centrosymmetric-matrix/benchmark/benchmark.size.js new file mode 100644 index 00000000..f79cf412 --- /dev/null +++ b/is-skew-centrosymmetric-matrix/benchmark/benchmark.size.js @@ -0,0 +1,122 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isOdd = require( '@stdlib/math/base/assert/is-odd' ); +var pkg = require( './../package.json' ).name; +var isSkewCentrosymmetricMatrix = require( './../lib' ); // eslint-disable-line id-length + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} size - matrix size +* @param {PositiveInteger} N - matrix order +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, size, N ) { + var strides; + var buffer; + var shape; + var x; + + buffer = new Float64Array( size ); + shape = [ N, N ]; + strides = [ N, 1 ]; + x = ndarray( 'float64', buffer, shape, strides, 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var n; + var i; + var j; + + // Compute the index of the last checked element... + n = floor( N/2.0 ); + if ( isOdd( N ) ) { + j = ( n*N ) + n - 1; + } else { + j = n * N; + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire matrix before finding a failing value. + buffer[ j ] = i + 3.14; + bool = fcn( x ); + if ( bool !== false ) { + b.fail( 'should return false' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var sz; + var N; + var f; + var i; + + min = 4; // minimum number of elements + + for ( i = 0; i <= 16; i += 2 ) { + sz = min * pow( 2, i ); + N = sqrt( sz ); + f = createBenchmark( isSkewCentrosymmetricMatrix, sz, N ); + bench( pkg+':size='+sz+',dims='+N+'x'+N, f ); + } +} + +main(); diff --git a/is-skew-centrosymmetric-matrix/docs/repl.txt b/is-skew-centrosymmetric-matrix/docs/repl.txt new file mode 100644 index 00000000..95ac89a2 --- /dev/null +++ b/is-skew-centrosymmetric-matrix/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( value ) + Tests if a value is a skew-centrosymmetric matrix. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a skew-centrosymmetric matrix. + + Examples + -------- + > var buf = [ 2, 1, -1, -2 ]; + > var sh = [ 2, 2 ]; + > var st = [ 2, 1 ]; + > var M = {{alias:@stdlib/ndarray/ctor}}( 'generic', buf, sh, st, 0, 'row-major' ); + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-skew-centrosymmetric-matrix/docs/types/index.d.ts b/is-skew-centrosymmetric-matrix/docs/types/index.d.ts new file mode 100644 index 00000000..c6fe9291 --- /dev/null +++ b/is-skew-centrosymmetric-matrix/docs/types/index.d.ts @@ -0,0 +1,47 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a skew-centrosymmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} = -M_{N-i-1,N-j-1}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param v - value to test +* @returns boolean indicating if a value is a skew-centrosymmetric matrix +* +* @example +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'generic', [ 2, 1, -1, -2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSkewCentrosymmetricMatrix( arr ); +* // returns true +* +* bool = isSkewCentrosymmetricMatrix( [] ); +* // returns false +*/ +declare function isSkewCentrosymmetricMatrix( v: any ): boolean; + + +// EXPORTS // + +export = isSkewCentrosymmetricMatrix; diff --git a/is-skew-centrosymmetric-matrix/docs/types/test.ts b/is-skew-centrosymmetric-matrix/docs/types/test.ts new file mode 100644 index 00000000..35de43b2 --- /dev/null +++ b/is-skew-centrosymmetric-matrix/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* tslint:disable:no-invalid-this */ + +import isSkewCentrosymmetricMatrix = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const matrix = { + 'data': [ 2, 1, -1, -2 ], + 'ndims': 2, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': function get( i: number, j: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + return this.data[ idx ]; + }, + 'set': function set( i: number, j: number, v: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + this.data[ idx ] = v; + return v; + } + }; + isSkewCentrosymmetricMatrix( matrix ); // $ExpectType boolean + isSkewCentrosymmetricMatrix( [] ); // $ExpectType boolean + isSkewCentrosymmetricMatrix( false ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSkewCentrosymmetricMatrix(); // $ExpectError +} diff --git a/is-skew-centrosymmetric-matrix/examples/index.js b/is-skew-centrosymmetric-matrix/examples/index.js new file mode 100644 index 00000000..336423dd --- /dev/null +++ b/is-skew-centrosymmetric-matrix/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isSkewCentrosymmetricMatrix = require( './../lib' ); // eslint-disable-line id-length + +var arr = ndarray( 'generic', [ 2, 1, -1, -2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +console.log( isSkewCentrosymmetricMatrix( arr ) ); +// => true + +console.log( isSkewCentrosymmetricMatrix( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isSkewCentrosymmetricMatrix( {} ) ); +// => false + +console.log( isSkewCentrosymmetricMatrix( null ) ); +// => false diff --git a/is-skew-centrosymmetric-matrix/lib/index.js b/is-skew-centrosymmetric-matrix/lib/index.js new file mode 100644 index 00000000..30be26f3 --- /dev/null +++ b/is-skew-centrosymmetric-matrix/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a skew-centrosymmetric matrix. +* +* @module @stdlib/assert/is-skew-centrosymmetric-matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isSkewCentrosymmetricMatrix = require( '@stdlib/assert/is-skew-centrosymmetric-matrix' ); +* +* var arr = ndarray( 'generic', [ 2, 1, -1, -2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSkewCentrosymmetricMatrix( arr ); +* // returns true +* +* bool = isSkewCentrosymmetricMatrix( [] ); +* // returns false +*/ + +// MODULES // + +var isSkewCentrosymmetricMatrix = require( './main.js' ); // eslint-disable-line id-length + + +// EXPORTS // + +module.exports = isSkewCentrosymmetricMatrix; diff --git a/is-skew-centrosymmetric-matrix/lib/main.js b/is-skew-centrosymmetric-matrix/lib/main.js new file mode 100644 index 00000000..f81e8731 --- /dev/null +++ b/is-skew-centrosymmetric-matrix/lib/main.js @@ -0,0 +1,86 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isSquareMatrix = require( '@stdlib/assert/is-square-matrix' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var isOdd = require( '@stdlib/math/base/assert/is-odd' ); + + +// MAIN // + +/** +* Tests if a value is a skew-centrosymmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} = -M_{N-i-1,N-j-1}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a skew-centrosymmetric matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var arr = ndarray( 'generic', [ 2, 1, -1, -2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSkewCentrosymmetricMatrix( arr ); +* // returns true +* +* bool = isSkewCentrosymmetricMatrix( [] ); +* // returns false +*/ +function isSkewCentrosymmetricMatrix( v ) { // eslint-disable-line id-length + var m1; + var M; + var N; + var n; + var i; + var j; + if ( !isSquareMatrix( v ) ) { + return false; + } + M = v.shape[ 0 ]; + N = floor( M/2.0 ); // corresponds to a row index + 1 + m1 = M - 1; + for ( i = 0; i < N; i++ ) { + n = m1 - i; + for ( j = 0; j < M; j++ ) { + if ( v.get( i, j ) !== -v.get( n, m1-j ) ) { + return false; + } + } + } + if ( isOdd( M ) ) { + // Only need to examine the first half of the row (including the center element) due to symmetry... + for ( j = 0; j <= N; j++ ) { + if ( v.get( i, j ) !== -v.get( N, m1-j ) ) { + return false; + } + } + } + return true; +} + + +// EXPORTS // + +module.exports = isSkewCentrosymmetricMatrix; diff --git a/is-skew-centrosymmetric-matrix/package.json b/is-skew-centrosymmetric-matrix/package.json new file mode 100644 index 00000000..fca861b6 --- /dev/null +++ b/is-skew-centrosymmetric-matrix/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/assert/is-skew-centrosymmetric-matrix", + "version": "0.0.0", + "description": "Test if a value is a skew-centrosymmetric matrix.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "matrix", + "ismatrix", + "2d", + "square", + "symmetric", + "symm", + "centrosymmetric", + "bisymmetric", + "skew", + "skew-symmetric", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-skew-centrosymmetric-matrix/test/test.js b/is-skew-centrosymmetric-matrix/test/test.js new file mode 100644 index 00000000..b2f64ff5 --- /dev/null +++ b/is-skew-centrosymmetric-matrix/test/test.js @@ -0,0 +1,195 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable array-element-newline */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var isSkewCentrosymmetricMatrix = require( './../lib' ); // eslint-disable-line id-length + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSkewCentrosymmetricMatrix, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a skew-centrosymmetric matrix (odd order)', function test( t ) { + var buffer; + var arr; + + buffer = [ + 0, 1, 2, 3, 4, + 5, 6, 7, 8, 9, + 10, 11, 0, -11, -10, + -9, -8, -7, -6, -5, + -4, -3, -2, -1, -0 + ]; + arr = ndarray( 'generic', buffer, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' ); + + t.equal( isSkewCentrosymmetricMatrix( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a skew-centrosymmetric matrix (even order)', function test( t ) { + var buffer; + var arr; + + buffer = [ + 0, 1, 2, 3, 4, 5, + 6, 7, 8, 9, 10, 11, + 12, 13, 14, 15, 16, 17, + -17, -16, -15, -14, -13, -12, + -11, -10, -9, -8, -7, -6, + -5, -4, -3, -2, -1, -0 + ]; + arr = ndarray( 'generic', buffer, [ 6, 6 ], [ 6, 1 ], 0, 'row-major' ); + + t.equal( isSkewCentrosymmetricMatrix( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray-like object which is skew-centrosymmetric', function test( t ) { + var arr = { + 'data': [ + 4, 1, 2, + 3, 0, -3, + -2, -1, -4 + ], + 'shape': [ 3, 3 ], + 'strides': [ 3, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 9, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isSkewCentrosymmetricMatrix( arr ), true, 'returns true' ); + t.end(); + + function get( i, j ) { + return arr.data[ (arr.strides[0]*i) + j ]; + } +}); + +tape( 'the function returns `false` if not provided a square matrix', function test( t ) { + var arr = ndarray( 'generic', [ 0, 0, 0, 0, 0, 0 ], [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isSkewCentrosymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a skew-centrosymmetric matrix (even order)', function test( t ) { + var arr = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isSkewCentrosymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a skew-centrosymmetric matrix (odd order)', function test( t ) { + var buffer; + var arr; + + buffer = [ + 1, 2, 3, + 4, 5, -4, + -3, -2, -1 + ]; + arr = ndarray( 'generic', buffer, [ 3, 3 ], [ 3, 1 ], 0, 'row-major' ); + + t.equal( isSkewCentrosymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray-like object not having equal dimensions', function test( t ) { + var arr = { + 'data': [ 0, 0, 0, 0, 0, 0 ], + 'shape': [ 3, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 6, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isSkewCentrosymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray-like object which is not skew-centrosymmetric', function test( t ) { + var arr = { + 'data': [ 1, 2, 3, 4, 0, -6, -7, -8, -9 ], + 'shape': [ 3, 3 ], + 'strides': [ 3, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 9, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isSkewCentrosymmetricMatrix( arr ), false, 'returns false' ); + t.end(); + + function get( i, j ) { + return arr.data[ (arr.strides[0]*i) + j ]; + } +}); + +tape( 'the function returns `false` if not provided a 2-dimensional ndarray-like object', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 0, 0, 0 ], [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + values = [ + arr, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isSkewCentrosymmetricMatrix( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-skew-persymmetric-matrix/README.md b/is-skew-persymmetric-matrix/README.md new file mode 100644 index 00000000..9f91946a --- /dev/null +++ b/is-skew-persymmetric-matrix/README.md @@ -0,0 +1,83 @@ + + +# isSkewPersymmetricMatrix + +> Test if a value is a [skew-persymmetric matrix][persymmetric-matrix]. + +
+ +## Usage + +```javascript +var isSkewPersymmetricMatrix = require( '@stdlib/assert/is-skew-persymmetric-matrix' ); +``` + +#### isSkewPersymmetricMatrix( value ) + +Tests if a value is a [skew-persymmetric matrix][persymmetric-matrix]. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'generic', [ 2, 0, 0, -2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var bool = isSkewPersymmetricMatrix( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isSkewPersymmetricMatrix = require( '@stdlib/assert/is-skew-persymmetric-matrix' ); + +var arr = ndarray( 'generic', [ 1, 0, 0, -1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var out = isSkewPersymmetricMatrix( arr ); +// returns true + +out = isSkewPersymmetricMatrix( [ 1, 2, 3, 4 ] ); +// returns false + +out = isSkewPersymmetricMatrix( {} ); +// returns false + +out = isSkewPersymmetricMatrix( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-skew-persymmetric-matrix/benchmark/benchmark.js b/is-skew-persymmetric-matrix/benchmark/benchmark.js new file mode 100644 index 00000000..a4696061 --- /dev/null +++ b/is-skew-persymmetric-matrix/benchmark/benchmark.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var isSkewPersymmetricMatrix = require( './../lib' ); + + +// MAIN // + +bench( pkg + '::true', function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = [ 0, 0, 0, 0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + + values = [ + arr, + arr, + arr, + arr, + arr + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSkewPersymmetricMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg + '::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSkewPersymmetricMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-skew-persymmetric-matrix/benchmark/benchmark.size.js b/is-skew-persymmetric-matrix/benchmark/benchmark.size.js new file mode 100644 index 00000000..cf67188d --- /dev/null +++ b/is-skew-persymmetric-matrix/benchmark/benchmark.size.js @@ -0,0 +1,113 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isSkewPersymmetricMatrix = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} size - matrix size +* @param {PositiveInteger} N - matrix order +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, size, N ) { + var strides; + var buffer; + var shape; + var x; + + buffer = new Float64Array( size ); + shape = [ N, N ]; + strides = [ N, 1 ]; + x = ndarray( 'float64', buffer, shape, strides, 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var j; + var i; + + j = ((N-1)*N) + 1; // index of last checked element + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire matrix before finding a failing value. + buffer[ j ] = i + 3.14; + bool = fcn( x ); + if ( bool !== false ) { + b.fail( 'should return false' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var sz; + var N; + var f; + var i; + + min = 4; // minimum number of elements + + for ( i = 0; i <= 16; i += 2 ) { + sz = min * pow( 2, i ); + N = sqrt( sz ); + f = createBenchmark( isSkewPersymmetricMatrix, sz, N ); + bench( pkg+':size='+sz+',dims='+N+'x'+N, f ); + } +} + +main(); diff --git a/is-skew-persymmetric-matrix/docs/repl.txt b/is-skew-persymmetric-matrix/docs/repl.txt new file mode 100644 index 00000000..1c3168dd --- /dev/null +++ b/is-skew-persymmetric-matrix/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( value ) + Tests if a value is a skew-persymmetric matrix. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a skew-persymmetric matrix. + + Examples + -------- + > var buf = [ 1, 0, 0, -1 ]; + > var sh = [ 2, 2 ]; + > var st = [ 2, 1 ]; + > var M = {{alias:@stdlib/ndarray/ctor}}( 'generic', buf, sh, st, 0, 'row-major' ); + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-skew-persymmetric-matrix/docs/types/index.d.ts b/is-skew-persymmetric-matrix/docs/types/index.d.ts new file mode 100644 index 00000000..0c63515b --- /dev/null +++ b/is-skew-persymmetric-matrix/docs/types/index.d.ts @@ -0,0 +1,47 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a skew-persymmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} = -M_{N-j-1,N-i-1}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param v - value to test +* @returns boolean indicating if a value is a skew-persymmetric matrix +* +* @example +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'generic', [ 1, 0, 0, -1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSkewPersymmetricMatrix( arr ); +* // returns true +* +* bool = isSkewPersymmetricMatrix( [] ); +* // returns false +*/ +declare function isSkewPersymmetricMatrix( v: any ): boolean; + + +// EXPORTS // + +export = isSkewPersymmetricMatrix; diff --git a/is-skew-persymmetric-matrix/docs/types/test.ts b/is-skew-persymmetric-matrix/docs/types/test.ts new file mode 100644 index 00000000..9e59d972 --- /dev/null +++ b/is-skew-persymmetric-matrix/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* tslint:disable:no-invalid-this */ + +import isSkewPersymmetricMatrix = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const matrix = { + 'data': [ 2, 1, 1, 2 ], + 'ndims': 2, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': function get( i: number, j: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + return this.data[ idx ]; + }, + 'set': function set( i: number, j: number, v: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + this.data[ idx ] = v; + return v; + } + }; + isSkewPersymmetricMatrix( matrix ); // $ExpectType boolean + isSkewPersymmetricMatrix( [] ); // $ExpectType boolean + isSkewPersymmetricMatrix( false ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSkewPersymmetricMatrix(); // $ExpectError +} diff --git a/is-skew-persymmetric-matrix/examples/index.js b/is-skew-persymmetric-matrix/examples/index.js new file mode 100644 index 00000000..92a114cf --- /dev/null +++ b/is-skew-persymmetric-matrix/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isSkewPersymmetricMatrix = require( './../lib' ); + +var arr = ndarray( 'generic', [ 1, 0, 0, -1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +console.log( isSkewPersymmetricMatrix( arr ) ); +// => true + +console.log( isSkewPersymmetricMatrix( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isSkewPersymmetricMatrix( {} ) ); +// => false + +console.log( isSkewPersymmetricMatrix( null ) ); +// => false diff --git a/is-skew-persymmetric-matrix/lib/index.js b/is-skew-persymmetric-matrix/lib/index.js new file mode 100644 index 00000000..e6d7be2a --- /dev/null +++ b/is-skew-persymmetric-matrix/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a skew-persymmetric matrix. +* +* @module @stdlib/assert/is-skew-persymmetric-matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isSkewPersymmetricMatrix = require( '@stdlib/assert/is-skew-persymmetric-matrix' ); +* +* var arr = ndarray( 'generic', [ 1, 0, 0, -1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSkewPersymmetricMatrix( arr ); +* // returns true +* +* bool = isSkewPersymmetricMatrix( [] ); +* // returns false +*/ + +// MODULES // + +var isSkewPersymmetricMatrix = require( './main.js' ); + + +// EXPORTS // + +module.exports = isSkewPersymmetricMatrix; diff --git a/is-skew-persymmetric-matrix/lib/main.js b/is-skew-persymmetric-matrix/lib/main.js new file mode 100644 index 00000000..2d52f4fc --- /dev/null +++ b/is-skew-persymmetric-matrix/lib/main.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isSquareMatrix = require( '@stdlib/assert/is-square-matrix' ); + + +// MAIN // + +/** +* Tests if a value is a skew-persymmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} = -M_{N-j-1,N-i-1}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a skew-persymmetric matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var arr = ndarray( 'generic', [ 1, 0, 0, -1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSkewPersymmetricMatrix( arr ); +* // returns true +* +* bool = isSkewPersymmetricMatrix( [] ); +* // returns false +*/ +function isSkewPersymmetricMatrix( v ) { + var M; + var n; + var i; + var j; + if ( !isSquareMatrix( v ) ) { + return false; + } + M = v.shape[ 0 ] - 1; + for ( i = 0; i < M; i++ ) { + n = M - i; + for ( j = 0; j <= n; j++ ) { + if ( v.get( i, j ) !== -v.get( M-j, n ) ) { + return false; + } + } + } + return true; +} + + +// EXPORTS // + +module.exports = isSkewPersymmetricMatrix; diff --git a/is-skew-persymmetric-matrix/package.json b/is-skew-persymmetric-matrix/package.json new file mode 100644 index 00000000..f03bf013 --- /dev/null +++ b/is-skew-persymmetric-matrix/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/assert/is-skew-persymmetric-matrix", + "version": "0.0.0", + "description": "Test if a value is a skew-persymmetric matrix.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "matrix", + "ismatrix", + "2d", + "square", + "symmetric", + "persymmetric", + "bisymmetric", + "symm", + "skew", + "skew-symmetric", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-skew-persymmetric-matrix/test/test.js b/is-skew-persymmetric-matrix/test/test.js new file mode 100644 index 00000000..de012fe1 --- /dev/null +++ b/is-skew-persymmetric-matrix/test/test.js @@ -0,0 +1,157 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable array-element-newline */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var isSkewPersymmetricMatrix = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSkewPersymmetricMatrix, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a skew-persymmetric matrix', function test( t ) { + var buffer; + var arr; + + buffer = [ + 1, 2, 3, 0, + 5, 6, 0, -3, + 8, 0, -6, -2, + 0, -8, -5, -1 + ]; + arr = ndarray( 'generic', buffer, [ 4, 4 ], [ 4, 1 ], 0, 'row-major' ); + + t.equal( isSkewPersymmetricMatrix( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray-like object which is skew-persymmetric', function test( t ) { + var arr = { + 'data': [ 1, 0, 0, -1 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isSkewPersymmetricMatrix( arr ), true, 'returns true' ); + t.end(); + + function get( i, j ) { + return arr.data[ (2*i) + j ]; + } +}); + +tape( 'the function returns `false` if not provided a square matrix', function test( t ) { + var arr = ndarray( 'generic', [ 0, 0, 0, 0, 0, 0 ], [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isSkewPersymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a skew-persymmetric matrix', function test( t ) { + var arr = ndarray( 'generic', [ 1, 2, 2, -1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isSkewPersymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray-like object not having equal dimensions', function test( t ) { + var arr = { + 'data': [ 0, 0, 0, 0, 0, 0 ], + 'shape': [ 3, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 6, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isSkewPersymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray-like object which is not skew-persymmetric', function test( t ) { + var arr = { + 'data': [ 1, 2, 2, -1 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isSkewPersymmetricMatrix( arr ), false, 'returns false' ); + t.end(); + + function get( i, j ) { + return arr.data[ (2*i) + j ]; + } +}); + +tape( 'the function returns `false` if not provided a 2-dimensional ndarray-like object', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 0, 0, 0 ], [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + values = [ + arr, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isSkewPersymmetricMatrix( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-skew-symmetric-matrix/README.md b/is-skew-symmetric-matrix/README.md new file mode 100644 index 00000000..fd94731a --- /dev/null +++ b/is-skew-symmetric-matrix/README.md @@ -0,0 +1,83 @@ + + +# isSkewSymmetricMatrix + +> Test if a value is a [skew-symmetric matrix][skew-symmetric-matrix]. + +
+ +## Usage + +```javascript +var isSkewSymmetricMatrix = require( '@stdlib/assert/is-skew-symmetric-matrix' ); +``` + +#### isSkewSymmetricMatrix( value ) + +Tests if a value is a [skew-symmetric matrix][skew-symmetric-matrix]. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'generic', [ 0, 1, -1, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var bool = isSkewSymmetricMatrix( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isSkewSymmetricMatrix = require( '@stdlib/assert/is-skew-symmetric-matrix' ); + +var arr = ndarray( 'generic', [ 0, -1, 1, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var out = isSkewSymmetricMatrix( arr ); +// returns true + +out = isSkewSymmetricMatrix( [ 1, 2, 3, 4 ] ); +// returns false + +out = isSkewSymmetricMatrix( {} ); +// returns false + +out = isSkewSymmetricMatrix( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-skew-symmetric-matrix/benchmark/benchmark.js b/is-skew-symmetric-matrix/benchmark/benchmark.js new file mode 100644 index 00000000..501bdb3a --- /dev/null +++ b/is-skew-symmetric-matrix/benchmark/benchmark.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var isSkewSymmetricMatrix = require( './../lib' ); + + +// MAIN // + +bench( pkg + '::true', function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = [ 0, -1, 1, 0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + + values = [ + arr, + arr, + arr, + arr, + arr + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSkewSymmetricMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg + '::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSkewSymmetricMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-skew-symmetric-matrix/benchmark/benchmark.size.js b/is-skew-symmetric-matrix/benchmark/benchmark.size.js new file mode 100644 index 00000000..0011b589 --- /dev/null +++ b/is-skew-symmetric-matrix/benchmark/benchmark.size.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isSkewSymmetricMatrix = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} size - matrix size +* @param {PositiveInteger} N - matrix order +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, size, N ) { + var strides; + var buffer; + var shape; + var x; + + buffer = new Float64Array( size ); + shape = [ N, N ]; + strides = [ N, 1 ]; + x = ndarray( 'float64', buffer, shape, strides, 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire matrix before finding a failing value. + buffer[ size-2 ] = i + 3.14; + bool = fcn( x ); + if ( bool !== false ) { + b.fail( 'should return false' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var sz; + var N; + var f; + var i; + + min = 4; // minimum number of elements + + for ( i = 0; i <= 16; i += 2 ) { + sz = min * pow( 2, i ); + N = sqrt( sz ); + f = createBenchmark( isSkewSymmetricMatrix, sz, N ); + bench( pkg+':size='+sz+',dims='+N+'x'+N, f ); + } +} + +main(); diff --git a/is-skew-symmetric-matrix/docs/repl.txt b/is-skew-symmetric-matrix/docs/repl.txt new file mode 100644 index 00000000..ebe5aa5e --- /dev/null +++ b/is-skew-symmetric-matrix/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( value ) + Tests if a value is a skew-symmetric (or antisymmetric) matrix. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a skew-symmetric matrix. + + Examples + -------- + > var buf = [ 0, -1, 1, 0 ]; + > var sh = [ 2, 2 ]; + > var st = [ 2, 1 ]; + > var M = {{alias:@stdlib/ndarray/ctor}}( 'generic', buf, sh, st, 0, 'row-major' ); + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-skew-symmetric-matrix/docs/types/index.d.ts b/is-skew-symmetric-matrix/docs/types/index.d.ts new file mode 100644 index 00000000..312570c7 --- /dev/null +++ b/is-skew-symmetric-matrix/docs/types/index.d.ts @@ -0,0 +1,47 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a skew-symmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} = -M_{ji}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param v - value to test +* @returns boolean indicating if a value is a skew-symmetric matrix +* +* @example +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'generic', [ 0, -1, 1, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSkewSymmetricMatrix( arr ); +* // returns true +* +* bool = isSkewSymmetricMatrix( [] ); +* // returns false +*/ +declare function isSkewSymmetricMatrix( v: any ): boolean; + + +// EXPORTS // + +export = isSkewSymmetricMatrix; diff --git a/is-skew-symmetric-matrix/docs/types/test.ts b/is-skew-symmetric-matrix/docs/types/test.ts new file mode 100644 index 00000000..053d86e2 --- /dev/null +++ b/is-skew-symmetric-matrix/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* tslint:disable:no-invalid-this */ + +import isSkewSymmetricMatrix = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const matrix = { + 'data': [ 2, 1, 1, 2 ], + 'ndims': 2, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': function get( i: number, j: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + return this.data[ idx ]; + }, + 'set': function set( i: number, j: number, v: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + this.data[ idx ] = v; + return v; + } + }; + isSkewSymmetricMatrix( matrix ); // $ExpectType boolean + isSkewSymmetricMatrix( [] ); // $ExpectType boolean + isSkewSymmetricMatrix( false ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSkewSymmetricMatrix(); // $ExpectError +} diff --git a/is-skew-symmetric-matrix/examples/index.js b/is-skew-symmetric-matrix/examples/index.js new file mode 100644 index 00000000..0232e4d2 --- /dev/null +++ b/is-skew-symmetric-matrix/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isSkewSymmetricMatrix = require( './../lib' ); + +var arr = ndarray( 'generic', [ 0, -1, 1, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +console.log( isSkewSymmetricMatrix( arr ) ); +// => true + +console.log( isSkewSymmetricMatrix( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isSkewSymmetricMatrix( {} ) ); +// => false + +console.log( isSkewSymmetricMatrix( null ) ); +// => false diff --git a/is-skew-symmetric-matrix/lib/index.js b/is-skew-symmetric-matrix/lib/index.js new file mode 100644 index 00000000..bbaa50f8 --- /dev/null +++ b/is-skew-symmetric-matrix/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a skew-symmetric matrix. +* +* @module @stdlib/assert/is-skew-symmetric-matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isSkewSymmetricMatrix = require( '@stdlib/assert/is-skew-symmetric-matrix' ); +* +* var arr = ndarray( 'generic', [ 0, -1, 1, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSkewSymmetricMatrix( arr ); +* // returns true +* +* bool = isSkewSymmetricMatrix( [] ); +* // returns false +*/ + +// MODULES // + +var isSkewSymmetricMatrix = require( './main.js' ); + + +// EXPORTS // + +module.exports = isSkewSymmetricMatrix; diff --git a/is-skew-symmetric-matrix/lib/main.js b/is-skew-symmetric-matrix/lib/main.js new file mode 100644 index 00000000..7b13d189 --- /dev/null +++ b/is-skew-symmetric-matrix/lib/main.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isSquareMatrix = require( '@stdlib/assert/is-square-matrix' ); + + +// MAIN // + +/** +* Tests if a value is a skew-symmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} = -M_{ji}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a skew-symmetric matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var arr = ndarray( 'generic', [ 0, -1, 1, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSkewSymmetricMatrix( arr ); +* // returns true +* +* bool = isSkewSymmetricMatrix( [] ); +* // returns false +*/ +function isSkewSymmetricMatrix( v ) { + var M; + var i; + var j; + if ( !isSquareMatrix( v ) ) { + return false; + } + M = v.shape[ 0 ]; + for ( i = 0; i < M; i++ ) { + for ( j = 0; j <= i; j++ ) { // includes diagonal + if ( v.get( i, j ) !== -v.get( j, i ) ) { + return false; + } + } + } + return true; +} + + +// EXPORTS // + +module.exports = isSkewSymmetricMatrix; diff --git a/is-skew-symmetric-matrix/package.json b/is-skew-symmetric-matrix/package.json new file mode 100644 index 00000000..77e56a3f --- /dev/null +++ b/is-skew-symmetric-matrix/package.json @@ -0,0 +1,80 @@ +{ + "name": "@stdlib/assert/is-skew-symmetric-matrix", + "version": "0.0.0", + "description": "Test if a value is a skew-symmetric matrix.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "matrix", + "ismatrix", + "2d", + "square", + "symmetric", + "symm", + "antisymmetric", + "antimetric", + "skew", + "skew-symmetric", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-skew-symmetric-matrix/test/test.js b/is-skew-symmetric-matrix/test/test.js new file mode 100644 index 00000000..ebbc7bd1 --- /dev/null +++ b/is-skew-symmetric-matrix/test/test.js @@ -0,0 +1,145 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var isSkewSymmetricMatrix = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSkewSymmetricMatrix, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a skew-symmetric matrix', function test( t ) { + var arr = ndarray( 'generic', [ 0, 1, -1, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isSkewSymmetricMatrix( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a skew-symmetric 2-dimensional ndarray-like object', function test( t ) { + var arr = { + 'data': [ 0, 1, -1, 0 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isSkewSymmetricMatrix( arr ), true, 'returns true' ); + t.end(); + + function get( i, j ) { + return arr.data[ (2*i) + j ]; + } +}); + +tape( 'the function returns `false` if not provided a square matrix', function test( t ) { + var arr = ndarray( 'generic', [ 0, 0, 0, 0, 0, 0 ], [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isSkewSymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a skew-symmetric matrix', function test( t ) { + var arr = ndarray( 'generic', [ 0, 1, -1, 1 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isSkewSymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray-like object not having equal dimensions', function test( t ) { + var arr = { + 'data': [ 0, 0, 0, 0, 0, 0 ], + 'shape': [ 3, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 6, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isSkewSymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a skew-symmetric 2-dimensional ndarray-like object', function test( t ) { + var arr = { + 'data': [ 0, 1, 1, 0 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isSkewSymmetricMatrix( arr ), false, 'returns false' ); + t.end(); + + function get( i, j ) { + return arr.data[ (2*i) + j ]; + } +}); + +tape( 'the function returns `false` if not provided a 2-dimensional ndarray-like object', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 0, 0, 0 ], [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + values = [ + arr, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isSkewSymmetricMatrix( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-square-matrix/README.md b/is-square-matrix/README.md new file mode 100644 index 00000000..47588a83 --- /dev/null +++ b/is-square-matrix/README.md @@ -0,0 +1,83 @@ + + +# isSquareMatrix + +> Test if a value is a 2-dimensional [ndarray][@stdlib/ndarray/ctor]-like object having equal dimensions. + +
+ +## Usage + +```javascript +var isSquareMatrix = require( '@stdlib/assert/is-square-matrix' ); +``` + +#### isSquareMatrix( value ) + +Tests if a value is a 2-dimensional [ndarray][@stdlib/ndarray/ctor]-like object having equal dimensions. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var bool = isSquareMatrix( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isSquareMatrix = require( '@stdlib/assert/is-square-matrix' ); + +var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var out = isSquareMatrix( arr ); +// returns true + +out = isSquareMatrix( [ 1, 2, 3, 4 ] ); +// returns false + +out = isSquareMatrix( {} ); +// returns false + +out = isSquareMatrix( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-square-matrix/benchmark/benchmark.js b/is-square-matrix/benchmark/benchmark.js new file mode 100644 index 00000000..472eca92 --- /dev/null +++ b/is-square-matrix/benchmark/benchmark.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var isSquareMatrix = require( './../lib' ); + + +// MAIN // + +bench( pkg + '::true', function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = [ 0, 0, 0, 0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + + values = [ + arr, + arr, + arr, + arr, + arr + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg + '::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-square-matrix/docs/repl.txt b/is-square-matrix/docs/repl.txt new file mode 100644 index 00000000..a7a3ad95 --- /dev/null +++ b/is-square-matrix/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value ) + Tests if a value is a 2-dimensional ndarray-like object having equal + dimensions. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a 2-dimensional ndarray-like + object having equal dimensions. + + Examples + -------- + > var buf = [ 0, 0, 0, 0 ]; + > var sh = [ 2, 2 ]; + > var st = [ 2, 1 ]; + > var M = {{alias:@stdlib/ndarray/ctor}}( 'generic', buf, sh, st, 0, 'row-major' ); + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-square-matrix/docs/types/index.d.ts b/is-square-matrix/docs/types/index.d.ts new file mode 100644 index 00000000..d2bb0fb4 --- /dev/null +++ b/is-square-matrix/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a 2-dimensional ndarray-like object having equal dimensions. +* +* @param v - value to test +* @returns boolean indicating if a value is a 2-dimensional ndarray-like object having equal dimensions +* +* @example +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSquareMatrix( arr ); +* // returns true +* +* bool = isSquareMatrix( [] ); +* // returns false +*/ +declare function isSquareMatrix( v: any ): boolean; + + +// EXPORTS // + +export = isSquareMatrix; diff --git a/is-square-matrix/docs/types/test.ts b/is-square-matrix/docs/types/test.ts new file mode 100644 index 00000000..3eadba15 --- /dev/null +++ b/is-square-matrix/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* tslint:disable:no-invalid-this */ + +import isSquareMatrix = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const matrix = { + 'data': [ 0, 0, 0, 0 ], + 'ndims': 2, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': function get( i: number, j: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + return this.data[ idx ]; + }, + 'set': function set( i: number, j: number, v: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + this.data[ idx ] = v; + return v; + } + }; + isSquareMatrix( matrix ); // $ExpectType boolean + isSquareMatrix( [] ); // $ExpectType boolean + isSquareMatrix( false ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSquareMatrix(); // $ExpectError +} diff --git a/is-square-matrix/examples/index.js b/is-square-matrix/examples/index.js new file mode 100644 index 00000000..1984d338 --- /dev/null +++ b/is-square-matrix/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isSquareMatrix = require( './../lib' ); + +var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +console.log( isSquareMatrix( arr ) ); +// => true + +console.log( isSquareMatrix( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isSquareMatrix( {} ) ); +// => false + +console.log( isSquareMatrix( null ) ); +// => false diff --git a/is-square-matrix/lib/index.js b/is-square-matrix/lib/index.js new file mode 100644 index 00000000..ef66b92a --- /dev/null +++ b/is-square-matrix/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a 2-dimensional ndarray-like object having equal dimensions. +* +* @module @stdlib/assert/is-square-matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isSquareMatrix = require( '@stdlib/assert/is-square-matrix' ); +* +* var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSquareMatrix( arr ); +* // returns true +* +* bool = isSquareMatrix( [] ); +* // returns false +*/ + +// MODULES // + +var isSquareMatrix = require( './main.js' ); + + +// EXPORTS // + +module.exports = isSquareMatrix; diff --git a/is-square-matrix/lib/main.js b/is-square-matrix/lib/main.js new file mode 100644 index 00000000..035c70da --- /dev/null +++ b/is-square-matrix/lib/main.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isMatrixLike = require( '@stdlib/assert/is-matrix-like' ); + + +// MAIN // + +/** +* Tests if a value is a 2-dimensional ndarray-like object having equal dimensions. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a 2-dimensional ndarray-like object having equal dimensions +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSquareMatrix( arr ); +* // returns true +* +* bool = isSquareMatrix( [] ); +* // returns false +*/ +function isSquareMatrix( v ) { + return ( + isMatrixLike( v ) && + v.shape[ 0 ] === v.shape[ 1 ] + ); +} + + +// EXPORTS // + +module.exports = isSquareMatrix; diff --git a/is-square-matrix/package.json b/is-square-matrix/package.json new file mode 100644 index 00000000..f8ec91d7 --- /dev/null +++ b/is-square-matrix/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-square-matrix", + "version": "0.0.0", + "description": "Test if a value is a 2-dimensional ndarray-like object having equal dimensions.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "matrix", + "ismatrix", + "2d", + "square", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-square-matrix/test/test.js b/is-square-matrix/test/test.js new file mode 100644 index 00000000..68d72e1b --- /dev/null +++ b/is-square-matrix/test/test.js @@ -0,0 +1,112 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var isSquareMatrix = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSquareMatrix, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray having equal dimensions', function test( t ) { + var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isSquareMatrix( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray-like object having equal dimensions', function test( t ) { + var arr = { + 'data': [ 0, 0, 0, 0 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isSquareMatrix( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray not having equal dimensions', function test( t ) { + var arr = ndarray( 'generic', [ 0, 0, 0, 0, 0, 0 ], [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isSquareMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray-like object not having equal dimensions', function test( t ) { + var arr = { + 'data': [ 0, 0, 0, 0, 0, 0 ], + 'shape': [ 3, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 6, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isSquareMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a 2-dimensional ndarray-like object', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 0, 0, 0 ], [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + values = [ + arr, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isSquareMatrix( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-square-number/README.md b/is-square-number/README.md new file mode 100644 index 00000000..e71048d8 --- /dev/null +++ b/is-square-number/README.md @@ -0,0 +1,153 @@ + + +# isSquareNumber + +> Test if a value is a square number. + +
+ +A **square number** is defined as an integer value which is the square of an integer. + +
+ + + +
+ +## Usage + +```javascript +var isSquareNumber = require( '@stdlib/assert/is-square-number' ); +``` + +#### isSquareNumber( value ) + +Tests if a `value` is a square number. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isSquareNumber( 4.0 ); +// returns true + +bool = isSquareNumber( new Number( 4.0 ) ); +// returns true + +bool = isSquareNumber( 3.14 ); +// returns false + +bool = isSquareNumber( -5.0 ); +// returns false + +bool = isSquareNumber( NaN ); +// returns false + +bool = isSquareNumber( null ); +// returns false +``` + +#### isSquareNumber.isPrimitive( value ) + +Tests if a `value` is a primitive square number. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isSquareNumber.isPrimitive( 4.0 ); +// returns true + +bool = isSquareNumber.isPrimitive( new Number( 4.0 ) ); +// returns false +``` + +#### isSquareNumber.isObject( value ) + +Tests if a `value` is a `Number` object having a value which is a square number. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isSquareNumber.isObject( 4.0 ); +// returns false + +bool = isSquareNumber.isObject( new Number( 4.0 ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isSquareNumber = require( '@stdlib/assert/is-square-number' ); + +var bool = isSquareNumber( 4.0 ); +// returns true + +bool = isSquareNumber( new Number( 4.0 ) ); +// returns true + +bool = isSquareNumber( 0.0 ); +// returns true + +bool = isSquareNumber( 1.0 ); +// returns true + +bool = isSquareNumber( 3.14 ); +// returns false + +bool = isSquareNumber( -5.0 ); +// returns false + +bool = isSquareNumber( NaN ); +// returns false + +bool = isSquareNumber( '0.5' ); +// returns false + +bool = isSquareNumber( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-square-number/benchmark/benchmark.js b/is-square-number/benchmark/benchmark.js new file mode 100644 index 00000000..563d4bad --- /dev/null +++ b/is-square-number/benchmark/benchmark.js @@ -0,0 +1,233 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isSquareNumber = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 0.5, + 1.0, + 0.0, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 4.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 4.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 4.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-square-number/docs/repl.txt b/is-square-number/docs/repl.txt new file mode 100644 index 00000000..ed9add80 --- /dev/null +++ b/is-square-number/docs/repl.txt @@ -0,0 +1,75 @@ + +{{alias}}( value ) + Tests if a value is a square number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a square number. + + Examples + -------- + > var bool = {{alias}}( 4.0 ) + true + > bool = {{alias}}( new Number( 4.0 ) ) + true + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( -5.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive which is a square number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive which is a + square number. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 4.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( 4.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a value which is a square number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a value which + is a square number. + + Examples + -------- + > var bool = {{alias}}.isObject( 4.0 ) + false + > bool = {{alias}}.isObject( new Number( 4.0 ) ) + true + + + See Also + -------- + diff --git a/is-square-number/docs/types/index.d.ts b/is-square-number/docs/types/index.d.ts new file mode 100644 index 00000000..d34cdb1d --- /dev/null +++ b/is-square-number/docs/types/index.d.ts @@ -0,0 +1,125 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface describing methods for testing for primitives and objects. +*/ +interface IsSquareNumber { + /** + * Tests if a value is a square number. + * + * @param value - value to test + * @returns boolean indicating whether value is a square number + * + * @example + * var bool = isSquareNumber( 4.0 ); + * // returns true + * + * @example + * var bool = isSquareNumber( new Number( 4.0 ) ); + * // returns true + * + * @example + * var bool = isSquareNumber( 3.14 ); + * // returns false + * + * @example + * var bool = isSquareNumber( -5.0 ); + * // returns false + * + * @example + * var bool = isSquareNumber( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a value which is a square number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a value which is a square number + * + * @example + * var bool = isSquareNumber.isPrimitive( 4.0 ); + * // returns true + * + * @example + * var bool = isSquareNumber.isPrimitive( new Number( 4.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a value which is a square number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a value which is a square number + * + * @example + * var bool = isSquareNumber.isObject( 4.0 ); + * // returns false + * + * @example + * var bool = isSquareNumber.isObject( new Number( 4.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a square number. +* +* @param value - value to test +* @returns boolean indicating whether value is a square number +* +* @example +* var bool = isSquareNumber( 4.0 ); +* // returns true +* +* @example +* var bool = isSquareNumber( new Number( 4.0 ) ); +* // returns true +* +* @example +* var bool = isSquareNumber( 3.14 ); +* // returns false +* +* @example +* var bool = isSquareNumber( -5.0 ); +* // returns false +* +* @example +* var bool = isSquareNumber( null ); +* // returns false +* +* @example +* var bool = isSquareNumber.isPrimitive( 4.0 ); +* // returns true +* +* @example +* var bool = isSquareNumber.isObject( new Number( 4.0 ) ); +* // returns true +*/ +declare var isSquareNumber: IsSquareNumber; + + +// EXPORTS // + +export = isSquareNumber; diff --git a/is-square-number/docs/types/test.ts b/is-square-number/docs/types/test.ts new file mode 100644 index 00000000..fba368c0 --- /dev/null +++ b/is-square-number/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isSquareNumber = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isSquareNumber( 3 ); // $ExpectType boolean + isSquareNumber( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSquareNumber(); // $ExpectError + isSquareNumber( 0.2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isSquareNumber.isPrimitive( new Number( 0.2 ) ); // $ExpectType boolean + isSquareNumber.isPrimitive( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isSquareNumber.isPrimitive(); // $ExpectError + isSquareNumber.isPrimitive( 0.2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isSquareNumber.isObject( new Number( 0.2 ) ); // $ExpectType boolean + isSquareNumber.isObject( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isSquareNumber.isObject(); // $ExpectError + isSquareNumber.isObject( 0.2, 123 ); // $ExpectError +} diff --git a/is-square-number/examples/index.js b/is-square-number/examples/index.js new file mode 100644 index 00000000..c8266521 --- /dev/null +++ b/is-square-number/examples/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isSquareNumber = require( './../lib' ); + +console.log( isSquareNumber( 4.0 ) ); +// => true + +console.log( isSquareNumber( new Number( 4.0 ) ) ); +// => true + +console.log( isSquareNumber( 0.0 ) ); +// => true + +console.log( isSquareNumber( 1.0 ) ); +// => true + +console.log( isSquareNumber( 3.14 ) ); +// => false + +console.log( isSquareNumber( -5.0 ) ); +// => false + +console.log( isSquareNumber( NaN ) ); +// => false + +console.log( isSquareNumber( '0.5' ) ); +// => false + +console.log( isSquareNumber( null ) ); +// => false diff --git a/is-square-number/lib/index.js b/is-square-number/lib/index.js new file mode 100644 index 00000000..ae058cfb --- /dev/null +++ b/is-square-number/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a square number. +* +* @module @stdlib/assert/is-square-number +* +* @example +* var isSquareNumber = require( '@stdlib/assert/is-square-number' ); +* +* var bool = isSquareNumber( 4.0 ); +* // returns true +* +* bool = isSquareNumber( new Number( 4.0 ) ); +* // returns true +* +* bool = isSquareNumber( 3.14 ); +* // returns false +* +* bool = isSquareNumber( -5.0 ); +* // returns false +* +* bool = isSquareNumber( null ); +* // returns false +* +* @example +* var isSquareNumber = require( '@stdlib/assert/is-square-number' ).isPrimitive; +* +* var bool = isSquareNumber( 4.0 ); +* // returns true +* +* bool = isSquareNumber( new Number( 4.0 ) ); +* // returns false +* +* @example +* var isSquareNumber = require( '@stdlib/assert/is-square-number' ).isObject; +* +* var bool = isSquareNumber( 4.0 ); +* // returns false +* +* bool = isSquareNumber( new Number( 4.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isSquareNumber = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isSquareNumber, 'isPrimitive', isPrimitive ); +setReadOnly( isSquareNumber, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isSquareNumber; diff --git a/is-square-number/lib/main.js b/is-square-number/lib/main.js new file mode 100644 index 00000000..d027052a --- /dev/null +++ b/is-square-number/lib/main.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a square number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a square number +* +* @example +* var bool = isSquareNumber( 4.0 ); +* // returns true +* +* @example +* var bool = isSquareNumber( new Number( 4.0 ) ); +* // returns true +* +* @example +* var bool = isSquareNumber( 3.14 ); +* // returns false +* +* @example +* var bool = isSquareNumber( -5.0 ); +* // returns false +* +* @example +* var bool = isSquareNumber( null ); +* // returns false +*/ +function isSquareNumber( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isSquareNumber; diff --git a/is-square-number/lib/object.js b/is-square-number/lib/object.js new file mode 100644 index 00000000..d45a208c --- /dev/null +++ b/is-square-number/lib/object.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isObject; +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); + + +// MAIN // + +/** +* Tests if a value is a number object having a value which is a square number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a value which is a square number +* +* @example +* var bool = isSquareNumber( 4.0 ); +* // returns false +* +* @example +* var bool = isSquareNumber( new Number( 4.0 ) ); +* // returns true +*/ +function isSquareNumber( value ) { + var n; + var x; + if ( !isInteger( value ) ) { + return false; + } + x = value.valueOf(); + n = floor( sqrt( x ) + 0.5 ); + return n*n === x; +} + + +// EXPORTS // + +module.exports = isSquareNumber; diff --git a/is-square-number/lib/primitive.js b/is-square-number/lib/primitive.js new file mode 100644 index 00000000..b92f4e8f --- /dev/null +++ b/is-square-number/lib/primitive.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); + + +// MAIN // + +/** +* Tests if a value is a number primitive having a value which is a square number. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a value which is a square number +* +* @example +* var bool = isSquareNumber( 4.0 ); +* // returns true +* +* @example +* var bool = isSquareNumber( new Number( 4.0 ) ); +* // returns false +*/ +function isSquareNumber( value ) { + var n; + if ( !isInteger( value ) ) { + return false; + } + n = floor( sqrt( value ) + 0.5 ); + return n*n === value; +} + + +// EXPORTS // + +module.exports = isSquareNumber; diff --git a/is-square-number/package.json b/is-square-number/package.json new file mode 100644 index 00000000..c0be7969 --- /dev/null +++ b/is-square-number/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-square-number", + "version": "0.0.0", + "description": "Test if a value is a square number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "square", + "is", + "issquare", + "type", + "check", + "valid", + "validate", + "test", + "primitive", + "object" + ] +} diff --git a/is-square-number/test/test.js b/is-square-number/test/test.js new file mode 100644 index 00000000..6dad4b1d --- /dev/null +++ b/is-square-number/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isSquareNumber = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSquareNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test primitives', function test( t ) { + t.equal( typeof isSquareNumber.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test objects', function test( t ) { + t.equal( typeof isSquareNumber.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-square-number/test/test.main.js b/is-square-number/test/test.main.js new file mode 100644 index 00000000..1307052c --- /dev/null +++ b/is-square-number/test/test.main.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isSquareNumber = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSquareNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a square number', function test( t ) { + t.equal( isSquareNumber( 4.0 ), true, 'returns true' ); + t.equal( isSquareNumber( new Number( 4.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a square number', function test( t ) { + var values; + var i; + + values = [ + '5', + 5.0, + -3.14, + -1.0, + NaN, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isSquareNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-square-number/test/test.object.js b/is-square-number/test/test.object.js new file mode 100644 index 00000000..2a215d28 --- /dev/null +++ b/is-square-number/test/test.object.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isSquareNumber = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSquareNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a square number', function test( t ) { + t.equal( isSquareNumber( new Number( 4.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive square number', function test( t ) { + t.equal( isSquareNumber( 4.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a square number', function test( t ) { + var values; + var i; + + values = [ + '5', + new Number( 5.0 ), // eslint-disable-line no-new-wrappers + null, + true, + NaN, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), // eslint-disable-line prefer-regex-literals + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isSquareNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-square-number/test/test.primitive.js b/is-square-number/test/test.primitive.js new file mode 100644 index 00000000..54500fc3 --- /dev/null +++ b/is-square-number/test/test.primitive.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isSquareNumber = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSquareNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive square number', function test( t ) { + t.equal( isSquareNumber( 4.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object', function test( t ) { + t.equal( isSquareNumber( new Number( 4.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a square number', function test( t ) { + var values; + var i; + + values = [ + '5', + 5.0, + null, + true, + NaN, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isSquareNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-square-triangular-number/README.md b/is-square-triangular-number/README.md new file mode 100644 index 00000000..2995d177 --- /dev/null +++ b/is-square-triangular-number/README.md @@ -0,0 +1,195 @@ + + +# isSquareTriangularNumber + +> Test if a value is a [square triangular number][square-triangular-number]. + +
+ +A **square triangular number** is an integer value which is both a [square number][@stdlib/assert/is-square-number] and a [triangular number][@stdlib/assert/is-triangular-number]. + +[Triangular numbers][@stdlib/assert/is-triangular-number] can be computed using the following formula + + + +
+ Triangular number formula. +
+
+ + + +for `n >= 0`. + +By analogy with the square root of `x`, one can define the positive triangular root of `x` such that `T_n = x` + + + +
+ Triangular root formula. +
+
+ + + +Accordingly, an integer `x` is a [triangular number][@stdlib/assert/is-triangular-number] **if and only** if `8x+1` is a [square number][@stdlib/assert/is-square-number]. + +
+ + + +
+ +## Usage + +```javascript +var isSquareTriangularNumber = require( '@stdlib/assert/is-square-triangular-number' ); +``` + +#### isSquareTriangularNumber( value ) + +Tests if a `value` is a [square triangular number][square-triangular-number]. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isSquareTriangularNumber( 36.0 ); +// returns true + +bool = isSquareTriangularNumber( new Number( 36.0 ) ); +// returns true + +bool = isSquareTriangularNumber( 3.14 ); +// returns false + +bool = isSquareTriangularNumber( -5.0 ); +// returns false + +bool = isSquareTriangularNumber( NaN ); +// returns false + +bool = isSquareTriangularNumber( null ); +// returns false +``` + +#### isSquareTriangularNumber.isPrimitive( value ) + +Tests if a `value` is a primitive [square triangular number][square-triangular-number]. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isSquareTriangularNumber.isPrimitive( 36.0 ); +// returns true + +bool = isSquareTriangularNumber.isPrimitive( new Number( 36.0 ) ); +// returns false +``` + +#### isSquareTriangularNumber.isObject( value ) + +Tests if a `value` is a `Number` object having a value which is a [square triangular number][square-triangular-number]. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isSquareTriangularNumber.isObject( 36.0 ); +// returns false + +bool = isSquareTriangularNumber.isObject( new Number( 36.0 ) ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Return values are not reliable for numbers greater than `1125899906842624`. + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isSquareTriangularNumber = require( '@stdlib/assert/is-square-triangular-number' ); + +var bool = isSquareTriangularNumber( 36.0 ); +// returns true + +bool = isSquareTriangularNumber( new Number( 36.0 ) ); +// returns true + +bool = isSquareTriangularNumber( 0.0 ); +// returns true + +bool = isSquareTriangularNumber( 1.0 ); +// returns true + +bool = isSquareTriangularNumber( 3.14 ); +// returns false + +bool = isSquareTriangularNumber( -5.0 ); +// returns false + +bool = isSquareTriangularNumber( NaN ); +// returns false + +bool = isSquareTriangularNumber( '0.5' ); +// returns false + +bool = isSquareTriangularNumber( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-square-triangular-number/benchmark/benchmark.js b/is-square-triangular-number/benchmark/benchmark.js new file mode 100644 index 00000000..1bbefa28 --- /dev/null +++ b/is-square-triangular-number/benchmark/benchmark.js @@ -0,0 +1,234 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isSquareTriangularNumber = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 0.5, + 1.0, + 0.0, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareTriangularNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 36.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareTriangularNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 36, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareTriangularNumber.isPrimitive( values[ i % values.length ] ); // eslint-disable-line max-len + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 36.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareTriangularNumber.isPrimitive( values[ i % values.length ] ); // eslint-disable-line max-len + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareTriangularNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 36.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSquareTriangularNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-square-triangular-number/docs/img/equation_triangular_number.svg b/is-square-triangular-number/docs/img/equation_triangular_number.svg new file mode 100644 index 00000000..c02bc1f1 --- /dev/null +++ b/is-square-triangular-number/docs/img/equation_triangular_number.svg @@ -0,0 +1,32 @@ + +upper T Subscript n Baseline equals StartFraction n left-parenthesis n plus 1 right-parenthesis Over 2 EndFraction + + + \ No newline at end of file diff --git a/is-square-triangular-number/docs/img/equation_triangular_root.svg b/is-square-triangular-number/docs/img/equation_triangular_root.svg new file mode 100644 index 00000000..0b2b5366 --- /dev/null +++ b/is-square-triangular-number/docs/img/equation_triangular_root.svg @@ -0,0 +1,36 @@ + +n equals StartFraction StartRoot 8 x plus 1 EndRoot minus 1 Over 2 EndFraction + + + \ No newline at end of file diff --git a/is-square-triangular-number/docs/repl.txt b/is-square-triangular-number/docs/repl.txt new file mode 100644 index 00000000..aed7cf7f --- /dev/null +++ b/is-square-triangular-number/docs/repl.txt @@ -0,0 +1,78 @@ + +{{alias}}( value ) + Tests if a value is a square triangular number. + + Return values are not reliable for numbers greater than 1125899906842624. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a square triangular number. + + Examples + -------- + > var bool = {{alias}}( 36.0 ) + true + > bool = {{alias}}( new Number( 36.0 ) ) + true + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( -5.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive which is a square triangular number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive which is a + square triangular number. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 36.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( 36.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a value which is a square + triangular number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a value which + is a square triangular number. + + Examples + -------- + > var bool = {{alias}}.isObject( 36.0 ) + false + > bool = {{alias}}.isObject( new Number( 36.0 ) ) + true + + + See Also + -------- + diff --git a/is-square-triangular-number/docs/types/index.d.ts b/is-square-triangular-number/docs/types/index.d.ts new file mode 100644 index 00000000..5254f393 --- /dev/null +++ b/is-square-triangular-number/docs/types/index.d.ts @@ -0,0 +1,129 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface describing methods for testing for primitives and objects. +*/ +interface IsSquareTriangularNumber { + /** + * Tests if a value is a square triangular number. + * + * @param value - value to test + * @returns boolean indicating whether value is a square triangular number + * + * @example + * var bool = isSquareTriangularNumber( 36.0 ); + * // returns true + * + * @example + * var bool = isSquareTriangularNumber( new Number( 36.0 ) ); + * // returns true + * + * @example + * var bool = isSquareTriangularNumber( 3.14 ); + * // returns false + * + * @example + * var bool = isSquareTriangularNumber( -5.0 ); + * // returns false + * + * @example + * var bool = isSquareTriangularNumber( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a value which is a square triangular number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a value which is a square triangular number + * + * @example + * var bool = isSquareTriangularNumber.isPrimitive( 36.0 ); + * // returns true + * + * @example + * var bool = isSquareTriangularNumber.isPrimitive( new Number( 36.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a value which is a square triangular number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a value which is a square triangular number + * + * @example + * var bool = isSquareTriangularNumber.isObject( 36.0 ); + * // returns false + * + * @example + * var bool = isSquareTriangularNumber.isObject( new Number( 36.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a square triangular number. +* +* ## Notes +* +* - Return values are not reliable for numbers greater than `1125899906842624`. +* +* @param value - value to test +* @returns boolean indicating whether value is a square triangular number +* +* @example +* var bool = isSquareTriangularNumber( 36.0 ); +* // returns true +* +* @example +* var bool = isSquareTriangularNumber( new Number( 36.0 ) ); +* // returns true +* +* @example +* var bool = isSquareTriangularNumber( 3.14 ); +* // returns false +* +* @example +* var bool = isSquareTriangularNumber( -5.0 ); +* // returns false +* +* @example +* var bool = isSquareTriangularNumber( null ); +* // returns false +* +* @example +* var bool = isSquareTriangularNumber.isPrimitive( 36.0 ); +* // returns true +* +* @example +* var bool = isSquareTriangularNumber.isObject( new Number( 36.0 ) ); +* // returns true +*/ +declare var isSquareTriangularNumber: IsSquareTriangularNumber; + + +// EXPORTS // + +export = isSquareTriangularNumber; diff --git a/is-square-triangular-number/docs/types/test.ts b/is-square-triangular-number/docs/types/test.ts new file mode 100644 index 00000000..fd77b351 --- /dev/null +++ b/is-square-triangular-number/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isSquareTriangularNumber = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isSquareTriangularNumber( 3 ); // $ExpectType boolean + isSquareTriangularNumber( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSquareTriangularNumber(); // $ExpectError + isSquareTriangularNumber( 0.2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isSquareTriangularNumber.isPrimitive( new Number( 0.2 ) ); // $ExpectType boolean + isSquareTriangularNumber.isPrimitive( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isSquareTriangularNumber.isPrimitive(); // $ExpectError + isSquareTriangularNumber.isPrimitive( 0.2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isSquareTriangularNumber.isObject( new Number( 0.2 ) ); // $ExpectType boolean + isSquareTriangularNumber.isObject( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isSquareTriangularNumber.isObject(); // $ExpectError + isSquareTriangularNumber.isObject( 0.2, 123 ); // $ExpectError +} diff --git a/is-square-triangular-number/examples/index.js b/is-square-triangular-number/examples/index.js new file mode 100644 index 00000000..8aca7596 --- /dev/null +++ b/is-square-triangular-number/examples/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isSquareTriangularNumber = require( './../lib' ); + +console.log( isSquareTriangularNumber( 36.0 ) ); +// => true + +console.log( isSquareTriangularNumber( new Number( 36.0 ) ) ); +// => true + +console.log( isSquareTriangularNumber( 0.0 ) ); +// => true + +console.log( isSquareTriangularNumber( 1.0 ) ); +// => true + +console.log( isSquareTriangularNumber( 3.14 ) ); +// => false + +console.log( isSquareTriangularNumber( -5.0 ) ); +// => false + +console.log( isSquareTriangularNumber( NaN ) ); +// => false + +console.log( isSquareTriangularNumber( '0.5' ) ); +// => false + +console.log( isSquareTriangularNumber( null ) ); +// => false diff --git a/is-square-triangular-number/lib/index.js b/is-square-triangular-number/lib/index.js new file mode 100644 index 00000000..d7ce5be0 --- /dev/null +++ b/is-square-triangular-number/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a square triangular number. +* +* @module @stdlib/assert/is-square-triangular-number +* +* @example +* var isSquareTriangularNumber = require( '@stdlib/assert/is-square-triangular-number' ); +* +* var bool = isSquareTriangularNumber( 36.0 ); +* // returns true +* +* bool = isSquareTriangularNumber( new Number( 36.0 ) ); +* // returns true +* +* bool = isSquareTriangularNumber( 3.14 ); +* // returns false +* +* bool = isSquareTriangularNumber( -5.0 ); +* // returns false +* +* bool = isSquareTriangularNumber( null ); +* // returns false +* +* @example +* var isSquareTriangularNumber = require( '@stdlib/assert/is-square-triangular-number' ).isPrimitive; +* +* var bool = isSquareTriangularNumber( 36.0 ); +* // returns true +* +* bool = isSquareTriangularNumber( new Number( 36.0 ) ); +* // returns false +* +* @example +* var isSquareTriangularNumber = require( '@stdlib/assert/is-square-triangular-number' ).isObject; +* +* var bool = isSquareTriangularNumber( 36.0 ); +* // returns false +* +* bool = isSquareTriangularNumber( new Number( 36.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isSquareTriangularNumber = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isSquareTriangularNumber, 'isPrimitive', isPrimitive ); +setReadOnly( isSquareTriangularNumber, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isSquareTriangularNumber; diff --git a/is-square-triangular-number/lib/main.js b/is-square-triangular-number/lib/main.js new file mode 100644 index 00000000..3a309b02 --- /dev/null +++ b/is-square-triangular-number/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a square triangular number. +* +* ## Notes +* +* - Return values are not reliable for numbers greater than `1125899906842624`. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a square triangular number +* +* @example +* var bool = isSquareTriangularNumber( 36.0 ); +* // returns true +* +* @example +* var bool = isSquareTriangularNumber( new Number( 36.0 ) ); +* // returns true +* +* @example +* var bool = isSquareTriangularNumber( 3.14 ); +* // returns false +* +* @example +* var bool = isSquareTriangularNumber( -5.0 ); +* // returns false +* +* @example +* var bool = isSquareTriangularNumber( null ); +* // returns false +*/ +function isSquareTriangularNumber( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isSquareTriangularNumber; diff --git a/is-square-triangular-number/lib/object.js b/is-square-triangular-number/lib/object.js new file mode 100644 index 00000000..513107f7 --- /dev/null +++ b/is-square-triangular-number/lib/object.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isObject; +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); + + +// MAIN // + +/** +* Tests if a value is a number object having a value which is a square triangular number. +* +* ## Notes +* +* - Return values are not reliable for numbers greater than `1125899906842624`. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a value which is a square triangular number +* +* @example +* var bool = isSquareTriangularNumber( 36.0 ); +* // returns false +* +* @example +* var bool = isSquareTriangularNumber( new Number( 36.0 ) ); +* // returns true +*/ +function isSquareTriangularNumber( value ) { + var n; + var x; + if ( !isInteger( value ) ) { + return false; + } + // Test if a square number: + x = value.valueOf(); + n = floor( sqrt( x ) + 0.5 ); + if ( n*n !== x ) { + return false; + } + // Test if a triangular number: + x = ( 8.0*x ) + 1.0; + n = floor( sqrt( x ) + 0.5 ); + return n*n === x; +} + + +// EXPORTS // + +module.exports = isSquareTriangularNumber; diff --git a/is-square-triangular-number/lib/primitive.js b/is-square-triangular-number/lib/primitive.js new file mode 100644 index 00000000..71699ede --- /dev/null +++ b/is-square-triangular-number/lib/primitive.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); + + +// MAIN // + +/** +* Tests if a value is a number primitive having a value which is a square triangular number. +* +* ## Notes +* +* - Return values are not reliable for numbers greater than `1125899906842624`. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a value which is a square triangular number +* +* @example +* var bool = isSquareTriangularNumber( 36.0 ); +* // returns true +* +* @example +* var bool = isSquareTriangularNumber( new Number( 36.0 ) ); +* // returns false +*/ +function isSquareTriangularNumber( value ) { + var n; + var x; + if ( !isInteger( value ) ) { + return false; + } + // Test if a square number: + n = floor( sqrt( value ) + 0.5 ); + if ( n*n !== value ) { + return false; + } + // Test if a triangular number: + x = ( 8.0*value ) + 1.0; + n = floor( sqrt( x ) + 0.5 ); + return n*n === x; +} + + +// EXPORTS // + +module.exports = isSquareTriangularNumber; diff --git a/is-square-triangular-number/package.json b/is-square-triangular-number/package.json new file mode 100644 index 00000000..b6cce190 --- /dev/null +++ b/is-square-triangular-number/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-square-triangular-number", + "version": "0.0.0", + "description": "Test if a value is a square triangular number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "triangular", + "triangle", + "binomial", + "binomcoef", + "square", + "is", + "istriangular", + "issquare", + "type", + "check", + "valid", + "validate", + "test", + "primitive", + "object" + ] +} diff --git a/is-square-triangular-number/test/test.js b/is-square-triangular-number/test/test.js new file mode 100644 index 00000000..7291a07e --- /dev/null +++ b/is-square-triangular-number/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isSquareTriangularNumber = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSquareTriangularNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test primitives', function test( t ) { + t.equal( typeof isSquareTriangularNumber.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test objects', function test( t ) { + t.equal( typeof isSquareTriangularNumber.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-square-triangular-number/test/test.main.js b/is-square-triangular-number/test/test.main.js new file mode 100644 index 00000000..610e547e --- /dev/null +++ b/is-square-triangular-number/test/test.main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isSquareTriangularNumber = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSquareTriangularNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a square triangular number', function test( t ) { + t.equal( isSquareTriangularNumber( 36.0 ), true, 'returns true' ); + t.equal( isSquareTriangularNumber( new Number( 36.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a square triangular number', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.0, + 6.0, + 5.0, + -3.14, + -1.0, + NaN, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isSquareTriangularNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-square-triangular-number/test/test.object.js b/is-square-triangular-number/test/test.object.js new file mode 100644 index 00000000..7e14e63c --- /dev/null +++ b/is-square-triangular-number/test/test.object.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isSquareTriangularNumber = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSquareTriangularNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a square triangular number', function test( t ) { + t.equal( isSquareTriangularNumber( new Number( 36.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive square triangular number', function test( t ) { + t.equal( isSquareTriangularNumber( 36.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a square triangular number', function test( t ) { + var values; + var i; + + values = [ + '5', + new Number( 6.0 ), // eslint-disable-line no-new-wrappers + null, + true, + NaN, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), // eslint-disable-line prefer-regex-literals + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isSquareTriangularNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-square-triangular-number/test/test.primitive.js b/is-square-triangular-number/test/test.primitive.js new file mode 100644 index 00000000..c2ef49d7 --- /dev/null +++ b/is-square-triangular-number/test/test.primitive.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isSquareTriangularNumber = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSquareTriangularNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive square triangular number', function test( t ) { + t.equal( isSquareTriangularNumber( 36.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object', function test( t ) { + t.equal( isSquareTriangularNumber( new Number( 36.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a square triangular number', function test( t ) { + var values; + var i; + + values = [ + '5', + 6.0, + null, + true, + NaN, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isSquareTriangularNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-strict-equal/README.md b/is-strict-equal/README.md new file mode 100644 index 00000000..864f5838 --- /dev/null +++ b/is-strict-equal/README.md @@ -0,0 +1,121 @@ + + +# isStrictEqual + +> Test if two arguments are strictly equal. + +
+ +## Usage + +```javascript +var isStrictEqual = require( '@stdlib/assert/is-strict-equal' ); +``` + +#### isStrictEqual( a, b ) + +Tests if two arguments `a` and `b` are strictly equal. + +```javascript +var bool = isStrictEqual( false, false ); +// returns true + +bool = isStrictEqual( '', '' ); +// returns true + +bool = isStrictEqual( {}, {} ); +// returns false + +bool = isStrictEqual( NaN, NaN ); +// returns false +``` + +In contrast to the strict equality operator `===`, the function distinguishes between `+0` and `-0`. + + + +```javascript +var bool = ( 0.0 === -0.0 ); +// returns true + +bool = isStrictEqual( 0.0, -0.0 ); +// returns false + +bool = isStrictEqual( -0.0, -0.0 ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isStrictEqual = require( '@stdlib/assert/is-strict-equal' ); + +var bool = isStrictEqual( true, true ); +// returns true + +bool = isStrictEqual( true, false ); +// returns false + +bool = isStrictEqual( 'beep', 'beep' ); +// returns true + +bool = isStrictEqual( 3.14, 3.14 ); +// returns true + +bool = isStrictEqual( null, null ); +// returns true + +bool = isStrictEqual( 0.0, 0.0 ); +// returns true + +bool = isStrictEqual( -0.0, 0.0 ); +// returns false + +bool = isStrictEqual( NaN, NaN ); +// returns false + +bool = isStrictEqual( {}, {} ); +// returns false + +bool = isStrictEqual( [], [] ); +// returns false + +bool = isStrictEqual( isStrictEqual, isStrictEqual ); +// returns true +``` + +
+ + + + + + diff --git a/is-strict-equal/benchmark/benchmark.js b/is-strict-equal/benchmark/benchmark.js new file mode 100644 index 00000000..d0360143 --- /dev/null +++ b/is-strict-equal/benchmark/benchmark.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isStrictEqual = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var v; + var i; + + values = [ + '', + '5', + 0, + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + new RegExp( '.*' ), + new Date(), + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = values[ i%values.length ]; + bool = isStrictEqual( v, v ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-strict-equal/docs/repl.txt b/is-strict-equal/docs/repl.txt new file mode 100644 index 00000000..f3d6bda7 --- /dev/null +++ b/is-strict-equal/docs/repl.txt @@ -0,0 +1,36 @@ + +{{alias}}( a, b ) + Tests if two arguments are strictly equal. + + The function differs from the `===` operator in that the function treats + `-0` and `+0` as distinct. + + Parameters + ---------- + a: any + First input value. + + b: any + Second input value. + + Returns + ------- + bool: boolean + Boolean indicating whether two arguments are strictly equal. + + Examples + -------- + > var bool = {{alias}}( true, true ) + true + > bool = {{alias}}( {}, {} ) + false + > bool = {{alias}}( -0.0, -0.0 ) + true + > bool = {{alias}}( -0.0, 0.0 ) + false + > bool = {{alias}}( NaN, NaN ) + false + + See Also + -------- + diff --git a/is-strict-equal/docs/types/index.d.ts b/is-strict-equal/docs/types/index.d.ts new file mode 100644 index 00000000..9a8ea4c2 --- /dev/null +++ b/is-strict-equal/docs/types/index.d.ts @@ -0,0 +1,65 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if two arguments are strictly equal. +* +* ## Notes +* +* - The function differs from the `===` operator in that the function treats `-0` and `+0` as distinct. +* +* @param a - first input value +* @param b - second input value +* @returns boolean indicating whether two arguments are strictly equal +* +* @example +* var bool = isStrictEqual( true, true ); +* // returns true +* +* @example +* var bool = isStrictEqual( 3.14, 3.14 ); +* // returns true +* +* @example +* var bool = isStrictEqual( {}, {} ); +* // returns false +* +* @example +* var bool = isStrictEqual( -0.0, -0.0 ); +* // returns true +* +* @example +* var bool = isStrictEqual( -0.0, 0.0 ); +* // returns false +* +* @example +* var bool = isStrictEqual( NaN, NaN ); +* // returns false +* +* @example +* var bool = isStrictEqual( [], [] ); +* // returns false +*/ +declare function isStrictEqual( a: any, b: any ): boolean; + + +// EXPORTS // + +export = isStrictEqual; diff --git a/is-strict-equal/docs/types/test.ts b/is-strict-equal/docs/types/test.ts new file mode 100644 index 00000000..b6eac3f5 --- /dev/null +++ b/is-strict-equal/docs/types/test.ts @@ -0,0 +1,36 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isStrictEqual = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isStrictEqual( 3.14, 3.14 ); // $ExpectType boolean + isStrictEqual( null, null ); // $ExpectType boolean + isStrictEqual( 'beep', 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isStrictEqual(); // $ExpectError + isStrictEqual( 3.14 ); // $ExpectError + isStrictEqual( 'beep', 'beep', 3.14 ); // $ExpectError +} diff --git a/is-strict-equal/examples/index.js b/is-strict-equal/examples/index.js new file mode 100644 index 00000000..0f5e7cd9 --- /dev/null +++ b/is-strict-equal/examples/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isStrictEqual = require( './../lib' ); + +console.log( isStrictEqual( true, true ) ); +// => true + +console.log( isStrictEqual( true, false ) ); +// => false + +console.log( isStrictEqual( 'beep', 'beep' ) ); +// => true + +console.log( isStrictEqual( 3.14, 3.14 ) ); +// => true + +console.log( isStrictEqual( null, null ) ); +// => true + +console.log( isStrictEqual( 0.0, 0.0 ) ); +// => true + +console.log( isStrictEqual( -0.0, 0.0 ) ); +// => false + +console.log( isStrictEqual( NaN, NaN ) ); +// => false + +console.log( isStrictEqual( {}, {} ) ); +// => false + +console.log( isStrictEqual( [], [] ) ); +// => false + +console.log( isStrictEqual( isStrictEqual, isStrictEqual ) ); +// => true diff --git a/is-strict-equal/lib/index.js b/is-strict-equal/lib/index.js new file mode 100644 index 00000000..ba0a5f1e --- /dev/null +++ b/is-strict-equal/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if two arguments are strictly equal. +* +* @module @stdlib/assert/is-strict-equal +* +* @example +* var isStrictEqual = require( '@stdlib/assert/is-strict-equal' ); +* +* var bool = isStrictEqual( true, true ); +* // returns true +* +* bool = isStrictEqual( 3.14, 3.14 ); +* // returns true +* +* bool = isStrictEqual( {}, {} ); +* // returns false +* +* bool = isStrictEqual( -0.0, -0.0 ); +* // returns true +* +* bool = isStrictEqual( -0.0, 0.0 ); +* // returns false +* +* bool = isStrictEqual( NaN, NaN ); +* // returns false +* +* bool = isStrictEqual( [], [] ); +* // returns false +*/ + +// MODULES // + +var isStrictEqual = require( './main.js' ); + + +// EXPORTS // + +module.exports = isStrictEqual; diff --git a/is-strict-equal/lib/main.js b/is-strict-equal/lib/main.js new file mode 100644 index 00000000..de1823ce --- /dev/null +++ b/is-strict-equal/lib/main.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests if two arguments are strictly equal. +* +* ## Notes +* +* - In contrast to the strict equality operator `===`, `-0` and `+0` are distinguishable. +* +* +* @param {*} a - first input value +* @param {*} b - second input value +* @returns {boolean} boolean indicating whether two arguments are strictly equal +* +* @example +* var bool = isStrictEqual( true, true ); +* // returns true +* +* @example +* var bool = isStrictEqual( 3.14, 3.14 ); +* // returns true +* +* @example +* var bool = isStrictEqual( {}, {} ); +* // returns false +* +* @example +* var bool = isStrictEqual( -0.0, -0.0 ); +* // returns true +* +* @example +* var bool = isStrictEqual( -0.0, 0.0 ); +* // returns false +* +* @example +* var bool = isStrictEqual( NaN, NaN ); +* // returns false +* +* @example +* var bool = isStrictEqual( [], [] ); +* // returns false +*/ +function isStrictEqual( a, b ) { + if ( a === b ) { + if ( a === 0.0 ) { + return 1.0 / a === 1.0 / b; // handles +-0 + } + return true; + } + return false; +} + + +// EXPORTS // + +module.exports = isStrictEqual; diff --git a/is-strict-equal/package.json b/is-strict-equal/package.json new file mode 100644 index 00000000..5bde386a --- /dev/null +++ b/is-strict-equal/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-strict-equal", + "version": "0.0.0", + "description": "Test if two arguments are strictly equal.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "equal", + "same", + "strict", + "is", + "isequal", + "isstrictequal", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-strict-equal/test/test.js b/is-strict-equal/test/test.js new file mode 100644 index 00000000..22f6bece --- /dev/null +++ b/is-strict-equal/test/test.js @@ -0,0 +1,119 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isStrictEqual = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isStrictEqual, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided two arguments which are strictly equal', function test( t ) { + var values; + var i; + + values = [ + '', + 'beep', + 5, + 3.14, + -3.14, + 0.0, + -0.0, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + new Date(), + /.*/ + ]; + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isStrictEqual( values[ i ], values[ i ] ), true, 'returns true when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided two arguments which are strictly equal', function test( t ) { + var a; + var b; + var i; + + a = [ + '', + 'beep', + new String( 'beep' ), + 5, + 3.14, + -3.14, + 0.0, + -0.0, + NaN, + new Number( 5 ), + true, + false, + new Boolean( true ), + null, + void 0, + [], + {}, + function noop() {}, + new Date(), + /.*/ + ]; + b = [ + 'abc', + 'boop', + new String( 'beep' ), + -5, + -3.14, + 3.14, + -0.0, + 0.0, + NaN, + new Number( 5 ), + false, + true, + new Boolean( true ), + void 0, + null, + [], + {}, + function noop() {}, + new Date(), + /.*/ + ]; + for ( i = 0; i < a.length; i++ ) { + t.strictEqual( isStrictEqual( a[ i ], b[ i ] ), false, 'returns false when provided '+a[ i ]+' and '+b[ i ] ); + } + t.end(); +}); diff --git a/is-string-array/README.md b/is-string-array/README.md new file mode 100644 index 00000000..a63bad81 --- /dev/null +++ b/is-string-array/README.md @@ -0,0 +1,117 @@ + + +# isStringArray + +> Test if a value is an array of strings. + +
+ +## Usage + +```javascript +var isStringArray = require( '@stdlib/assert/is-string-array' ); +``` + +#### isStringArray( value ) + +Tests if a `value` is an array of strings. + + + +```javascript +var bool = isStringArray( [ 'beep', new String('boop') ] ); +// returns true + +bool = isStringArray( [ 'beep', null ] ); +// returns false +``` + +#### isStringArray.primitives( value ) + +Tests if a `value` is an `array` containing **only** `string` primitives. + + + +```javascript +var bool = isStringArray.primitives( [ 'beep', 'boop' ] ); +// returns true + +bool = isStringArray.primitives( [ 'beep', new String('boop') ] ); +// returns false +``` + +#### isStringArray.objects( value ) + +Tests if a `value` is an `array` containing **only** `String` objects. + + + +```javascript +var bool = isStringArray.objects( [ new String('beep'), new String('boop') ] ); +// returns true + +bool = isStringArray.objects( [ 'beep', new String('boop') ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isStringArray = require( '@stdlib/assert/is-string-array' ); + +var bool = isStringArray( [ 'Hello World!' ] ); +// returns true + +bool = isStringArray( [ 'a', 'b', 'c' ] ); +// returns true + +bool = isStringArray( [ new String( 'abc' ), 'def', 'ghi' ] ); +// returns true + +bool = isStringArray( 'abc' ); +// returns false + +bool = isStringArray( [] ); +// returns false + +bool = isStringArray( [ 'a', NaN ] ); +// returns false +``` + +
+ + + + + + diff --git a/is-string-array/benchmark/benchmark.js b/is-string-array/benchmark/benchmark.js new file mode 100644 index 00000000..fa64f33c --- /dev/null +++ b/is-string-array/benchmark/benchmark.js @@ -0,0 +1,118 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var isStringArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + for ( i = 0; i < len; i++ ) { + x.push( 'beep' ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( new String( 'beep' ) ); // eslint-disable-line no-new-wrappers + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isStringArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isStringArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isStringArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-string-array/docs/repl.txt b/is-string-array/docs/repl.txt new file mode 100644 index 00000000..ec46c016 --- /dev/null +++ b/is-string-array/docs/repl.txt @@ -0,0 +1,72 @@ + +{{alias}}( value ) + Tests if a value is an array of strings. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array of strings. + + Examples + -------- + > var bool = {{alias}}( [ 'abc', 'def' ] ) + true + > bool = {{alias}}( [ 'abc', 123 ] ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array containing only string primitives. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array containing only string + primitives. + + Examples + -------- + > var arr = [ 'abc', 'def' ]; + > var bool = {{alias}}.primitives( arr ) + true + > arr = [ 'abc', new String( 'def' ) ]; + > bool = {{alias}}.primitives( arr ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array containing only `String` objects. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array containing only `String` + objects. + + Examples + -------- + > var arr = [ new String( 'ab' ), new String( 'cd' ) ]; + > var bool = {{alias}}.objects( arr ) + true + > arr = [ new String( 'abc' ), 'def' ]; + > bool = {{alias}}.objects( arr ) + false + + See Also + -------- + diff --git a/is-string-array/docs/types/index.d.ts b/is-string-array/docs/types/index.d.ts new file mode 100644 index 00000000..7bfd1149 --- /dev/null +++ b/is-string-array/docs/types/index.d.ts @@ -0,0 +1,101 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isStringArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsStringArray { + /** + * Tests if a value is an array of strings. + * + * @param value - value to test + * @returns boolean indicating whether value is an array of strings + * + * @example + * var bool = isStringArray( [ 'abc', 'def' ] ); + * // returns true + * + * @example + * var bool = isStringArray( [ 'abc', 123 ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array containing only string primitives. + * + * @param value - value to test + * @returns boolean indicating whether value is an array containing only string primitives + * + * @example + * var bool = isStringArray.primitives( [ 'abc', 'def' ] ); + * // returns true + * + * @example + * var bool = isStringArray.primitives( [ 'abc', new String( 'def' ) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array containing only `String` objects. + * + * @param value - value to test + * @returns boolean indicating whether value is an array containing only `String` objects + * + * @example + * var bool = isStringArray.objects( [ new String( 'abc' ), new String( 'def' ) ] ); + * // returns true + * + * @example + * var bool = isStringArray.objects( [ new String( 'abc' ), 'def' ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array of strings. +* +* @param value - value to test +* @returns boolean indicating whether value is an array of strings +* +* @example +* var bool = isStringArray( [ 'abc', 'def' ] ); +* // returns true +* +* @example +* var bool = isStringArray( [ 'abc', 123 ] ); +* // returns false +* +* @example +* var bool = isStringArray.primitives( [ 'abc', 'def' ] ); +* // returns true +* +* @example +* var bool = isStringArray.objects( [ new String( 'abc' ), new String( 'def' ) ] ); +* // returns true +*/ +declare var isStringArray: IsStringArray; + + +// EXPORTS // + +export = isStringArray; diff --git a/is-string-array/docs/types/test.ts b/is-string-array/docs/types/test.ts new file mode 100644 index 00000000..a95746f1 --- /dev/null +++ b/is-string-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isStringArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isStringArray( [ 'abc' ] ); // $ExpectType boolean + isStringArray( [ 2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isStringArray(); // $ExpectError + isStringArray( [ 'abc' ], 10.23 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isStringArray.primitives( [ new String( 'abc' ) ] ); // $ExpectType boolean + isStringArray.primitives( [ 'abc' ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isStringArray.primitives(); // $ExpectError + isStringArray.primitives( [ 'abc' ], 123 ); // $ExpectError +} + + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isStringArray.objects( [ new String( 'abc' ) ] ); // $ExpectType boolean + isStringArray.objects( [ 'abc' ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isStringArray.objects(); // $ExpectError + isStringArray.objects( [ 'abc' ], 123 ); // $ExpectError +} diff --git a/is-string-array/examples/index.js b/is-string-array/examples/index.js new file mode 100644 index 00000000..6b80f314 --- /dev/null +++ b/is-string-array/examples/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var isStringArray = require( './../lib' ); + +var bool = isStringArray( [ 'Hello World!' ] ); +console.log( bool ); +// => true + +bool = isStringArray( [ 'a', 'b', 'c' ] ); +console.log( bool ); +// => true + +bool = isStringArray( [ new String( 'abc' ), 'def', 'ghi' ] ); +console.log( bool ); +// => true + +bool = isStringArray( 'abc' ); +console.log( bool ); +// => false + +bool = isStringArray( [] ); +console.log( bool ); +// => false + +bool = isStringArray( [ 'a', NaN ] ); +console.log( bool ); +// => false diff --git a/is-string-array/lib/index.js b/is-string-array/lib/index.js new file mode 100644 index 00000000..28cb3c39 --- /dev/null +++ b/is-string-array/lib/index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array of strings. +* +* @module @stdlib/assert/is-string-array +* +* @example +* var isStringArray = require( '@stdlib/assert/is-string-array' ); +* +* var bool = isStringArray( [ 'abc', 'def' ] ); +* // returns true +* +* bool = isStringArray( [ 'abc', 123 ] ); +* // returns false +* +* @example +* var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives; +* +* var bool = isStringArray( [ 'abc', 'def' ] ); +* // returns true +* +* bool = isStringArray( [ 'abc', new String( 'def' ) ] ); +* // returns false +* +* @example +* var isStringArray = require( '@stdlib/assert/is-string-array' ).objects; +* +* var bool = isStringArray( [ new String( 'abc' ), new String( 'def' ) ] ); +* // returns true +* +* bool = isStringArray( [ new String( 'abc' ), 'def' ] ); +* // returns false +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-function' ); +var isString = require( '@stdlib/assert/is-string' ); + + +// MAIN // + +var isStringArray = arrayfun( isString ); +setReadOnly( isStringArray, 'primitives', arrayfun( isString.isPrimitive ) ); +setReadOnly( isStringArray, 'objects', arrayfun( isString.isObject ) ); + + +// EXPORTS // + +module.exports = isStringArray; diff --git a/is-string-array/package.json b/is-string-array/package.json new file mode 100644 index 00000000..15121e1c --- /dev/null +++ b/is-string-array/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-string-array", + "version": "0.0.0", + "description": "Test if a value is an array of strings.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "function", + "array", + "is", + "isstring", + "isarray", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-string-array/test/test.js b/is-string-array/test/test.js new file mode 100644 index 00000000..bb8ffc4b --- /dev/null +++ b/is-string-array/test/test.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isStringArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isStringArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array of `string` values', function test( t ) { + var arr; + + arr = [ 'a', new String( 'b' ) ]; // eslint-disable-line no-new-wrappers + t.equal( isStringArray( arr ), true, 'returns true' ); + + arr = [ 'a', 5, null ]; + t.equal( isStringArray( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array of `string` primitives', function test( t ) { + var arr; + + arr = [ 'a', 'b' ]; + t.equal( isStringArray.primitives( arr ), true, 'returns true' ); + + arr = [ new String( 'a' ), 'b' ]; // eslint-disable-line no-new-wrappers + t.equal( isStringArray.primitives( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function provides a method to test for an array of `String` objects', function test( t ) { + var arr; + + arr = [ new String( 'a' ), new String( 'b' ) ]; // eslint-disable-line no-new-wrappers + t.equal( isStringArray.objects( arr ), true, 'returns true' ); + + arr = [ 'a', 'b' ]; + t.equal( isStringArray.objects( arr ), false, 'returns false' ); + + t.end(); +}); diff --git a/is-string/README.md b/is-string/README.md new file mode 100644 index 00000000..e0615889 --- /dev/null +++ b/is-string/README.md @@ -0,0 +1,123 @@ + + +# isString + +> Test if a value is a string. + +
+ +## Usage + +```javascript +var isString = require( '@stdlib/assert/is-string' ); +``` + +#### isString( value ) + +Tests if a value is a `string`. + + + +```javascript +var bool = isString( 'beep' ); +// returns true + +bool = isString( new String( 'beep' ) ); +// returns true +``` + +#### isString.isPrimitive( value ) + +Tests if a `value` is a primitive `string`. + + + +```javascript +var bool = isString.isPrimitive( 'beep' ); +// returns true + +bool = isString.isPrimitive( new String( 'boop' ) ); +// returns false +``` + +#### isString.isObject( value ) + +Tests if a `value` is a `String` object. + + + +```javascript +var bool = isString.isObject( 'beep' ); +// returns false + +bool = isString.isObject( new String( 'boop' ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isString = require( '@stdlib/assert/is-string' ); + +var bool = isString( 'beep' ); +// returns true + +bool = isString( new String( 'beep' ) ); +// returns true + +bool = isString( 5 ); +// returns false + +bool = isString( null ); +// returns false + +bool = isString( void 0 ); +// returns false + +bool = isString( {} ); +// returns false + +bool = isString( [] ); +// returns false + +bool = isString( function foo() {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-string/benchmark/benchmark.js b/is-string/benchmark/benchmark.js new file mode 100644 index 00000000..aab9e061 --- /dev/null +++ b/is-string/benchmark/benchmark.js @@ -0,0 +1,202 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isString = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isString( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new String( 'beep' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isString( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isString.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new String( 'beep' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isString.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isString.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new String( 'beep' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isString.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-string/docs/repl.txt b/is-string/docs/repl.txt new file mode 100644 index 00000000..4f2f7d3a --- /dev/null +++ b/is-string/docs/repl.txt @@ -0,0 +1,68 @@ + +{{alias}}( value ) + Tests if a value is a string. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a string. + + Examples + -------- + > var bool = {{alias}}( 'beep' ) + true + > bool = {{alias}}( new String( 'beep' ) ) + true + > bool = {{alias}}( 5 ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a string primitive. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a string primitive. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 'beep' ) + true + > bool = {{alias}}.isPrimitive( new String( 'beep' ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a `String` object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a `String` object. + + Examples + -------- + > var bool = {{alias}}.isObject( new String( 'beep' ) ) + true + > bool = {{alias}}.isObject( 'beep' ) + false + + See Also + -------- + diff --git a/is-string/docs/types/index.d.ts b/is-string/docs/types/index.d.ts new file mode 100644 index 00000000..6db3c410 --- /dev/null +++ b/is-string/docs/types/index.d.ts @@ -0,0 +1,101 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isString` with methods for testing for primitives and objects, respectively. +*/ +interface IsString { + /** + * Tests if a value is a string. + * + * @param value - value to test + * @returns boolean indicating whether value is a string + * + * @example + * var bool = isString( new String( 'beep' ) ); + * // returns true + * + * @example + * var bool = isString( 'beep' ); + * // returns true + */ + ( value: any ): boolean; + + /** + * Tests if a value is a string primitive. + * + * @param value - value to test + * @returns boolean indicating if a value is a string primitive + * + * @example + * var bool = isString.isPrimitive( 'beep' ); + * // returns true + * + * @example + * var bool = isString.isPrimitive( new String( 'beep' ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a string object. + * + * @param value - value to test + * @returns boolean indicating if a value is a string object + * + * @example + * var bool = isString.isObject( new String( 'beep' ) ); + * // returns true + * + * @example + * var bool = isString.isObject( 'beep' ); + * // returns false + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a string. +* +* @param value - value to test +* @returns boolean indicating whether value is a string +* +* @example +* var bool = isString( new String( 'beep' ) ); +* // returns true +* +* @example +* var bool = isString( 'beep' ); +* // returns true +* +* @example +* var bool = isString.isPrimitive( 'beep' ); +* // returns true +* +* @example +* var bool = isString.isObject( 'beep' ); +* // returns false +*/ +declare var isString: IsString; + + +// EXPORTS // + +export = isString; diff --git a/is-string/docs/types/test.ts b/is-string/docs/types/test.ts new file mode 100644 index 00000000..77a8566f --- /dev/null +++ b/is-string/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isString = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isString( 3 ); // $ExpectType boolean + isString( 'abc' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isString(); // $ExpectError + isString( 'abc', 123 ); // $ExpectError +} + +// Attached to main export is an `isPrimitive` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isString.isPrimitive( new String( 'abc' ) ); // $ExpectType boolean + isString.isPrimitive( 'abc' ); // $ExpectType boolean +} + +// The compiler throws an error if the `isPrimitive` method is provided an unsupported number of arguments... +{ + isString.isPrimitive(); // $ExpectError + isString.isPrimitive( 'abc', 123 ); // $ExpectError +} + +// Attached to main export is an `isObject` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isString.isObject( new String( 'abc' ) ); // $ExpectType boolean + isString.isObject( 'abc' ); // $ExpectType boolean +} + +// The compiler throws an error if the `isObject` method is provided an unsupported number of arguments... +{ + isString.isObject(); // $ExpectError + isString.isObject( 'abc', 123 ); // $ExpectError +} diff --git a/is-string/examples/index.js b/is-string/examples/index.js new file mode 100644 index 00000000..0b29de6e --- /dev/null +++ b/is-string/examples/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-restricted-syntax, no-empty-function */ + +'use strict'; + +var isString = require( './../lib' ); + +console.log( isString( 'beep' ) ); +// => true + +console.log( isString( new String( 'beep' ) ) ); +// => true + +console.log( isString( 5 ) ); +// => false + +console.log( isString( null ) ); +// => false + +console.log( isString( void 0 ) ); +// => false + +console.log( isString( {} ) ); +// => false + +console.log( isString( [] ) ); +// => false + +console.log( isString( function foo() {} ) ); +// => false diff --git a/is-string/lib/index.js b/is-string/lib/index.js new file mode 100644 index 00000000..2793c6e0 --- /dev/null +++ b/is-string/lib/index.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a string. +* +* @module @stdlib/assert/is-string +* +* @example +* var isString = require( '@stdlib/assert/is-string' ); +* +* var bool = isString( 'beep' ); +* // returns true +* +* bool = isString( new String( 'beep' ) ); +* // returns true +* +* bool = isString( 5 ); +* // returns false +* +* @example +* var isString = require( '@stdlib/assert/is-string' ).isObject; +* +* var bool = isString( new String( 'beep' ) ); +* // returns true +* +* bool = isString( 'beep' ); +* // returns false +* +* @example +* var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +* +* var bool = isString( 'beep' ); +* // returns true +* +* bool = isString( new String( 'beep' ) ); +* // returns false +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isString = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isString, 'isPrimitive', isPrimitive ); +setReadOnly( isString, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isString; diff --git a/is-string/lib/main.js b/is-string/lib/main.js new file mode 100644 index 00000000..5e459b04 --- /dev/null +++ b/is-string/lib/main.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a string. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a string +* +* @example +* var bool = isString( new String( 'beep' ) ); +* // returns true +* +* @example +* var bool = isString( 'beep' ); +* // returns true +*/ +function isString( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isString; diff --git a/is-string/lib/object.js b/is-string/lib/object.js new file mode 100644 index 00000000..d249ace5 --- /dev/null +++ b/is-string/lib/object.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); +var nativeClass = require( '@stdlib/utils/native-class' ); +var test = require( './try2valueof.js' ); + + +// VARIABLES // + +var FLG = hasToStringTag(); + + +// MAIN // + +/** +* Tests if a value is a string object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a string object +* +* @example +* var bool = isString( new String( 'beep' ) ); +* // returns true +* +* @example +* var bool = isString( 'beep' ); +* // returns false +*/ +function isString( value ) { + if ( typeof value === 'object' ) { + if ( value instanceof String ) { + return true; + } + if ( FLG ) { + return test( value ); + } + return ( nativeClass( value ) === '[object String]' ); + } + return false; +} + + +// EXPORTS // + +module.exports = isString; diff --git a/is-string/lib/primitive.js b/is-string/lib/primitive.js new file mode 100644 index 00000000..c10f2372 --- /dev/null +++ b/is-string/lib/primitive.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Tests if a value is a string primitive. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a string primitive +* +* @example +* var bool = isString( 'beep' ); +* // returns true +* +* @example +* var bool = isString( new String( 'beep' ) ); +* // returns false +*/ +function isString( value ) { + return ( typeof value === 'string' ); +} + + +// EXPORTS // + +module.exports = isString; diff --git a/is-string/lib/try2valueof.js b/is-string/lib/try2valueof.js new file mode 100644 index 00000000..d50d75c8 --- /dev/null +++ b/is-string/lib/try2valueof.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var valueOf = require( './valueof.js' ); // eslint-disable-line stdlib/no-redeclare + + +// MAIN // + +/** +* Attempts to extract a string value. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a string can be extracted +*/ +function test( value ) { + try { + valueOf.call( value ); + return true; + } catch ( err ) { // eslint-disable-line no-unused-vars + return false; + } +} + + +// EXPORTS // + +module.exports = test; diff --git a/is-string/lib/valueof.js b/is-string/lib/valueof.js new file mode 100644 index 00000000..35947d04 --- /dev/null +++ b/is-string/lib/valueof.js @@ -0,0 +1,27 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// eslint-disable-next-line stdlib/no-redeclare +var valueOf = String.prototype.valueOf; // non-generic + + +// EXPORTS // + +module.exports = valueOf; diff --git a/is-string/package.json b/is-string/package.json new file mode 100644 index 00000000..90e021f6 --- /dev/null +++ b/is-string/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-string", + "version": "0.0.0", + "description": "Test if a value is a string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "string", + "str", + "is", + "isstring", + "type", + "check", + "primitive", + "object", + "valid", + "validate", + "test", + "isvalid" + ] +} diff --git a/is-string/test/test.js b/is-string/test/test.js new file mode 100644 index 00000000..53e11d6e --- /dev/null +++ b/is-string/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isString = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive string', function test( t ) { + t.equal( typeof isString.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a string object', function test( t ) { + t.equal( typeof isString.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-string/test/test.main.js b/is-string/test/test.main.js new file mode 100644 index 00000000..9ba6e471 --- /dev/null +++ b/is-string/test/test.main.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isString = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a string', function test( t ) { + t.equal( isString( 'a' ), true, 'returns true' ); + t.equal( isString( new String( 'a' ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a string', function test( t ) { + var values; + var i; + + values = [ + 5, + NaN, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isString( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-string/test/test.object.js b/is-string/test/test.object.js new file mode 100644 index 00000000..c114c90d --- /dev/null +++ b/is-string/test/test.object.js @@ -0,0 +1,121 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var hasToStringTag = require( '@stdlib/assert/has-tostringtag-support' ); +var isString = require( './../lib/object.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a string object', function test( t ) { + t.equal( isString( new String( 'a' ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive string', function test( t ) { + t.equal( isString( 'a' ), false, 'returns false' ); + t.end(); +}); + +tape( 'if `Symbol.toStringTag` is supported, the function guards against objects masquerading as `String` objects', opts, function test( t ) { + var isString; + var mock; + + isString = proxyquire( './../lib/object.js', { + '@stdlib/assert/has-tostringtag-support': detect + }); + + mock = { + 'toString': toString, + 'valueOf': valueOf + }; + if ( hasToStringTag() ) { + mock[ Symbol.toStringTag ] = 'String'; + } + t.equal( isString( mock ), false, 'returns false' ); + t.end(); + + function detect() { + return true; + } + function toString() { + return 'a'; + } + function valueOf() { + return 'a'; + } +}); + +tape( 'if `Symbol.toStringTag` is not supported, the function attempts to determine the native class', function test( t ) { + var isString = proxyquire( './../lib/object.js', { + '@stdlib/assert/has-tostringtag-support': detect + }); + + t.equal( isString( new String( 'a' ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.equal( isString( {} ), false, 'returns false' ); + + t.end(); + + function detect() { + return false; + } +}); + +tape( 'the function returns `false` if not provided a string', function test( t ) { + var values; + var i; + + values = [ + 5, + NaN, + null, + true, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isString( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-string/test/test.primitive.js b/is-string/test/test.primitive.js new file mode 100644 index 00000000..e10db481 --- /dev/null +++ b/is-string/test/test.primitive.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isString = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive string', function test( t ) { + t.equal( isString( 'a' ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a string object', function test( t ) { + t.equal( isString( new String( 'a' ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a string', function test( t ) { + var values; + var i; + + values = [ + 5, + NaN, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isString( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-string/test/test.try2valueof.js b/is-string/test/test.try2valueof.js new file mode 100644 index 00000000..85349a5e --- /dev/null +++ b/is-string/test/test.try2valueof.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var valueOf = require( './../lib/try2valueof.js' ); // eslint-disable-line stdlib/no-redeclare + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof valueOf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if able to extract a string value', function test( t ) { + t.equal( valueOf( 'a' ), true, 'returns true' ); + t.equal( valueOf( new String( 'a' ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if unable to extract a string value', function test( t ) { + t.equal( valueOf( {} ), false, 'returns false' ); + t.end(); +}); diff --git a/is-symbol-array/README.md b/is-symbol-array/README.md new file mode 100644 index 00000000..27d6abb0 --- /dev/null +++ b/is-symbol-array/README.md @@ -0,0 +1,128 @@ + + +# isSymbolArray + +> Test if a value is an array-like object containing only [symbols][mdn-symbol]. + +
+ +## Usage + +```javascript +var isSymbolArray = require( '@stdlib/assert/is-symbol-array' ); +``` + +#### isSymbolArray( value ) + +Tests if a `value` is an array-like object containing only [symbols][mdn-symbol]. + +```javascript +var Symbol = require( '@stdlib/symbol/ctor' ); + +var bool = isSymbolArray( [ Symbol( 'beep' ), Symbol( 'boop' ) ] ); +// returns true + +bool = isSymbolArray( [ 'beep', 'boop' ] ); +// returns false +``` + +#### isSymbolArray.primitives( value ) + +Tests if a `value` is an array-like object containing only [`symbol`][mdn-symbol] primitives. + +```javascript +var Symbol = require( '@stdlib/symbol/ctor' ); + +var bool = isSymbolArray.primitives( [ Symbol( 'beep' ), Symbol( 'boop' ) ] ); +// returns true + +bool = isSymbolArray.primitives( [ Symbol( 'beep' ), Object( Symbol( 'boop' ) ) ] ); +// returns false +``` + +#### isSymbolArray.objects( value ) + +Tests if a `value` is an array-like object containing only [`Symbol`][mdn-symbol] objects. + +```javascript +var Symbol = require( '@stdlib/symbol/ctor' ); + +var bool = isSymbolArray.objects( [ Object( Symbol( 'beep' ) ), Object( Symbol( 'boop' ) ) ] ); +// returns true + +bool = isSymbolArray.objects( [ Symbol( 'beep' ), Symbol( 'boop' ) ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var isSymbolArray = require( '@stdlib/assert/is-symbol-array' ); + +var hasSymbols = hasSymbolSupport(); +var bool; + +if ( hasSymbols ) { + bool = isSymbolArray( [ Symbol( 'beep' ), Symbol( 'boop' ) ] ); + // returns true + + bool = isSymbolArray( [ Symbol( 'beep' ), 'boop' ] ); + // returns false + + bool = isSymbolArray( Symbol( 'beep' ) ); + // returns false +} else { + console.log( 'Environment does not support symbols.' ); +} +bool = isSymbolArray( [ 'beep', 'boop' ] ); +// returns false + +bool = isSymbolArray( [] ); +// returns false + +bool = isSymbolArray( 'abc' ); +// returns false + +bool = isSymbolArray( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-symbol-array/benchmark/benchmark.js b/is-symbol-array/benchmark/benchmark.js new file mode 100644 index 00000000..27103071 --- /dev/null +++ b/is-symbol-array/benchmark/benchmark.js @@ -0,0 +1,134 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var pkg = require( './../package.json' ).name; +var isSymbolArray = require( './../lib' ); + + +// VARIABLES // + +var hasSymbols = hasSymbolSupport(); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @param {boolean} primitives - boolean indicating whether to fill array with primitives or objects +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len, primitives ) { + var x; + var i; + + x = []; + if ( primitives ) { + if ( hasSymbols ) { + for ( i = 0; i < len; i++ ) { + x.push( Symbol( 'beep' ) ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( 'beep' ); + } + } + } else if ( hasSymbols ) { + for ( i = 0; i < len; i++ ) { + x.push( Object( Symbol( 'beep' ) ) ); + } + } else { + for ( i = 0; i < len; i++ ) { + x.push( {} ); + } + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + f = createBenchmark( isSymbolArray, len, false ); + bench( pkg+':len='+len, f ); + + f = createBenchmark( isSymbolArray.primitives, len, true ); + bench( pkg+':primitives:len='+len, f ); + + f = createBenchmark( isSymbolArray.objects, len, false ); + bench( pkg+':objects:len='+len, f ); + } +} + +main(); diff --git a/is-symbol-array/docs/repl.txt b/is-symbol-array/docs/repl.txt new file mode 100644 index 00000000..0be95094 --- /dev/null +++ b/is-symbol-array/docs/repl.txt @@ -0,0 +1,101 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only symbols. + + In pre-ES2015 environments, the function always returns `false`. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an array-like object containing + only symbols. + + Examples + -------- + > var bool = {{alias}}( [ Symbol( 'beep' ), Symbol( 'boop' ) ] ) + true + > bool = {{alias}}( Symbol( 'beep' ) ) + false + > bool = {{alias}}( [] ) + false + > bool = {{alias}}( {} ) + false + > bool = {{alias}}( null ) + false + > bool = {{alias}}( true ) + false + + +{{alias}}.primitives( value ) + Tests if a value is an array-like object containing only `symbol` + primitives. + + In pre-ES2015 environments, the function always returns `false`. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an array-like object containing + only `symbol` primitives. + + Examples + -------- + > var bool = {{alias}}.primitives( [ Symbol( 'beep' ) ] ) + true + > bool = {{alias}}.primitives( [ Object( Symbol( 'beep' ) ) ] ) + false + > bool = {{alias}}.primitives( [] ) + false + > bool = {{alias}}.primitives( {} ) + false + > bool = {{alias}}.primitives( null ) + false + > bool = {{alias}}.primitives( true ) + false + + +{{alias}}.objects( value ) + Tests if a value is an array-like object containing only `Symbol` + objects. + + In pre-ES2015 environments, the function always returns `false`. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an array-like object containing + only `Symbol` objects. + + Examples + -------- + > var bool = {{alias}}.objects( [ Object( Symbol( 'beep' ) ) ] ) + true + > bool = {{alias}}.objects( [ Symbol( 'beep' ) ] ) + false + > bool = {{alias}}.objects( [] ) + false + > bool = {{alias}}.objects( {} ) + false + > bool = {{alias}}.objects( null ) + false + > bool = {{alias}}.objects( true ) + false + + See Also + -------- + diff --git a/is-symbol-array/docs/types/index.d.ts b/is-symbol-array/docs/types/index.d.ts new file mode 100644 index 00000000..ccb18684 --- /dev/null +++ b/is-symbol-array/docs/types/index.d.ts @@ -0,0 +1,118 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isSymbolArray` with methods for testing for primitive and object arrays, respectively. +*/ +interface IsSymbolArray { + /** + * Tests if a value is an array-like object containing only symbols. + * + * ## Notes + * + * - In pre-ES2015 environments, the function always returns `false`. + * + * @param value - value to test + * @returns boolean indicating whether value is an array of strings + * + * @example + * var bool = isSymbolArray( [ Symbol( 'abc' ), Symbol( 'def' ) ] ); + * // returns true + * + * @example + * var bool = isSymbolArray( [ Symbol( 'abc' ), 'def' ] ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only `symbol` primitives. + * + * ## Notes + * + * - In pre-ES2015 environments, the function always returns `false`. + * + * @param value - value to test + * @returns boolean indicating whether value is an array containing only string primitives + * + * @example + * var bool = isSymbolArray.primitives( [ Symbol( 'abc' ), Symbol( 'def' ) ] ); + * // returns true + * + * @example + * var bool = isSymbolArray.primitives( [ Symbol( 'abc' ), Object( Symbol( 'def' ) ) ] ); + * // returns false + */ + primitives( value: any ): boolean; + + /** + * Tests if a value is an array-like object containing only `Symbol` objects. + * + * + * ## Notes + * + * - In pre-ES2015 environments, the function always returns `false`. + * + * @param value - value to test + * @returns boolean indicating whether a value is an array-like object containing only `Symbol` objects + * + * @example + * var bool = isSymbolArray.objects( [ Object( Symbol( 'abc' ) ), Object( Symbol( 'def' ) ) ] ); + * // returns true + * + * @example + * var bool = isSymbolArray.objects( [ Symbol( 'abc' ), Symbol( 'def' ) ] ); + * // returns false + */ + objects( value: any ): boolean; +} + +/** +* Tests if a value is an array-like object containing only symbols. +* +* ## Notes +* +* - In pre-ES2015 environments, the function always returns `false`. +* +* @param value - value to test +* @returns boolean indicating whether value is an array of strings +* +* @example +* var bool = isSymbolArray( [ Symbol( 'abc' ), Symbol( 'def' ) ] ); +* // returns true +* +* @example +* var bool = isSymbolArray( [ Symbol( 'abc' ), 'def' ] ); +* // returns false +* +* @example +* var bool = isSymbolArray.primitives( [ Symbol( 'abc' ), Symbol( 'def' ) ] ); +* // returns true +* +* @example +* var bool = isSymbolArray.objects( [ Object( Symbol( 'abc' ) ), Object( Symbol( 'def' ) ) ] ); +* // returns true +*/ +declare var isSymbolArray: IsSymbolArray; + + +// EXPORTS // + +export = isSymbolArray; diff --git a/is-symbol-array/docs/types/test.ts b/is-symbol-array/docs/types/test.ts new file mode 100644 index 00000000..b0574915 --- /dev/null +++ b/is-symbol-array/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isSymbolArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isSymbolArray( [ Symbol( 'abc' ) ] ); // $ExpectType boolean + isSymbolArray( [ 2 ] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSymbolArray(); // $ExpectError + isSymbolArray( [ Symbol( 'abc' ) ], 10.23 ); // $ExpectError +} + +// Attached to main export is a `primitives` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isSymbolArray.primitives( [ Object( Symbol( 'abc' ) ) ] ); // $ExpectType boolean + isSymbolArray.primitives( [ Symbol( 'abc' ) ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `primitives` method is provided an unsupported number of arguments... +{ + isSymbolArray.primitives(); // $ExpectError + isSymbolArray.primitives( [ Symbol( 'abc' ) ], 123 ); // $ExpectError +} + + +// Attached to main export is an `objects` method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isSymbolArray.objects( [ Object( Symbol( 'abc' ) ) ] ); // $ExpectType boolean + isSymbolArray.objects( [ Symbol( 'abc' ) ] ); // $ExpectType boolean +} + +// The compiler throws an error if the `objects` method is provided an unsupported number of arguments... +{ + isSymbolArray.objects(); // $ExpectError + isSymbolArray.objects( [ Symbol( 'abc' ) ], 123 ); // $ExpectError +} diff --git a/is-symbol-array/examples/index.js b/is-symbol-array/examples/index.js new file mode 100644 index 00000000..25926016 --- /dev/null +++ b/is-symbol-array/examples/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var isSymbolArray = require( './../lib' ); + +var hasSymbols = hasSymbolSupport(); +var bool; + +if ( hasSymbols ) { + bool = isSymbolArray( [ Symbol( 'beep' ), Symbol( 'boop' ) ] ); + console.log( bool ); + // => true + + bool = isSymbolArray( [ Symbol( 'beep' ), 'boop' ] ); + console.log( bool ); + // => false + + bool = isSymbolArray( Symbol( 'beep' ) ); + console.log( bool ); + // => false +} else { + console.log( 'Environment does not support symbols.' ); +} +bool = isSymbolArray( [ 'beep', 'boop' ] ); +console.log( bool ); +// => false + +bool = isSymbolArray( [] ); +console.log( bool ); +// => false + +bool = isSymbolArray( 'abc' ); +console.log( bool ); +// => false + +bool = isSymbolArray( null ); +console.log( bool ); +// => false diff --git a/is-symbol-array/lib/index.js b/is-symbol-array/lib/index.js new file mode 100644 index 00000000..a0702aff --- /dev/null +++ b/is-symbol-array/lib/index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only symbols. +* +* @module @stdlib/assert/is-symbol-array +* +* @example +* var isSymbolArray = require( '@stdlib/assert/is-symbol-array' ); +* +* var bool = isSymbolArray( [ Symbol( 'abc' ), Symbol( 'def' ) ] ); +* // returns true +* +* bool = isSymbolArray( [ Symbol( 'abc' ), 'def' ] ); +* // returns false +* +* @example +* var isSymbolArray = require( '@stdlib/assert/is-symbol-array' ).primitives; +* +* var bool = isSymbolArray( [ Symbol( 'abc' ), Symbol( 'def' ) ] ); +* // returns true +* +* bool = isSymbolArray( [ Symbol( 'abc' ), Object( Symbol( 'def' ) ) ] ); +* // returns false +* +* @example +* var isSymbolArray = require( '@stdlib/assert/is-symbol-array' ).objects; +* +* var bool = isSymbolArray( [ Object( Symbol( 'abc' ) ), Object( Symbol( 'def' ) ) ] ); +* // returns true +* +* bool = isSymbolArray( [ Symbol( 'abc' ), Symbol( 'def' ) ] ); +* // returns false +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); +var isSymbol = require( '@stdlib/assert/is-symbol' ); + + +// MAIN // + +var isSymbolArray = arrayfun( isSymbol ); +setReadOnly( isSymbolArray, 'primitives', arrayfun( isSymbol.isPrimitive ) ); +setReadOnly( isSymbolArray, 'objects', arrayfun( isSymbol.isObject ) ); + + +// EXPORTS // + +module.exports = isSymbolArray; diff --git a/is-symbol-array/package.json b/is-symbol-array/package.json new file mode 100644 index 00000000..9fce5f61 --- /dev/null +++ b/is-symbol-array/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-symbol-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only symbols.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "function", + "array", + "array-like", + "symbol", + "sym", + "is", + "issymbol", + "isarray", + "isarraylike", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-symbol-array/test/test.js b/is-symbol-array/test/test.js new file mode 100644 index 00000000..5695750a --- /dev/null +++ b/is-symbol-array/test/test.js @@ -0,0 +1,120 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var isSymbolArray = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSymbolArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function tests for an array-like object containing only symbols (ES2015+ environments)', opts, function test( t ) { + var arr; + + arr = [ Symbol( 'a' ), Symbol( 'b' ) ]; + t.strictEqual( isSymbolArray( arr ), true, 'returns true' ); + + arr = [ Object( Symbol( 'a' ) ), Object( Symbol( 'b' ) ) ]; + t.strictEqual( isSymbolArray( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': Symbol( 'a' ), + '1': Symbol( 'b' ) + }; + t.strictEqual( isSymbolArray( arr ), true, 'returns true' ); + + arr = [ 'a', 5, null ]; + t.strictEqual( isSymbolArray( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if not provided an array-like object containing only symbols (all environments)', function test( t ) { + var arr = [ 'a', 5, null ]; + t.strictEqual( isSymbolArray( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only `symbol` primitives (ES2015+ environments)', opts, function test( t ) { + var arr; + + arr = [ Symbol( 'a' ), Symbol( 'b' ) ]; + t.strictEqual( isSymbolArray.primitives( arr ), true, 'returns true' ); + + arr = { + 'length': 2, + '0': Symbol( 'a' ), + '1': Symbol( 'b' ) + }; + t.strictEqual( isSymbolArray.primitives( arr ), true, 'returns true' ); + + arr = [ Object( Symbol( 'a' ) ), Object( Symbol( 'b' ) ) ]; + t.strictEqual( isSymbolArray.primitives( arr ), false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if not provided an array-like object containing only `symbol` primitives (all environments; primitives)', function test( t ) { + var arr = [ 'a', 5, null ]; + t.strictEqual( isSymbolArray.primitives( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function provides a method to test for an array-like object containing only `Symbol` objects (ES2015+ environments)', opts, function test( t ) { + var arr; + + arr = [ Symbol( 'a' ), Symbol( 'b' ) ]; + t.strictEqual( isSymbolArray.objects( arr ), false, 'returns false' ); + + arr = { + 'length': 2, + '0': Object( Symbol( 'a' ) ), + '1': Object( Symbol( 'b' ) ) + }; + t.strictEqual( isSymbolArray.objects( arr ), true, 'returns true' ); + + arr = [ Object( Symbol( 'a' ) ), Object( Symbol( 'b' ) ) ]; + t.strictEqual( isSymbolArray.objects( arr ), true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if not provided an array-like object containing only `Symbol` objects (all environments; objects)', function test( t ) { + var arr = [ {}, {}, null ]; + t.strictEqual( isSymbolArray.objects( arr ), false, 'returns false' ); + t.end(); +}); diff --git a/is-symbol/README.md b/is-symbol/README.md new file mode 100644 index 00000000..8d776dbe --- /dev/null +++ b/is-symbol/README.md @@ -0,0 +1,131 @@ + + +# isSymbol + +> Test if a value is a [symbol][mdn-symbol]. + +
+ +## Usage + +```javascript +var isSymbol = require( '@stdlib/assert/is-symbol' ); +``` + +#### isSymbol( value ) + +Tests if a value is a [symbol][mdn-symbol]. + +```javascript +var Symbol = require( '@stdlib/symbol/ctor' ); + +var bool = isSymbol( Symbol( 'beep' ) ); +// returns true + +bool = isSymbol( Object( Symbol( 'beep' ) ) ); +// returns true +``` + +#### isSymbol.isPrimitive( value ) + +Tests if a `value` is a primitive [symbol][mdn-symbol]. + +```javascript +var Symbol = require( '@stdlib/symbol/ctor' ); + +var bool = isSymbol.isPrimitive( Symbol( 'beep' ) ); +// returns true + +bool = isSymbol.isPrimitive( Object( Symbol( 'boop' ) ) ); +// returns false +``` + +#### isSymbol.isObject( value ) + +Tests if a `value` is a [`Symbol`][mdn-symbol] object. + +```javascript +var Symbol = require( '@stdlib/symbol/ctor' ); + +var bool = isSymbol.isObject( Symbol( 'beep' ) ); +// returns false + +bool = isSymbol.isObject( Object( Symbol( 'boop' ) ) ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var isSymbol = require( '@stdlib/assert/is-symbol' ); + +var bool; +if ( hasSymbolSupport() ) { + bool = isSymbol( Symbol( 'beep' ) ); + // returns true +} else { + console.log( 'Environment does not support symbols.' ); +} +bool = isSymbol( 'beep' ); +// returns false + +bool = isSymbol( {} ); +// returns false + +bool = isSymbol( [] ); +// returns false + +bool = isSymbol( null ); +// returns false + +bool = isSymbol( void 0 ); +// returns false + +bool = isSymbol( true ); +// returns false + +bool = isSymbol( function foo() {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-symbol/benchmark/benchmark.js b/is-symbol/benchmark/benchmark.js new file mode 100644 index 00000000..009df18e --- /dev/null +++ b/is-symbol/benchmark/benchmark.js @@ -0,0 +1,384 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-empty-function, stdlib/require-globals */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var pkg = require( './../package.json' ).name; +var isSymbol = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymbol( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new String( 'beep' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymbol( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymbol.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new String( 'beep' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymbol.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymbol.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new String( 'beep' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymbol.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives,symbols', opts, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + Symbol( 'beep' ), + Symbol( 'boop' ), + Symbol( 'foo' ), + Symbol( 'bar' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymbol( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects,symbols', opts, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + Object( Symbol( 'beep' ) ), + Object( Symbol( 'boop' ) ), + Object( Symbol( 'foo' ) ), + Object( Symbol( 'bar' ) ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymbol( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives,symbols:isPrimitive', opts, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + Symbol( 'beep' ), + Symbol( 'boop' ), + Symbol( 'foo' ), + Symbol( 'bar' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymbol.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects,symbols:isPrimitive', opts, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + Object( Symbol( 'beep' ) ), + Object( Symbol( 'boop' ) ), + Object( Symbol( 'foo' ) ), + Object( Symbol( 'bar' ) ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymbol.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::primitives,symbols:isObject', opts, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + Symbol( 'beep' ), + Symbol( 'boop' ), + Symbol( 'foo' ), + Symbol( 'bar' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymbol.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); + +bench( pkg+'::objects,symbols:isObject', opts, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + Object( Symbol( 'beep' ) ), + Object( Symbol( 'boop' ) ), + Object( Symbol( 'foo' ) ), + Object( Symbol( 'bar' ) ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymbol.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-symbol/docs/repl.txt b/is-symbol/docs/repl.txt new file mode 100644 index 00000000..d690205d --- /dev/null +++ b/is-symbol/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( value ) + Tests if a value is a symbol. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a symbol. + + Examples + -------- + > var bool = {{alias}}( {{alias:@stdlib/symbol/ctor}}( 'beep' ) ) + true + > bool = {{alias}}( Object( {{alias:@stdlib/symbol/ctor}}( 'beep' ) ) ) + true + > bool = {{alias}}( {} ) + false + > bool = {{alias}}( null ) + false + > bool = {{alias}}( true ) + false + + See Also + -------- + diff --git a/is-symbol/docs/types/index.d.ts b/is-symbol/docs/types/index.d.ts new file mode 100644 index 00000000..abf3dfaf --- /dev/null +++ b/is-symbol/docs/types/index.d.ts @@ -0,0 +1,145 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface defining `isSymbol` with methods for testing for primitives and objects, respectively. +*/ +interface IsSymbol { + /** + * Tests if a value is a symbol. + * + * @param value - value to test + * @returns boolean indicating whether value is a symbol + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * + * var bool = isSymbol( Symbol( 'beep' ) ); + * // returns true + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * + * var bool = isSymbol( Object( Symbol( 'beep' ) ) ); + * // returns true + * + * @example + * var bool = isSymbol( {} ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a symbol primitive. + * + * @param value - value to test + * @returns boolean indicating if a value is a symbol primitive + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * + * var bool = isSymbol.isPrimitive( Symbol( 'beep' ) ); + * // returns true + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * + * var bool = isSymbol.isPrimitive( Object( Symbol( 'beep' ) ) ); + * // returns false + * + * @example + * var bool = isSymbol.isPrimitive( {} ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a symbol object. + * + * @param value - value to test + * @returns boolean indicating if a value is a symbol object + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * + * var bool = isSymbol.isObject( Symbol( 'beep' ) ); + * // returns false + * + * @example + * var Symbol = require( `@stdlib/symbol/ctor` ); + * + * var bool = isSymbol.isObject( Object( Symbol( 'beep' ) ) ); + * // returns true + * + * @example + * var bool = isSymbol.isObject( {} ); + * // returns false + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a symbol. +* +* @param value - value to test +* @returns boolean indicating whether value is a symbol +* +* @example +* var Symbol = require( `@stdlib/symbol/ctor` ); +* +* var bool = isSymbol.isPrimitive( Symbol( 'beep' ) ); +* // returns true +* +* @example +* var Symbol = require( `@stdlib/symbol/ctor` ); +* +* var bool = isSymbol.isPrimitive( Object( Symbol( 'beep' ) ) ); +* // returns false +* +* @example +* var Symbol = require( `@stdlib/symbol/ctor` ); +* +* var bool = isSymbol.isPrimitive( Symbol( 'beep' ) ); +* // returns true +* +* @example +* var Symbol = require( `@stdlib/symbol/ctor` ); +* +* var bool = isSymbol.isPrimitive( Object( Symbol( 'beep' ) ) ); +* // returns false +* +* @example +* var Symbol = require( `@stdlib/symbol/ctor` ); +* +* var bool = isSymbol.isObject( Symbol( 'beep' ) ); +* // returns false +* +* @example +* var Symbol = require( `@stdlib/symbol/ctor` ); +* +* var bool = isSymbol.isObject( Object( Symbol( 'beep' ) ) ); +* // returns true +*/ +declare var isSymbol: IsSymbol; + + +// EXPORTS // + +export = isSymbol; diff --git a/is-symbol/docs/types/test.ts b/is-symbol/docs/types/test.ts new file mode 100644 index 00000000..6f9480ec --- /dev/null +++ b/is-symbol/docs/types/test.ts @@ -0,0 +1,58 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isSymbol = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isSymbol( Symbol( 'beep' ) ); // $ExpectType boolean + isSymbol( 'abc' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSymbol(); // $ExpectError + isSymbol( Symbol( 'beep' ), 123 ); // $ExpectError +} + +// Attached to main export is an `isPrimitive` method which returns a boolean... +{ + isSymbol.isPrimitive( Symbol( 'beep' ) ); // $ExpectType boolean + isSymbol.isPrimitive( Object( Symbol( 'beep' ) ) ); // $ExpectType boolean +} + +// The compiler throws an error if the `isPrimitive` method is provided an unsupported number of arguments... +{ + isSymbol.isPrimitive(); // $ExpectError + isSymbol.isPrimitive( Symbol( 'beep' ), 123 ); // $ExpectError +} + +// Attached to main export is an `isObject` method which returns a boolean... +{ + isSymbol.isObject( Symbol( 'beep' ) ); // $ExpectType boolean + isSymbol.isObject( Object( Symbol( 'beep' ) ) ); // $ExpectType boolean +} + +// The compiler throws an error if the `isObject` method is provided an unsupported number of arguments... +{ + isSymbol.isObject(); // $ExpectError + isSymbol.isObject( Symbol( 'beep' ), 123 ); // $ExpectError +} diff --git a/is-symbol/examples/index.js b/is-symbol/examples/index.js new file mode 100644 index 00000000..a8042a89 --- /dev/null +++ b/is-symbol/examples/index.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-restricted-syntax, no-empty-function */ + +'use strict'; + +var hasSymbolSupport = require( '@stdlib/assert/has-symbol-support' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var isSymbol = require( './../lib' ); + +var bool; +if ( hasSymbolSupport() ) { + bool = isSymbol( Symbol( 'beep' ) ); + console.log( bool ); + // => true +} else { + console.log( 'Environment does not support symbols.' ); +} +bool = isSymbol( 'beep' ); +console.log( bool ); +// => false + +bool = isSymbol( {} ); +console.log( bool ); +// => false + +bool = isSymbol( [] ); +console.log( bool ); +// => false + +bool = isSymbol( null ); +console.log( bool ); +// => false + +bool = isSymbol( void 0 ); +console.log( bool ); +// => false + +bool = isSymbol( true ); +console.log( bool ); +// => false + +bool = isSymbol( function foo() {} ); +console.log( bool ); +// => false diff --git a/is-symbol/lib/generic.js b/is-symbol/lib/generic.js new file mode 100644 index 00000000..95e7290d --- /dev/null +++ b/is-symbol/lib/generic.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a symbol. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a symbol +*/ +function isSymbol( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isSymbol; diff --git a/is-symbol/lib/index.js b/is-symbol/lib/index.js new file mode 100644 index 00000000..9b74458d --- /dev/null +++ b/is-symbol/lib/index.js @@ -0,0 +1,85 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a symbol. +* +* @module @stdlib/assert/is-symbol +* +* @example +* var Symbol = require( '@stdlib/symbol/ctor' ); +* var isSymbol = require( '@stdlib/assert/is-symbol' ); +* +* var bool = isSymbol( Symbol( 'beep' ) ); +* // returns true +* +* bool = isSymbol( Object( Symbol( 'beep' ) ) ); +* // returns true +* +* bool = isSymbol( {} ); +* // returns false +* +* @example +* var Symbol = require( '@stdlib/symbol/ctor' ); +* var isSymbol = require( '@stdlib/assert/is-symbol' ).isPrimitive; +* +* var bool = isSymbol( Symbol( 'beep' ) ); +* // returns true +* +* bool = isSymbol( Object( Symbol( 'beep' ) ) ); +* // returns false +* +* bool = isSymbol( {} ); +* // returns false +* +* @example +* var Symbol = require( '@stdlib/symbol/ctor' ); +* var isSymbolObject = require( '@stdlib/assert/is-symbol' ).isObject; +* +* var bool = isSymbolObject( Symbol( 'beep' ) ); +* // returns false +* +* bool = isSymbolObject( Object( Symbol( 'beep' ) ) ); +* // returns true +* +* bool = isSymbolObject( {} ); +* // returns false +*/ + +// MODULES // + +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var main = require( './main.js' ); +var polyfill = require( './polyfill.js' ); + + +// MAIN // + +var isSymbol; +if ( hasSymbols() ) { + isSymbol = main; +} else { + isSymbol = polyfill; +} + + +// EXPORTS // + +module.exports = isSymbol; diff --git a/is-symbol/lib/main.js b/is-symbol/lib/main.js new file mode 100644 index 00000000..1eed08c2 --- /dev/null +++ b/is-symbol/lib/main.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isSymbol = require( './generic.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isSymbol, 'isPrimitive', isPrimitive ); +setReadOnly( isSymbol, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isSymbol; diff --git a/is-symbol/lib/object.js b/is-symbol/lib/object.js new file mode 100644 index 00000000..64ebca70 --- /dev/null +++ b/is-symbol/lib/object.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); +var test = require( './try2tostring.js' ); + + +// MAIN // + +/** +* Tests if a value is a symbol object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a symbol object +*/ +function isSymbol( value ) { + return ( + typeof value === 'object' && + nativeClass( value ) === '[object Symbol]' && + test( value ) + ); +} + + +// EXPORTS // + +module.exports = isSymbol; diff --git a/is-symbol/lib/polyfill.js b/is-symbol/lib/polyfill.js new file mode 100644 index 00000000..74c32f8f --- /dev/null +++ b/is-symbol/lib/polyfill.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var constantFunction = require( '@stdlib/utils/constant-function' ); + + +// MAIN // + +var isSymbol = constantFunction( false ); +var isPrimitive = constantFunction( false ); +var isObject = constantFunction( false ); + +setReadOnly( isSymbol, 'isPrimitive', isPrimitive ); +setReadOnly( isSymbol, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isSymbol; diff --git a/is-symbol/lib/primitive.js b/is-symbol/lib/primitive.js new file mode 100644 index 00000000..35673768 --- /dev/null +++ b/is-symbol/lib/primitive.js @@ -0,0 +1,34 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Tests if a value is a symbol primitive. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a symbol primitive +*/ +function isSymbol( value ) { + return ( typeof value === 'symbol' ); +} + + +// EXPORTS // + +module.exports = isSymbol; diff --git a/is-symbol/lib/tostring.js b/is-symbol/lib/tostring.js new file mode 100644 index 00000000..443250ed --- /dev/null +++ b/is-symbol/lib/tostring.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/* +* Note: we wrap `toString` to allow loading this file in non-ES2015 environments. +*/ + +// MAIN // + +/** +* Attempts to serialize a value as a symbol. +* +* @private +* @param {*} value - value to serialize +* @returns {string} serialized value +*/ +function toStr( value ) { + // Non-generic: + return Symbol.prototype.toString.call( value ); +} + + +// EXPORTS // + +module.exports = toStr; diff --git a/is-symbol/lib/try2tostring.js b/is-symbol/lib/try2tostring.js new file mode 100644 index 00000000..fea30c82 --- /dev/null +++ b/is-symbol/lib/try2tostring.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var toStr = require( './tostring.js' ); + + +// VARIABLES // + +var RE = /^Symbol\(.*\)$/; + + +// MAIN // + +/** +* Attempts to call a `Symbol` method. +* +* @private +* @param {*} value - value to test +* @returns {boolean} boolean indicating if able to call a `Symbol` method +*/ +function test( value ) { + try { + return ( + // Objects created via `Object.create( null )` have no `valueOf()` method: + typeof value.valueOf() === 'symbol' && + + // Test output of a non-generic method: + RE.test( toStr( value ) ) + ); + } catch ( err ) { // eslint-disable-line no-unused-vars + return false; + } +} + + +// EXPORTS // + +module.exports = test; diff --git a/is-symbol/package.json b/is-symbol/package.json new file mode 100644 index 00000000..8d408e27 --- /dev/null +++ b/is-symbol/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-symbol", + "version": "0.0.0", + "description": "Test if a value is a symbol.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "symbol", + "sym", + "is", + "issymbol", + "es2015", + "es6", + "type", + "check", + "primitive", + "object", + "valid", + "validate", + "test", + "isvalid" + ] +} diff --git a/is-symbol/test/test.generic.js b/is-symbol/test/test.generic.js new file mode 100644 index 00000000..7bf4e72e --- /dev/null +++ b/is-symbol/test/test.generic.js @@ -0,0 +1,71 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var isSymbol = require( './../lib/generic.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSymbol, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a symbol', opts, function test( t ) { + t.strictEqual( isSymbol( Symbol( 'a' ) ), true, 'returns true' ); + t.strictEqual( isSymbol( Object( Symbol( 'a' ) ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a symbol', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSymbol( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-symbol/test/test.js b/is-symbol/test/test.js new file mode 100644 index 00000000..abd0e12b --- /dev/null +++ b/is-symbol/test/test.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var isSymbol = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSymbol, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive symbol', function test( t ) { + t.strictEqual( typeof isSymbol.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a Symbol object', function test( t ) { + t.strictEqual( typeof isSymbol.isObject, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'if an environment does not support symbols, the main export is a polyfill which always returns `false`', function test( t ) { + var isSymbol; + var values; + var i; + + isSymbol = proxyquire( './../lib', { + '@stdlib/assert/has-symbol-support': hasSupport + }); + + t.strictEqual( isSymbol, require( './../lib/polyfill.js' ), 'exports a polyfill' ); + + values = [ + '5', + 5, + NaN, + null, + void 0, + false, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSymbol( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + t.strictEqual( isSymbol.isObject( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + t.strictEqual( isSymbol.isPrimitive( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); + + function hasSupport() { + return false; + } +}); + +tape( 'if an environment does support symbols, the main export is not the polyfill', function test( t ) { + var isSymbol = proxyquire( './../lib', { + '@stdlib/assert/has-symbol-support': hasSupport, + './main.js': isSym + }); + + t.strictEqual( isSymbol, isSym, 'exports expected function' ); + t.end(); + + function hasSupport() { + return true; + } + + function isSym() { + return false; + } +}); diff --git a/is-symbol/test/test.main.js b/is-symbol/test/test.main.js new file mode 100644 index 00000000..4faaa5b8 --- /dev/null +++ b/is-symbol/test/test.main.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isSymbol = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSymbol, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive symbol', function test( t ) { + t.strictEqual( typeof isSymbol.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a Symbol object', function test( t ) { + t.strictEqual( typeof isSymbol.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-symbol/test/test.object.js b/is-symbol/test/test.object.js new file mode 100644 index 00000000..6c51a006 --- /dev/null +++ b/is-symbol/test/test.object.js @@ -0,0 +1,108 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var isSymbol = require( './../lib/object.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSymbol, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a Symbol object', opts, function test( t ) { + t.strictEqual( isSymbol( Object( Symbol( 'a' ) ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive symbol', opts, function test( t ) { + t.strictEqual( isSymbol( Symbol( 'a' ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a symbol', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSymbol( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a symbol, even if `toStringTag` is overridden (in environments supporting `toStringTag`)', function test( t ) { + var isSymbol; + var values; + var i; + + isSymbol = proxyquire( './../lib/object.js', { + '@stdlib/utils/native-class': nativeClass + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSymbol( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); + + function nativeClass() { + return '[object Symbol]'; + } +}); diff --git a/is-symbol/test/test.polyfill.js b/is-symbol/test/test.polyfill.js new file mode 100644 index 00000000..5479ba3f --- /dev/null +++ b/is-symbol/test/test.polyfill.js @@ -0,0 +1,112 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isSymbol = require( './../lib/polyfill.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSymbol, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a primitive symbol', function test( t ) { + t.strictEqual( typeof isSymbol.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test for a Symbol object', function test( t ) { + t.strictEqual( typeof isSymbol.isObject, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the main export always returns `false`', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + void 0, + false, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSymbol( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the `isPrimitive` method always returns `false`', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + void 0, + false, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSymbol.isPrimitive( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the `isObject` method always returns `false`', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + void 0, + false, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSymbol.isObject( values[ i ] ), false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); diff --git a/is-symbol/test/test.primitive.js b/is-symbol/test/test.primitive.js new file mode 100644 index 00000000..214485f3 --- /dev/null +++ b/is-symbol/test/test.primitive.js @@ -0,0 +1,75 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var isSymbol = require( './../lib/primitive.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSymbol, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive symbol', opts, function test( t ) { + t.strictEqual( isSymbol( Symbol( 'a' ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a Symbol object', opts, function test( t ) { + t.strictEqual( isSymbol( Object( Symbol( 'a' ) ) ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a symbol', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSymbol( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-symbol/test/test.tostring.js b/is-symbol/test/test.tostring.js new file mode 100644 index 00000000..a9d59633 --- /dev/null +++ b/is-symbol/test/test.tostring.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var toStr = require( './../lib/tostring.js' ); + + +// VARIABLES // + +var opts1 = { + 'skip': !hasSymbols() +}; +var opts2 = { + 'skip': hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof toStr, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if an environment supports symbols, the function returns the result of `Symbol.prototype.toString`', opts1, function test( t ) { + var s = Symbol( 'beep' ); + t.strictEqual( toStr( s ), s.toString(), 'returns expected result' ); + t.strictEqual( toStr( s ), Symbol.prototype.toString.call( s ), 'returns expected result' ); + t.end(); +}); + +tape( 'if an environment does not support symbols, the function throws an error', opts2, function test( t ) { + t.throws( foo, Error, 'throws an error' ); + t.end(); + + function foo() { + toStr( 'foo' ); + } +}); diff --git a/is-symbol/test/test.try2tostring.js b/is-symbol/test/test.try2tostring.js new file mode 100644 index 00000000..705e681b --- /dev/null +++ b/is-symbol/test/test.try2tostring.js @@ -0,0 +1,113 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var Symbol = require( '@stdlib/symbol/ctor' ); +var hasSymbols = require( '@stdlib/assert/has-symbol-support' ); +var tryToString = require( './../lib/try2tostring.js' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasSymbols() +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof tryToString, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a symbol', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( tryToString( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if provided an empty object without a prototype', function test( t ) { + var o = Object.create( null ); + t.strictEqual( tryToString( o ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided an object whose primitive value is a symbol', opts, function test( t ) { + var o = Object.create( null ); + o.valueOf = valueOf; + + t.strictEqual( tryToString( o ), false, 'returns false' ); + t.end(); + + function valueOf() { + return Symbol( 'beep' ); + } +}); + +tape( 'the function returns `true` if able to successfully call a Symbol method', opts, function test( t ) { + var tryToString; + var o; + + tryToString = proxyquire( './../lib/try2tostring.js', { + './tostring.js': toStr + }); + + o = Object.create( null ); + o.valueOf = valueOf; + + t.strictEqual( tryToString( o ), true, 'returns true' ); + t.end(); + + function valueOf() { + return Symbol( 'beep' ); + } + + function toStr() { + return 'Symbol(beep)'; + } +}); + +tape( 'if an environment supports symbols, the function returns `true`', opts, function test( t ) { + t.strictEqual( tryToString( Symbol( 'beep' ) ), true, 'returns true' ); + t.strictEqual( tryToString( Object( Symbol( 'beep' ) ) ), true, 'returns true' ); + t.end(); +}); diff --git a/is-symmetric-matrix/README.md b/is-symmetric-matrix/README.md new file mode 100644 index 00000000..2bf8b2a0 --- /dev/null +++ b/is-symmetric-matrix/README.md @@ -0,0 +1,83 @@ + + +# isSymmetricMatrix + +> Test if a value is a [symmetric matrix][symmetric-matrix]. + +
+ +## Usage + +```javascript +var isSymmetricMatrix = require( '@stdlib/assert/is-symmetric-matrix' ); +``` + +#### isSymmetricMatrix( value ) + +Tests if a value is a [symmetric matrix][symmetric-matrix]. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'generic', [ 0, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var bool = isSymmetricMatrix( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isSymmetricMatrix = require( '@stdlib/assert/is-symmetric-matrix' ); + +var arr = ndarray( 'generic', [ 0, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +var out = isSymmetricMatrix( arr ); +// returns true + +out = isSymmetricMatrix( [ 1, 2, 3, 4 ] ); +// returns false + +out = isSymmetricMatrix( {} ); +// returns false + +out = isSymmetricMatrix( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-symmetric-matrix/benchmark/benchmark.js b/is-symmetric-matrix/benchmark/benchmark.js new file mode 100644 index 00000000..22e5a445 --- /dev/null +++ b/is-symmetric-matrix/benchmark/benchmark.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var isSymmetricMatrix = require( './../lib' ); + + +// MAIN // + +bench( pkg + '::true', function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = [ 0, 0, 0, 0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + + values = [ + arr, + arr, + arr, + arr, + arr + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymmetricMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg + '::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSymmetricMatrix( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-symmetric-matrix/benchmark/benchmark.size.js b/is-symmetric-matrix/benchmark/benchmark.size.js new file mode 100644 index 00000000..1843d565 --- /dev/null +++ b/is-symmetric-matrix/benchmark/benchmark.size.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isSymmetricMatrix = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} size - matrix size +* @param {PositiveInteger} N - matrix order +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, size, N ) { + var strides; + var buffer; + var shape; + var x; + + buffer = new Float64Array( size ); + shape = [ N, N ]; + strides = [ N, 1 ]; + x = ndarray( 'float64', buffer, shape, strides, 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire matrix before finding a failing value. + buffer[ size-2 ] = i + 3.14; + bool = fcn( x ); + if ( bool !== false ) { + b.fail( 'should return false' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var sz; + var N; + var f; + var i; + + min = 4; // minimum number of elements + + for ( i = 0; i <= 16; i += 2 ) { + sz = min * pow( 2, i ); + N = sqrt( sz ); + f = createBenchmark( isSymmetricMatrix, sz, N ); + bench( pkg+':size='+sz+',dims='+N+'x'+N, f ); + } +} + +main(); diff --git a/is-symmetric-matrix/docs/repl.txt b/is-symmetric-matrix/docs/repl.txt new file mode 100644 index 00000000..91cd601a --- /dev/null +++ b/is-symmetric-matrix/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( value ) + Tests if a value is a square matrix which equals its transpose. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a symmetric matrix. + + Examples + -------- + > var buf = [ 0, 1, 1, 2 ]; + > var sh = [ 2, 2 ]; + > var st = [ 2, 1 ]; + > var M = {{alias:@stdlib/ndarray/ctor}}( 'generic', buf, sh, st, 0, 'row-major' ); + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-symmetric-matrix/docs/types/index.d.ts b/is-symmetric-matrix/docs/types/index.d.ts new file mode 100644 index 00000000..f3056de4 --- /dev/null +++ b/is-symmetric-matrix/docs/types/index.d.ts @@ -0,0 +1,47 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a symmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} = M_{ji}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param v - value to test +* @returns boolean indicating if a value is a symmetric matrix +* +* @example +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'generic', [ 0, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSymmetricMatrix( arr ); +* // returns true +* +* bool = isSymmetricMatrix( [] ); +* // returns false +*/ +declare function isSymmetricMatrix( v: any ): boolean; + + +// EXPORTS // + +export = isSymmetricMatrix; diff --git a/is-symmetric-matrix/docs/types/test.ts b/is-symmetric-matrix/docs/types/test.ts new file mode 100644 index 00000000..3e6e2cbe --- /dev/null +++ b/is-symmetric-matrix/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* tslint:disable:no-invalid-this */ + +import isSymmetricMatrix = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const matrix = { + 'data': [ 0, 1, 1, 2 ], + 'ndims': 2, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': function get( i: number, j: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + return this.data[ idx ]; + }, + 'set': function set( i: number, j: number, v: number ): number { + const idx = ( this.strides[ 0 ] * i ) + ( this.strides[ 1 ] * j ); + this.data[ idx ] = v; + return v; + } + }; + isSymmetricMatrix( matrix ); // $ExpectType boolean + isSymmetricMatrix( [] ); // $ExpectType boolean + isSymmetricMatrix( false ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSymmetricMatrix(); // $ExpectError +} diff --git a/is-symmetric-matrix/examples/index.js b/is-symmetric-matrix/examples/index.js new file mode 100644 index 00000000..9bd6550f --- /dev/null +++ b/is-symmetric-matrix/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isSymmetricMatrix = require( './../lib' ); + +var arr = ndarray( 'generic', [ 0, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +console.log( isSymmetricMatrix( arr ) ); +// => true + +console.log( isSymmetricMatrix( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isSymmetricMatrix( {} ) ); +// => false + +console.log( isSymmetricMatrix( null ) ); +// => false diff --git a/is-symmetric-matrix/lib/index.js b/is-symmetric-matrix/lib/index.js new file mode 100644 index 00000000..ec30cd99 --- /dev/null +++ b/is-symmetric-matrix/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a symmetric matrix. +* +* @module @stdlib/assert/is-symmetric-matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isSymmetricMatrix = require( '@stdlib/assert/is-symmetric-matrix' ); +* +* var arr = ndarray( 'generic', [ 0, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSymmetricMatrix( arr ); +* // returns true +* +* bool = isSymmetricMatrix( [] ); +* // returns false +*/ + +// MODULES // + +var isSymmetricMatrix = require( './main.js' ); + + +// EXPORTS // + +module.exports = isSymmetricMatrix; diff --git a/is-symmetric-matrix/lib/main.js b/is-symmetric-matrix/lib/main.js new file mode 100644 index 00000000..4ab61c5d --- /dev/null +++ b/is-symmetric-matrix/lib/main.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isSquareMatrix = require( '@stdlib/assert/is-square-matrix' ); + + +// MAIN // + +/** +* Tests if a value is a symmetric matrix. +* +* ## Notes +* +* - The implementation must rely on manually checking that \\(M_{ij} = M_{ji}\\), and, while element access is deterministic, no way exists to prevent cache misses outside of reordering the underlying matrix elements, thus incurring a larger performance penalty than just "jumping around" in a single pass. +* - Worst case scenario: O(N^2). +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a symmetric matrix +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var arr = ndarray( 'generic', [ 0, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); +* var bool = isSymmetricMatrix( arr ); +* // returns true +* +* bool = isSymmetricMatrix( [] ); +* // returns false +*/ +function isSymmetricMatrix( v ) { + var M; + var i; + var j; + if ( !isSquareMatrix( v ) ) { + return false; + } + M = v.shape[ 0 ]; + for ( i = 0; i < M; i++ ) { + for ( j = 0; j < i; j++ ) { + if ( v.get( i, j ) !== v.get( j, i ) ) { + return false; + } + } + } + return true; +} + + +// EXPORTS // + +module.exports = isSymmetricMatrix; diff --git a/is-symmetric-matrix/package.json b/is-symmetric-matrix/package.json new file mode 100644 index 00000000..11b27b65 --- /dev/null +++ b/is-symmetric-matrix/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-symmetric-matrix", + "version": "0.0.0", + "description": "Test if a value is a symmetric matrix.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "matrix", + "ismatrix", + "2d", + "square", + "symmetric", + "symm", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-symmetric-matrix/test/test.js b/is-symmetric-matrix/test/test.js new file mode 100644 index 00000000..120232ea --- /dev/null +++ b/is-symmetric-matrix/test/test.js @@ -0,0 +1,146 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var isSymmetricMatrix = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isSymmetricMatrix, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a symmetric matrix', function test( t ) { + var arr = ndarray( 'generic', [ 0, 1, 1, 2 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isSymmetricMatrix( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 2-dimensional ndarray-like object having equal dimensions and which equals its transpose', function test( t ) { + var arr = { + 'data': [ 0, 1, 1, 2 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isSymmetricMatrix( arr ), true, 'returns true' ); + t.end(); + + function get( i, j ) { + return arr.data[ (2*i) + j ]; + } +}); + +tape( 'the function returns `false` if not provided a square matrix', function test( t ) { + var arr = ndarray( 'generic', [ 0, 0, 0, 0, 0, 0 ], [ 3, 2 ], [ 2, 1 ], 0, 'row-major' ); + t.equal( isSymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a symmetric matrix', function test( t ) { + var arr = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + t.equal( isSymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray-like object not having equal dimensions', function test( t ) { + var arr = { + 'data': [ 0, 0, 0, 0, 0, 0 ], + 'shape': [ 3, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 6, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isSymmetricMatrix( arr ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a 2-dimensional ndarray-like object which does not equal its transpose', function test( t ) { + var arr = { + 'data': [ 1, 2, 3, 4 ], + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 2, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': get, + 'set': noop + }; + + t.equal( isSymmetricMatrix( arr ), false, 'returns false' ); + t.end(); + + function get( i, j ) { + return arr.data[ (2*i) + j ]; + } +}); + +tape( 'the function returns `false` if not provided a 2-dimensional ndarray-like object', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 0, 0, 0 ], [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + values = [ + arr, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isSymmetricMatrix( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-syntax-error/README.md b/is-syntax-error/README.md new file mode 100644 index 00000000..c33fc26f --- /dev/null +++ b/is-syntax-error/README.md @@ -0,0 +1,129 @@ + + +# isSyntaxError + +> Test if a value is a [SyntaxError][mdn-syntax-error] object. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var isSyntaxError = require( '@stdlib/assert/is-syntax-error' ); +``` + +#### isSyntaxError( value ) + +Tests if a `value` is a [`SyntaxError`][mdn-syntax-error] object. + +```javascript +var bool = isSyntaxError( new SyntaxError( 'beep' ) ); +// returns true +``` + +
+ + + + + +
+ +## Notes + +- This function should **not** be considered robust. While the function should **always** return `true` if provided a [`SyntaxError`][mdn-syntax-error] (or a descendant) object, false positives may occur due to the fact that the [`SyntaxError`][mdn-syntax-error] constructor inherits from [`Error`][mdn-error] and has no internal class of its own. Hence, [`SyntaxError`][mdn-syntax-error] impersonation is possible. + +
+ + + + + +
+ +## Examples + + + +```javascript +var isSyntaxError = require( '@stdlib/assert/is-syntax-error' ); + +var bool = isSyntaxError( new SyntaxError( 'syntax error' ) ); +// returns true + +bool = isSyntaxError( new Error( 'error' ) ); +// returns false + +bool = isSyntaxError( new EvalError( 'eval error' ) ); +// returns false + +bool = isSyntaxError( new ReferenceError( 'reference error' ) ); +// returns false + +bool = isSyntaxError( new RangeError( 'range error' ) ); +// returns false + +bool = isSyntaxError( new TypeError( 'type error' ) ); +// returns false + +bool = isSyntaxError( new URIError( 'URI error' ) ); +// returns false + +bool = isSyntaxError( {} ); +// returns false + +bool = isSyntaxError( null ); +// returns false +``` + +
+ + + + + +
+ +
+ + + + + + + + diff --git a/is-syntax-error/benchmark/benchmark.js b/is-syntax-error/benchmark/benchmark.js new file mode 100644 index 00000000..1512c891 --- /dev/null +++ b/is-syntax-error/benchmark/benchmark.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isSyntaxError = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + {}, + [], + null, + false, + true, + new Error( 'beep' ), + new SyntaxError( 'bar' ), + new TypeError( 'boop' ), + new RangeError( 'foo' ) + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isSyntaxError( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-syntax-error/docs/repl.txt b/is-syntax-error/docs/repl.txt new file mode 100644 index 00000000..87ac42c5 --- /dev/null +++ b/is-syntax-error/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( value ) + Tests if a value is a SyntaxError object. + + This function should *not* be considered robust. While the function should + always return `true` if provided a SyntaxError (or a descendant) object, + false positives may occur due to the fact that the SyntaxError constructor + inherits from Error and has no internal class of its own. Hence, SyntaxError + impersonation is possible. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a SyntaxError object. + + Examples + -------- + > var bool = {{alias}}( new SyntaxError( 'beep' ) ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-syntax-error/docs/types/index.d.ts b/is-syntax-error/docs/types/index.d.ts new file mode 100644 index 00000000..93126732 --- /dev/null +++ b/is-syntax-error/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a `SyntaxError` object. +* +* ## Notes +* +* - This function should **not** be considered robust. While the function should always return `true` if provided a SyntaxError (or a descendant) object, false positives may occur due to the fact that the SyntaxError constructor inherits from Error and has no internal class of its own. Hence, SyntaxError impersonation is possible. +* +* @param value - value to test +* @returns boolean indicating whether a value is a `SyntaxError` object +* +* @example +* var bool = isSyntaxError( new SyntaxError( 'beep' ) ); +* // returns true +* +* @example +* var bool = isSyntaxError( {} ); +* // returns false +*/ +declare function isSyntaxError( value: any ): boolean; + + +// EXPORTS // + +export = isSyntaxError; diff --git a/is-syntax-error/docs/types/test.ts b/is-syntax-error/docs/types/test.ts new file mode 100644 index 00000000..3c909e1b --- /dev/null +++ b/is-syntax-error/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isSyntaxError = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isSyntaxError( new SyntaxError( 'beep' ) ); // $ExpectType boolean + isSyntaxError( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isSyntaxError(); // $ExpectError + isSyntaxError( undefined, 123 ); // $ExpectError +} diff --git a/is-syntax-error/examples/index.js b/is-syntax-error/examples/index.js new file mode 100644 index 00000000..f52e4dbb --- /dev/null +++ b/is-syntax-error/examples/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isSyntaxError = require( './../lib' ); + +var bool = isSyntaxError( new SyntaxError( 'syntax error' ) ); +console.log( bool ); +// => true + +bool = isSyntaxError( new Error( 'error' ) ); +console.log( bool ); +// => false + +bool = isSyntaxError( new EvalError( 'eval error' ) ); +console.log( bool ); +// => false + +bool = isSyntaxError( new ReferenceError( 'reference error' ) ); +console.log( bool ); +// => false + +bool = isSyntaxError( new RangeError( 'range error' ) ); +console.log( bool ); +// => false + +bool = isSyntaxError( new TypeError( 'type error' ) ); +console.log( bool ); +// => false + +bool = isSyntaxError( new URIError( 'URI error' ) ); +console.log( bool ); +// => false + +bool = isSyntaxError( {} ); +console.log( bool ); +// => false + +bool = isSyntaxError( null ); +console.log( bool ); +// => false diff --git a/is-syntax-error/lib/index.js b/is-syntax-error/lib/index.js new file mode 100644 index 00000000..cc3f33ca --- /dev/null +++ b/is-syntax-error/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a `SyntaxError` object. +* +* @module @stdlib/assert/is-syntax-error +* +* @example +* var isSyntaxError = require( '@stdlib/assert/is-syntax-error' ); +* +* var bool = isSyntaxError( new SyntaxError( 'beep' ) ); +* // returns true +* +* bool = isSyntaxError( {} ); +* // returns false +*/ + +// MODULES // + +var isSyntaxError = require( './main.js' ); + + +// EXPORTS // + +module.exports = isSyntaxError; diff --git a/is-syntax-error/lib/main.js b/is-syntax-error/lib/main.js new file mode 100644 index 00000000..45d38aef --- /dev/null +++ b/is-syntax-error/lib/main.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var getPrototypeOf = require( '@stdlib/utils/get-prototype-of' ); +var typeOf = require( '@stdlib/utils/type-of' ); +var isError = require( '@stdlib/assert/is-error' ); + + +// MAIN // + +/** +* Tests if a value is a `SyntaxError` object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is a `SyntaxError` object +* +* @example +* var bool = isSyntaxError( new SyntaxError( 'beep' ) ); +* // returns true +* +* @example +* var bool = isSyntaxError( {} ); +* // returns false +*/ +function isSyntaxError( value ) { + if ( typeof value !== 'object' || value === null ) { + return false; + } + // Check for `SyntaxError` objects from the same realm (same Node.js `vm` or same `Window` object)... + if ( value instanceof SyntaxError ) { + return true; + } + // All `SyntaxError` objects are `Error` objects... + if ( isError( value ) ) { + // Walk the prototype tree until we find the desired constructor... + while ( value ) { + if ( typeOf( value ) === 'syntaxerror' ) { + return true; + } + value = getPrototypeOf( value ); + } + } + return false; +} + + +// EXPORTS // + +module.exports = isSyntaxError; diff --git a/is-syntax-error/package.json b/is-syntax-error/package.json new file mode 100644 index 00000000..de1bb080 --- /dev/null +++ b/is-syntax-error/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-syntax-error", + "version": "0.0.0", + "description": "Test if a value is a SyntaxError object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "syntax", + "error", + "err", + "exception", + "is", + "iserr", + "iserror", + "valid", + "isvalid", + "type", + "check", + "validate", + "validation", + "test" + ] +} diff --git a/is-syntax-error/test/test.js b/is-syntax-error/test/test.js new file mode 100644 index 00000000..52c75530 --- /dev/null +++ b/is-syntax-error/test/test.js @@ -0,0 +1,115 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var vm = require( 'vm' ); // TODO: handle in-browser tests +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var inherit = require( '@stdlib/utils/inherit' ); +var isSyntaxError = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': IS_BROWSER +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isSyntaxError, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a `SyntaxError` object', function test( t ) { + var values; + var i; + + values = [ + new SyntaxError( 'syntax error' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSyntaxError( values[ i ] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an object inheriting from `SyntaxError`', function test( t ) { + function CustomError( msg ) { + SyntaxError.call( this ); + this.name = 'CustomError'; + this.message = msg; + return this; + } + + inherit( CustomError, SyntaxError ); + + t.strictEqual( isSyntaxError( new CustomError( 'custom error' ) ), true, 'returns true when provided a value which inherits from SyntaxError' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a `SyntaxError` object from a different realm', opts, function test( t ) { + var error = vm.runInNewContext( 'new SyntaxError()' ); + t.strictEqual( isSyntaxError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an object from a different realm which inherits from a `SyntaxError` object', opts, function test( t ) { + var error = vm.runInNewContext( 'function Err() { return this; }; Err.prototype = new SyntaxError(); new Err();' ); + t.strictEqual( isSyntaxError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a `SyntaxError` object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined, + [], + {}, + function noop() {}, + function error() {}, + new Date(), + new RegExp( '.*' ), + new Error( 'error' ), + new EvalError( 'eval error' ), + new ReferenceError( 'reference error' ), + new RangeError( 'range error' ), + new TypeError( 'type error' ), + new URIError( 'URI error' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isSyntaxError( values[ i ] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-triangular-number/README.md b/is-triangular-number/README.md new file mode 100644 index 00000000..370c4998 --- /dev/null +++ b/is-triangular-number/README.md @@ -0,0 +1,191 @@ + + +# isTriangularNumber + +> Test if a value is a [triangular number][triangular-number]. + +
+ +[Triangular numbers][triangular-number] can be computed using the following formula + + + +
+ Triangular number formula. +
+
+ + + +for `n >= 0`. + +By analogy with the square root of `x`, one can define the positive triangular root of `x` such that `T_n = x` + + + +
+ Triangular root formula. +
+
+ + + +Accordingly, an integer `x` is a [triangular number][triangular-number] **if and only** if `8x+1` is a [square number][@stdlib/assert/is-square-number]. + +
+ + + +
+ +## Usage + +```javascript +var isTriangularNumber = require( '@stdlib/assert/is-triangular-number' ); +``` + +#### isTriangularNumber( value ) + +Tests if a `value` is a [triangular number][triangular-number]. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isTriangularNumber( 36.0 ); +// returns true + +bool = isTriangularNumber( new Number( 36.0 ) ); +// returns true + +bool = isTriangularNumber( 3.14 ); +// returns false + +bool = isTriangularNumber( -5.0 ); +// returns false + +bool = isTriangularNumber( NaN ); +// returns false + +bool = isTriangularNumber( null ); +// returns false +``` + +#### isTriangularNumber.isPrimitive( value ) + +Tests if a `value` is a primitive [triangular number][triangular-number]. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isTriangularNumber.isPrimitive( 36.0 ); +// returns true + +bool = isTriangularNumber.isPrimitive( new Number( 36.0 ) ); +// returns false +``` + +#### isTriangularNumber.isObject( value ) + +Tests if a `value` is a `Number` object having a value which is a [triangular number][triangular-number]. + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); + +var bool = isTriangularNumber.isObject( 36.0 ); +// returns false + +bool = isTriangularNumber.isObject( new Number( 36.0 ) ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Return values are not reliable for numbers greater than `1125899906842624`. + +
+ + + +
+ +## Examples + + + + + +```javascript +var Number = require( '@stdlib/number/ctor' ); +var isTriangularNumber = require( '@stdlib/assert/is-triangular-number' ); + +var bool = isTriangularNumber( 36.0 ); +// returns true + +bool = isTriangularNumber( new Number( 36.0 ) ); +// returns true + +bool = isTriangularNumber( 0.0 ); +// returns true + +bool = isTriangularNumber( 1.0 ); +// returns true + +bool = isTriangularNumber( 3.14 ); +// returns false + +bool = isTriangularNumber( -5.0 ); +// returns false + +bool = isTriangularNumber( NaN ); +// returns false + +bool = isTriangularNumber( '0.5' ); +// returns false + +bool = isTriangularNumber( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-triangular-number/benchmark/benchmark.js b/is-triangular-number/benchmark/benchmark.js new file mode 100644 index 00000000..8135a5f3 --- /dev/null +++ b/is-triangular-number/benchmark/benchmark.js @@ -0,0 +1,234 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers, no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Number = require( '@stdlib/number/ctor' ); +var pkg = require( './../package.json' ).name; +var isTriangularNumber = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::primitives', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 0.5, + 1.0, + 0.0, + 3.14, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isTriangularNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 36.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isTriangularNumber( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 36, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isTriangularNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isPrimitive', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 36.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isTriangularNumber.isPrimitive( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::primitives:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 0.5, + 1.0, + 0.0, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isTriangularNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::objects:isObject', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [], + {}, + function noop() {}, + new Number( 36.0 ), + new Number( 0.5 ), + new Number( NaN ), + new Number( 3.14 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isTriangularNumber.isObject( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-triangular-number/docs/img/equation_triangular_number.svg b/is-triangular-number/docs/img/equation_triangular_number.svg new file mode 100644 index 00000000..c02bc1f1 --- /dev/null +++ b/is-triangular-number/docs/img/equation_triangular_number.svg @@ -0,0 +1,32 @@ + +upper T Subscript n Baseline equals StartFraction n left-parenthesis n plus 1 right-parenthesis Over 2 EndFraction + + + \ No newline at end of file diff --git a/is-triangular-number/docs/img/equation_triangular_root.svg b/is-triangular-number/docs/img/equation_triangular_root.svg new file mode 100644 index 00000000..0b2b5366 --- /dev/null +++ b/is-triangular-number/docs/img/equation_triangular_root.svg @@ -0,0 +1,36 @@ + +n equals StartFraction StartRoot 8 x plus 1 EndRoot minus 1 Over 2 EndFraction + + + \ No newline at end of file diff --git a/is-triangular-number/docs/repl.txt b/is-triangular-number/docs/repl.txt new file mode 100644 index 00000000..0d1f06be --- /dev/null +++ b/is-triangular-number/docs/repl.txt @@ -0,0 +1,78 @@ + +{{alias}}( value ) + Tests if a value is a triangular number. + + Return values are not reliable for numbers greater than 1125899906842624. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a triangular number. + + Examples + -------- + > var bool = {{alias}}( 36.0 ) + true + > bool = {{alias}}( new Number( 36.0 ) ) + true + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( -5.0 ) + false + > bool = {{alias}}( null ) + false + + +{{alias}}.isPrimitive( value ) + Tests if a value is a number primitive which is a triangular number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number primitive which is a + triangular number. + + Examples + -------- + > var bool = {{alias}}.isPrimitive( 36.0 ) + true + > bool = {{alias}}.isPrimitive( new Number( 36.0 ) ) + false + + +{{alias}}.isObject( value ) + Tests if a value is a number object having a value which is a triangular + number. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a number object having a value which + is a triangular number. + + Examples + -------- + > var bool = {{alias}}.isObject( 36.0 ) + false + > bool = {{alias}}.isObject( new Number( 36.0 ) ) + true + + + See Also + -------- + diff --git a/is-triangular-number/docs/types/index.d.ts b/is-triangular-number/docs/types/index.d.ts new file mode 100644 index 00000000..63105d7a --- /dev/null +++ b/is-triangular-number/docs/types/index.d.ts @@ -0,0 +1,129 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Interface describing methods for testing for primitives and objects. +*/ +interface IsTriangularNumber { + /** + * Tests if a value is a triangular number. + * + * @param value - value to test + * @returns boolean indicating whether value is a triangular number + * + * @example + * var bool = isTriangularNumber( 36.0 ); + * // returns true + * + * @example + * var bool = isTriangularNumber( new Number( 36.0 ) ); + * // returns true + * + * @example + * var bool = isTriangularNumber( 3.14 ); + * // returns false + * + * @example + * var bool = isTriangularNumber( -5.0 ); + * // returns false + * + * @example + * var bool = isTriangularNumber( null ); + * // returns false + */ + ( value: any ): boolean; + + /** + * Tests if a value is a number primitive having a value which is a triangular number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number primitive having a value which is a triangular number + * + * @example + * var bool = isTriangularNumber.isPrimitive( 36.0 ); + * // returns true + * + * @example + * var bool = isTriangularNumber.isPrimitive( new Number( 36.0 ) ); + * // returns false + */ + isPrimitive( value: any ): boolean; + + /** + * Tests if a value is a number object having a value which is a triangular number. + * + * @param value - value to test + * @returns boolean indicating if a value is a number object having a value which is a triangular number + * + * @example + * var bool = isTriangularNumber.isObject( 36.0 ); + * // returns false + * + * @example + * var bool = isTriangularNumber.isObject( new Number( 36.0 ) ); + * // returns true + */ + isObject( value: any ): boolean; +} + +/** +* Tests if a value is a triangular number. +* +* ## Notes +* +* - Return values are not reliable for numbers greater than `1125899906842624`. +* +* @param value - value to test +* @returns boolean indicating whether value is a triangular number +* +* @example +* var bool = isTriangularNumber( 36.0 ); +* // returns true +* +* @example +* var bool = isTriangularNumber( new Number( 36.0 ) ); +* // returns true +* +* @example +* var bool = isTriangularNumber( 3.14 ); +* // returns false +* +* @example +* var bool = isTriangularNumber( -5.0 ); +* // returns false +* +* @example +* var bool = isTriangularNumber( null ); +* // returns false +* +* @example +* var bool = isTriangularNumber.isPrimitive( 36.0 ); +* // returns true +* +* @example +* var bool = isTriangularNumber.isObject( new Number( 36.0 ) ); +* // returns true +*/ +declare var isTriangularNumber: IsTriangularNumber; + + +// EXPORTS // + +export = isTriangularNumber; diff --git a/is-triangular-number/docs/types/test.ts b/is-triangular-number/docs/types/test.ts new file mode 100644 index 00000000..023a88e1 --- /dev/null +++ b/is-triangular-number/docs/types/test.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isTriangularNumber = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isTriangularNumber( 3 ); // $ExpectType boolean + isTriangularNumber( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isTriangularNumber(); // $ExpectError + isTriangularNumber( 0.2, 123 ); // $ExpectError +} + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isTriangularNumber.isPrimitive( new Number( 0.2 ) ); // $ExpectType boolean + isTriangularNumber.isPrimitive( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isPrimitive method is provided an unsupported number of arguments... +{ + isTriangularNumber.isPrimitive(); // $ExpectError + isTriangularNumber.isPrimitive( 0.2, 123 ); // $ExpectError +} + + +// Attached to main export is an isPrimitive method which returns a boolean... +{ + // tslint:disable-next-line:no-construct + isTriangularNumber.isObject( new Number( 0.2 ) ); // $ExpectType boolean + isTriangularNumber.isObject( 0.2 ); // $ExpectType boolean +} + +// The compiler throws an error if the isObject method is provided an unsupported number of arguments... +{ + isTriangularNumber.isObject(); // $ExpectError + isTriangularNumber.isObject( 0.2, 123 ); // $ExpectError +} diff --git a/is-triangular-number/examples/index.js b/is-triangular-number/examples/index.js new file mode 100644 index 00000000..63b37f3d --- /dev/null +++ b/is-triangular-number/examples/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-wrappers */ + +'use strict'; + +var Number = require( '@stdlib/number/ctor' ); +var isTriangularNumber = require( './../lib' ); + +console.log( isTriangularNumber( 36.0 ) ); +// => true + +console.log( isTriangularNumber( new Number( 36.0 ) ) ); +// => true + +console.log( isTriangularNumber( 0.0 ) ); +// => true + +console.log( isTriangularNumber( 1.0 ) ); +// => true + +console.log( isTriangularNumber( 3.14 ) ); +// => false + +console.log( isTriangularNumber( -5.0 ) ); +// => false + +console.log( isTriangularNumber( NaN ) ); +// => false + +console.log( isTriangularNumber( '0.5' ) ); +// => false + +console.log( isTriangularNumber( null ) ); +// => false diff --git a/is-triangular-number/lib/index.js b/is-triangular-number/lib/index.js new file mode 100644 index 00000000..f4b24bc5 --- /dev/null +++ b/is-triangular-number/lib/index.js @@ -0,0 +1,79 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a triangular number. +* +* @module @stdlib/assert/is-triangular-number +* +* @example +* var isTriangularNumber = require( '@stdlib/assert/is-triangular-number' ); +* +* var bool = isTriangularNumber( 36.0 ); +* // returns true +* +* bool = isTriangularNumber( new Number( 36.0 ) ); +* // returns true +* +* bool = isTriangularNumber( 3.14 ); +* // returns false +* +* bool = isTriangularNumber( -5.0 ); +* // returns false +* +* bool = isTriangularNumber( null ); +* // returns false +* +* @example +* var isTriangularNumber = require( '@stdlib/assert/is-triangular-number' ).isPrimitive; +* +* var bool = isTriangularNumber( 36.0 ); +* // returns true +* +* bool = isTriangularNumber( new Number( 36.0 ) ); +* // returns false +* +* @example +* var isTriangularNumber = require( '@stdlib/assert/is-triangular-number' ).isObject; +* +* var bool = isTriangularNumber( 36.0 ); +* // returns false +* +* bool = isTriangularNumber( new Number( 36.0 ) ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var isTriangularNumber = require( './main.js' ); +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +setReadOnly( isTriangularNumber, 'isPrimitive', isPrimitive ); +setReadOnly( isTriangularNumber, 'isObject', isObject ); + + +// EXPORTS // + +module.exports = isTriangularNumber; diff --git a/is-triangular-number/lib/main.js b/is-triangular-number/lib/main.js new file mode 100644 index 00000000..bf7d6309 --- /dev/null +++ b/is-triangular-number/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isPrimitive = require( './primitive.js' ); +var isObject = require( './object.js' ); + + +// MAIN // + +/** +* Tests if a value is a triangular number. +* +* ## Notes +* +* - Return values are not reliable for numbers greater than `1125899906842624`. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a triangular number +* +* @example +* var bool = isTriangularNumber( 36.0 ); +* // returns true +* +* @example +* var bool = isTriangularNumber( new Number( 36.0 ) ); +* // returns true +* +* @example +* var bool = isTriangularNumber( 3.14 ); +* // returns false +* +* @example +* var bool = isTriangularNumber( -5.0 ); +* // returns false +* +* @example +* var bool = isTriangularNumber( null ); +* // returns false +*/ +function isTriangularNumber( value ) { + return ( isPrimitive( value ) || isObject( value ) ); +} + + +// EXPORTS // + +module.exports = isTriangularNumber; diff --git a/is-triangular-number/lib/object.js b/is-triangular-number/lib/object.js new file mode 100644 index 00000000..24b2d6df --- /dev/null +++ b/is-triangular-number/lib/object.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isObject; +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); + + +// MAIN // + +/** +* Tests if a value is a number object having a value which is a triangular number. +* +* ## Notes +* +* - Return values are not reliable for numbers greater than `1125899906842624`. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number object having a value which is a triangular number +* +* @example +* var bool = isTriangularNumber( 36.0 ); +* // returns false +* +* @example +* var bool = isTriangularNumber( new Number( 36.0 ) ); +* // returns true +*/ +function isTriangularNumber( value ) { + var n; + var x; + if ( !isInteger( value ) ) { + return false; + } + x = ( 8.0*value.valueOf() ) + 1.0; + n = floor( sqrt( x ) + 0.5 ); + return n*n === x; +} + + +// EXPORTS // + +module.exports = isTriangularNumber; diff --git a/is-triangular-number/lib/primitive.js b/is-triangular-number/lib/primitive.js new file mode 100644 index 00000000..76b0cfc9 --- /dev/null +++ b/is-triangular-number/lib/primitive.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); + + +// MAIN // + +/** +* Tests if a value is a number primitive having a value which is a triangular number. +* +* ## Notes +* +* - Return values are not reliable for numbers greater than `1125899906842624`. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a number primitive having a value which is a triangular number +* +* @example +* var bool = isTriangularNumber( 36.0 ); +* // returns true +* +* @example +* var bool = isTriangularNumber( new Number( 36.0 ) ); +* // returns false +*/ +function isTriangularNumber( value ) { + var n; + var x; + if ( !isInteger( value ) ) { + return false; + } + x = ( 8.0*value ) + 1.0; + n = floor( sqrt( x ) + 0.5 ); + return n*n === x; +} + + +// EXPORTS // + +module.exports = isTriangularNumber; diff --git a/is-triangular-number/package.json b/is-triangular-number/package.json new file mode 100644 index 00000000..6f5e9596 --- /dev/null +++ b/is-triangular-number/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-triangular-number", + "version": "0.0.0", + "description": "Test if a value is a triangular number.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "number", + "numeric", + "triangular", + "triangle", + "binomial", + "binomcoef", + "is", + "istriangular", + "type", + "check", + "valid", + "validate", + "test", + "primitive", + "object" + ] +} diff --git a/is-triangular-number/test/test.js b/is-triangular-number/test/test.js new file mode 100644 index 00000000..800dc931 --- /dev/null +++ b/is-triangular-number/test/test.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isTriangularNumber = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isTriangularNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test primitives', function test( t ) { + t.equal( typeof isTriangularNumber.isPrimitive, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to test objects', function test( t ) { + t.equal( typeof isTriangularNumber.isObject, 'function', 'export is a function' ); + t.end(); +}); diff --git a/is-triangular-number/test/test.main.js b/is-triangular-number/test/test.main.js new file mode 100644 index 00000000..5f898c94 --- /dev/null +++ b/is-triangular-number/test/test.main.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isTriangularNumber = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isTriangularNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a triangular number', function test( t ) { + t.equal( isTriangularNumber( 36.0 ), true, 'returns true' ); + t.equal( isTriangularNumber( new Number( 36.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a triangular number', function test( t ) { + var values; + var i; + + values = [ + '5', + 5.0, + -3.14, + -1.0, + NaN, + null, + true, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isTriangularNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-triangular-number/test/test.object.js b/is-triangular-number/test/test.object.js new file mode 100644 index 00000000..02078bae --- /dev/null +++ b/is-triangular-number/test/test.object.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isTriangularNumber = require( './../lib/object.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isTriangularNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a number object having a triangular number', function test( t ) { + t.equal( isTriangularNumber( new Number( 36.0 ) ), true, 'returns true' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if provided a primitive triangular number', function test( t ) { + t.equal( isTriangularNumber( 36.0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a triangular number', function test( t ) { + var values; + var i; + + values = [ + '5', + new Number( 5.0 ), // eslint-disable-line no-new-wrappers + null, + true, + NaN, + void 0, + [], + {}, + new Date(), + /./, + new RegExp( '.' ), // eslint-disable-line prefer-regex-literals + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isTriangularNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-triangular-number/test/test.primitive.js b/is-triangular-number/test/test.primitive.js new file mode 100644 index 00000000..af21b71d --- /dev/null +++ b/is-triangular-number/test/test.primitive.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Number = require( '@stdlib/number/ctor' ); +var isTriangularNumber = require( './../lib/primitive.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isTriangularNumber, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a primitive triangular number', function test( t ) { + t.equal( isTriangularNumber( 36.0 ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a number object', function test( t ) { + t.equal( isTriangularNumber( new Number( 36.0 ) ), false, 'returns false' ); // eslint-disable-line no-new-wrappers + t.end(); +}); + +tape( 'the function returns `false` if not provided a triangular number', function test( t ) { + var values; + var i; + + values = [ + '5', + 5.0, + null, + true, + NaN, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isTriangularNumber( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-truthy-array/README.md b/is-truthy-array/README.md new file mode 100644 index 00000000..391bb7ef --- /dev/null +++ b/is-truthy-array/README.md @@ -0,0 +1,82 @@ + + +# isTruthyArray + +> Test if a value is an array-like object containing only truthy values. + +
+ +## Usage + +```javascript +var isTruthyArray = require( '@stdlib/assert/is-truthy-array' ); +``` + +#### isTruthyArray( value ) + +Tests if a `value` is an array-like `object` containing only truthy values. + +```javascript +var bool = isTruthyArray( [ [], {} ] ); +// returns true + +bool = isTruthyArray( [ '', null, void 0, false, 0, NaN ] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isTruthyArray = require( '@stdlib/assert/is-truthy-array' ); + +var bool = isTruthyArray( [ [], {}, true, 'a', 5, -5 ] ); +// returns true + +bool = isTruthyArray( [ null, '', NaN, 0, void 0, false ] ); +// returns false + +bool = isTruthyArray( [] ); +// returns false + +bool = isTruthyArray( true ); +// returns false + +bool = isTruthyArray( {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-truthy-array/benchmark/benchmark.js b/is-truthy-array/benchmark/benchmark.js new file mode 100644 index 00000000..43b2af60 --- /dev/null +++ b/is-truthy-array/benchmark/benchmark.js @@ -0,0 +1,102 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isTruthyArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( 1 ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i; + bool = isTruthyArray( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/is-truthy-array/docs/repl.txt b/is-truthy-array/docs/repl.txt new file mode 100644 index 00000000..ce194db0 --- /dev/null +++ b/is-truthy-array/docs/repl.txt @@ -0,0 +1,27 @@ + +{{alias}}( value ) + Tests if a value is an array-like object containing only truthy values. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is an array-like object containing + only truthy values. + + Examples + -------- + > var bool = {{alias}}( [ {}, [] ] ) + true + > bool = {{alias}}( [ null, '' ] ) + false + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-truthy-array/docs/types/index.d.ts b/is-truthy-array/docs/types/index.d.ts new file mode 100644 index 00000000..6b5a9567 --- /dev/null +++ b/is-truthy-array/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an array-like object containing only truthy values. +* +* @param value - value to test +* @returns boolean indicating whether an input value is an array-like object containing only truthy values +* +* @example +* var bool = isTruthyArray( [ {}, [] ] ); +* // returns true +* +* bool = isTruthyArray( [ false, null, void 0, '', 0, NaN ] ); +* // returns false +* +* bool = isTruthyArray( [] ); +* // returns false +*/ +declare function isTruthyArray( value: any ): boolean; + + +// EXPORTS // + +export = isTruthyArray; diff --git a/is-truthy-array/docs/types/test.ts b/is-truthy-array/docs/types/test.ts new file mode 100644 index 00000000..3d6fe749 --- /dev/null +++ b/is-truthy-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isTruthyArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isTruthyArray( [ {}, [] ] ); // $ExpectType boolean + isTruthyArray( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isTruthyArray(); // $ExpectError + isTruthyArray( undefined, 123 ); // $ExpectError +} diff --git a/is-truthy-array/examples/index.js b/is-truthy-array/examples/index.js new file mode 100644 index 00000000..e7ce68f1 --- /dev/null +++ b/is-truthy-array/examples/index.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isTruthyArray = require( './../lib' ); + +var bool = isTruthyArray( [ [], {}, true, 'a', 5, -5 ] ); +console.log( bool ); +// => true + +bool = isTruthyArray( [ null, '', NaN, 0, void 0, false ] ); +console.log( bool ); +// => false + +bool = isTruthyArray( [] ); +console.log( bool ); +// => false + +bool = isTruthyArray( true ); +console.log( bool ); +// => false + +bool = isTruthyArray( {} ); +console.log( bool ); +// => false diff --git a/is-truthy-array/lib/index.js b/is-truthy-array/lib/index.js new file mode 100644 index 00000000..9cbed941 --- /dev/null +++ b/is-truthy-array/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array-like object containing only truthy values. +* +* @module @stdlib/assert/is-truthy-array +* +* @example +* var isTruthyArray = require( '@stdlib/assert/is-truthy-array' ); +* +* var bool = isTruthyArray( [ {}, [] ] ); +* // returns true +* +* bool = isTruthyArray( [ false, null, void 0, '', 0, NaN ] ); +* // returns false +* +* bool = isTruthyArray( [] ); +* // returns false +*/ + +// MODULES // + +var isTruthyArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isTruthyArray; diff --git a/is-truthy-array/lib/main.js b/is-truthy-array/lib/main.js new file mode 100644 index 00000000..43b0b2ff --- /dev/null +++ b/is-truthy-array/lib/main.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var arrayfun = require( '@stdlib/assert/tools/array-like-function' ); +var isTruthy = require( '@stdlib/assert/is-truthy' ); + + +// MAIN // + +/** +* Tests if a value is an array-like object containing only truthy values. +* +* @name isTruthyArray +* @type {Function} +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether an input value is an array-like object containing only truthy values +* +* @example +* var bool = isTruthyArray( [ {}, [] ] ); +* // returns true +* +* bool = isTruthyArray( [ false, null, void 0, '', 0, NaN ] ); +* // returns false +* +* bool = isTruthyArray( [] ); +* // returns false +*/ +var isTruthyArray = arrayfun( isTruthy ); + + +// EXPORTS // + +module.exports = isTruthyArray; diff --git a/is-truthy-array/package.json b/is-truthy-array/package.json new file mode 100644 index 00000000..41fb7d36 --- /dev/null +++ b/is-truthy-array/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-truthy-array", + "version": "0.0.0", + "description": "Test if a value is an array-like object containing only truthy values.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "falsy", + "falsey", + "truthy", + "array", + "is", + "istruthy", + "isarray", + "array-like", + "typed-array", + "typed array", + "collection", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-truthy-array/test/test.js b/is-truthy-array/test/test.js new file mode 100644 index 00000000..49ccd989 --- /dev/null +++ b/is-truthy-array/test/test.js @@ -0,0 +1,123 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isTruthyArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isTruthyArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an array-like object containing only "truthy" values (array)', function test( t ) { + var bool; + var arr; + + arr = [ true, 5, 'beep', [], {}, isTruthyArray, new Date(), /.*/ ]; + bool = isTruthyArray( arr ); + t.strictEqual( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `true` if provided an array-like object containing only "truthy" values (object)', function test( t ) { + var bool; + var arr; + + arr = { + 'length': 6, + '0': true, + '1': 5, + '2': 'beep', + '3': [], + '4': {}, + '5': isTruthyArray + }; + bool = isTruthyArray( arr ); + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an array-like object containing only "truthy" values', function test( t ) { + var bool; + var arr; + + arr = [ false, null, void 0, '', 0, NaN ]; + bool = isTruthyArray( arr ); + t.strictEqual( bool, false, 'returns false when provided a falsy array' ); + + arr = [ 'beep', false ]; + bool = isTruthyArray( arr ); + t.strictEqual( bool, false, 'returns false when provided an array containing a falsy value' ); + + arr = [ 'beep', null ]; + bool = isTruthyArray( arr ); + t.strictEqual( bool, false, 'returns false when provided an array containing a falsy value' ); + + arr = [ 'beep', void 0 ]; + bool = isTruthyArray( arr ); + t.strictEqual( bool, false, 'returns false when provided an array containing a falsy value' ); + + arr = [ 'beep', '' ]; + bool = isTruthyArray( arr ); + t.strictEqual( bool, false, 'returns false when provided an array containing a falsy value' ); + + arr = [ 'beep', 0 ]; + bool = isTruthyArray( arr ); + t.strictEqual( bool, false, 'returns false when provided an array containing a falsy value' ); + + arr = [ 'beep', NaN ]; + bool = isTruthyArray( arr ); + t.strictEqual( bool, false, 'returns false when provided an array containing a falsy value' ); + + arr = []; + bool = isTruthyArray( arr ); + t.strictEqual( bool, false, 'returns false when provided an empty array' ); + + arr = { + 'length': 0 + }; + bool = isTruthyArray( arr ); + t.strictEqual( bool, false, 'returns false when provided an empty array-like object' ); + + bool = isTruthyArray( null ); + t.strictEqual( bool, false, 'returns false when provided null' ); + + bool = isTruthyArray( true ); + t.strictEqual( bool, false, 'returns false when provided true' ); + + bool = isTruthyArray( '' ); + t.strictEqual( bool, false, 'returns false when provided an empty string' ); + + bool = isTruthyArray( foo ); + t.strictEqual( bool, false, 'returns false when provided a function' ); + + t.end(); + + function foo( a, b ) { + return ( a === b ); + } +}); diff --git a/is-truthy/README.md b/is-truthy/README.md new file mode 100644 index 00000000..08987388 --- /dev/null +++ b/is-truthy/README.md @@ -0,0 +1,123 @@ + + +# isTruthy + +> Test if a value is truthy. + +
+ +## Usage + +```javascript +var isTruthy = require( '@stdlib/assert/is-truthy' ); +``` + +#### isTruthy( value ) + +Tests if a `value` is a value which translates to `true` when evaluated in a boolean context. + +```javascript +var bool = isTruthy( true ); +// returns true + +bool = isTruthy( [] ); +// returns true + +bool = isTruthy( false ); +// returns false + +bool = isTruthy( '' ); +// returns false + +bool = isTruthy( 0 ); +// returns false + +bool = isTruthy( NaN ); +// returns false + +bool = isTruthy( null ); +// returns false + +bool = isTruthy( void 0 ); +// returns false +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isTruthy = require( '@stdlib/assert/is-truthy' ); + +var bool = isTruthy( true ); +// returns true + +bool = isTruthy( 'beep' ); +// returns true + +bool = isTruthy( 5 ); +// returns true + +bool = isTruthy( [] ); +// returns true + +bool = isTruthy( {} ); +// returns true + +bool = isTruthy( function foo() {} ); +// returns true + +bool = isTruthy( false ); +// returns false + +bool = isTruthy( 0 ); +// returns false + +bool = isTruthy( NaN ); +// returns false + +bool = isTruthy( '' ); +// returns false + +bool = isTruthy( void 0 ); +// returns false + +bool = isTruthy( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-truthy/benchmark/benchmark.js b/is-truthy/benchmark/benchmark.js new file mode 100644 index 00000000..c09a6819 --- /dev/null +++ b/is-truthy/benchmark/benchmark.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isTruthy = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '', + '5', + 0, + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + new RegExp( '.*' ), + new Date(), + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isTruthy( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-truthy/docs/repl.txt b/is-truthy/docs/repl.txt new file mode 100644 index 00000000..1305c4e4 --- /dev/null +++ b/is-truthy/docs/repl.txt @@ -0,0 +1,39 @@ + +{{alias}}( value ) + Tests if a value is a value which translates to `true` when evaluated in a + boolean context. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is truthy. + + Examples + -------- + > var bool = {{alias}}( true ) + true + > bool = {{alias}}( {} ) + true + > bool = {{alias}}( [] ) + true + > bool = {{alias}}( false ) + false + > bool = {{alias}}( '' ) + false + > bool = {{alias}}( 0 ) + false + > bool = {{alias}}( null ) + false + > bool = {{alias}}( void 0 ) + false + > bool = {{alias}}( NaN ) + false + + See Also + -------- + diff --git a/is-truthy/docs/types/index.d.ts b/is-truthy/docs/types/index.d.ts new file mode 100644 index 00000000..bc41adfd --- /dev/null +++ b/is-truthy/docs/types/index.d.ts @@ -0,0 +1,64 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is truthy. +* +* @param value - value to test +* @returns boolean indicating whether a value is truthy +* +* @example +* var bool = isTruthy( true ); +* // returns true +* +* @example +* var bool = isTruthy( [] ); +* // returns true +* +* @example +* var bool = isTruthy( false ); +* // returns false +* +* @example +* var bool = isTruthy( null ); +* // returns false +* +* @example +* var bool = isTruthy( '' ); +* // returns false +* +* @example +* var bool = isTruthy( 0 ); +* // returns false +* +* @example +* var bool = isTruthy( void 0 ); +* // returns false +* +* @example +* var bool = isTruthy( NaN ); +* // returns false +*/ +declare function isTruthy( value: any ): boolean; + + +// EXPORTS // + +export = isTruthy; diff --git a/is-truthy/docs/types/test.ts b/is-truthy/docs/types/test.ts new file mode 100644 index 00000000..19d90256 --- /dev/null +++ b/is-truthy/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isTruthy = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isTruthy( true ); // $ExpectType boolean + isTruthy( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isTruthy(); // $ExpectError + isTruthy( undefined, 123 ); // $ExpectError +} diff --git a/is-truthy/examples/index.js b/is-truthy/examples/index.js new file mode 100644 index 00000000..13517652 --- /dev/null +++ b/is-truthy/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function, no-restricted-syntax */ + +'use strict'; + +var isTruthy = require( './../lib' ); + +console.log( isTruthy( true ) ); +// => true + +console.log( isTruthy( 'beep' ) ); +// => true + +console.log( isTruthy( 5 ) ); +// => true + +console.log( isTruthy( [] ) ); +// => true + +console.log( isTruthy( {} ) ); +// => true + +console.log( isTruthy( function foo() {} ) ); +// => true + +console.log( isTruthy( false ) ); +// => false + +console.log( isTruthy( 0 ) ); +// => false + +console.log( isTruthy( NaN ) ); +// => false + +console.log( isTruthy( '' ) ); +// => false + +console.log( isTruthy( void 0 ) ); +// => false + +console.log( isTruthy( null ) ); +// => false diff --git a/is-truthy/lib/index.js b/is-truthy/lib/index.js new file mode 100644 index 00000000..6e15618b --- /dev/null +++ b/is-truthy/lib/index.js @@ -0,0 +1,61 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is truthy. +* +* @module @stdlib/assert/is-truthy +* +* @example +* var isTruthy = require( '@stdlib/assert/is-truthy' ); +* +* var bool = isTruthy( true ); +* // returns true +* +* bool = isTruthy( [] ); +* // returns true +* +* bool = isTruthy( false ); +* // returns false +* +* bool = isTruthy( null ); +* // returns false +* +* bool = isTruthy( '' ); +* // returns false +* +* bool = isTruthy( 0 ); +* // returns false +* +* bool = isTruthy( void 0 ); +* // returns false +* +* bool = isTruthy( NaN ); +* // returns false +*/ + +// MODULES // + +var isTruthy = require( './main.js' ); + + +// EXPORTS // + +module.exports = isTruthy; diff --git a/is-truthy/lib/main.js b/is-truthy/lib/main.js new file mode 100644 index 00000000..c9a6e7ac --- /dev/null +++ b/is-truthy/lib/main.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Tests if a value is truthy. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is truthy +* +* @example +* var bool = isTruthy( true ); +* // returns true +* +* @example +* var bool = isTruthy( [] ); +* // returns true +* +* @example +* var bool = isTruthy( false ); +* // returns false +* +* @example +* var bool = isTruthy( null ); +* // returns false +* +* @example +* var bool = isTruthy( '' ); +* // returns false +* +* @example +* var bool = isTruthy( 0 ); +* // returns false +* +* @example +* var bool = isTruthy( void 0 ); +* // returns false +* +* @example +* var bool = isTruthy( NaN ); +* // returns false +*/ +function isTruthy( value ) { + return ( value ) ? true : false; // eslint-disable-line no-unneeded-ternary +} + + +// EXPORTS // + +module.exports = isTruthy; diff --git a/is-truthy/package.json b/is-truthy/package.json new file mode 100644 index 00000000..627710d0 --- /dev/null +++ b/is-truthy/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-truthy", + "version": "0.0.0", + "description": "Test if a value is truthy.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "falsy", + "falsey", + "truthy", + "is", + "istruthy", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-truthy/test/test.js b/is-truthy/test/test.js new file mode 100644 index 00000000..fe76461b --- /dev/null +++ b/is-truthy/test/test.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isTruthy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isTruthy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `false` if provided a "falsy" value', function test( t ) { + t.strictEqual( isTruthy( false ), false, 'returns false' ); + t.strictEqual( isTruthy( '' ), false, 'returns false' ); + t.strictEqual( isTruthy( 0 ), false, 'returns false' ); + t.strictEqual( isTruthy( NaN ), false, 'returns false' ); + t.strictEqual( isTruthy( null ), false, 'returns false' ); + t.strictEqual( isTruthy( void 0 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `true` if not provided a "truthy" value', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + true, + [], + {}, + function noop() {}, + new Date(), + /.*/ + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isTruthy( values[i] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-type-error/README.md b/is-type-error/README.md new file mode 100644 index 00000000..6c057b19 --- /dev/null +++ b/is-type-error/README.md @@ -0,0 +1,129 @@ + + +# isTypeError + +> Test if a value is a [TypeError][mdn-type-error] object. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var isTypeError = require( '@stdlib/assert/is-type-error' ); +``` + +#### isTypeError( value ) + +Tests if a `value` is a [`TypeError`][mdn-type-error] object. + +```javascript +var bool = isTypeError( new TypeError( 'beep' ) ); +// returns true +``` + +
+ + + + + +
+ +## Notes + +- This function should **not** be considered robust. While the function should **always** return `true` if provided a [`TypeError`][mdn-type-error] (or a descendant) object, false positives may occur due to the fact that the [`TypeError`][mdn-type-error] constructor inherits from [`Error`][mdn-error] and has no internal class of its own. Hence, [`TypeError`][mdn-type-error] impersonation is possible. + +
+ + + + + +
+ +## Examples + + + +```javascript +var isTypeError = require( '@stdlib/assert/is-type-error' ); + +var bool = isTypeError( new TypeError( 'type error' ) ); +// returns true + +bool = isTypeError( new Error( 'error' ) ); +// returns false + +bool = isTypeError( new EvalError( 'eval error' ) ); +// returns false + +bool = isTypeError( new ReferenceError( 'reference error' ) ); +// returns false + +bool = isTypeError( new SyntaxError( 'syntax error' ) ); +// returns false + +bool = isTypeError( new RangeError( 'range error' ) ); +// returns false + +bool = isTypeError( new URIError( 'URI error' ) ); +// returns false + +bool = isTypeError( {} ); +// returns false + +bool = isTypeError( null ); +// returns false +``` + +
+ + + + + +
+ +
+ + + + + + + + diff --git a/is-type-error/benchmark/benchmark.js b/is-type-error/benchmark/benchmark.js new file mode 100644 index 00000000..d1d3618d --- /dev/null +++ b/is-type-error/benchmark/benchmark.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isTypeError = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + {}, + [], + null, + false, + true, + new Error( 'beep' ), + new TypeError( 'bar' ), + new RangeError( 'boop' ), + new SyntaxError( 'foo' ) + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isTypeError( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-type-error/docs/repl.txt b/is-type-error/docs/repl.txt new file mode 100644 index 00000000..d9fb317b --- /dev/null +++ b/is-type-error/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( value ) + Tests if a value is a TypeError object. + + This function should *not* be considered robust. While the function should + always return `true` if provided a TypeError (or a descendant) object, + false positives may occur due to the fact that the TypeError constructor + inherits from Error and has no internal class of its own. Hence, TypeError + impersonation is possible. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a TypeError object. + + Examples + -------- + > var bool = {{alias}}( new TypeError( 'beep' ) ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-type-error/docs/types/index.d.ts b/is-type-error/docs/types/index.d.ts new file mode 100644 index 00000000..e9ad20b2 --- /dev/null +++ b/is-type-error/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a `TypeError` object. +* +* ## Notes +* +* - This function should **not** be considered robust. While the function should always return `true` if provided a TypeError (or a descendant) object, false positives may occur due to the fact that the TypeError constructor inherits from Error and has no internal class of its own. Hence, TypeError impersonation is possible. +* +* @param value - value to test +* @returns boolean indicating whether a value is a `TypeError` object +* +* @example +* var bool = isTypeError( new TypeError( 'beep' ) ); +* // returns true +* +* @example +* var bool = isTypeError( {} ); +* // returns false +*/ +declare function isTypeError( value: any ): boolean; + + +// EXPORTS // + +export = isTypeError; diff --git a/is-type-error/docs/types/test.ts b/is-type-error/docs/types/test.ts new file mode 100644 index 00000000..ede35191 --- /dev/null +++ b/is-type-error/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isTypeError = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isTypeError( new TypeError( 'beep' ) ); // $ExpectType boolean + isTypeError( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isTypeError(); // $ExpectError + isTypeError( undefined, 123 ); // $ExpectError +} diff --git a/is-type-error/examples/index.js b/is-type-error/examples/index.js new file mode 100644 index 00000000..6e01b966 --- /dev/null +++ b/is-type-error/examples/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isTypeError = require( './../lib' ); + +var bool = isTypeError( new TypeError( 'type error' ) ); +console.log( bool ); +// => true + +bool = isTypeError( new Error( 'error' ) ); +console.log( bool ); +// => false + +bool = isTypeError( new EvalError( 'eval error' ) ); +console.log( bool ); +// => false + +bool = isTypeError( new ReferenceError( 'reference error' ) ); +console.log( bool ); +// => false + +bool = isTypeError( new SyntaxError( 'syntax error' ) ); +console.log( bool ); +// => false + +bool = isTypeError( new RangeError( 'range error' ) ); +console.log( bool ); +// => false + +bool = isTypeError( new URIError( 'URI error' ) ); +console.log( bool ); +// => false + +bool = isTypeError( {} ); +console.log( bool ); +// => false + +bool = isTypeError( null ); +console.log( bool ); +// => false diff --git a/is-type-error/lib/index.js b/is-type-error/lib/index.js new file mode 100644 index 00000000..f927369d --- /dev/null +++ b/is-type-error/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a `TypeError` object. +* +* @module @stdlib/assert/is-type-error +* +* @example +* var isTypeError = require( '@stdlib/assert/is-type-error' ); +* +* var bool = isTypeError( new TypeError( 'beep' ) ); +* // returns true +* +* bool = isTypeError( {} ); +* // returns false +*/ + +// MODULES // + +var isTypeError = require( './main.js' ); + + +// EXPORTS // + +module.exports = isTypeError; diff --git a/is-type-error/lib/main.js b/is-type-error/lib/main.js new file mode 100644 index 00000000..dd03165d --- /dev/null +++ b/is-type-error/lib/main.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var getPrototypeOf = require( '@stdlib/utils/get-prototype-of' ); +var typeOf = require( '@stdlib/utils/type-of' ); +var isError = require( '@stdlib/assert/is-error' ); + + +// MAIN // + +/** +* Tests if a value is a `TypeError` object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is a `TypeError` object +* +* @example +* var bool = isTypeError( new TypeError( 'beep' ) ); +* // returns true +* +* @example +* var bool = isTypeError( {} ); +* // returns false +*/ +function isTypeError( value ) { + if ( typeof value !== 'object' || value === null ) { + return false; + } + // Check for `TypeError` objects from the same realm (same Node.js `vm` or same `Window` object)... + if ( value instanceof TypeError ) { + return true; + } + // All `TypeError` objects are `Error` objects... + if ( isError( value ) ) { + // Walk the prototype tree until we find the desired constructor... + while ( value ) { + if ( typeOf( value ) === 'typeerror' ) { + return true; + } + value = getPrototypeOf( value ); + } + } + return false; +} + + +// EXPORTS // + +module.exports = isTypeError; diff --git a/is-type-error/package.json b/is-type-error/package.json new file mode 100644 index 00000000..7dabecaf --- /dev/null +++ b/is-type-error/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-type-error", + "version": "0.0.0", + "description": "Test if a value is a TypeError object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typeerror", + "error", + "err", + "exception", + "is", + "iserr", + "iserror", + "valid", + "isvalid", + "type", + "check", + "validate", + "validation", + "test" + ] +} diff --git a/is-type-error/test/test.js b/is-type-error/test/test.js new file mode 100644 index 00000000..4595a887 --- /dev/null +++ b/is-type-error/test/test.js @@ -0,0 +1,115 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var vm = require( 'vm' ); // TODO: handle in-browser tests +var tape = require( 'tape' ); +var inherit = require( '@stdlib/utils/inherit' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var isTypeError = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': IS_BROWSER +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isTypeError, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a `TypeError` object', function test( t ) { + var values; + var i; + + values = [ + new TypeError( 'type error' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isTypeError( values[ i ] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an object inheriting from `TypeError`', function test( t ) { + function CustomError( msg ) { + TypeError.call( this ); + this.name = 'CustomError'; + this.message = msg; + return this; + } + + inherit( CustomError, TypeError ); + + t.strictEqual( isTypeError( new CustomError( 'custom error' ) ), true, 'returns true when provided a value which inherits from TypeError' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a `TypeError` object from a different realm', opts, function test( t ) { + var error = vm.runInNewContext( 'new TypeError()' ); + t.strictEqual( isTypeError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an object from a different realm which inherits from a `TypeError` object', opts, function test( t ) { + var error = vm.runInNewContext( 'function Err() { return this; }; Err.prototype = new TypeError(); new Err();' ); + t.strictEqual( isTypeError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a `TypeError` object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + undefined, + [], + {}, + function noop() {}, + function error() {}, + new Date(), + new RegExp( '.*' ), + new Error( 'error' ), + new EvalError( 'eval error' ), + new ReferenceError( 'reference error' ), + new SyntaxError( 'syntax error' ), + new RangeError( 'range error' ), + new URIError( 'URI error' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isTypeError( values[ i ] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-typed-array-length/README.md b/is-typed-array-length/README.md new file mode 100644 index 00000000..6b0e4832 --- /dev/null +++ b/is-typed-array-length/README.md @@ -0,0 +1,115 @@ + + +# isTypedArrayLength + +> Test if a value is a valid [typed array][mdn-typed-array] length. + +
+ +## Usage + +```javascript +var isTypedArrayLength = require( '@stdlib/assert/is-typed-array-length' ); +``` + +#### isTypedArrayLength( value ) + +Tests if a value is a valid [typed array][mdn-typed-array] length. + +```javascript +var bool = isTypedArrayLength( 5 ); +// returns true + +bool = isTypedArrayLength( -1 ); +// returns false + +bool = isTypedArrayLength( 2.0e200 ); +// returns false + +bool = isTypedArrayLength( 3.14 ); +// returns false + +bool = isTypedArrayLength( null ); +// returns false +``` + +
+ + + +
+ +## Notes + +- A valid `length` property for a [typed array][mdn-typed-array] is any integer value on the interval `[0, 2^53-1]`. + +
+ + + +
+ +## Examples + + + +```javascript +var isTypedArrayLength = require( '@stdlib/assert/is-typed-array-length' ); + +var bool = isTypedArrayLength( 5 ); +// returns true + +bool = isTypedArrayLength( 0 ); +// returns true + +bool = isTypedArrayLength( 2.0e200 ); +// returns false + +bool = isTypedArrayLength( 5.256 ); +// returns false + +bool = isTypedArrayLength( 1.0/0.0 ); +// returns false + +bool = isTypedArrayLength( -1.0/0.0 ); +// returns false + +bool = isTypedArrayLength( NaN ); +// returns false + +bool = isTypedArrayLength( '5' ); +// returns false + +bool = isTypedArrayLength( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-typed-array-length/benchmark/benchmark.js b/is-typed-array-length/benchmark/benchmark.js new file mode 100644 index 00000000..de96503d --- /dev/null +++ b/is-typed-array-length/benchmark/benchmark.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isTypedArrayLength = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + 4, + 3.14, + 2e200, + -5, + -4, + NaN, + true, + false, + null, + void 0 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isTypedArrayLength( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-typed-array-length/docs/repl.txt b/is-typed-array-length/docs/repl.txt new file mode 100644 index 00000000..629840a8 --- /dev/null +++ b/is-typed-array-length/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( value ) + Tests if a value is a valid typed array length. + + A valid length property for a typed array instance is any integer value on + the interval [0, 2^53-1]. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a valid typed array length. + + Examples + -------- + > var bool = {{alias}}( 5 ) + true + > bool = {{alias}}( 2.0e200 ) + false + > bool = {{alias}}( -3.14 ) + false + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-typed-array-length/docs/types/index.d.ts b/is-typed-array-length/docs/types/index.d.ts new file mode 100644 index 00000000..7ad9653e --- /dev/null +++ b/is-typed-array-length/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a valid typed array length. +* +* ## Notes +* +* - A valid length property for a typed array instance is any integer value on the interval `[0, 2^53-1]`. +* +* @param value - value to test +* @returns boolean indicating if a value is a valid typed array length +* +* @example +* var bool = isTypedArrayLength( 3 ); +* // returns true +* +* @example +* var bool = isTypedArrayLength( 3.14 ); +* // returns false +*/ +declare function isTypedArrayLength( value: any ): boolean; + + +// EXPORTS // + +export = isTypedArrayLength; diff --git a/is-typed-array-length/docs/types/test.ts b/is-typed-array-length/docs/types/test.ts new file mode 100644 index 00000000..c389d97c --- /dev/null +++ b/is-typed-array-length/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isTypedArrayLength = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isTypedArrayLength( 3 ); // $ExpectType boolean + isTypedArrayLength( 3.14 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isTypedArrayLength(); // $ExpectError + isTypedArrayLength( 3, 123 ); // $ExpectError +} diff --git a/is-typed-array-length/examples/index.js b/is-typed-array-length/examples/index.js new file mode 100644 index 00000000..15c30fbc --- /dev/null +++ b/is-typed-array-length/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isTypedArrayLength = require( './../lib' ); + +console.log( isTypedArrayLength( 5 ) ); +// => true + +console.log( isTypedArrayLength( 0 ) ); +// => true + +console.log( isTypedArrayLength( 3.0e200 ) ); +// => false + +console.log( isTypedArrayLength( 5.256 ) ); +// => false + +console.log( isTypedArrayLength( 1.0/0.0 ) ); +// => false + +console.log( isTypedArrayLength( -1.0/0.0 ) ); +// => false + +console.log( isTypedArrayLength( NaN ) ); +// => false + +console.log( isTypedArrayLength( '5' ) ); +// => false + +console.log( isTypedArrayLength( null ) ); +// => false diff --git a/is-typed-array-length/lib/index.js b/is-typed-array-length/lib/index.js new file mode 100644 index 00000000..e82ccd61 --- /dev/null +++ b/is-typed-array-length/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a valid typed array length. +* +* @module @stdlib/assert/is-typed-array-length +* +* @example +* var isTypedArrayLength = require( '@stdlib/assert/is-typed-array-length' ); +* +* var bool = isTypedArrayLength( 5 ); +* // returns true +* +* bool = isTypedArrayLength( 2.0e200 ); +* // returns false +* +* bool = isTypedArrayLength( -3.14 ); +* // returns false +* +* bool = isTypedArrayLength( null ); +* // returns false +*/ + +// MODULES // + +var isTypedArrayLength = require( './main.js' ); + + +// EXPORTS // + +module.exports = isTypedArrayLength; diff --git a/is-typed-array-length/lib/main.js b/is-typed-array-length/lib/main.js new file mode 100644 index 00000000..ae0b2681 --- /dev/null +++ b/is-typed-array-length/lib/main.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var MAX_LENGTH = require( '@stdlib/constants/array/max-typed-array-length' ); + + +// MAIN // + +/** +* Tests if a value is a valid typed array length. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a valid typed array length +* +* @example +* var bool = isTypedArrayLength( 3 ); +* // returns true +* +* @example +* var bool = isTypedArrayLength( 3.14 ); +* // returns false +*/ +function isTypedArrayLength( value ) { + return ( + isInteger( value ) && + value >= 0 && + value <= MAX_LENGTH + ); +} + + +// EXPORTS // + +module.exports = isTypedArrayLength; diff --git a/is-typed-array-length/package.json b/is-typed-array-length/package.json new file mode 100644 index 00000000..062982e9 --- /dev/null +++ b/is-typed-array-length/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-typed-array-length", + "version": "0.0.0", + "description": "Test if a value is a valid typed array length.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typedarray", + "typed-array", + "typed", + "array", + "arr", + "length", + "is", + "islength", + "type", + "check", + "validate", + "isvalid", + "valid", + "test" + ] +} diff --git a/is-typed-array-length/test/test.js b/is-typed-array-length/test/test.js new file mode 100644 index 00000000..f7864783 --- /dev/null +++ b/is-typed-array-length/test/test.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var MAX_LENGTH = require( '@stdlib/constants/array/max-typed-array-length' ); +var isTypedArrayLength = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isTypedArrayLength, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a valid typed array length', function test( t ) { + t.strictEqual( isTypedArrayLength( 0 ), true, 'returns true' ); + t.strictEqual( isTypedArrayLength( 10 ), true, 'returns true' ); + t.strictEqual( isTypedArrayLength( MAX_LENGTH ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a valid typed array length', function test( t ) { + t.strictEqual( isTypedArrayLength( MAX_LENGTH+1 ), false, 'returns false' ); + t.strictEqual( isTypedArrayLength( -1 ), false, 'returns false' ); + t.strictEqual( isTypedArrayLength( 3.14 ), false, 'returns false' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a number', function test( t ) { + var values; + var i; + + values = [ + '5', + NaN, + null, + true, + false, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isTypedArrayLength( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-typed-array-like/README.md b/is-typed-array-like/README.md new file mode 100644 index 00000000..a247029f --- /dev/null +++ b/is-typed-array-like/README.md @@ -0,0 +1,109 @@ + + +# isTypedArrayLike + +> Test if a value is [typed-array][mdn-typed-array]-like. + +
+ +## Usage + +```javascript +var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' ); +``` + +#### isTypedArrayLike( value ) + +Tests if a value is [typed-array][mdn-typed-array]-like. + +```javascript +var Int16Array = require( '@stdlib/array/int16' ); + +var bool = isTypedArrayLike( new Int16Array() ); +// returns true + +bool = isTypedArrayLike({ + 'length': 10, + 'byteOffset': 0, + 'byteLength': 10, + 'BYTES_PER_ELEMENT': 4 +}); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' ); + +var bool; +var arr; + +arr = { + 'BYTES_PER_ELEMENT': 8, + 'length': 10, + 'byteOffset': 0, + 'byteLength': 10 +}; +bool = isTypedArrayLike( arr ); +// returns true + +bool = isTypedArrayLike( new Int8Array( 4 ) ); +// returns true + +bool = isTypedArrayLike( [] ); +// returns false + +bool = isTypedArrayLike( {} ); +// returns false + +bool = isTypedArrayLike( null ); +// returns false + +bool = isTypedArrayLike( 'beep' ); +// returns false + +bool = isTypedArrayLike( function foo( a, b ) {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-typed-array-like/benchmark/benchmark.js b/is-typed-array-like/benchmark/benchmark.js new file mode 100644 index 00000000..5973822c --- /dev/null +++ b/is-typed-array-like/benchmark/benchmark.js @@ -0,0 +1,189 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isTypedArrayLike = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + new Date() + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isTypedArrayLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var arr; + var i; + + arr = {}; + arr.BYTES_PER_ELEMENT = 4; + arr.length = 10; + arr.byteOffset = 0; + arr.byteLength = 10; + + values = [ + arr, + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isTypedArrayLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array', function benchmark( b ) { + var bool; + var obj; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = [ i, i+1 ]; + bool = isTypedArrayLike( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::typed_array', function benchmark( b ) { + var values; + var bool; + var obj; + var N; + var i; + + values = [ + new Float64Array( [ 1.0, 2.0 ] ), + new Float64Array( [ 3.0, 4.0 ] ) + ]; + N = values.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = values[ i%N ]; + bool = isTypedArrayLike( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::array_like_object', function benchmark( b ) { + var bool; + var obj; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + obj = { + 'length': 2, + '0': i, + '1': i + 1 + }; + bool = isTypedArrayLike( obj ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-typed-array-like/docs/repl.txt b/is-typed-array-like/docs/repl.txt new file mode 100644 index 00000000..933f5aee --- /dev/null +++ b/is-typed-array-like/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( value ) + Tests if a value is typed-array-like. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is typed-array-like. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/int16}}() ) + true + > bool = {{alias}}({ + ... 'length': 10, + ... 'byteOffset': 0, + ... 'byteLength': 10, + ... 'BYTES_PER_ELEMENT': 4 + ... }) + true + + See Also + -------- + diff --git a/is-typed-array-like/docs/types/index.d.ts b/is-typed-array-like/docs/types/index.d.ts new file mode 100644 index 00000000..190feb89 --- /dev/null +++ b/is-typed-array-like/docs/types/index.d.ts @@ -0,0 +1,64 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is typed-array-like. +* +* @param value - value to test +* @returns boolean indicating if a value is typed-array-like +* +* @example +* var arr = { +* 'BYTES_PER_ELEMENT': 8, +* 'length': 10, +* 'byteOffset': 0, +* 'byteLength': 10 +* }; +* var val = isTypedArrayLike( arr ); +* // returns true +* +* @example +* var Int8Array = require( `@stdlib/array/int8` ); +* +* var val = isTypedArrayLike( new Int8Array( 4 ) ); +* // returns true +* +* @example +* var val = isTypedArrayLike( [] ); +* // returns false +* +* @example +* var val = isTypedArrayLike( {} ); +* // returns false +* +* @example +* var val = isTypedArrayLike( null ); +* // returns false +* +* @example +* var val = isTypedArrayLike( 'beep' ); +* // returns false +*/ +declare function isTypedArrayLike( value: any ): boolean; + + +// EXPORTS // + +export = isTypedArrayLike; diff --git a/is-typed-array-like/docs/types/test.ts b/is-typed-array-like/docs/types/test.ts new file mode 100644 index 00000000..486f2189 --- /dev/null +++ b/is-typed-array-like/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isTypedArrayLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isTypedArrayLike( new Int8Array( 4 ) ); // $ExpectType boolean + isTypedArrayLike( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isTypedArrayLike(); // $ExpectError + isTypedArrayLike( new Int8Array( 4 ), 123 ); // $ExpectError +} diff --git a/is-typed-array-like/examples/index.js b/is-typed-array-like/examples/index.js new file mode 100644 index 00000000..8d545d07 --- /dev/null +++ b/is-typed-array-like/examples/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-restricted-syntax, no-empty-function, no-unused-vars */ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var isTypedArrayLike = require( './../lib' ); + +var arr = { + 'BYTES_PER_ELEMENT': 8, + 'length': 10, + 'byteOffset': 0, + 'byteLength': 10 +}; +console.log( isTypedArrayLike( arr ) ); +// => true + +console.log( isTypedArrayLike( new Int8Array( 4 ) ) ); +// => true + +console.log( isTypedArrayLike( [] ) ); +// => false + +console.log( isTypedArrayLike( {} ) ); +// => false + +console.log( isTypedArrayLike( null ) ); +// => false + +console.log( isTypedArrayLike( 'beep' ) ); +// => false + +console.log( isTypedArrayLike( function foo( a, b ) {} ) ); +// => false diff --git a/is-typed-array-like/lib/index.js b/is-typed-array-like/lib/index.js new file mode 100644 index 00000000..b343e420 --- /dev/null +++ b/is-typed-array-like/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is typed-array-like. +* +* @module @stdlib/assert/is-typed-array-like +* +* @example +* var Int16Array = require( '@stdlib/array/int16' ); +* var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' ); +* +* var bool = isTypedArrayLike( new Int16Array() ); +* // returns true +* +* bool = isTypedArrayLike({ +* 'length': 10, +* 'byteOffset': 0, +* 'byteLength': 10, +* 'BYTES_PER_ELEMENT': 4 +* }); +* // returns true +*/ + +// MODULES // + +var isTypedArrayLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isTypedArrayLike; diff --git a/is-typed-array-like/lib/main.js b/is-typed-array-like/lib/main.js new file mode 100644 index 00000000..e7bf42d7 --- /dev/null +++ b/is-typed-array-like/lib/main.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var MAX = require( '@stdlib/constants/array/max-typed-array-length' ); + + +// MAIN // + +/** +* Tests if a value is typed-array-like. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is typed-array-like +* +* @example +* var arr = { +* 'BYTES_PER_ELEMENT': 8, +* 'length': 10, +* 'byteOffset': 0, +* 'byteLength': 10 +* }; +* var val = isTypedArrayLike( arr ); +* // returns true +* +* @example +* var Int8Array = require( '@stdlib/array/int8' ); +* +* var val = isTypedArrayLike( new Int8Array( 4 ) ); +* // returns true +* +* @example +* var val = isTypedArrayLike( [] ); +* // returns false +* +* @example +* var val = isTypedArrayLike( {} ); +* // returns false +* +* @example +* var val = isTypedArrayLike( null ); +* // returns false +* +* @example +* var val = isTypedArrayLike( 'beep' ); +* // returns false +*/ +function isTypedArrayLike( value ) { + return ( + value !== null && + typeof value === 'object' && + isNonNegativeInteger( value.length ) && + value.length <= MAX && + typeof value.BYTES_PER_ELEMENT === 'number' && + typeof value.byteOffset === 'number' && + typeof value.byteLength === 'number' + ); +} + + +// EXPORTS // + +module.exports = isTypedArrayLike; diff --git a/is-typed-array-like/package.json b/is-typed-array-like/package.json new file mode 100644 index 00000000..3696e8a6 --- /dev/null +++ b/is-typed-array-like/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-typed-array-like", + "version": "0.0.0", + "description": "Test if a value is typed-array-like.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed-array-like", + "like", + "is", + "istypedarraylike", + "length", + "duck type", + "duck", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-typed-array-like/test/test.js b/is-typed-array-like/test/test.js new file mode 100644 index 00000000..b6c94934 --- /dev/null +++ b/is-typed-array-like/test/test.js @@ -0,0 +1,138 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isTypedArrayLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isTypedArrayLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a typed-array-like object', function test( t ) { + var values; + var i; + + values = [ + new Int8Array(), + new Uint8Array(), + new Int16Array(), + new Uint16Array(), + new Int32Array(), + new Uint32Array(), + new Float32Array(), + new Float64Array(), + new Uint8ClampedArray(), + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0, + 'byteLength': 10 + } + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isTypedArrayLike( values[i] ), true, 'returns true when provided '+values[i] ); + } + + t.end(); +}); + +tape( 'the function returns `false` if not provided a typed-array-like object', function test( t ) { + var values; + var i; + + values = [ + 'beep', + 5, + null, + undefined, + NaN, + true, + false, + {}, + [], + function boop() {}, + { + 'length': 3.14, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0, + 'byteLength': 10 + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': true, + 'byteOffset': 0, + 'byteLength': 10 + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': [], + 'byteLength': 10 + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0, + 'byteLength': null + }, + { + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0, + 'byteLength': 10 + }, + { + 'length': 10, + 'byteOffset': 0, + 'byteLength': 10 + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteLength': 10 + }, + { + 'length': 10, + 'BYTES_PER_ELEMENT': 4, + 'byteOffset': 0 + } + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isTypedArrayLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-typed-array/README.md b/is-typed-array/README.md new file mode 100644 index 00000000..c6659310 --- /dev/null +++ b/is-typed-array/README.md @@ -0,0 +1,125 @@ + + +# isTypedArray + +> Test if a value is a [typed array][mdn-typed-array]. + +
+ +## Usage + +```javascript +var isTypedArray = require( '@stdlib/assert/is-typed-array' ); +``` + +#### isTypedArray( value ) + +Tests if a value is a [typed array][mdn-typed-array]. + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); + +var bool = isTypedArray( new Int8Array( 10 ) ); +// returns true + +bool = isTypedArray( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isTypedArray = require( '@stdlib/assert/is-typed-array' ); + +var arr = new Int8Array( 10 ); +var bool = isTypedArray( arr ); +// returns true + +arr = new Uint8Array( 10 ); +bool = isTypedArray( arr ); +// returns true + +arr = new Uint8ClampedArray( 10 ); +bool = isTypedArray( arr ); +// returns true + +arr = new Int16Array( 10 ); +bool = isTypedArray( arr ); +// returns true + +arr = new Uint16Array( 10 ); +bool = isTypedArray( arr ); +// returns true + +arr = new Int32Array( 10 ); +bool = isTypedArray( arr ); +// returns true + +arr = new Uint32Array( 10 ); +bool = isTypedArray( arr ); +// returns true + +arr = new Float32Array( 10 ); +bool = isTypedArray( arr ); +// returns true + +arr = new Float64Array( 10 ); +bool = isTypedArray( arr ); +// returns true + +bool = isTypedArray( [] ); +// returns false + +bool = isTypedArray( {} ); +// returns false + +bool = isTypedArray( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-typed-array/benchmark/benchmark.js b/is-typed-array/benchmark/benchmark.js new file mode 100644 index 00000000..293c8e94 --- /dev/null +++ b/is-typed-array/benchmark/benchmark.js @@ -0,0 +1,115 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isTypedArray = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + new Date(), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isTypedArray( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isTypedArray( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-typed-array/docs/repl.txt b/is-typed-array/docs/repl.txt new file mode 100644 index 00000000..872c942e --- /dev/null +++ b/is-typed-array/docs/repl.txt @@ -0,0 +1,22 @@ + +{{alias}}( value ) + Tests if a value is a typed array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a typed array. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/int8}}( 10 ) ) + true + + See Also + -------- + diff --git a/is-typed-array/docs/types/index.d.ts b/is-typed-array/docs/types/index.d.ts new file mode 100644 index 00000000..a310aabc --- /dev/null +++ b/is-typed-array/docs/types/index.d.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a typed array. +* +* @param value - value to test +* @returns boolean indicating if a value is a typed array +* +* @example +* var Int8Array = require( `@stdlib/array/int8` ); +* +* var bool = isTypedArray( new Int8Array( 10 ) ); +* // returns true +*/ +declare function isTypedArray( value: any ): boolean; + + +// EXPORTS // + +export = isTypedArray; diff --git a/is-typed-array/docs/types/test.ts b/is-typed-array/docs/types/test.ts new file mode 100644 index 00000000..f9be2e1d --- /dev/null +++ b/is-typed-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isTypedArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isTypedArray( new Int8Array( 10 ) ); // $ExpectType boolean + isTypedArray( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isTypedArray(); // $ExpectError + isTypedArray( new Int8Array( 10 ), 123 ); // $ExpectError +} diff --git a/is-typed-array/examples/index.js b/is-typed-array/examples/index.js new file mode 100644 index 00000000..22f737ee --- /dev/null +++ b/is-typed-array/examples/index.js @@ -0,0 +1,75 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isTypedArray = require( './../lib' ); + +var arr = new Int8Array( 10 ); +console.log( isTypedArray( arr ) ); +// => true + +arr = new Uint8Array( 10 ); +console.log( isTypedArray( arr ) ); +// => true + +arr = new Uint8ClampedArray( 10 ); +console.log( isTypedArray( arr ) ); +// => true + +arr = new Int16Array( 10 ); +console.log( isTypedArray( arr ) ); +// => true + +arr = new Uint16Array( 10 ); +console.log( isTypedArray( arr ) ); +// => true + +arr = new Int32Array( 10 ); +console.log( isTypedArray( arr ) ); +// => true + +arr = new Uint32Array( 10 ); +console.log( isTypedArray( arr ) ); +// => true + +arr = new Float32Array( 10 ); +console.log( isTypedArray( arr ) ); +// => true + +arr = new Float64Array( 10 ); +console.log( isTypedArray( arr ) ); +// => true + +console.log( isTypedArray( [] ) ); +// => false + +console.log( isTypedArray( {} ) ); +// => false + +console.log( isTypedArray( null ) ); +// => false diff --git a/is-typed-array/lib/ctors.js b/is-typed-array/lib/ctors.js new file mode 100644 index 00000000..ca236b11 --- /dev/null +++ b/is-typed-array/lib/ctors.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); + + +// MAIN // + +var CTORS = [ + Float64Array, + Float32Array, + Int32Array, + Uint32Array, + Int16Array, + Uint16Array, + Int8Array, + Uint8Array, + Uint8ClampedArray +]; + + +// EXPORTS // + +module.exports = CTORS; diff --git a/is-typed-array/lib/index.js b/is-typed-array/lib/index.js new file mode 100644 index 00000000..8df49952 --- /dev/null +++ b/is-typed-array/lib/index.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a typed array. +* +* @module @stdlib/assert/is-typed-array +* +* @example +* var Int8Array = require( '@stdlib/array/int8' ); +* var isTypedArray = require( '@stdlib/assert/is-typed-array' ); +* +* var bool = isTypedArray( new Int8Array( 10 ) ); +* // returns true +*/ + +// MODULES // + +var isTypedArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isTypedArray; diff --git a/is-typed-array/lib/main.js b/is-typed-array/lib/main.js new file mode 100644 index 00000000..e28c9659 --- /dev/null +++ b/is-typed-array/lib/main.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var ctorName = require( '@stdlib/utils/constructor-name' ); +var fcnName = require( '@stdlib/utils/function-name' ); +var getPrototypeOf = require( '@stdlib/utils/get-prototype-of' ); +var hasFloat64ArraySupport = require( '@stdlib/assert/has-float64array-support' ); +var Float64Array = require( '@stdlib/array/float64' ); +var CTORS = require( './ctors.js' ); +var NAMES = require( './names.json' ); + + +// VARIABLES // + +// Abstract `TypedArray` class: +var TypedArray = ( hasFloat64ArraySupport() ) ? getPrototypeOf( Float64Array ) : Dummy; // eslint-disable-line max-len + +// Ensure abstract typed array class has expected name: +TypedArray = ( fcnName( TypedArray ) === 'TypedArray' ) ? TypedArray : Dummy; + + +// FUNCTIONS // + +/** +* Dummy constructor. +* +* @private +*/ +function Dummy() {} // eslint-disable-line no-empty-function + + +// MAIN // + +/** +* Tests if a value is a typed array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a typed array +* +* @example +* var Int8Array = require( '@stdlib/array/int8' ); +* +* var bool = isTypedArray( new Int8Array( 10 ) ); +* // returns true +*/ +function isTypedArray( value ) { + var v; + var i; + + if ( typeof value !== 'object' || value === null ) { + return false; + } + // Check for the abstract class... + if ( value instanceof TypedArray ) { + return true; + } + // Check for typed array objects from the same realm (same Node.js `vm` or same `Window` object)... + for ( i = 0; i < CTORS.length; i++ ) { + if ( value instanceof CTORS[ i ] ) { + return true; + } + } + // Walk the prototype tree until we find an object having a desired class... + while ( value ) { + v = ctorName( value ); + for ( i = 0; i < NAMES.length; i++ ) { + if ( NAMES[ i ] === v ) { + return true; + } + } + value = getPrototypeOf( value ); + } + + return false; +} + + +// EXPORTS // + +module.exports = isTypedArray; diff --git a/is-typed-array/lib/names.json b/is-typed-array/lib/names.json new file mode 100644 index 00000000..5eaa90a3 --- /dev/null +++ b/is-typed-array/lib/names.json @@ -0,0 +1,11 @@ +[ + "Int8Array", + "Uint8Array", + "Uint8ClampedArray", + "Int16Array", + "Uint16Array", + "Int32Array", + "Uint32Array", + "Float32Array", + "Float64Array" +] diff --git a/is-typed-array/package.json b/is-typed-array/package.json new file mode 100644 index 00000000..89f54c91 --- /dev/null +++ b/is-typed-array/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-typed-array", + "version": "0.0.0", + "description": "Test if a value is a typed array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "typed", + "array", + "typed array", + "is", + "istypedarray", + "istyped", + "isarray", + "class", + "constructor", + "name", + "type", + "check" + ] +} diff --git a/is-typed-array/test/test.js b/is-typed-array/test/test.js new file mode 100644 index 00000000..48244da7 --- /dev/null +++ b/is-typed-array/test/test.js @@ -0,0 +1,188 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var vm = require( 'vm' ); // TODO: handle in-browser tests +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var inherit = require( '@stdlib/utils/inherit' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var isTypedArray = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': IS_BROWSER +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isTypedArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a typed array', function test( t ) { + var values; + var i; + + values = [ + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Float32Array( 10 ), + new Float64Array( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isTypedArray( values[i] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided a typed array (older environments)', function test( t ) { + var isTypedArray; + var values; + var i; + + isTypedArray = proxyquire( './../lib/main.js', { + '@stdlib/assert/has-float64array-support': hasSupport + }); + + values = [ + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Float32Array( 10 ), + new Float64Array( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isTypedArray( values[i] ), true, 'returns true when provided '+values[i] ); + } + t.end(); + + function hasSupport() { + return false; + } +}); + +tape( 'the function returns `true` if an environment does not support the abstract TypedArray class (e.g., IE 11)', function test( t ) { + var isTypedArray; + var values; + var i; + + isTypedArray = proxyquire( './../lib/main.js', { + '@stdlib/utils/get-prototype-of': getPrototypeOf + }); + + values = [ + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Float32Array( 10 ), + new Float64Array( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isTypedArray( values[i] ), true, 'returns true when provided '+values[i] ); + } + t.end(); + + function getPrototypeOf() { + // Return an anonymous function: + return function () {}; // eslint-disable-line func-names + } +}); + +tape( 'the function returns `true` if provided an object inheriting from a typed array', function test( t ) { + function CustomArray( data ) { + var i; + for ( i = 0; i < data.length; i++ ) { + this[ i ] = data[ i ]; + } + return this; + } + + inherit( CustomArray, Float64Array ); + + t.strictEqual( isTypedArray( new CustomArray( [ 5.0, 3.0 ] ) ), true, 'returns true when provided a value which inherits from a typed array' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a typed array from a different realm', opts, function test( t ) { + var arr = vm.runInNewContext( 'new Float64Array( [ 5.0, 3.0 ] )' ); + t.strictEqual( isTypedArray( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an object from a different realm which inherits from a typed array', opts, function test( t ) { + var arr = vm.runInNewContext( 'function Arr() { return this; }; Arr.prototype = Object.create( Float64Array.prototype ); Arr.prototype.constructor = Arr; new Arr( [ 5.0, 3.0 ] );' ); + t.strictEqual( isTypedArray( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a typed array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isTypedArray( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-uint16array/README.md b/is-uint16array/README.md new file mode 100644 index 00000000..22ebffc9 --- /dev/null +++ b/is-uint16array/README.md @@ -0,0 +1,116 @@ + + +# isUint16Array + +> Test if a value is a [Uint16Array][mdn-uint16array]. + +
+ +## Usage + +```javascript +var isUint16Array = require( '@stdlib/assert/is-uint16array' ); +``` + +#### isUint16Array( value ) + +Tests if a value is a [`Uint16Array`][mdn-uint16array]. + +```javascript +var Uint16Array = require( '@stdlib/array/uint16' ); + +var bool = isUint16Array( new Uint16Array( 10 ) ); +// returns true + +bool = isUint16Array( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isUint16Array = require( '@stdlib/assert/is-uint16array' ); + +var bool = isUint16Array( new Uint16Array( 10 ) ); +// returns true + +bool = isUint16Array( new Int8Array( 10 ) ); +// returns false + +bool = isUint16Array( new Uint8Array( 10 ) ); +// returns false + +bool = isUint16Array( new Uint8ClampedArray( 10 ) ); +// returns false + +bool = isUint16Array( new Int16Array( 10 ) ); +// returns false + +bool = isUint16Array( new Int32Array( 10 ) ); +// returns false + +bool = isUint16Array( new Uint32Array( 10 ) ); +// returns false + +bool = isUint16Array( new Float32Array( 10 ) ); +// returns false + +bool = isUint16Array( new Float64Array( 10 ) ); +// returns false + +bool = isUint16Array( new Array( 10 ) ); +// returns false + +bool = isUint16Array( {} ); +// returns false + +bool = isUint16Array( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-uint16array/benchmark/benchmark.js b/is-uint16array/benchmark/benchmark.js new file mode 100644 index 00000000..755b12fb --- /dev/null +++ b/is-uint16array/benchmark/benchmark.js @@ -0,0 +1,126 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isUint16Array = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUint16Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Uint16Array( 10 ), + new Uint16Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUint16Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUint16Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-uint16array/docs/repl.txt b/is-uint16array/docs/repl.txt new file mode 100644 index 00000000..2f83e854 --- /dev/null +++ b/is-uint16array/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a Uint16Array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a Uint16Array. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/uint16}}( 10 ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-uint16array/docs/types/index.d.ts b/is-uint16array/docs/types/index.d.ts new file mode 100644 index 00000000..1b528b90 --- /dev/null +++ b/is-uint16array/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a Uint16Array. +* +* @param value - value to test +* @returns boolean indicating whether value is a Uint16Array +* +* @example +* var bool = isUint16Array( new Uint16Array( 10 ) ); +* // returns true +* +* @example +* var bool = isUint16Array( [] ); +* // returns false +*/ +declare function isUint16Array( value: any ): boolean; + + +// EXPORTS // + +export = isUint16Array; diff --git a/is-uint16array/docs/types/test.ts b/is-uint16array/docs/types/test.ts new file mode 100644 index 00000000..fb84bc2a --- /dev/null +++ b/is-uint16array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isUint16Array = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isUint16Array( new Uint16Array( 4 ) ); // $ExpectType boolean + isUint16Array( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isUint16Array(); // $ExpectError + isUint16Array( new Uint16Array( 4 ), 123 ); // $ExpectError +} diff --git a/is-uint16array/examples/index.js b/is-uint16array/examples/index.js new file mode 100644 index 00000000..456a3824 --- /dev/null +++ b/is-uint16array/examples/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isUint16Array = require( './../lib' ); + +var bool = isUint16Array( new Uint16Array( 10 ) ); +console.log( bool ); +// => true + +bool = isUint16Array( new Int8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint16Array( new Uint8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint16Array( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => false + +bool = isUint16Array( new Int16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint16Array( new Int32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint16Array( new Uint32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint16Array( new Float32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint16Array( new Float64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint16Array( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint16Array( {} ); +console.log( bool ); +// => false + +bool = isUint16Array( null ); +console.log( bool ); +// => false diff --git a/is-uint16array/lib/index.js b/is-uint16array/lib/index.js new file mode 100644 index 00000000..8ec611f1 --- /dev/null +++ b/is-uint16array/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a Uint16Array. +* +* @module @stdlib/assert/is-uint16array +* +* @example +* var isUint16Array = require( '@stdlib/assert/is-uint16array' ); +* +* var bool = isUint16Array( new Uint16Array( 10 ) ); +* // returns true +* +* bool = isUint16Array( [] ); +* // returns false +*/ + +// MODULES // + +var isUint16Array = require( './main.js' ); + + +// EXPORTS // + +module.exports = isUint16Array; diff --git a/is-uint16array/lib/main.js b/is-uint16array/lib/main.js new file mode 100644 index 00000000..727e6244 --- /dev/null +++ b/is-uint16array/lib/main.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var hasUint16Array = ( typeof Uint16Array === 'function' ); // eslint-disable-line stdlib/require-globals + + +// MAIN // + +/** +* Tests if a value is a Uint16Array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a Uint16Array +* +* @example +* var bool = isUint16Array( new Uint16Array( 10 ) ); +* // returns true +* +* @example +* var bool = isUint16Array( [] ); +* // returns false +*/ +function isUint16Array( value ) { + return ( + ( hasUint16Array && value instanceof Uint16Array ) || // eslint-disable-line stdlib/require-globals + nativeClass( value ) === '[object Uint16Array]' + ); +} + + +// EXPORTS // + +module.exports = isUint16Array; diff --git a/is-uint16array/package.json b/is-uint16array/package.json new file mode 100644 index 00000000..22b39358 --- /dev/null +++ b/is-uint16array/package.json @@ -0,0 +1,83 @@ +{ + "name": "@stdlib/assert/is-uint16array", + "version": "0.0.0", + "description": "Test if a value is a Uint16Array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "uint16array", + "uint16", + "unsigned", + "integer", + "int", + "uint", + "halfword", + "short", + "word", + "typed", + "typed array", + "typed-array", + "array", + "is", + "isarray", + "istypedarray", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-uint16array/test/test.js b/is-uint16array/test/test.js new file mode 100644 index 00000000..0ab76daa --- /dev/null +++ b/is-uint16array/test/test.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isUint16Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isUint16Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a Uint16Array', function test( t ) { + t.strictEqual( isUint16Array( new Uint16Array( 10 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a Uint16Array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + undefined, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Uint32Array( 10 ), + new Int32Array( 10 ), + new Int16Array( 10 ), + new Uint8Array( 10 ), + new Int8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isUint16Array( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-uint32array/README.md b/is-uint32array/README.md new file mode 100644 index 00000000..58a0d8ca --- /dev/null +++ b/is-uint32array/README.md @@ -0,0 +1,116 @@ + + +# isUint32Array + +> Test if a value is a [Uint32Array][mdn-uint32array]. + +
+ +## Usage + +```javascript +var isUint32Array = require( '@stdlib/assert/is-uint32array' ); +``` + +#### isUint32Array( value ) + +Tests if a value is a [`Uint32Array`][mdn-uint32array]. + +```javascript +var Uint32Array = require( '@stdlib/array/uint32' ); + +var bool = isUint32Array( new Uint32Array( 10 ) ); +// returns true + +bool = isUint32Array( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isUint32Array = require( '@stdlib/assert/is-uint32array' ); + +var bool = isUint32Array( new Uint32Array( 10 ) ); +// returns true + +bool = isUint32Array( new Int8Array( 10 ) ); +// returns false + +bool = isUint32Array( new Uint8Array( 10 ) ); +// returns false + +bool = isUint32Array( new Uint8ClampedArray( 10 ) ); +// returns false + +bool = isUint32Array( new Int16Array( 10 ) ); +// returns false + +bool = isUint32Array( new Uint16Array( 10 ) ); +// returns false + +bool = isUint32Array( new Int32Array( 10 ) ); +// returns false + +bool = isUint32Array( new Float32Array( 10 ) ); +// returns false + +bool = isUint32Array( new Float64Array( 10 ) ); +// returns false + +bool = isUint32Array( new Array( 10 ) ); +// returns false + +bool = isUint32Array( {} ); +// returns false + +bool = isUint32Array( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-uint32array/benchmark/benchmark.js b/is-uint32array/benchmark/benchmark.js new file mode 100644 index 00000000..df87aaec --- /dev/null +++ b/is-uint32array/benchmark/benchmark.js @@ -0,0 +1,126 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isUint32Array = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUint32Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Uint32Array( 10 ), + new Uint32Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUint32Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUint32Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-uint32array/docs/repl.txt b/is-uint32array/docs/repl.txt new file mode 100644 index 00000000..6bb096f9 --- /dev/null +++ b/is-uint32array/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a Uint32Array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a Uint32Array. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/uint32}}( 10 ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-uint32array/docs/types/index.d.ts b/is-uint32array/docs/types/index.d.ts new file mode 100644 index 00000000..4cda6bb0 --- /dev/null +++ b/is-uint32array/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a Uint32Array. +* +* @param value - value to test +* @returns boolean indicating whether value is a Uint32Array +* +* @example +* var bool = isUint32Array( new Uint32Array( 10 ) ); +* // returns true +* +* @example +* var bool = isUint32Array( [] ); +* // returns false +*/ +declare function isUint32Array( value: any ): boolean; + + +// EXPORTS // + +export = isUint32Array; diff --git a/is-uint32array/docs/types/test.ts b/is-uint32array/docs/types/test.ts new file mode 100644 index 00000000..501e610e --- /dev/null +++ b/is-uint32array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isUint32Array = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isUint32Array( new Uint32Array( 4 ) ); // $ExpectType boolean + isUint32Array( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isUint32Array(); // $ExpectError + isUint32Array( new Uint32Array( 4 ), 123 ); // $ExpectError +} diff --git a/is-uint32array/examples/index.js b/is-uint32array/examples/index.js new file mode 100644 index 00000000..86d919f3 --- /dev/null +++ b/is-uint32array/examples/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isUint32Array = require( './../lib' ); + +var bool = isUint32Array( new Uint32Array( 10 ) ); +console.log( bool ); +// => true + +bool = isUint32Array( new Int8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint32Array( new Uint8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint32Array( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => false + +bool = isUint32Array( new Int16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint32Array( new Uint16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint32Array( new Int32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint32Array( new Float32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint32Array( new Float64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint32Array( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint32Array( {} ); +console.log( bool ); +// => false + +bool = isUint32Array( null ); +console.log( bool ); +// => false diff --git a/is-uint32array/lib/index.js b/is-uint32array/lib/index.js new file mode 100644 index 00000000..a7f17e25 --- /dev/null +++ b/is-uint32array/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a Uint32Array. +* +* @module @stdlib/assert/is-uint32array +* +* @example +* var isUint32Array = require( '@stdlib/assert/is-uint32array' ); +* +* var bool = isUint32Array( new Uint32Array( 10 ) ); +* // returns true +* +* bool = isUint32Array( [] ); +* // returns false +*/ + +// MODULES // + +var isUint32Array = require( './main.js' ); + + +// EXPORTS // + +module.exports = isUint32Array; diff --git a/is-uint32array/lib/main.js b/is-uint32array/lib/main.js new file mode 100644 index 00000000..86b09aaa --- /dev/null +++ b/is-uint32array/lib/main.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var hasUint32Array = ( typeof Uint32Array === 'function' ); // eslint-disable-line stdlib/require-globals + + +// MAIN // + +/** +* Tests if a value is a Uint32Array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a Uint32Array +* +* @example +* var bool = isUint32Array( new Uint32Array( 10 ) ); +* // returns true +* +* @example +* var bool = isUint32Array( [] ); +* // returns false +*/ +function isUint32Array( value ) { + return ( + ( hasUint32Array && value instanceof Uint32Array ) || // eslint-disable-line stdlib/require-globals + nativeClass( value ) === '[object Uint32Array]' + ); +} + + +// EXPORTS // + +module.exports = isUint32Array; diff --git a/is-uint32array/package.json b/is-uint32array/package.json new file mode 100644 index 00000000..9850b367 --- /dev/null +++ b/is-uint32array/package.json @@ -0,0 +1,84 @@ +{ + "name": "@stdlib/assert/is-uint32array", + "version": "0.0.0", + "description": "Test if a value is a Uint32Array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "uint32array", + "uint32", + "unsigned", + "integer", + "int", + "uint", + "doubleword", + "long", + "word", + "longword", + "typed", + "typed array", + "typed-array", + "array", + "is", + "isarray", + "istypedarray", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-uint32array/test/test.js b/is-uint32array/test/test.js new file mode 100644 index 00000000..f3a6c056 --- /dev/null +++ b/is-uint32array/test/test.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isUint32Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isUint32Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a Uint32Array', function test( t ) { + t.strictEqual( isUint32Array( new Uint32Array( 10 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a Uint32Array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + undefined, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint16Array( 10 ), + new Int16Array( 10 ), + new Uint8Array( 10 ), + new Int8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isUint32Array( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-uint8array/README.md b/is-uint8array/README.md new file mode 100644 index 00000000..039241b5 --- /dev/null +++ b/is-uint8array/README.md @@ -0,0 +1,116 @@ + + +# isUint8Array + +> Test if a value is a [Uint8Array][mdn-uint8array]. + +
+ +## Usage + +```javascript +var isUint8Array = require( '@stdlib/assert/is-uint8array' ); +``` + +#### isUint8Array( value ) + +Tests if a value is a [`Uint8Array`][mdn-uint8array]. + +```javascript +var Uint8Array = require( '@stdlib/array/uint8' ); + +var bool = isUint8Array( new Uint8Array( 10 ) ); +// returns true + +bool = isUint8Array( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isUint8Array = require( '@stdlib/assert/is-uint8array' ); + +var bool = isUint8Array( new Uint8Array( 10 ) ); +// returns true + +bool = isUint8Array( new Int8Array( 10 ) ); +// returns false + +bool = isUint8Array( new Uint8ClampedArray( 10 ) ); +// returns false + +bool = isUint8Array( new Int16Array( 10 ) ); +// returns false + +bool = isUint8Array( new Uint16Array( 10 ) ); +// returns false + +bool = isUint8Array( new Int32Array( 10 ) ); +// returns false + +bool = isUint8Array( new Uint32Array( 10 ) ); +// returns false + +bool = isUint8Array( new Float32Array( 10 ) ); +// returns false + +bool = isUint8Array( new Float64Array( 10 ) ); +// returns false + +bool = isUint8Array( new Array( 10 ) ); +// returns false + +bool = isUint8Array( {} ); +// returns false + +bool = isUint8Array( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-uint8array/benchmark/benchmark.js b/is-uint8array/benchmark/benchmark.js new file mode 100644 index 00000000..6e3ab413 --- /dev/null +++ b/is-uint8array/benchmark/benchmark.js @@ -0,0 +1,126 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isUint8Array = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUint8Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Uint8Array( 10 ), + new Uint8Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUint8Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUint8Array( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-uint8array/docs/repl.txt b/is-uint8array/docs/repl.txt new file mode 100644 index 00000000..d66f2a64 --- /dev/null +++ b/is-uint8array/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a Uint8Array. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a Uint8Array. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/uint8}}( 10 ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-uint8array/docs/types/index.d.ts b/is-uint8array/docs/types/index.d.ts new file mode 100644 index 00000000..9790f49d --- /dev/null +++ b/is-uint8array/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a Uint8Array. +* +* @param value - value to test +* @returns boolean indicating whether value is a Uint8Array +* +* @example +* var bool = isUint8Array( new Uint8Array( 10 ) ); +* // returns true +* +* @example +* var bool = isUint8Array( [] ); +* // returns false +*/ +declare function isUint8Array( value: any ): boolean; + + +// EXPORTS // + +export = isUint8Array; diff --git a/is-uint8array/docs/types/test.ts b/is-uint8array/docs/types/test.ts new file mode 100644 index 00000000..0533ca4b --- /dev/null +++ b/is-uint8array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isUint8Array = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isUint8Array( new Uint8Array( 4 ) ); // $ExpectType boolean + isUint8Array( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isUint8Array(); // $ExpectError + isUint8Array( new Uint8Array( 4 ), 123 ); // $ExpectError +} diff --git a/is-uint8array/examples/index.js b/is-uint8array/examples/index.js new file mode 100644 index 00000000..2b321487 --- /dev/null +++ b/is-uint8array/examples/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isUint8Array = require( './../lib' ); + +var bool = isUint8Array( new Uint8Array( 10 ) ); +console.log( bool ); +// => true + +bool = isUint8Array( new Int8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8Array( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8Array( new Int16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8Array( new Uint16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8Array( new Int32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8Array( new Uint32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8Array( new Float32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8Array( new Float64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8Array( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8Array( {} ); +console.log( bool ); +// => false + +bool = isUint8Array( null ); +console.log( bool ); +// => false diff --git a/is-uint8array/lib/index.js b/is-uint8array/lib/index.js new file mode 100644 index 00000000..2916e096 --- /dev/null +++ b/is-uint8array/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a Uint8Array. +* +* @module @stdlib/assert/is-uint8array +* +* @example +* var isUint8Array = require( '@stdlib/assert/is-uint8array' ); +* +* var bool = isUint8Array( new Uint8Array( 10 ) ); +* // returns true +* +* bool = isUint8Array( [] ); +* // returns false +*/ + +// MODULES // + +var isUint8Array = require( './main.js' ); + + +// EXPORTS // + +module.exports = isUint8Array; diff --git a/is-uint8array/lib/main.js b/is-uint8array/lib/main.js new file mode 100644 index 00000000..cc941ecc --- /dev/null +++ b/is-uint8array/lib/main.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var hasUint8Array = ( typeof Uint8Array === 'function' ); // eslint-disable-line stdlib/require-globals + + +// MAIN // + +/** +* Tests if a value is a Uint8Array. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a Uint8Array +* +* @example +* var bool = isUint8Array( new Uint8Array( 10 ) ); +* // returns true +* +* @example +* var bool = isUint8Array( [] ); +* // returns false +*/ +function isUint8Array( value ) { + return ( + ( hasUint8Array && value instanceof Uint8Array ) || // eslint-disable-line stdlib/require-globals + nativeClass( value ) === '[object Uint8Array]' + ); +} + + +// EXPORTS // + +module.exports = isUint8Array; diff --git a/is-uint8array/package.json b/is-uint8array/package.json new file mode 100644 index 00000000..70c973d0 --- /dev/null +++ b/is-uint8array/package.json @@ -0,0 +1,83 @@ +{ + "name": "@stdlib/assert/is-uint8array", + "version": "0.0.0", + "description": "Test if a value is a Uint8Array.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "uint8array", + "uint8", + "unsigned", + "integer", + "int", + "uint", + "byte", + "octet", + "buffer", + "typed", + "typed array", + "typed-array", + "array", + "is", + "isarray", + "istypedarray", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-uint8array/test/test.js b/is-uint8array/test/test.js new file mode 100644 index 00000000..572163e4 --- /dev/null +++ b/is-uint8array/test/test.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isUint8Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isUint8Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a Uint8Array', function test( t ) { + t.strictEqual( isUint8Array( new Uint8Array( 10 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a Uint8Array', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + undefined, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Uint32Array( 10 ), + new Int32Array( 10 ), + new Uint16Array( 10 ), + new Int16Array( 10 ), + new Int8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isUint8Array( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-uint8clampedarray/README.md b/is-uint8clampedarray/README.md new file mode 100644 index 00000000..963b7eb3 --- /dev/null +++ b/is-uint8clampedarray/README.md @@ -0,0 +1,116 @@ + + +# isUint8ClampedArray + +> Test if a value is a [Uint8ClampedArray][mdn-uint8clampedarray]. + +
+ +## Usage + +```javascript +var isUint8ClampedArray = require( '@stdlib/assert/is-uint8clampedarray' ); +``` + +#### isUint8ClampedArray( value ) + +Tests if a value is a [`Uint8ClampedArray`][mdn-uint8clampedarray]. + +```javascript +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); + +var bool = isUint8ClampedArray( new Uint8ClampedArray( 10 ) ); +// returns true + +bool = isUint8ClampedArray( [] ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isUint8ClampedArray = require( '@stdlib/assert/is-uint8clampedarray' ); + +var bool = isUint8ClampedArray( new Uint8ClampedArray( 10 ) ); +// returns true + +bool = isUint8ClampedArray( new Int8Array( 10 ) ); +// returns false + +bool = isUint8ClampedArray( new Uint8Array( 10 ) ); +// returns false + +bool = isUint8ClampedArray( new Int16Array( 10 ) ); +// returns false + +bool = isUint8ClampedArray( new Uint16Array( 10 ) ); +// returns false + +bool = isUint8ClampedArray( new Int32Array( 10 ) ); +// returns false + +bool = isUint8ClampedArray( new Uint32Array( 10 ) ); +// returns false + +bool = isUint8ClampedArray( new Float32Array( 10 ) ); +// returns false + +bool = isUint8ClampedArray( new Float64Array( 10 ) ); +// returns false + +bool = isUint8ClampedArray( new Array( 10 ) ); +// returns false + +bool = isUint8ClampedArray( {} ); +// returns false + +bool = isUint8ClampedArray( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-uint8clampedarray/benchmark/benchmark.js b/is-uint8clampedarray/benchmark/benchmark.js new file mode 100644 index 00000000..4748958e --- /dev/null +++ b/is-uint8clampedarray/benchmark/benchmark.js @@ -0,0 +1,126 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var isUint8ClampedArray = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUint8ClampedArray( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::true', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Uint8ClampedArray( 10 ), + new Uint8ClampedArray( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUint8ClampedArray( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + new Float64Array( 10 ), + new Float32Array( 10 ), + new Int32Array( 10 ), + new Uint32Array( 10 ), + new Int16Array( 10 ), + new Uint16Array( 10 ), + new Int8Array( 10 ), + new Uint8Array( 10 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUint8ClampedArray( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-uint8clampedarray/docs/repl.txt b/is-uint8clampedarray/docs/repl.txt new file mode 100644 index 00000000..4a03bfe2 --- /dev/null +++ b/is-uint8clampedarray/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a Uint8ClampedArray. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a Uint8ClampedArray. + + Examples + -------- + > var bool = {{alias}}( new {{alias:@stdlib/array/uint8c}}( 10 ) ) + true + > bool = {{alias}}( [] ) + false + + See Also + -------- + diff --git a/is-uint8clampedarray/docs/types/index.d.ts b/is-uint8clampedarray/docs/types/index.d.ts new file mode 100644 index 00000000..519648f0 --- /dev/null +++ b/is-uint8clampedarray/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a Uint8ClampedArray. +* +* @param value - value to test +* @returns boolean indicating whether value is a Uint8ClampedArray +* +* @example +* var bool = isUint8ClampedArray( new Uint8ClampedArray( 10 ) ); +* // returns true +* +* @example +* var bool = isUint8ClampedArray( [] ); +* // returns false +*/ +declare function isUint8ClampedArray( value: any ): boolean; + + +// EXPORTS // + +export = isUint8ClampedArray; diff --git a/is-uint8clampedarray/docs/types/test.ts b/is-uint8clampedarray/docs/types/test.ts new file mode 100644 index 00000000..3e53cbf0 --- /dev/null +++ b/is-uint8clampedarray/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isUint8ClampedArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isUint8ClampedArray( new Uint8ClampedArray( 4 ) ); // $ExpectType boolean + isUint8ClampedArray( [] ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isUint8ClampedArray(); // $ExpectError + isUint8ClampedArray( new Uint8ClampedArray( 4 ), 123 ); // $ExpectError +} diff --git a/is-uint8clampedarray/examples/index.js b/is-uint8clampedarray/examples/index.js new file mode 100644 index 00000000..01e19953 --- /dev/null +++ b/is-uint8clampedarray/examples/index.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isUint8ClampedArray = require( './../lib' ); + +var bool = isUint8ClampedArray( new Uint8ClampedArray( 10 ) ); +console.log( bool ); +// => true + +bool = isUint8ClampedArray( new Int8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8ClampedArray( new Uint8Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8ClampedArray( new Int16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8ClampedArray( new Uint16Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8ClampedArray( new Int32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8ClampedArray( new Uint32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8ClampedArray( new Float32Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8ClampedArray( new Float64Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8ClampedArray( new Array( 10 ) ); +console.log( bool ); +// => false + +bool = isUint8ClampedArray( {} ); +console.log( bool ); +// => false + +bool = isUint8ClampedArray( null ); +console.log( bool ); +// => false diff --git a/is-uint8clampedarray/lib/index.js b/is-uint8clampedarray/lib/index.js new file mode 100644 index 00000000..a572bd00 --- /dev/null +++ b/is-uint8clampedarray/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a Uint8ClampedArray. +* +* @module @stdlib/assert/is-uint8clampedarray +* +* @example +* var isUint8ClampedArray = require( '@stdlib/assert/is-uint8clampedarray' ); +* +* var bool = isUint8ClampedArray( new Uint8ClampedArray( 10 ) ); +* // returns true +* +* bool = isUint8ClampedArray( [] ); +* // returns false +*/ + +// MODULES // + +var isUint8ClampedArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isUint8ClampedArray; diff --git a/is-uint8clampedarray/lib/main.js b/is-uint8clampedarray/lib/main.js new file mode 100644 index 00000000..b670e485 --- /dev/null +++ b/is-uint8clampedarray/lib/main.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var nativeClass = require( '@stdlib/utils/native-class' ); + + +// VARIABLES // + +var hasUint8ClampedArray = ( typeof Uint8ClampedArray === 'function' ); // eslint-disable-line stdlib/require-globals + + +// MAIN // + +/** +* Tests if a value is a Uint8ClampedArray. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is a Uint8ClampedArray +* +* @example +* var bool = isUint8ClampedArray( new Uint8ClampedArray( 10 ) ); +* // returns true +* +* @example +* var bool = isUint8ClampedArray( [] ); +* // returns false +*/ +function isUint8ClampedArray( value ) { + return ( + ( hasUint8ClampedArray && value instanceof Uint8ClampedArray ) || // eslint-disable-line stdlib/require-globals + nativeClass( value ) === '[object Uint8ClampedArray]' + ); +} + + +// EXPORTS // + +module.exports = isUint8ClampedArray; diff --git a/is-uint8clampedarray/package.json b/is-uint8clampedarray/package.json new file mode 100644 index 00000000..c2a2c0d2 --- /dev/null +++ b/is-uint8clampedarray/package.json @@ -0,0 +1,84 @@ +{ + "name": "@stdlib/assert/is-uint8clampedarray", + "version": "0.0.0", + "description": "Test if a value is a Uint8ClampedArray.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "uint8array", + "uint8", + "clamped", + "unsigned", + "integer", + "int", + "uint", + "byte", + "octet", + "buffer", + "typed", + "typed array", + "typed-array", + "array", + "is", + "isarray", + "istypedarray", + "type", + "check", + "validate", + "valid", + "isvalid", + "test" + ] +} diff --git a/is-uint8clampedarray/test/test.js b/is-uint8clampedarray/test/test.js new file mode 100644 index 00000000..a4b53e52 --- /dev/null +++ b/is-uint8clampedarray/test/test.js @@ -0,0 +1,78 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var isUint8ClampedArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isUint8ClampedArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a Uint8ClampedArray', function test( t ) { + t.strictEqual( isUint8ClampedArray( new Uint8ClampedArray( 10 ) ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a Uint8ClampedArray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + undefined, + [], + {}, + function noop() {}, + new Array( 10 ), + new Float64Array( 10 ), + new Float32Array( 10 ), + new Uint32Array( 10 ), + new Int32Array( 10 ), + new Uint16Array( 10 ), + new Int16Array( 10 ), + new Uint8Array( 10 ), + new Int8Array( 10 ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isUint8ClampedArray( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-unc-path/README.md b/is-unc-path/README.md new file mode 100644 index 00000000..06fe82f3 --- /dev/null +++ b/is-unc-path/README.md @@ -0,0 +1,223 @@ + + +# isUNCPath + +> Test if a value is a [UNC][unc] path. + +
+ +
+ + + +
+ +## Usage + +```javascript +var isUNCPath = require( '@stdlib/assert/is-unc-path' ); +``` + +#### isUNCPath( value ) + +Tests if a `value` is a [UNC][unc] path. + +```javascript +var bool = isUNCPath( '\\\\server\\share\\foo\\bar\\baz' ); +// returns true + +bool = isUNCPath( '/foo/bar/baz' ); +// returns false +``` + +
+ + + +
+ +## Examples + + + +```javascript +var isUNCPath = require( '@stdlib/assert/is-unc-path' ); + +var bool; +var path; + +path = '\\\\server\\share\\foo\\bar\\baz:a:b'; +bool = isUNCPath( path ); +// returns true + +path = '\\\\server\\share\\foo\\bar\\baz::b'; +bool = isUNCPath( path ); +// returns true + +path = '\\\\server\\share\\foo\\bar\\baz:a'; +bool = isUNCPath( path ); +// returns true + +path = '\\\\server\\share\\foo\\bar\\baz'; +bool = isUNCPath( path ); +// returns true + +path = '\\\\server\\share\\foo\\bar'; +bool = isUNCPath( path ); +// returns true + +path = '\\\\server\\share\\foo'; +bool = isUNCPath( path ); +// returns true + +path = '\\\\server\\share'; +bool = isUNCPath( path ); +// returns true + +path = '\\\\server\\\\share'; +bool = isUNCPath( path ); +// returns false + +path = '\\\\\\\\server\\share'; +bool = isUNCPath( path ); +// returns false + +path = 'beep boop \\\\server\\share'; +bool = isUNCPath( path ); +// returns false + +path = '\\\\server'; +bool = isUNCPath( path ); +// returns false + +path = '\\'; +bool = isUNCPath( path ); +// returns false + +path = ''; +bool = isUNCPath( path ); +// returns false + +path = '\\\\server\\share\\'; +bool = isUNCPath( path ); +// returns false + +path = '\\\\server\\share\\foo\\bar\\baz:'; +bool = isUNCPath( path ); +// returns false + +path = '\\\\server\\share\\foo\\bar\\baz:a:'; +bool = isUNCPath( path ); +// returns false + +path = '\\\\server\\share\\foo\\bar\\baz::'; +bool = isUNCPath( path ); +// returns false + +path = '\\\\server\\share\\foo\\bar\\baz:a:b:c'; +bool = isUNCPath( path ); +// returns false + +path = '\\\\server\\share\\foo\\bar\\'; +bool = isUNCPath( path ); +// returns false + +path = '//server/share'; +bool = isUNCPath( path ); +// returns false + +path = '/foo/bar'; +bool = isUNCPath( path ); +// returns false + +path = 'foo/bar'; +bool = isUNCPath( path ); +// returns false + +path = './foo/bar'; +bool = isUNCPath( path ); +// returns false + +path = '/foo/../bar'; +bool = isUNCPath( path ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-unc-path [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-unc-path '\\\\server\\share\\foo' +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n '\\\\server\\share\\foo' | is-unc-path +true +``` + +
+ + + +
+ + + + + + diff --git a/is-unc-path/benchmark/benchmark.js b/is-unc-path/benchmark/benchmark.js new file mode 100644 index 00000000..105b76e2 --- /dev/null +++ b/is-unc-path/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isUNCPath = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = '\\\\server\\share\\foo\\bar\\baz'+fromCodePoint( 97 + (i%26) ); + bool = isUNCPath( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-unc-path/bin/cli b/is-unc-path/bin/cli new file mode 100644 index 00000000..615b3eed --- /dev/null +++ b/is-unc-path/bin/cli @@ -0,0 +1,93 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var trim = require( '@stdlib/string/trim' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isUNCPath = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isUNCPath( trim( args[ 0 ] ) ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isUNCPath( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-unc-path/docs/repl.txt b/is-unc-path/docs/repl.txt new file mode 100644 index 00000000..b18e387e --- /dev/null +++ b/is-unc-path/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is a UNC path. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a UNC path. + + Examples + -------- + > var bool = {{alias}}( '\\\\server\\share\\foo\\bar\\baz' ) + true + > bool = {{alias}}( '/foo/bar/baz' ) + false + + See Also + -------- + diff --git a/is-unc-path/docs/types/index.d.ts b/is-unc-path/docs/types/index.d.ts new file mode 100644 index 00000000..29608190 --- /dev/null +++ b/is-unc-path/docs/types/index.d.ts @@ -0,0 +1,40 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a UNC path. +* +* @param value - value to test +* @returns boolean indicating if a value is a UNC path +* +* @example +* var bool = isUNCPath( '\\\\server\\share\\foo\\bar\\baz' ); +* // returns true +* +* @example +* var bool = isUNCPath( '/foo/bar/baz' ); +* // returns false +*/ +declare function isUNCPath( value: any ): boolean; + + +// EXPORTS // + +export = isUNCPath; diff --git a/is-unc-path/docs/types/test.ts b/is-unc-path/docs/types/test.ts new file mode 100644 index 00000000..56214a1c --- /dev/null +++ b/is-unc-path/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isUNCPath = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isUNCPath( '\\\\server\\share\\foo\\bar\\baz' ); // $ExpectType boolean + isUNCPath( '/foo/bar/baz' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isUNCPath(); // $ExpectError + isUNCPath( '\\\\server\\share\\foo\\bar\\baz', 123 ); // $ExpectError +} diff --git a/is-unc-path/docs/usage.txt b/is-unc-path/docs/usage.txt new file mode 100644 index 00000000..43dfb0be --- /dev/null +++ b/is-unc-path/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-unc-path [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-unc-path/etc/cli_opts.json b/is-unc-path/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-unc-path/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-unc-path/examples/index.js b/is-unc-path/examples/index.js new file mode 100644 index 00000000..6d4c70e0 --- /dev/null +++ b/is-unc-path/examples/index.js @@ -0,0 +1,120 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isUNCPath = require( './../lib' ); + +var bool; +var path; + +path = '\\\\server\\share\\foo\\bar\\baz:a:b'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server\\share\\foo\\bar\\baz::b'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server\\share\\foo\\bar\\baz:a'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server\\share\\foo\\bar\\baz'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server\\share\\foo\\bar'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server\\share\\foo'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server\\share'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server\\\\share'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\\\\\server\\share'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = 'beep boop \\\\server\\share'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = ''; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server\\share\\'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server\\share\\foo\\bar\\baz:'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server\\share\\foo\\bar\\baz:a:'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server\\share\\foo\\bar\\baz::'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server\\share\\foo\\bar\\baz:a:b:c'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '\\\\server\\share\\foo\\bar\\'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '//server/share'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '/foo/bar'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = 'foo/bar'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = './foo/bar'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); + +path = '/foo/../bar'; +bool = isUNCPath( path ); +console.log( '%s => %s', path, bool ); diff --git a/is-unc-path/lib/index.js b/is-unc-path/lib/index.js new file mode 100644 index 00000000..44a74ccc --- /dev/null +++ b/is-unc-path/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a UNC path. +* +* @module @stdlib/assert/is-unc-path +* +* @example +* var isUNCPath = require( '@stdlib/assert/is-unc-path' ); +* +* var bool = isUNCPath( '\\\\server\\share\\foo\\bar\\baz' ); +* // returns true +* +* bool = isUNCPath( '/foo/bar/baz' ); +* // returns false +*/ + +// MODULES // + +var isUNCPath = require( './main.js' ); + + +// EXPORTS // + +module.exports = isUNCPath; diff --git a/is-unc-path/lib/main.js b/is-unc-path/lib/main.js new file mode 100644 index 00000000..99c024a0 --- /dev/null +++ b/is-unc-path/lib/main.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var RE_UNC_PATH = require( '@stdlib/regexp/unc-path' ).REGEXP; + + +// MAIN // + +/** +* Tests if a value is a UNC path. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a UNC path +* +* @example +* var bool = isUNCPath( '\\\\server\\share\\foo\\bar\\baz' ); +* // returns true +* +* @example +* var bool = isUNCPath( '/foo/bar/baz' ); +* // returns false +*/ +function isUNCPath( value ) { + return ( + isString( value ) && + RE_UNC_PATH.test( value ) + ); +} + + +// EXPORTS // + +module.exports = isUNCPath; diff --git a/is-unc-path/package.json b/is-unc-path/package.json new file mode 100644 index 00000000..a5f23795 --- /dev/null +++ b/is-unc-path/package.json @@ -0,0 +1,90 @@ +{ + "name": "@stdlib/assert/is-unc-path", + "version": "0.0.0", + "description": "Test if a value is a UNC path.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-unc-path": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "is-unc", + "is-path", + "unc", + "universal", + "unified", + "uniform", + "naming", + "convention", + "path", + "network", + "device", + "filesystem", + "file", + "filepath", + "filename", + "dir", + "directory", + "dirpath", + "check", + "validate", + "valid", + "isvalid", + "is-valid", + "test" + ] +} diff --git a/is-unc-path/test/fixtures/stdin_error.js.txt b/is-unc-path/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-unc-path/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-unc-path/test/test.cli.js b/is-unc-path/test/test.cli.js new file mode 100644 index 00000000..5ffa82f6 --- /dev/null +++ b/is-unc-path/test/test.cli.js @@ -0,0 +1,220 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument is a UNC path', opts, function test( t ) { + var str = '\\\\\\\\\\\\\\\\server\\\\\\\\share'; + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \''+str+'\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "./index.js\n\\\\\\\\\\\\\\\\server\\\\\\\\share"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'false\ntrue\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-unc-path/test/test.js b/is-unc-path/test/test.js new file mode 100644 index 00000000..981c81e1 --- /dev/null +++ b/is-unc-path/test/test.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isUNCPath = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isUNCPath, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a UNC path', function test( t ) { + var values; + var bool; + var i; + + values = [ + '\\\\server\\share\\foo\\bar\\baz:a:b', + '\\\\server\\share\\foo\\bar\\baz::b', + '\\\\server\\share\\foo\\bar\\baz', + '\\\\server\\share\\foo\\bar', + '\\\\server\\share\\foo', + '\\\\server\\share' + ]; + + for ( i = 0; i < values.length; i++ ) { + bool = isUNCPath( values[ i ] ); + t.equal( bool, true, 'returns true when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a UNC path', function test( t ) { + var values; + var bool; + var i; + + values = [ + '\\\\server\\\\share', + '\\\\\\\\server\\share', + '\\', + '', + '\\\\server\\share\\', + 'beep boop \\\\server\\share', + '\\\\server\\share\\foo\\bar\\baz:', + '\\\\server\\share\\foo\\bar\\baz:a:', + '\\\\server\\share\\foo\\bar\\baz::', + '\\\\server\\share\\foo\\bar\\baz:a:b:c', + '\\\\server\\share\\foo\\bar\\', + '//server/share', + '/foo/bar', + 'foo/bar', + './foo/bar', + '/foo/../bar', + '', + 5, + NaN, + null, + undefined, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + bool = isUNCPath( values[ i ] ); + t.equal( bool, false, 'returns false when provided '+values[ i ] ); + } + t.end(); +}); diff --git a/is-undefined-or-null/README.md b/is-undefined-or-null/README.md new file mode 100644 index 00000000..cbdbf48e --- /dev/null +++ b/is-undefined-or-null/README.md @@ -0,0 +1,104 @@ + + +# isUndefinedOrNull + +> Test if a value is undefined or null. + +
+ +## Usage + +```javascript +var isUndefinedOrNull = require( '@stdlib/assert/is-undefined-or-null' ); +``` + +#### isUndefinedOrNull( value ) + +Tests if a `value` is `undefined` or `null`. + + + +```javascript +var bool = isUndefinedOrNull( undefined ); +// returns true + +bool = isUndefinedOrNull( null ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isUndefinedOrNull = require( '@stdlib/assert/is-undefined-or-null' ); + +var bool; +var x; + +bool = isUndefinedOrNull( x ); +// returns true + +bool = isUndefinedOrNull( undefined ); +// returns true + +bool = isUndefinedOrNull( void 0 ); +// returns true + +bool = isUndefinedOrNull( null ); +// returns true + +bool = isUndefinedOrNull( 'beep' ); +// returns false + +bool = isUndefinedOrNull( 5 ); +// returns false + +bool = isUndefinedOrNull( true ); +// returns false + +bool = isUndefinedOrNull( {} ); +// returns false + +bool = isUndefinedOrNull( [] ); +// returns false + +bool = isUndefinedOrNull( function foo() {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-undefined-or-null/benchmark/benchmark.js b/is-undefined-or-null/benchmark/benchmark.js new file mode 100644 index 00000000..f7aae217 --- /dev/null +++ b/is-undefined-or-null/benchmark/benchmark.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isUndefinedOrNull = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUndefinedOrNull( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-undefined-or-null/docs/repl.txt b/is-undefined-or-null/docs/repl.txt new file mode 100644 index 00000000..10178a6f --- /dev/null +++ b/is-undefined-or-null/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( value ) + Tests if a value is undefined or null. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is undefined or null. + + Examples + -------- + > var bool = {{alias}}( void 0 ) + true + > bool = {{alias}}( null ) + true + > bool = {{alias}}( false ) + false + + See Also + -------- + diff --git a/is-undefined-or-null/docs/types/index.d.ts b/is-undefined-or-null/docs/types/index.d.ts new file mode 100644 index 00000000..39116aac --- /dev/null +++ b/is-undefined-or-null/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is `undefined` or `null`. +* +* @param value - value to test +* @returns boolean indicating whether value is undefined +* +* @example +* var bool = isUndefinedOrNull( undefined ); +* // returns true +* +* bool = isUndefinedOrNull( null ); +* // returns true +* +* bool = isUndefinedOrNull( false ); +* // returns false +*/ +declare function isUndefinedOrNull( value: any ): boolean; + + +// EXPORTS // + +export = isUndefinedOrNull; diff --git a/is-undefined-or-null/docs/types/test.ts b/is-undefined-or-null/docs/types/test.ts new file mode 100644 index 00000000..9852bf65 --- /dev/null +++ b/is-undefined-or-null/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isUndefinedOrNull = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isUndefinedOrNull( null ); // $ExpectType boolean + isUndefinedOrNull( NaN ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isUndefinedOrNull(); // $ExpectError + isUndefinedOrNull( undefined, 123 ); // $ExpectError +} diff --git a/is-undefined-or-null/examples/index.js b/is-undefined-or-null/examples/index.js new file mode 100644 index 00000000..de85f639 --- /dev/null +++ b/is-undefined-or-null/examples/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-undefined, no-restricted-syntax, no-empty-function */ + +'use strict'; + +var isUndefinedOrNull = require( './../lib' ); + +var x; + +console.log( isUndefinedOrNull( x ) ); +// => true + +console.log( isUndefinedOrNull( undefined ) ); +// => true + +console.log( isUndefinedOrNull( void 0 ) ); +// => true + +console.log( isUndefinedOrNull( null ) ); +// => true + +console.log( isUndefinedOrNull( 'beep' ) ); +// => false + +console.log( isUndefinedOrNull( 5 ) ); +// => false + +console.log( isUndefinedOrNull( true ) ); +// => false + +console.log( isUndefinedOrNull( {} ) ); +// => false + +console.log( isUndefinedOrNull( [] ) ); +// => false + +console.log( isUndefinedOrNull( function foo() {} ) ); +// => false diff --git a/is-undefined-or-null/lib/index.js b/is-undefined-or-null/lib/index.js new file mode 100644 index 00000000..cf7b177a --- /dev/null +++ b/is-undefined-or-null/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is `undefined` or `null`. +* +* @module @stdlib/assert/is-undefined-or-null +* +* @example +* var isUndefinedOrNull = require( '@stdlib/assert/is-undefined-or-null' ); +* +* var bool = isUndefinedOrNull( void 0 ); +* // returns true +* +* bool = isUndefinedOrNull( null ); +* // returns true +* +* bool = isUndefinedOrNull( false ); +* // returns false +*/ + +// MODULES // + +var isUndefinedOrNull = require( './main.js' ); + + +// EXPORTS // + +module.exports = isUndefinedOrNull; diff --git a/is-undefined-or-null/lib/main.js b/is-undefined-or-null/lib/main.js new file mode 100644 index 00000000..92590c78 --- /dev/null +++ b/is-undefined-or-null/lib/main.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Tests if a value is `undefined` or `null`. +* +* ## Notes +* +* - In older browsers, `undefined` is a global which can be overridden. `void`, however, is an operator which **cannot** be overridden. Consequently, better to use `void` to check for `undefined`. See [Stack Overflow][1]. +* +* [1]: http://stackoverflow.com/a/19369078/2225624 +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is undefined +* +* @example +* var bool = isUndefinedOrNull( undefined ); +* // returns true +* +* bool = isUndefinedOrNull( null ); +* // returns true +* +* bool = isUndefinedOrNull( false ); +* // returns false +*/ +function isUndefinedOrNull( value ) { + return ( value === void 0 || value === null ); +} + + +// EXPORTS // + +module.exports = isUndefinedOrNull; diff --git a/is-undefined-or-null/package.json b/is-undefined-or-null/package.json new file mode 100644 index 00000000..139fbb37 --- /dev/null +++ b/is-undefined-or-null/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/assert/is-undefined-or-null", + "version": "0.0.0", + "description": "Test if a value is undefined or null.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "undefined", + "void", + "null", + "is", + "isundefined", + "isnull", + "isvoid", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-undefined-or-null/test/test.js b/is-undefined-or-null/test/test.js new file mode 100644 index 00000000..2ddfd416 --- /dev/null +++ b/is-undefined-or-null/test/test.js @@ -0,0 +1,74 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isUndefinedOrNull = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isUndefinedOrNull, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided `undefined` or `null`', function test( t ) { + var values; + var bool; + var foo; + var i; + + values = [ + foo, + undefined, + void 0, + null + ]; + + for ( i = 0; i < values.length; i++ ) { + bool = isUndefinedOrNull( values[ i ] ); + t.equal( bool, true, 'returns true when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided `undefined` or `null`', function test( t ) { + var values; + var i; + + values = [ + '', + 0, + NaN, + false, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isUndefinedOrNull( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-undefined/README.md b/is-undefined/README.md new file mode 100644 index 00000000..d4fa292f --- /dev/null +++ b/is-undefined/README.md @@ -0,0 +1,101 @@ + + +# isUndefined + +> Test if a value is undefined. + +
+ +## Usage + +```javascript +var isUndefined = require( '@stdlib/assert/is-undefined' ); +``` + +#### isUndefined( value ) + +Tests if a `value` is `undefined`. + + + +```javascript +var bool = isUndefined( undefined ); +// returns true +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isUndefined = require( '@stdlib/assert/is-undefined' ); + +var bool; +var x; + +bool = isUndefined( x ); +// returns true + +bool = isUndefined( undefined ); +// returns true + +bool = isUndefined( void 0 ); +// returns true + +bool = isUndefined( 'beep' ); +// returns false + +bool = isUndefined( 5 ); +// returns false + +bool = isUndefined( null ); +// returns false + +bool = isUndefined( true ); +// returns false + +bool = isUndefined( {} ); +// returns false + +bool = isUndefined( [] ); +// returns false + +bool = isUndefined( function foo() {} ); +// returns false +``` + +
+ + + + + + diff --git a/is-undefined/benchmark/benchmark.js b/is-undefined/benchmark/benchmark.js new file mode 100644 index 00000000..a117a55f --- /dev/null +++ b/is-undefined/benchmark/benchmark.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isUndefined = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUndefined( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-undefined/docs/repl.txt b/is-undefined/docs/repl.txt new file mode 100644 index 00000000..ad69b0c4 --- /dev/null +++ b/is-undefined/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is undefined. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is undefined. + + Examples + -------- + > var bool = {{alias}}( void 0 ) + true + > bool = {{alias}}( null ) + false + + See Also + -------- + diff --git a/is-undefined/docs/types/index.d.ts b/is-undefined/docs/types/index.d.ts new file mode 100644 index 00000000..121415bd --- /dev/null +++ b/is-undefined/docs/types/index.d.ts @@ -0,0 +1,39 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is `undefined`. +* +* @param value - value to test +* @returns boolean indicating whether value is undefined +* +* @example +* var bool = isUndefined( undefined ); +* // returns true +* +* bool = isUndefined( null ); +* // returns false +*/ +declare function isUndefined( value: any ): boolean; + + +// EXPORTS // + +export = isUndefined; diff --git a/is-undefined/docs/types/test.ts b/is-undefined/docs/types/test.ts new file mode 100644 index 00000000..366dcd99 --- /dev/null +++ b/is-undefined/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isUndefined = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isUndefined( null ); // $ExpectType boolean + isUndefined( NaN ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isUndefined(); // $ExpectError + isUndefined( undefined, 123 ); // $ExpectError +} diff --git a/is-undefined/examples/index.js b/is-undefined/examples/index.js new file mode 100644 index 00000000..3c1a99e0 --- /dev/null +++ b/is-undefined/examples/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-undefined, no-restricted-syntax, no-empty-function */ + +'use strict'; + +var isUndefined = require( './../lib' ); + +var x; + +console.log( isUndefined( x ) ); +// => true + +console.log( isUndefined( undefined ) ); +// => true + +console.log( isUndefined( void 0 ) ); +// => true + +console.log( isUndefined( 'beep' ) ); +// => false + +console.log( isUndefined( 5 ) ); +// => false + +console.log( isUndefined( null ) ); +// => false + +console.log( isUndefined( true ) ); +// => false + +console.log( isUndefined( {} ) ); +// => false + +console.log( isUndefined( [] ) ); +// => false + +console.log( isUndefined( function foo() {} ) ); +// => false diff --git a/is-undefined/lib/index.js b/is-undefined/lib/index.js new file mode 100644 index 00000000..d861778a --- /dev/null +++ b/is-undefined/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is `undefined`. +* +* @module @stdlib/assert/is-undefined +* +* @example +* var isUndefined = require( '@stdlib/assert/is-undefined' ); +* +* var bool = isUndefined( void 0 ); +* // returns true +* +* bool = isUndefined( null ); +* // returns false +*/ + +// MODULES // + +var isUndefined = require( './main.js' ); + + +// EXPORTS // + +module.exports = isUndefined; diff --git a/is-undefined/lib/main.js b/is-undefined/lib/main.js new file mode 100644 index 00000000..a5b9011f --- /dev/null +++ b/is-undefined/lib/main.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Tests if a value is `undefined`. +* +* ## Notes +* +* - In older browsers, `undefined` is a global which can be overridden. `void`, however, is an operator which **cannot** be overridden. Consequently, better to use `void` to check for `undefined`. See [Stack Overflow][1]. +* +* [1]: http://stackoverflow.com/a/19369078/2225624 +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is undefined +* +* @example +* var bool = isUndefined( undefined ); +* // returns true +* +* bool = isUndefined( null ); +* // returns false +*/ +function isUndefined( value ) { + return value === void 0; +} + + +// EXPORTS // + +module.exports = isUndefined; diff --git a/is-undefined/package.json b/is-undefined/package.json new file mode 100644 index 00000000..d3d81c8b --- /dev/null +++ b/is-undefined/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-undefined", + "version": "0.0.0", + "description": "Test if a value is undefined.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "undefined", + "void", + "is", + "isundefined", + "isvoid", + "type", + "check", + "valid", + "validate", + "test" + ] +} diff --git a/is-undefined/test/test.js b/is-undefined/test/test.js new file mode 100644 index 00000000..a75a0252 --- /dev/null +++ b/is-undefined/test/test.js @@ -0,0 +1,74 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isUndefined = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isUndefined, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided `undefined`', function test( t ) { + var values; + var bool; + var foo; + var i; + + values = [ + foo, + undefined, + void 0 + ]; + + for ( i = 0; i < values.length; i++ ) { + bool = isUndefined( values[ i ] ); + t.equal( bool, true, 'returns true when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided `undefined`', function test( t ) { + var values; + var i; + + values = [ + '', + 0, + NaN, + null, + false, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isUndefined( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-unity-probability-array/README.md b/is-unity-probability-array/README.md new file mode 100644 index 00000000..87620590 --- /dev/null +++ b/is-unity-probability-array/README.md @@ -0,0 +1,130 @@ + + +# isUnityProbabilityArray + +> Test if a value is an array of probabilities that sum to one. + +
+ +## Usage + +```javascript +var isUnityProbabilityArray = require( '@stdlib/assert/is-unity-probability-array' ); +``` + +#### isUnityProbabilityArray( value ) + +Tests if a `value` is an array of probabilities that sum to one. + +```javascript +var Uint8Array = require( '@stdlib/array/uint8' ); + +var bool = isUnityProbabilityArray( [ 0.25, 0.5, 0.25 ] ); +// returns true + +bool = isUnityProbabilityArray( new Uint8Array( [ 0, 1 ] ) ); +// returns true + +bool = isUnityProbabilityArray( [ 3.14, 0.0 ] ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Summation of finite-precision floating-point numbers often has numerical error. For example, + + ```javascript + var arr = [ 0.1, 0.2, 0.1, 0.1, 0.2, 0.2, 0.1 ]; // => 1.0 + var sum = 0.0; + var i; + for ( i = 0; i < arr.length; i++ ) { + sum += arr[ i ]; + } + console.log( sum ); + // => 0.9999999999999999 + ``` + + To account for numerical error, the function tests if array elements sum to approximately one; specifically, + + ```text + 1.0 - sqrt(eps) <= sum(A) <= 1.0 + sqrt(eps) + ``` + + where `eps` is [double-precision floating-point][ieee754] epsilon (`~2.22e-16`) and `sqrt(eps) ~ 1.49e-8`. The above comparison ensures equality for approximately half the significand bits. + +
+ + + +
+ +## Examples + + + +```javascript +var Uint8Array = require( '@stdlib/array/uint8' ); +var isUnityProbabilityArray = require( '@stdlib/assert/is-unity-probability-array' ); + +var arr = [ 0.0, 1.0 ]; +var bool = isUnityProbabilityArray( arr ); +// returns true + +arr = [ 0.5, 0.25, 0.25 ]; +bool = isUnityProbabilityArray( arr ); +// returns true + +arr = new Uint8Array( [ 0, 0, 1, 0 ] ); +bool = isUnityProbabilityArray( arr ); +// returns true + +arr = [ 0.4, 0.4, 0.4 ]; +bool = isUnityProbabilityArray( arr ); +// returns false + +arr = [ 3.14, -1.0 ]; +bool = isUnityProbabilityArray( arr ); +// returns false + +bool = isUnityProbabilityArray( [] ); +// returns false + +bool = isUnityProbabilityArray( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-unity-probability-array/benchmark/benchmark.js b/is-unity-probability-array/benchmark/benchmark.js new file mode 100644 index 00000000..5defd72b --- /dev/null +++ b/is-unity-probability-array/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isUnityProbabilityArray = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( 1.0/len ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + // Note: we are testing the worst case scenario where a function must scan the entire array before finding a failing value. + x[ len-1 ] = i * 3.14; + bool = isUnityProbabilityArray( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/is-unity-probability-array/docs/repl.txt b/is-unity-probability-array/docs/repl.txt new file mode 100644 index 00000000..a2414bee --- /dev/null +++ b/is-unity-probability-array/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( value ) + Tests if a value is an array of probabilities that sum to one. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an array of probabilities that sum + to one. + + Examples + -------- + > var bool = {{alias}}( [ 0.25, 0.5, 0.25 ] ) + true + > bool = {{alias}}( new {{alias:@stdlib/array/uint8}}( [ 0, 1 ] ) ) + true + > bool = {{alias}}( [ 0.4, 0.4, 0.4 ] ) + false + > bool = {{alias}}( [ 3.14, 0.0 ] ) + false + + See Also + -------- + diff --git a/is-unity-probability-array/docs/types/index.d.ts b/is-unity-probability-array/docs/types/index.d.ts new file mode 100644 index 00000000..f77442a7 --- /dev/null +++ b/is-unity-probability-array/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an array of probabilities that sum to one. +* +* @param v - value to test +* @returns boolean indicating if a value is a probability array +* +* @example +* var bool = isUnityProbabilityArray( [ 0.25, 0.5, 0.25 ] ); +* // returns true +* +* @example +* var bool = isUnityProbabilityArray( new Uint8Array( [ 0, 1 ] ) ); +* // returns true +* +* @example +* var bool = isUnityProbabilityArray( [ 0.4, 0.4, 0.4 ] ); +* // returns false +* +* @example +* var bool = isUnityProbabilityArray( [ 3.14, 0.0 ] ); +* // returns false +*/ +declare function isUnityProbabilityArray( v: any ): boolean; + + +// EXPORTS // + +export = isUnityProbabilityArray; diff --git a/is-unity-probability-array/docs/types/test.ts b/is-unity-probability-array/docs/types/test.ts new file mode 100644 index 00000000..4e48f3e9 --- /dev/null +++ b/is-unity-probability-array/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isUnityProbabilityArray = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isUnityProbabilityArray( [ 0.25, 0.5, 0.25 ] ); // $ExpectType boolean + isUnityProbabilityArray( [ 2, 3 ] ); // $ExpectType boolean +} + +// The function does not compile if provided an unsupported number of arguments... +{ + isUnityProbabilityArray(); // $ExpectError + isUnityProbabilityArray( [ 0.25, 0.5, 0.25 ], 123 ); // $ExpectError +} diff --git a/is-unity-probability-array/examples/index.js b/is-unity-probability-array/examples/index.js new file mode 100644 index 00000000..cf296ad6 --- /dev/null +++ b/is-unity-probability-array/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Uint8Array = require( '@stdlib/array/uint8' ); +var isUnityProbabilityArray = require( './../lib' ); + +var arr = [ 0.0, 1.0 ]; +console.log( isUnityProbabilityArray( arr ) ); +// => true + +arr = [ 0.5, 0.25, 0.25 ]; +console.log( isUnityProbabilityArray( arr ) ); +// => true + +arr = new Uint8Array( [ 0, 0, 1, 0 ] ); +console.log( isUnityProbabilityArray( arr ) ); +// => true + +arr = [ 0.4, 0.4, 0.4 ]; +console.log( isUnityProbabilityArray( arr ) ); +// => false + +arr = [ 3.14, -1.0 ]; +console.log( isUnityProbabilityArray( arr ) ); +// => false + +console.log( isUnityProbabilityArray( [] ) ); +// => false + +console.log( isUnityProbabilityArray( null ) ); +// => false diff --git a/is-unity-probability-array/lib/index.js b/is-unity-probability-array/lib/index.js new file mode 100644 index 00000000..62734d96 --- /dev/null +++ b/is-unity-probability-array/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an array of probabilities that sum to one. +* +* @module @stdlib/assert/is-unity-probability-array +* +* @example +* var isUnityProbabilityArray = require( '@stdlib/assert/is-unity-probability-array' ); +* +* var bool = isUnityProbabilityArray( [ 0.25, 0.5, 0.25 ] ); +* // returns true +* +* bool = isUnityProbabilityArray( new Uint8Array( [ 0, 1 ] ) ); +* // returns true +* +* bool = isUnityProbabilityArray( [ 0.4, 0.4, 0.4 ] ); +* // returns false +* +* bool = isUnityProbabilityArray( [ 3.14, 0.0 ] ); +* // returns false +*/ + +// MODULES // + +var isUnityProbabilityArray = require( './main.js' ); + + +// EXPORTS // + +module.exports = isUnityProbabilityArray; diff --git a/is-unity-probability-array/lib/main.js b/is-unity-probability-array/lib/main.js new file mode 100644 index 00000000..83ef5053 --- /dev/null +++ b/is-unity-probability-array/lib/main.js @@ -0,0 +1,90 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isTypedArray = require( '@stdlib/assert/is-typed-array' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var absdiff = require( '@stdlib/math/base/utils/absolute-difference' ); +var FLOAT64_SQRT_EPS = require( '@stdlib/constants/math/float64-sqrt-eps' ); + + +// MAIN // + +/** +* Tests if a value is an array of probabilities that sum to one. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a probability array +* +* @example +* var bool = isUnityProbabilityArray( [ 0.25, 0.5, 0.25 ] ); +* // returns true +* +* @example +* var bool = isUnityProbabilityArray( new Uint8Array( [ 0, 1 ] ) ); +* // returns true +* +* @example +* var bool = isUnityProbabilityArray( [ 0.4, 0.4, 0.4 ] ); +* // returns false +* +* @example +* var bool = isUnityProbabilityArray( [ 3.14, 0.0 ] ); +* // returns false +*/ +function isUnityProbabilityArray( v ) { + var sum; + var i; + if ( isArray( v ) ) { + sum = 0.0; + for ( i = 0; i < v.length; i++ ) { + if ( + !isNumber( v[ i ] ) || + v[ i ] > 1.0 || + v[ i ] < 0.0 + ) { + return false; + } + sum += v[ i ]; + } + return ( absdiff( sum, 1.0 ) <= FLOAT64_SQRT_EPS ); + } + if ( isTypedArray( v ) ) { + sum = 0.0; + for ( i = 0; i < v.length; i++ ) { + if ( + v[ i ] > 1.0 || + v[ i ] < 0.0 + ) { + return false; + } + sum += v[ i ]; + } + return ( absdiff( sum, 1.0 ) <= FLOAT64_SQRT_EPS ); + } + return false; +} + + +// EXPORTS // + +module.exports = isUnityProbabilityArray; diff --git a/is-unity-probability-array/package.json b/is-unity-probability-array/package.json new file mode 100644 index 00000000..1eb78e4b --- /dev/null +++ b/is-unity-probability-array/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-unity-probability-array", + "version": "0.0.0", + "description": "Test if a value is an array of probabilities that sum to one.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "probability", + "probs", + "prob", + "distribution", + "discrete", + "sum", + "isarray", + "numeric", + "number", + "type", + "check", + "valid", + "validate", + "isvalid" + ] +} diff --git a/is-unity-probability-array/test/test.js b/is-unity-probability-array/test/test.js new file mode 100644 index 00000000..96b40915 --- /dev/null +++ b/is-unity-probability-array/test/test.js @@ -0,0 +1,106 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Float64Array = require( '@stdlib/array/float64' ); +var array2buffer = require( '@stdlib/buffer/from-array' ); +var isUnityProbabilityArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isUnityProbabilityArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an array of probabilities that sum to one', function test( t ) { + var values; + var i; + + values = [ + new Int8Array( [ 0, 0, 1 ] ), + new Uint8Array( [ 1, 0, 0 ] ), + new Uint8ClampedArray( [ 0, 1 ] ), + new Int16Array( [ 0, 0, 1, 0 ] ), + new Uint16Array( [ 1 ] ), + new Int32Array( [ 1, 0, 0 ] ), + new Uint32Array( [ 0, 1, 0 ] ), + new Float32Array( [ 0.0, 0.5, 0.5 ] ), + new Float64Array( [ 0.25, 0.25, 0.5 ] ), + [ 0.5, 0.25, 0.25 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isUnityProbabilityArray( values[i] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function allows a difference in sum within floating-point epsilon', function test( t ) { + var bool = isUnityProbabilityArray( [ 0.1, 0.2, 0.1, 0.1, 0.2, 0.2, 0.1 ] ); + t.strictEqual( bool, true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided an array of probabilities that sum to one', function test( t ) { + var values; + var i; + + values = [ + '10', + '01', + '0.5', + '5', + 5, + NaN, + null, + undefined, + true, + [], + [ 5, null ], + [ 5, '5' ], + [ 1.0, 3.14 ], + [ -1.0, 1.0, 1.0 ], + [ 0.25, 0.33, 0.5 ], + new Float64Array( [ 0.25, 0.33, 0.5 ] ), + new Float64Array( [ 0.25, 1.33, 0.5 ] ), + new Float64Array( [ 0.25, -0.33, 0.5 ] ), + {}, + function noop() {}, + array2buffer( [ 1, 1, 0 ] ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isUnityProbabilityArray( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-uppercase/README.md b/is-uppercase/README.md new file mode 100644 index 00000000..4f2e4f8e --- /dev/null +++ b/is-uppercase/README.md @@ -0,0 +1,138 @@ + + +# isUppercase + +> Test if a value is an uppercase string. + +
+ +## Usage + +```javascript +var isUppercase = require( '@stdlib/assert/is-uppercase' ); +``` + +#### isUppercase( value ) + +Tests if a `value` is an uppercase `string`. + +```javascript +var bool = isUppercase( 'HELLO' ); +// returns true + +bool = isUppercase( 'salt and light' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- The function validates that a `value` is a `string`. For all other types, the function returns `false`. + +
+ + + +
+ +## Examples + + + +```javascript +var isUppercase = require( '@stdlib/assert/is-uppercase' ); + +var bool = isUppercase( 'HELLO' ); +// returns true + +bool = isUppercase( '' ); +// returns false + +bool = isUppercase( 'Hello' ); +// returns false + +bool = isUppercase( 'hello' ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-uppercase [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-uppercase BEEP +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n 'boop' | is-uppercase +false +``` + +
+ + + +
+ + + + + + diff --git a/is-uppercase/benchmark/benchmark.js b/is-uppercase/benchmark/benchmark.js new file mode 100644 index 00000000..1cd7c7a0 --- /dev/null +++ b/is-uppercase/benchmark/benchmark.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isUppercase = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isUppercase( fromCodePoint( i%126 ) ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-uppercase/bin/cli b/is-uppercase/bin/cli new file mode 100644 index 00000000..a7f790ef --- /dev/null +++ b/is-uppercase/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isUppercase = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isUppercase( args[ 0 ] ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isUppercase( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-uppercase/docs/repl.txt b/is-uppercase/docs/repl.txt new file mode 100644 index 00000000..f757a7f8 --- /dev/null +++ b/is-uppercase/docs/repl.txt @@ -0,0 +1,24 @@ + +{{alias}}( value ) + Tests if a value is an uppercase string. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is an uppercase string. + + Examples + -------- + > var bool = {{alias}}( 'HELLO' ) + true + > bool = {{alias}}( 'World' ) + false + + See Also + -------- + diff --git a/is-uppercase/docs/types/index.d.ts b/is-uppercase/docs/types/index.d.ts new file mode 100644 index 00000000..bb3700a9 --- /dev/null +++ b/is-uppercase/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is an uppercase string. +* +* @param value - value to test +* @returns boolean indicating whether value is an uppercase string +* +* @example +* var bool = isUppercase( 'HELLO' ); +* // returns true +* +* @example +* var bool = isUppercase( 'World' ); +* // returns false +* +* @example +* var bool = isUppercase( '!' ); +* // returns false +* +* @example +* var bool = isUppercase( 'salt and light' ); +* // returns false +*/ +declare function isUppercase( value: any ): boolean; + + +// EXPORTS // + +export = isUppercase; diff --git a/is-uppercase/docs/types/test.ts b/is-uppercase/docs/types/test.ts new file mode 100644 index 00000000..50ad0bf1 --- /dev/null +++ b/is-uppercase/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isUppercase = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isUppercase( 'salt and light' ); // $ExpectType boolean + isUppercase( 'World' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isUppercase(); // $ExpectError + isUppercase( 'salt and light', 123 ); // $ExpectError +} diff --git a/is-uppercase/docs/usage.txt b/is-uppercase/docs/usage.txt new file mode 100644 index 00000000..67797f84 --- /dev/null +++ b/is-uppercase/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-uppercase [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-uppercase/etc/cli_opts.json b/is-uppercase/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-uppercase/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-uppercase/examples/index.js b/is-uppercase/examples/index.js new file mode 100644 index 00000000..2138c9ea --- /dev/null +++ b/is-uppercase/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isUppercase = require( './../lib' ); + +console.log( isUppercase( 'HELLO' ) ); +// => true + +console.log( isUppercase( '' ) ); +// => false + +console.log( isUppercase( 'Hello' ) ); +// => false + +console.log( isUppercase( 'hello' ) ); +// => false diff --git a/is-uppercase/lib/index.js b/is-uppercase/lib/index.js new file mode 100644 index 00000000..5bf4f484 --- /dev/null +++ b/is-uppercase/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is an uppercase string. +* +* @module @stdlib/assert/is-uppercase +* +* @example +* var isUppercase = require( '@stdlib/assert/is-uppercase' ); +* +* var bool = isUppercase( 'HELLO' ); +* // returns true +* +* bool = isUppercase( 'World' ); +* // returns false +*/ + +// MODULES // + +var isUppercase = require( './main.js' ); + + +// EXPORTS // + +module.exports = isUppercase; diff --git a/is-uppercase/lib/main.js b/is-uppercase/lib/main.js new file mode 100644 index 00000000..8c89338e --- /dev/null +++ b/is-uppercase/lib/main.js @@ -0,0 +1,63 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var lowercase = require( '@stdlib/string/lowercase' ); +var uppercase = require( '@stdlib/string/uppercase' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; + + +// MAIN // + +/** +* Tests if a value is an uppercase string. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether value is an uppercase string +* +* @example +* var bool = isUppercase( 'HELLO' ); +* // returns true +* +* @example +* var bool = isUppercase( 'World' ); +* // returns false +* +* @example +* var bool = isUppercase( '!' ); +* // returns false +* +* @example +* var bool = isUppercase( 'salt and light' ); +* // returns false +*/ +function isUppercase( value ) { + return ( + isString( value ) && + value === uppercase( value ) && + value !== lowercase( value ) + ); +} + + +// EXPORTS // + +module.exports = isUppercase; diff --git a/is-uppercase/package.json b/is-uppercase/package.json new file mode 100644 index 00000000..90e84d6c --- /dev/null +++ b/is-uppercase/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-uppercase", + "version": "0.0.0", + "description": "Test if a value is an uppercase string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-uppercase": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isuppercase", + "uppercase", + "string", + "valid", + "validate", + "test" + ] +} diff --git a/is-uppercase/test/fixtures/stdin_error.js.txt b/is-uppercase/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-uppercase/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-uppercase/test/test.cli.js b/is-uppercase/test/test.cli.js new file mode 100644 index 00000000..9e696f5c --- /dev/null +++ b/is-uppercase/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument contains only uppercase letters', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'Hello\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'false\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "BEEP\nboop"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\nfalse\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-uppercase/test/test.js b/is-uppercase/test/test.js new file mode 100644 index 00000000..df0fe9d2 --- /dev/null +++ b/is-uppercase/test/test.js @@ -0,0 +1,77 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isUppercase = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isUppercase, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an uppercase string', function test( t ) { + var values; + var bool; + var i; + + values = [ + 'HELLO WORLD!', + 'JUST WAIT, SOON YOU WILL REST AS WELL.' + ]; + + for ( i = 0; i < values.length; i++ ) { + bool = isUppercase( values[ i ] ); + t.strictEqual( bool, true, 'returns true when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided an uppercase string', function test( t ) { + var values; + var i; + + values = [ + 'abc', + 'Hello World', + '', + '1139094843', + '!', + void 0, + 0, + NaN, + null, + false, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isUppercase( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-uri-error/README.md b/is-uri-error/README.md new file mode 100644 index 00000000..246b1c96 --- /dev/null +++ b/is-uri-error/README.md @@ -0,0 +1,129 @@ + + +# isURIError + +> Test if a value is a [URIError][mdn-uri-error] object. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var isURIError = require( '@stdlib/assert/is-uri-error' ); +``` + +#### isURIError( value ) + +Tests if a `value` is a [`URIError`][mdn-uri-error] object. + +```javascript +var bool = isURIError( new URIError( 'beep' ) ); +// returns true +``` + +
+ + + + + +
+ +## Notes + +- This function should **not** be considered robust. While the function should **always** return `true` if provided a [`URIError`][mdn-uri-error] (or a descendant) object, false positives may occur due to the fact that the [`URIError`][mdn-uri-error] constructor inherits from [`Error`][mdn-error] and has no internal class of its own. Hence, [`URIError`][mdn-uri-error] impersonation is possible. + +
+ + + + + +
+ +## Examples + + + +```javascript +var isURIError = require( '@stdlib/assert/is-uri-error' ); + +var bool = isURIError( new URIError( 'URI error' ) ); +// returns true + +bool = isURIError( new Error( 'error' ) ); +// returns false + +bool = isURIError( new EvalError( 'eval error' ) ); +// returns false + +bool = isURIError( new ReferenceError( 'reference error' ) ); +// returns false + +bool = isURIError( new SyntaxError( 'syntax error' ) ); +// returns false + +bool = isURIError( new TypeError( 'type error' ) ); +// returns false + +bool = isURIError( new RangeError( 'range error' ) ); +// returns false + +bool = isURIError( {} ); +// returns false + +bool = isURIError( null ); +// returns false +``` + +
+ + + + + +
+ +
+ + + + + + + + diff --git a/is-uri-error/benchmark/benchmark.js b/is-uri-error/benchmark/benchmark.js new file mode 100644 index 00000000..96e3c155 --- /dev/null +++ b/is-uri-error/benchmark/benchmark.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isURIError = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + {}, + [], + null, + false, + true, + new Error( 'beep' ), + new URIError( 'bar' ), + new TypeError( 'boop' ), + new SyntaxError( 'foo' ) + ]; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isURIError( values[ i%values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-uri-error/docs/repl.txt b/is-uri-error/docs/repl.txt new file mode 100644 index 00000000..d7eca7d4 --- /dev/null +++ b/is-uri-error/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( value ) + Tests if a value is a URIError object. + + This function should *not* be considered robust. While the function should + always return `true` if provided a URIError (or a descendant) object, + false positives may occur due to the fact that the URIError constructor + inherits from Error and has no internal class of its own. Hence, URIError + impersonation is possible. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a URIError object. + + Examples + -------- + > var bool = {{alias}}( new URIError( 'beep' ) ) + true + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-uri-error/docs/types/index.d.ts b/is-uri-error/docs/types/index.d.ts new file mode 100644 index 00000000..4720769b --- /dev/null +++ b/is-uri-error/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a `URIError` object. +* +* ## Notes +* +* - This function should **not** be considered robust. While the function should always return `true` if provided a URIError (or a descendant) object, false positives may occur due to the fact that the URIError constructor inherits from Error and has no internal class of its own. Hence, URIError impersonation is possible. +* +* @param value - value to test +* @returns boolean indicating whether a value is a `URIError` object +* +* @example +* var bool = isURIError( new URIError( 'beep' ) ); +* // returns true +* +* @example +* var bool = isURIError( {} ); +* // returns false +*/ +declare function isURIError( value: any ): boolean; + + +// EXPORTS // + +export = isURIError; diff --git a/is-uri-error/docs/types/test.ts b/is-uri-error/docs/types/test.ts new file mode 100644 index 00000000..774f8e42 --- /dev/null +++ b/is-uri-error/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isURIError = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isURIError( new URIError( 'beep' ) ); // $ExpectType boolean + isURIError( {} ); // $ExpectType boolean +} + +// The function does not compile if provided insufficient arguments... +{ + isURIError(); // $ExpectError + isURIError( new URIError( 'beep' ), 123 ); // $ExpectError +} diff --git a/is-uri-error/examples/index.js b/is-uri-error/examples/index.js new file mode 100644 index 00000000..e8b3ade0 --- /dev/null +++ b/is-uri-error/examples/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isURIError = require( './../lib' ); + +var bool = isURIError( new URIError( 'URI error' ) ); +console.log( bool ); +// => true + +bool = isURIError( new Error( 'error' ) ); +console.log( bool ); +// => false + +bool = isURIError( new EvalError( 'eval error' ) ); +console.log( bool ); +// => false + +bool = isURIError( new ReferenceError( 'reference error' ) ); +console.log( bool ); +// => false + +bool = isURIError( new SyntaxError( 'syntax error' ) ); +console.log( bool ); +// => false + +bool = isURIError( new TypeError( 'type error' ) ); +console.log( bool ); +// => false + +bool = isURIError( new RangeError( 'range error' ) ); +console.log( bool ); +// => false + +bool = isURIError( {} ); +console.log( bool ); +// => false + +bool = isURIError( null ); +console.log( bool ); +// => false diff --git a/is-uri-error/lib/index.js b/is-uri-error/lib/index.js new file mode 100644 index 00000000..5c4e95de --- /dev/null +++ b/is-uri-error/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a `URIError` object. +* +* @module @stdlib/assert/is-uri-error +* +* @example +* var isURIError = require( '@stdlib/assert/is-uri-error' ); +* +* var bool = isURIError( new URIError( 'beep' ) ); +* // returns true +* +* bool = isURIError( {} ); +* // returns false +*/ + +// MODULES // + +var isURIError = require( './main.js' ); + + +// EXPORTS // + +module.exports = isURIError; diff --git a/is-uri-error/lib/main.js b/is-uri-error/lib/main.js new file mode 100644 index 00000000..383cfa0c --- /dev/null +++ b/is-uri-error/lib/main.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var getPrototypeOf = require( '@stdlib/utils/get-prototype-of' ); +var typeOf = require( '@stdlib/utils/type-of' ); +var isError = require( '@stdlib/assert/is-error' ); + + +// MAIN // + +/** +* Tests if a value is a `URIError` object. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating whether a value is a `URIError` object +* +* @example +* var bool = isURIError( new URIError( 'beep' ) ); +* // returns true +* +* @example +* var bool = isURIError( {} ); +* // returns false +*/ +function isURIError( value ) { + if ( typeof value !== 'object' || value === null ) { + return false; + } + // Check for `URIError` objects from the same realm (same Node.js `vm` or same `Window` object)... + if ( value instanceof URIError ) { + return true; + } + // All `URIError` objects are `Error` objects... + if ( isError( value ) ) { + // Walk the prototype tree until we find the desired constructor... + while ( value ) { + if ( typeOf( value ) === 'urierror' ) { + return true; + } + value = getPrototypeOf( value ); + } + } + return false; +} + + +// EXPORTS // + +module.exports = isURIError; diff --git a/is-uri-error/package.json b/is-uri-error/package.json new file mode 100644 index 00000000..d3de1485 --- /dev/null +++ b/is-uri-error/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/assert/is-uri-error", + "version": "0.0.0", + "description": "Test if a value is a URIError object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "uri", + "error", + "err", + "exception", + "is", + "iserr", + "iserror", + "valid", + "isvalid", + "type", + "check", + "validate", + "validation", + "test" + ] +} diff --git a/is-uri-error/test/test.js b/is-uri-error/test/test.js new file mode 100644 index 00000000..11b27485 --- /dev/null +++ b/is-uri-error/test/test.js @@ -0,0 +1,115 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var vm = require( 'vm' ); // TODO: handle in-browser tests +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var inherit = require( '@stdlib/utils/inherit' ); +var isURIError = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': IS_BROWSER +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isURIError, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a `URIError` object', function test( t ) { + var values; + var i; + + values = [ + new URIError( 'URI error' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isURIError( values[ i ] ), true, 'returns true when provided '+values[i] ); + } + t.end(); +}); + +tape( 'the function returns `true` if provided an object inheriting from `URIError`', function test( t ) { + function CustomError( msg ) { + URIError.call( this ); + this.name = 'CustomError'; + this.message = msg; + return this; + } + + inherit( CustomError, URIError ); + + t.strictEqual( isURIError( new CustomError( 'custom error' ) ), true, 'returns true when provided a value which inherits from URIError' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a `URIError` object from a different realm', opts, function test( t ) { + var error = vm.runInNewContext( 'new URIError()' ); + t.strictEqual( isURIError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided an object from a different realm which inherits from a `URIError` object', opts, function test( t ) { + var error = vm.runInNewContext( 'function Err() { return this; }; Err.prototype = new URIError(); new Err();' ); + t.strictEqual( isURIError( error ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a `URIError` object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + function error() {}, + new Date(), + new RegExp( '.*' ), + new Error( 'error' ), + new EvalError( 'eval error' ), + new ReferenceError( 'reference error' ), + new SyntaxError( 'syntax error' ), + new TypeError( 'type error' ), + new RangeError( 'range error' ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isURIError( values[ i ] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-uri/README.md b/is-uri/README.md new file mode 100644 index 00000000..b8ca9126 --- /dev/null +++ b/is-uri/README.md @@ -0,0 +1,225 @@ + + +# isURI + +> Test if a value is a [URI][uri]. + +
+ +## Usage + +```javascript +var isURI = require( '@stdlib/assert/is-uri' ); +``` + +#### isURI( value ) + +Tests if a `value` is a [URI][uri]. + +```javascript +var bool = isURI( 'https://google.com' ); +// returns true + +bool = isURI( 'ftp://ftp.is.co.za/rfc/rfc1808.txt' ); +// returns true + +bool = isURI( '' ); +// returns false + +bool = isURI( 'foo' ); +// returns false + +bool = isURI( null ); +// returns false + +bool = isURI( NaN ); +// returns false + +bool = isURI( true ); +// returns false +``` + +
+ + + +
+ +## Notes + +- For more information regarding the URI scheme, see [RFC 3986][rfc-3986] and [Wikipedia][uri]. +- On the distinction between URI, URL, and URN, see [The Difference Between URLs and URIs][difference-url-uri]. + +
+ + + +
+ +## Examples + + + +```javascript +var isURI = require( '@stdlib/assert/is-uri' ); + +/* Valid */ + +var bool = isURI( 'http://google.com' ); +// returns true + +bool = isURI( 'http://localhost/' ); +// returns true + +bool = isURI( 'http://example.w3.org/path%20with%20spaces.html' ); +// returns true + +bool = isURI( 'http://example.w3.org/%20' ); +// returns true + +bool = isURI( 'ftp://ftp.is.co.za/rfc/rfc1808.txt' ); +// returns true + +bool = isURI( 'ftp://ftp.is.co.za/../../../rfc/rfc1808.txt' ); +// returns true + +bool = isURI( 'http://www.ietf.org/rfc/rfc2396.txt' ); +// returns true + +bool = isURI( 'ldap://[2001:db8::7]/c=GB?objectClass?one' ); +// returns true + +bool = isURI( 'mailto:John.Doe@example.com' ); +// returns true + +bool = isURI( 'news:comp.infosystems.www.servers.unix' ); +// returns true + +bool = isURI( 'tel:+1-816-555-1212' ); +// returns true + +bool = isURI( 'telnet://192.0.2.16:80/' ); +// returns true + +bool = isURI( 'urn:oasis:names:specification:docbook:dtd:xml:4.1.2' ); +// returns true + +/* Invalid */ + +// No scheme: +bool = isURI( '' ); +// returns false + +// No scheme: +bool = isURI( 'foo' ); +// returns false + +// No scheme: +bool = isURI( 'foo@bar' ); +// returns false + +// No scheme: +bool = isURI( '://foo/' ); +// returns false + +// Illegal characters: +bool = isURI( 'http://' ); +// returns false + +// Invalid path: +bool = isURI( 'http:////foo.html' ); +// returns false + +// Incomplete hex escapes... +bool = isURI( 'http://example.w3.org/%a' ); +// returns false + +bool = isURI( 'http://example.w3.org/%a/foo' ); +// returns false + +bool = isURI( 'http://example.w3.org/%at' ); +// returns false +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: is-uri [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. +``` + +
+ + + +
+ +### Examples + +```bash +$ is-uri http://google.com +true +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n 'http://google.com' | is-uri +true +``` + +
+ + + +
+ + + + + + diff --git a/is-uri/benchmark/benchmark.js b/is-uri/benchmark/benchmark.js new file mode 100644 index 00000000..502b8e30 --- /dev/null +++ b/is-uri/benchmark/benchmark.js @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-empty-function */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isURI = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + 'http://google.com', + 'http://localhost/', + 'http://example.w3.org/path%20with%20spaces.html', + 'http://example.w3.org/%20', + 'ftp://ftp.is.co.za/rfc/rfc1808.txt', + 'ftp://ftp.is.co.za/../../../rfc/rfc1808.txt', + 'http://www.ietf.org/rfc/rfc2396.txt', + 'ldap://[2001:db8::7]/c=GB?objectClass?one', + 'mailto:John.Doe@example.com', + 'news:comp.infosystems.www.servers.unix', + 'tel:+1-816-555-1212', + 'telnet://192.0.2.16:80/', + 'urn:oasis:names:specification:docbook:dtd:xml:4.1.2', + 5, + null, + void 0, + true, + NaN, + {}, + [], + function noop() {}, + '', + 'foo', + 'foo@bar', + '://foo/', + '1http://foo', + 'http://', + 'http:////foo.html', + 'http://example.w3.org/%illegal.html', + 'http://example.w3.org/%a', + 'http://example.w3.org/%a/foo', + 'http://example.w3.org/%at' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isURI( values[ i % values.length ] ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( isBoolean( bool ) ) { + b.pass( 'benchmark finished' ); + } else { + b.fail( 'should return a boolean' ); + } + b.end(); +}); diff --git a/is-uri/bin/cli b/is-uri/bin/cli new file mode 100644 index 00000000..bbc600b3 --- /dev/null +++ b/is-uri/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isURI = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isURI( args[ 0 ] ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isURI( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-uri/docs/repl.txt b/is-uri/docs/repl.txt new file mode 100644 index 00000000..4d269b02 --- /dev/null +++ b/is-uri/docs/repl.txt @@ -0,0 +1,48 @@ + +{{alias}}( value ) + Tests if a value is a URI. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether value is a URI. + + Examples + -------- + > var bool = {{alias}}( 'http://google.com' ) + true + > bool = {{alias}}( 'http://localhost/' ) + true + > bool = {{alias}}( 'http://example.w3.org/path%20with%20spaces.html' ) + true + > bool = {{alias}}( 'ftp://ftp.is.co.za/rfc/rfc1808.txt' ) + true + + // No scheme: + > bool = {{alias}}( '' ) + false + > bool = {{alias}}( 'foo@bar' ) + false + > bool = {{alias}}( '://foo/' ) + false + + // Illegal characters: + > bool = {{alias}}( 'http://' ) + false + + // Invalid path: + > bool = {{alias}}( 'http:////foo.html' ) + false + + // Incomplete hex escapes: + > bool = {{alias}}( 'http://example.w3.org/%a' ) + false + + See Also + -------- + diff --git a/is-uri/docs/types/index.d.ts b/is-uri/docs/types/index.d.ts new file mode 100644 index 00000000..f0cd1546 --- /dev/null +++ b/is-uri/docs/types/index.d.ts @@ -0,0 +1,127 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a URI. +* +* @param value - value to test +* @returns boolean indicating if a value is a URI +* +* @example +* var bool = isURI( 'http://google.com' ); +* // returns true +* +* @example +* var bool = isURI( 'http://localhost/' ); +* // returns true +* +* @example +* var bool = isURI( 'http://example.w3.org/path%20with%20spaces.html' ); +* // returns true +* +* @example +* var bool = isURI( 'http://example.w3.org/%20' ); +* // returns true +* +* @example +* var bool = isURI( 'ftp://ftp.is.co.za/rfc/rfc1808.txt' ); +* // returns true +* +* @example +* var bool = isURI( 'ftp://ftp.is.co.za/../../../rfc/rfc1808.txt' ); +* // returns true +* +* @example +* var bool = isURI( 'http://www.ietf.org/rfc/rfc2396.txt' ); +* // returns true +* +* @example +* var bool = isURI( 'ldap://[2001:db8::7]/c=GB?objectClass?one' ); +* // returns true +* +* @example +* var bool = isURI( 'mailto:John.Doe@example.com' ); +* // returns true +* +* @example +* var bool = isURI( 'news:comp.infosystems.www.servers.unix' ); +* // returns true +* +* @example +* var bool = isURI( 'tel:+1-816-555-1212' ); +* // returns true +* +* @example +* var bool = isURI( 'telnet://192.0.2.16:80/' ); +* // returns true +* +* @example +* var bool = isURI( 'urn:oasis:names:specification:docbook:dtd:xml:4.1.2' ); +* // returns true +* +* @example +* // No scheme: +* var bool = isURI( '' ); +* // returns false +* +* @example +* // No scheme: +* var bool = isURI( 'foo' ); +* // returns false +* +* @example +* // No scheme: +* var bool = isURI( 'foo@bar' ); +* // returns false +* +* @example +* // No scheme: +* var bool = isURI( '://foo/' ); +* // returns false +* +* @example +* // Illegal characters: +* var bool = isURI( 'http://' ); +* // returns false +* +* @example +* // Invalid path: +* var bool = isURI( 'http:////foo.html' ); +* // returns false +* +* @example +* // Incomplete hex escapes... +* var bool = isURI( 'http://example.w3.org/%a' ); +* // returns false +* +* @example +* var bool = isURI( 'http://example.w3.org/%a/foo' ); +* // returns false +* +* @example +* var bool = isURI( 'http://example.w3.org/%at' ); +* // returns false +*/ +declare function isURI( value: any ): boolean; + + +// EXPORTS // + +export = isURI; diff --git a/is-uri/docs/types/test.ts b/is-uri/docs/types/test.ts new file mode 100644 index 00000000..014e6ba5 --- /dev/null +++ b/is-uri/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isURI = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isURI( 'http://google.com' ); // $ExpectType boolean + isURI( {} ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isURI(); // $ExpectError +} diff --git a/is-uri/docs/usage.txt b/is-uri/docs/usage.txt new file mode 100644 index 00000000..63479c9a --- /dev/null +++ b/is-uri/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-uri [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-uri/etc/cli_opts.json b/is-uri/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-uri/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-uri/examples/index.js b/is-uri/examples/index.js new file mode 100644 index 00000000..994f3cd9 --- /dev/null +++ b/is-uri/examples/index.js @@ -0,0 +1,120 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isURI = require( './../lib' ); + +/* Valid */ + +var bool = isURI( 'http://google.com' ); +console.log( bool ); +// => true + +bool = isURI( 'http://localhost/' ); +console.log( bool ); +// => true + +bool = isURI( 'http://example.w3.org/path%20with%20spaces.html' ); +console.log( bool ); +// => true + +bool = isURI( 'http://example.w3.org/%20' ); +console.log( bool ); +// => true + +bool = isURI( 'ftp://ftp.is.co.za/rfc/rfc1808.txt' ); +console.log( bool ); +// => true + +bool = isURI( 'ftp://ftp.is.co.za/../../../rfc/rfc1808.txt' ); +console.log( bool ); +// => true + +bool = isURI( 'http://www.ietf.org/rfc/rfc2396.txt' ); +console.log( bool ); +// => true + +bool = isURI( 'ldap://[2001:db8::7]/c=GB?objectClass?one' ); +console.log( bool ); +// => true + +bool = isURI( 'mailto:John.Doe@example.com' ); +console.log( bool ); +// => true + +bool = isURI( 'news:comp.infosystems.www.servers.unix' ); +console.log( bool ); +// => true + +bool = isURI( 'tel:+1-816-555-1212' ); +console.log( bool ); +// => true + +bool = isURI( 'telnet://192.0.2.16:80/' ); +console.log( bool ); +// => true + +bool = isURI( 'urn:oasis:names:specification:docbook:dtd:xml:4.1.2' ); +console.log( bool ); +// => true + +/* Invalid */ + +// No scheme: +bool = isURI( '' ); +console.log( bool ); +// => false + +// No scheme: +bool = isURI( 'foo' ); +console.log( bool ); +// => false + +// No scheme: +bool = isURI( 'foo@bar' ); +console.log( bool ); +// => false + +// No scheme: +bool = isURI( '://foo/' ); +console.log( bool ); +// => false + +// Illegal characters: +bool = isURI( 'http://' ); +console.log( bool ); +// => false + +// Invalid path: +bool = isURI( 'http:////foo.html' ); +console.log( bool ); +// => false + +// Incomplete hex escapes... +bool = isURI( 'http://example.w3.org/%a' ); +console.log( bool ); +// => false + +bool = isURI( 'http://example.w3.org/%a/foo' ); +console.log( bool ); +// => false + +bool = isURI( 'http://example.w3.org/%at' ); +console.log( bool ); +// => false diff --git a/is-uri/lib/index.js b/is-uri/lib/index.js new file mode 100644 index 00000000..d7fa6a7c --- /dev/null +++ b/is-uri/lib/index.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a URI. +* +* @module @stdlib/assert/is-uri +* +* @example +* var isURI = require( '@stdlib/assert/is-uri' ); +* +* var bool = isURI( 'http://google.com' ); +* // returns true +* +* bool = isURI( 'http://localhost/' ); +* // returns true +* +* bool = isURI( 'http://example.w3.org/path%20with%20spaces.html' ); +* // returns true +* +* bool = isURI( 'http://example.w3.org/%20' ); +* // returns true +* +* bool = isURI( 'ftp://ftp.is.co.za/rfc/rfc1808.txt' ); +* // returns true +* +* bool = isURI( 'ftp://ftp.is.co.za/../../../rfc/rfc1808.txt' ); +* // returns true +* +* bool = isURI( 'http://www.ietf.org/rfc/rfc2396.txt' ); +* // returns true +* +* bool = isURI( 'ldap://[2001:db8::7]/c=GB?objectClass?one' ); +* // returns true +* +* bool = isURI( 'mailto:John.Doe@example.com' ); +* // returns true +* +* bool = isURI( 'news:comp.infosystems.www.servers.unix' ); +* // returns true +* +* bool = isURI( 'tel:+1-816-555-1212' ); +* // returns true +* +* bool = isURI( 'telnet://192.0.2.16:80/' ); +* // returns true +* +* bool = isURI( 'urn:oasis:names:specification:docbook:dtd:xml:4.1.2' ); +* // returns true +* +* // No scheme: +* bool = isURI( '' ); +* // returns false +* +* // No scheme: +* bool = isURI( 'foo' ); +* // returns false +* +* // No scheme: +* bool = isURI( 'foo@bar' ); +* // returns false +* +* // No scheme: +* bool = isURI( '://foo/' ); +* // returns false +* +* // Illegal characters: +* bool = isURI( 'http://' ); +* // returns false +* +* // Invalid path: +* bool = isURI( 'http:////foo.html' ); +* // returns false +* +* // Incomplete hex escapes... +* bool = isURI( 'http://example.w3.org/%a' ); +* // returns false +* +* bool = isURI( 'http://example.w3.org/%a/foo' ); +* // returns false +* +* bool = isURI( 'http://example.w3.org/%at' ); +* // returns false +*/ + +// MODULES // + +var isURI = require( './main.js' ); + + +// EXPORTS // + +module.exports = isURI; diff --git a/is-uri/lib/main.js b/is-uri/lib/main.js new file mode 100644 index 00000000..c2033b6b --- /dev/null +++ b/is-uri/lib/main.js @@ -0,0 +1,244 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; + + +// VARIABLES // + +/** +* Matches parts of a URI according to RFC 3986. +* +* ```text +* : [ ? ] [ # ] +* ``` +* +* Regular expression: `/(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*)(?:\?[^#]*)?(?:#.*)?/` +* +* - `(?:([^:\/?#]+):)` +* +* - match the scheme, including the `:`, but only capture the scheme name +* +* - `?` +* +* - match the scheme zero or one times +* +* - `(?:\/\/([^\/?#]*))` +* +* - match the hierarchical part which is everything which is not a `/`, `#`, or `?`, but only capture whatever comes after the `//` +* +* - `?` +* +* - match the hierarchical part zero or one times +* +* - `([^?#]*)` +* +* - capture everything (the path) until meeting a `?` or `#` +* +* - `(?:\?[^#]*)` +* +* - match, but don't capture, a query +* +* - `?` +* +* - match the query zero or one times +* +* - `(?:#.*)` +* +* - match, but don't capture, a fragment +* +* - `?` +* +* - match the fragment zero or one times +* +* @private +* @constant +* @type {RegExp} +* @default /(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*)(?:\?[^#]*)?(?:#.*)?/ +*/ +var RE_URI = /(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*)(?:\?[^#]*)?(?:#.*)?/; // eslint-disable-line no-useless-escape + +// Illegal characters (anything which is not in between the square brackets): +var RE_ILLEGALS = /[^a-z0-9:\/?#\[\]@!$&'()*+,;=.\-_~%]/i; // eslint-disable-line no-useless-escape + +// Incomplete HEX escapes: +var RE_HEX1 = /%[^0-9a-f]/i; +var RE_HEX2 = /%[0-9a-f](:?[^0-9a-f]|$)/i; + +// If authority is not present, path must not begin with '//' +var RE_PATH = /^\/\//; + +// Scheme must begin with a letter, then consist of letters, digits, '+', '.', or '-' => e.g., 'http', 'https', 'ftp' +var RE_SCHEME = /^[a-z][a-z0-9+\-.]*$/; + + +// MAIN // + +/** +* Tests if a value is a URI. +* +* @param {*} value - value to test +* @returns {boolean} boolean indicating if a value is a URI +* +* @example +* var bool = isURI( 'http://google.com' ); +* // returns true +* +* @example +* var bool = isURI( 'http://localhost/' ); +* // returns true +* +* @example +* var bool = isURI( 'http://example.w3.org/path%20with%20spaces.html' ); +* // returns true +* +* @example +* var bool = isURI( 'http://example.w3.org/%20' ); +* // returns true +* +* @example +* var bool = isURI( 'ftp://ftp.is.co.za/rfc/rfc1808.txt' ); +* // returns true +* +* @example +* var bool = isURI( 'ftp://ftp.is.co.za/../../../rfc/rfc1808.txt' ); +* // returns true +* +* @example +* var bool = isURI( 'http://www.ietf.org/rfc/rfc2396.txt' ); +* // returns true +* +* @example +* var bool = isURI( 'ldap://[2001:db8::7]/c=GB?objectClass?one' ); +* // returns true +* +* @example +* var bool = isURI( 'mailto:John.Doe@example.com' ); +* // returns true +* +* @example +* var bool = isURI( 'news:comp.infosystems.www.servers.unix' ); +* // returns true +* +* @example +* var bool = isURI( 'tel:+1-816-555-1212' ); +* // returns true +* +* @example +* var bool = isURI( 'telnet://192.0.2.16:80/' ); +* // returns true +* +* @example +* var bool = isURI( 'urn:oasis:names:specification:docbook:dtd:xml:4.1.2' ); +* // returns true +* +* @example +* // No scheme: +* var bool = isURI( '' ); +* // returns false +* +* @example +* // No scheme: +* var bool = isURI( 'foo' ); +* // returns false +* +* @example +* // No scheme: +* var bool = isURI( 'foo@bar' ); +* // returns false +* +* @example +* // No scheme: +* var bool = isURI( '://foo/' ); +* // returns false +* +* @example +* // Illegal characters: +* var bool = isURI( 'http://' ); +* // returns false +* +* @example +* // Invalid path: +* var bool = isURI( 'http:////foo.html' ); +* // returns false +* +* @example +* // Incomplete hex escapes... +* var bool = isURI( 'http://example.w3.org/%a' ); +* // returns false +* +* @example +* var bool = isURI( 'http://example.w3.org/%a/foo' ); +* // returns false +* +* @example +* var bool = isURI( 'http://example.w3.org/%at' ); +* // returns false +*/ +function isURI( value ) { + var authority; + var scheme; + var parts; + var path; + + if ( !isString( value ) ) { + return false; + } + // Check for illegal characters: + if ( RE_ILLEGALS.test( value ) ) { + return false; + } + // Check for incomplete HEX escapes: + if ( + RE_HEX1.test( value ) || + RE_HEX2.test( value ) + ) { + return false; + } + // Split the string into various URI components: + parts = value.match( RE_URI ); + scheme = parts[ 1 ]; + authority = parts[ 2 ]; + path = parts[ 3 ]; + + // Scheme is required and must be valid: + if ( + !scheme || + !scheme.length || + !RE_SCHEME.test( scheme.toLowerCase() ) + ) { + return false; + } + // If authority is not present, path must not begin with `//`: + if ( + !authority && + RE_PATH.test( path ) + ) { + return false; + } + return true; +} + + +// EXPORTS // + +module.exports = isURI; diff --git a/is-uri/package.json b/is-uri/package.json new file mode 100644 index 00000000..6f552048 --- /dev/null +++ b/is-uri/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-uri", + "version": "0.0.0", + "description": "Test if a value is a URI.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-uri": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "bin": "./bin", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "url", + "uri", + "is", + "isurl", + "isuri", + "isvalid", + "check", + "valid", + "validate" + ] +} diff --git a/is-uri/test/fixtures/stdin_error.js.txt b/is-uri/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..10f4acae --- /dev/null +++ b/is-uri/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-uri/test/test.cli.js b/is-uri/test/test.cli.js new file mode 100644 index 00000000..8251cc62 --- /dev/null +++ b/is-uri/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument is an URI', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'http://localhost/\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "http://www.ietf.org/rfc/rfc2396.txt\nbeep"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\nfalse\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-uri/test/test.js b/is-uri/test/test.js new file mode 100644 index 00000000..2253acb0 --- /dev/null +++ b/is-uri/test/test.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isURI = require( './../lib' ); + + +// NOTES // + +/* +* These tests use the same test URIs as [valid-url]{@link https://github.com/ogt/valid-url} (a clone of a corresponding Perl [package]{@link http://anonscm.debian.org/cgit/users/dom/libdata-validate-uri-perl.git/tree/lib/Data/Validate/URI.pm)}, which are based on examples from [RFC 3986]{@link http://tools.ietf.org/html/rfc3986}. +*/ + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isURI, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a URI', function test( t ) { + var values; + var i; + + values = [ + 'http://google.com', + 'http://localhost/', + 'http://example.w3.org/path%20with%20spaces.html', + 'http://example.w3.org/%20', + 'ftp://ftp.is.co.za/rfc/rfc1808.txt', + 'ftp://ftp.is.co.za/../../../rfc/rfc1808.txt', + 'http://www.ietf.org/rfc/rfc2396.txt', + 'ldap://[2001:db8::7]/c=GB?objectClass?one', + 'mailto:John.Doe@example.com', + 'news:comp.infosystems.www.servers.unix', + 'tel:+1-816-555-1212', + 'telnet://192.0.2.16:80/', + 'urn:oasis:names:specification:docbook:dtd:xml:4.1.2' + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isURI( values[ i ] ), true, values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a URI', function test( t ) { + var values; + var i; + + values = [ + 5, + null, + undefined, + true, + NaN, + {}, + [], + function noop() {}, + '', + 'foo', + 'foo@bar', // no scheme + '://foo/', // empty scheme + '1http://foo', // invalid scheme + 'http://', // illegals + 'http:////foo.html', // invalid path + 'http://example.w3.org/%illegal.html', + 'http://example.w3.org/%a', // incomplete hex escape + 'http://example.w3.org/%a/foo', // incomplete hex escape + 'http://example.w3.org/%at' // incomplete hex escape + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isURI( values[ i ] ), false, values[ i ] ); + } + t.end(); +}); diff --git a/is-vector-like/README.md b/is-vector-like/README.md new file mode 100644 index 00000000..47bdb4e3 --- /dev/null +++ b/is-vector-like/README.md @@ -0,0 +1,83 @@ + + +# isVectorLike + +> Test if a value is a 1-dimensional [ndarray][@stdlib/ndarray/ctor]-like object. + +
+ +## Usage + +```javascript +var isVectorLike = require( '@stdlib/assert/is-vector-like' ); +``` + +#### isVectorLike( value ) + +Tests if a value is a 1-dimensional [ndarray][@stdlib/ndarray/ctor]-like object. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 4 ], [ 1 ], 0, 'row-major' ); +var bool = isVectorLike( arr ); +// returns true +``` + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isVectorLike = require( '@stdlib/assert/is-vector-like' ); + +var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 4 ], [ 1 ], 0, 'row-major' ); +var out = isVectorLike( arr ); +// returns true + +out = isVectorLike( [ 1, 2, 3, 4 ] ); +// returns false + +out = isVectorLike( {} ); +// returns false + +out = isVectorLike( null ); +// returns false +``` + +
+ + + + + + diff --git a/is-vector-like/benchmark/benchmark.js b/is-vector-like/benchmark/benchmark.js new file mode 100644 index 00000000..e28804f3 --- /dev/null +++ b/is-vector-like/benchmark/benchmark.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var isVectorLike = require( './../lib' ); + + +// MAIN // + +bench( pkg + '::true', function benchmark( b ) { + var strides; + var offset; + var buffer; + var values; + var shape; + var order; + var bool; + var arr; + var i; + + buffer = [ 0, 0, 0, 0 ]; + shape = [ 4 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + arr = ndarray( 'generic', buffer, shape, strides, offset, order ); + + values = [ + arr, + arr, + arr, + arr, + arr + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isVectorLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg + '::false', function benchmark( b ) { + var values; + var bool; + var i; + + values = [ + [ 1, 2, 3 ], + null, + 5, + 'beep' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isVectorLike( values[ i%values.length ] ); + if ( typeof bool !== 'boolean' ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-vector-like/docs/repl.txt b/is-vector-like/docs/repl.txt new file mode 100644 index 00000000..58a6cd52 --- /dev/null +++ b/is-vector-like/docs/repl.txt @@ -0,0 +1,41 @@ + +{{alias}}( value ) + Tests if a value is a 1-dimensional ndarray-like object. + + Parameters + ---------- + value: any + Value to test. + + Returns + ------- + bool: boolean + Boolean indicating whether a value is a 1-dimensional ndarray-like + object. + + Examples + -------- + > var M = {}; + > M.data = [ 0, 0, 0, 0 ]; + > M.ndims = 1; + > M.shape = [ 4 ]; + > M.strides = [ 1 ]; + > M.offset = 0; + > M.order = 'row-major'; + > M.dtype = 'generic'; + > M.length = 4; + > M.flags = {}; + > M.get = function get( i, j ) {}; + > M.set = function set( i, j ) {}; + > var bool = {{alias}}( M ) + true + > bool = {{alias}}( [ 1, 2, 3, 4 ] ) + false + > bool = {{alias}}( 3.14 ) + false + > bool = {{alias}}( {} ) + false + + See Also + -------- + diff --git a/is-vector-like/docs/types/index.d.ts b/is-vector-like/docs/types/index.d.ts new file mode 100644 index 00000000..6ca16f8e --- /dev/null +++ b/is-vector-like/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if a value is a 1-dimensional ndarray-like object. +* +* @param v - value to test +* @returns boolean indicating if a value is a 1-dimensional ndarray-like object +* +* @example +* var ndarray = require( `@stdlib/ndarray/ctor` ); +* +* var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 4 ], [ 1 ], 0, 'row-major' ); +* var bool = isVectorLike( arr ); +* // returns true +* +* bool = isVectorLike( [] ); +* // returns false +*/ +declare function isVectorLike( v: any ): boolean; + + +// EXPORTS // + +export = isVectorLike; diff --git a/is-vector-like/docs/types/test.ts b/is-vector-like/docs/types/test.ts new file mode 100644 index 00000000..be27fa56 --- /dev/null +++ b/is-vector-like/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isVectorLike = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isVectorLike( [] ); // $ExpectType boolean +} + +// The function does not compile if provided an unsupported number of arguments... +{ + isVectorLike(); // $ExpectError + isVectorLike( 'abc', 123 ); // $ExpectError +} diff --git a/is-vector-like/examples/index.js b/is-vector-like/examples/index.js new file mode 100644 index 00000000..f6a8dab1 --- /dev/null +++ b/is-vector-like/examples/index.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isVectorLike = require( './../lib' ); + +var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 4 ], [ 1 ], 0, 'row-major' ); +console.log( isVectorLike( arr ) ); +// => true + +console.log( isVectorLike( [ 1, 2, 3, 4 ] ) ); +// => false + +console.log( isVectorLike( {} ) ); +// => false + +console.log( isVectorLike( null ) ); +// => false diff --git a/is-vector-like/lib/index.js b/is-vector-like/lib/index.js new file mode 100644 index 00000000..6192f4b9 --- /dev/null +++ b/is-vector-like/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test if a value is a 1-dimensional ndarray-like object. +* +* @module @stdlib/assert/is-vector-like +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var isVectorLike = require( '@stdlib/assert/is-vector-like' ); +* +* var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 4 ], [ 1 ], 0, 'row-major' ); +* var bool = isVectorLike( arr ); +* // returns true +* +* bool = isVectorLike( [] ); +* // returns false +*/ + +// MODULES // + +var isVectorLike = require( './main.js' ); + + +// EXPORTS // + +module.exports = isVectorLike; diff --git a/is-vector-like/lib/main.js b/is-vector-like/lib/main.js new file mode 100644 index 00000000..e715ee71 --- /dev/null +++ b/is-vector-like/lib/main.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); + + +// MAIN // + +/** +* Tests if a value is a 1-dimensional ndarray-like object. +* +* @param {*} v - value to test +* @returns {boolean} boolean indicating if a value is a 1-dimensional ndarray-like object +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 4 ], [ 1 ], 0, 'row-major' ); +* var bool = isVectorLike( arr ); +* // returns true +* +* bool = isVectorLike( [] ); +* // returns false +*/ +function isVectorLike( v ) { + return ( + isndarrayLike( v ) && + v.ndims === 1 && + v.shape.length === 1 && + v.strides.length === 1 + ); +} + + +// EXPORTS // + +module.exports = isVectorLike; diff --git a/is-vector-like/package.json b/is-vector-like/package.json new file mode 100644 index 00000000..4c3b68b9 --- /dev/null +++ b/is-vector-like/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-vector-like", + "version": "0.0.0", + "description": "Test if a value is a 1-dimensional ndarray-like object.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "is", + "isndarray", + "ndarray", + "ndarray-like", + "vector", + "isvector", + "vec", + "1d", + "validate", + "isvalid", + "test", + "type", + "check" + ] +} diff --git a/is-vector-like/test/test.js b/is-vector-like/test/test.js new file mode 100644 index 00000000..a61e0ee1 --- /dev/null +++ b/is-vector-like/test/test.js @@ -0,0 +1,87 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var noop = require( '@stdlib/utils/noop' ); +var isVectorLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isVectorLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 1-dimensional ndarray', function test( t ) { + var arr = ndarray( 'generic', [ 0, 0, 0, 0 ], [ 4 ], [ 1 ], 0, 'row-major' ); + t.equal( isVectorLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a 1-dimensional ndarray-like object', function test( t ) { + var arr = { + 'data': [ 0, 0, 0, 0 ], + 'shape': [ 4 ], + 'strides': [ 1 ], + 'offset': 0, + 'order': 'row-major', + 'ndims': 1, + 'dtype': 'generic', + 'length': 4, + 'flags': {}, + 'get': noop, + 'set': noop + }; + + t.equal( isVectorLike( arr ), true, 'returns true' ); + t.end(); +}); + +tape( 'the function returns `false` if not provided a 1-dimensional ndarray-like object', function test( t ) { + var values; + var arr; + var i; + + arr = ndarray( 'generic', [ 0, 0, 0 ], [ 3, 1, 1 ], [ 1, 1, 1 ], 0, 'row-major' ); + + values = [ + arr, + '5', + 5, + NaN, + null, + void 0, + true, + false, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.equal( isVectorLike( values[i] ), false, 'returns false when provided '+values[i] ); + } + t.end(); +}); diff --git a/is-web-worker/README.md b/is-web-worker/README.md new file mode 100644 index 00000000..b499281b --- /dev/null +++ b/is-web-worker/README.md @@ -0,0 +1,81 @@ + + +# isWebWorker + +> Check if the runtime is a [web worker][mdn-web-workers-api]. + +
+ +## Usage + +```javascript +var IS_WEB_WORKER = require( '@stdlib/assert/is-web-worker' ); +``` + +#### IS_WEB_WORKER + +`Boolean` indicating if the runtime is a [web worker][mdn-web-workers-api]. + +```javascript +var bool = IS_WEB_WORKER; +// returns +``` + +
+ + + +
+ +## Notes + +- In order to determine whether the runtime is a [web worker][mdn-web-workers-api], the implementation must resolve the global scope, thus requiring function generation. The use of function generation may be problematic in browser contexts enforcing a strict [content security policy][mdn-csp] (CSP). + +
+ + + +
+ +## Examples + + + +```javascript +var IS_WEB_WORKER = require( '@stdlib/assert/is-web-worker' ); + +console.log( IS_WEB_WORKER ); +// => +``` + +
+ + + + + + diff --git a/is-web-worker/docs/repl.txt b/is-web-worker/docs/repl.txt new file mode 100644 index 00000000..d82f07fa --- /dev/null +++ b/is-web-worker/docs/repl.txt @@ -0,0 +1,12 @@ + +{{alias}} + Boolean indicating if the runtime is a web worker. + + Examples + -------- + > {{alias}} + + + See Also + -------- + diff --git a/is-web-worker/docs/types/index.d.ts b/is-web-worker/docs/types/index.d.ts new file mode 100644 index 00000000..432618f1 --- /dev/null +++ b/is-web-worker/docs/types/index.d.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Boolean indicating if the runtime is a web worker. +* +* @example +* var bool = IS_WEB_WORKER; +* // returns +*/ +declare const IS_WEB_WORKER: boolean; + + +// EXPORTS // + +export = IS_WEB_WORKER; diff --git a/is-web-worker/docs/types/test.ts b/is-web-worker/docs/types/test.ts new file mode 100644 index 00000000..958ee424 --- /dev/null +++ b/is-web-worker/docs/types/test.ts @@ -0,0 +1,28 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import IS_WEB_WORKER = require( './index' ); + + +// TESTS // + +// The variable is a boolean... +{ + // tslint:disable-next-line:no-unused-expression + IS_WEB_WORKER; // $ExpectType boolean +} diff --git a/is-web-worker/examples/index.js b/is-web-worker/examples/index.js new file mode 100644 index 00000000..37c180cc --- /dev/null +++ b/is-web-worker/examples/index.js @@ -0,0 +1,24 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var IS_WEB_WORKER = require( './../lib' ); + +console.log( IS_WEB_WORKER ); +// => diff --git a/is-web-worker/lib/global_scope.js b/is-web-worker/lib/global_scope.js new file mode 100644 index 00000000..86eafdbd --- /dev/null +++ b/is-web-worker/lib/global_scope.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new-func */ + +'use strict'; + +// VARIABLES // + +var scope; + + +// FUNCTIONS // + +/** +* Test if the global scope is bound to the "self" variable present in browser web worker environments. When creating a new function using the `Function(){}` constructor, the execution scope aliased by the `this` variable is the global scope. +* +* @private +* @returns {boolean} boolean indicating if global scope is bound to "self" variable +*/ +function globalScope() { + var fcn = ''; + fcn += 'try {'; + fcn += 'return this === self;'; + fcn += '} catch ( err ) {'; + fcn += 'return false;'; + fcn += '}'; + return (new Function( fcn ))(); +} + + +// MAIN // + +scope = globalScope(); + + +// EXPORTS // + +module.exports = scope; diff --git a/is-web-worker/lib/index.js b/is-web-worker/lib/index.js new file mode 100644 index 00000000..c87d804e --- /dev/null +++ b/is-web-worker/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Boolean indicating if the runtime is a web worker. +* +* @module @stdlib/assert/is-web-worker +* +* @example +* var IS_WEB_WORKER = require( '@stdlib/assert/is-web-worker' ); +* +* var bool = IS_WEB_WORKER; +* // returns +*/ + +// MODULES // + +var isWebWorker = require( './main.js' ); + + +// MAIN // + +var bool = isWebWorker(); + + +// EXPORTS // + +module.exports = bool; diff --git a/is-web-worker/lib/main.js b/is-web-worker/lib/main.js new file mode 100644 index 00000000..72c7d25f --- /dev/null +++ b/is-web-worker/lib/main.js @@ -0,0 +1,84 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* global WorkerGlobalScope, WorkerNavigator, WorkerLocation, self, importScripts, navigator, location */ + +'use strict'; + +// MODULES // + +var getGlobal = require( '@stdlib/utils/global' ); +var IS_NODE = require( '@stdlib/assert/is-node' ); +var isObject = require( '@stdlib/assert/is-plain-object' ); +var globalScope = require( './global_scope.js' ); + + +// VARIABLES // + +var Global = getGlobal(); + + +// MAIN // + +/** +* Returns a boolean indicating if the runtime is a web worker. +* +* @returns {boolean} boolean indicating if runtime is a web worker +* +* @example +* var bool = isWebWorker(); +* // returns +*/ +function isWebWorker() { + return ( + // Check that we are not running in a Node.js runtime: + IS_NODE === false && + + // Check for presence of `WorkerGlobalScope` global variable: + typeof WorkerGlobalScope === 'object' && + + // Check for presence of `WorkerNavigator` global variable: + isObject( WorkerNavigator ) && + + // Check that the `navigator` global object is an instance of `WorkerNavigator`: + navigator instanceof WorkerNavigator && + + // Check for presence of `WorkerLocation` global variable: + isObject( WorkerLocation ) && + + // Check that the `location` global object is an instance of `WorkerLocation`: + location instanceof WorkerLocation && + + // Check for presence of `self` variable: + typeof self === 'object' && + + // Check that the `self` variable matches the determined global variable: + self === Global && + + // Check that the `self` variable is equal to the global scope: + globalScope === true && + + // Check for presence of `importScripts` function: + typeof importScripts === 'function' + ); +} + + +// EXPORTS // + +module.exports = isWebWorker; diff --git a/is-web-worker/package.json b/is-web-worker/package.json new file mode 100644 index 00000000..db6032d1 --- /dev/null +++ b/is-web-worker/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/assert/is-web-worker", + "version": "0.0.0", + "description": "Check if the runtime is a web worker.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "runtime", + "environment", + "web", + "browser", + "worker", + "web worker" + ] +} diff --git a/is-web-worker/test/test.js b/is-web-worker/test/test.js new file mode 100644 index 00000000..d24debf9 --- /dev/null +++ b/is-web-worker/test/test.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var IS_WEB_WORKER = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_WEB_WORKER, 'boolean', 'main export is a boolean' ); + t.end(); +}); diff --git a/is-web-worker/test/test.main.js b/is-web-worker/test/test.main.js new file mode 100644 index 00000000..a615aa45 --- /dev/null +++ b/is-web-worker/test/test.main.js @@ -0,0 +1,400 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* global global */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var isWebWorker = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isWebWorker, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if the runtime is a web worker', function test( t ) { + var isWebWorker; + var scope; + + scope = { + 'self': {} + }; + global.WorkerGlobalScope = scope; + global.WorkerNavigator = WorkerNavigator; + global.WorkerLocation = WorkerLocation; + + global.self = scope.self; + global.importScripts = importScripts; + global.navigator = new WorkerNavigator(); + global.location = new WorkerLocation(); + + isWebWorker = proxyquire( './../lib/main.js', { + '@stdlib/utils/global': getGlobal, + '@stdlib/assert/is-node': false, + '@stdlib/assert/is-plain-object': alwaysTrue, + './global_scope.js': true + }); + + t.equal( isWebWorker(), true, 'returns true' ); + t.end(); + + function getGlobal() { + return scope.self; + } + + function importScripts() {} + + function alwaysTrue() { + return true; + } + + function WorkerNavigator() { + return this; + } + + function WorkerLocation() { + return this; + } +}); + +tape( 'the function returns `false` if the runtime is not a web worker (`WorkerGlobalScope` global is not an object)', function test( t ) { + var isWebWorker; + var scope; + + scope = true; + global.WorkerGlobalScope = scope; + global.WorkerNavigator = WorkerNavigator; + global.WorkerLocation = WorkerLocation; + + global.self = {}; + global.importScripts = importScripts; + global.navigator = new WorkerNavigator(); + global.location = new WorkerLocation(); + + isWebWorker = proxyquire( './../lib/main.js', { + '@stdlib/utils/global': getGlobal, + '@stdlib/assert/is-node': false, + '@stdlib/assert/is-plain-object': alwaysTrue, + './global_scope.js': true + }); + + t.equal( isWebWorker(), false, 'returns false' ); + t.end(); + + function getGlobal() { + return global.self; + } + + function importScripts() {} + + function alwaysTrue() { + return true; + } + + function WorkerNavigator() { + return this; + } + + function WorkerLocation() { + return this; + } +}); + +tape( 'the function returns `false` if the runtime is not a web worker (`WorkerNavigator` global is not an object)', function test( t ) { + var isWebWorker; + var scope; + + scope = { + 'self': {} + }; + global.WorkerGlobalScope = scope; + global.WorkerNavigator = WorkerNavigator; + global.WorkerLocation = WorkerLocation; + + global.self = scope.self; + global.importScripts = importScripts; + global.navigator = new WorkerNavigator(); + global.location = new WorkerLocation(); + + isWebWorker = proxyquire( './../lib/main.js', { + '@stdlib/utils/global': getGlobal, + '@stdlib/assert/is-node': false, + '@stdlib/assert/is-plain-object': isObject, + './global_scope.js': true + }); + + t.equal( isWebWorker(), false, 'returns false' ); + t.end(); + + function getGlobal() { + return scope.self; + } + + function importScripts() {} + + function isObject( v ) { + if ( v === WorkerNavigator ) { + return false; + } + return true; + } + + function WorkerNavigator() { + return this; + } + + function WorkerLocation() { + return this; + } +}); + +tape( 'the function returns `false` if the runtime is not a web worker (`navigator` global is not an instance of `WorkerNavigator`)', function test( t ) { + var isWebWorker; + var scope; + + scope = { + 'self': {} + }; + global.WorkerGlobalScope = scope; + global.WorkerNavigator = WorkerNavigator; + global.WorkerLocation = WorkerLocation; + + global.self = scope.self; + global.importScripts = importScripts; + global.navigator = {}; + global.location = new WorkerLocation(); + + isWebWorker = proxyquire( './../lib/main.js', { + '@stdlib/utils/global': getGlobal, + '@stdlib/assert/is-node': false, + '@stdlib/assert/is-plain-object': alwaysTrue, + './global_scope.js': true + }); + + t.equal( isWebWorker(), false, 'returns false' ); + t.end(); + + function getGlobal() { + return scope.self; + } + + function importScripts() {} + + function alwaysTrue() { + return true; + } + + function WorkerNavigator() { + return this; + } + + function WorkerLocation() { + return this; + } +}); + +tape( 'the function returns `false` if the runtime is not a web worker (`WorkerLocation` global is not an object)', function test( t ) { + var isWebWorker; + var scope; + + scope = { + 'self': {} + }; + global.WorkerGlobalScope = scope; + global.WorkerNavigator = WorkerNavigator; + global.WorkerLocation = WorkerLocation; + + global.self = scope.self; + global.importScripts = importScripts; + global.navigator = new WorkerNavigator(); + global.location = new WorkerLocation(); + + isWebWorker = proxyquire( './../lib/main.js', { + '@stdlib/utils/global': getGlobal, + '@stdlib/assert/is-node': false, + '@stdlib/assert/is-plain-object': isObject, + './global_scope.js': true + }); + + t.equal( isWebWorker(), false, 'returns false' ); + t.end(); + + function getGlobal() { + return scope.self; + } + + function importScripts() {} + + function isObject( v ) { + if ( v === WorkerLocation ) { + return false; + } + return true; + } + + function WorkerNavigator() { + return this; + } + + function WorkerLocation() { + return this; + } +}); + +tape( 'the function returns `false` if the runtime is not a web worker (`location` global is not an instance of `WorkerLocation`)', function test( t ) { + var isWebWorker; + var scope; + + scope = { + 'self': {} + }; + global.WorkerGlobalScope = scope; + global.WorkerNavigator = WorkerNavigator; + global.WorkerLocation = WorkerLocation; + + global.self = scope.self; + global.importScripts = importScripts; + global.navigator = new WorkerNavigator(); + global.location = {}; + + isWebWorker = proxyquire( './../lib/main.js', { + '@stdlib/utils/global': getGlobal, + '@stdlib/assert/is-node': false, + '@stdlib/assert/is-plain-object': alwaysTrue, + './global_scope.js': true + }); + + t.equal( isWebWorker(), false, 'returns false' ); + t.end(); + + function getGlobal() { + return scope.self; + } + + function importScripts() {} + + function alwaysTrue() { + return true; + } + + function WorkerNavigator() { + return this; + } + + function WorkerLocation() { + return this; + } +}); + +tape( 'the function returns `false` if the runtime is not a web worker (`self` global is not the detected global variable)', function test( t ) { + var isWebWorker; + var scope; + + scope = { + 'self': {} + }; + global.WorkerGlobalScope = scope; + global.WorkerNavigator = WorkerNavigator; + global.WorkerLocation = WorkerLocation; + + global.self = scope.self; + global.importScripts = importScripts; + global.navigator = new WorkerNavigator(); + global.location = new WorkerLocation(); + + isWebWorker = proxyquire( './../lib/main.js', { + '@stdlib/utils/global': getGlobal, + '@stdlib/assert/is-node': false, + '@stdlib/assert/is-plain-object': alwaysTrue, + './global_scope.js': true + }); + + t.equal( isWebWorker(), false, 'returns false' ); + t.end(); + + function getGlobal() { + return {}; + } + + function importScripts() {} + + function alwaysTrue() { + return true; + } + + function WorkerNavigator() { + return this; + } + + function WorkerLocation() { + return this; + } +}); + +tape( 'the function returns `false` if the runtime is not a web worker (`self` global is not equal to the global scope)', function test( t ) { + var isWebWorker; + var scope; + + scope = { + 'self': {} + }; + global.WorkerGlobalScope = scope; + global.WorkerNavigator = WorkerNavigator; + global.WorkerLocation = WorkerLocation; + + global.self = scope.self; + global.importScripts = importScripts; + global.navigator = new WorkerNavigator(); + global.location = new WorkerLocation(); + + isWebWorker = proxyquire( './../lib/main.js', { + '@stdlib/utils/global': getGlobal, + '@stdlib/assert/is-node': false, + '@stdlib/assert/is-plain-object': alwaysTrue, + './global_scope.js': false + }); + + t.equal( isWebWorker(), false, 'returns false' ); + t.end(); + + function getGlobal() { + return scope.self; + } + + function importScripts() {} + + function alwaysTrue() { + return true; + } + + function WorkerNavigator() { + return this; + } + + function WorkerLocation() { + return this; + } +}); diff --git a/is-whitespace/README.md b/is-whitespace/README.md new file mode 100644 index 00000000..ad660010 --- /dev/null +++ b/is-whitespace/README.md @@ -0,0 +1,89 @@ + + +# isWhitespace + +> Test whether a string contains only [white space][whitespace] characters. + +
+ +## Usage + +```javascript +var isWhitespace = require( '@stdlib/assert/is-whitespace' ); +``` + +#### isWhitespace( value ) + +Tests whether a string contains only [white space][whitespace] characters. + +```javascript +var bool = isWhitespace( ' ' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- A [white space][whitespace] character is defined as one of the 25 characters defined as a [white space][whitespace] ("WSpace=Y","WS") character in the Unicode 9.0 character database, as well as one related [white space][whitespace] character without the Unicode character property "WSpace=Y" (zero width non-breaking space which was deprecated as of Unicode 3.2). +- For non-string values, the function returns `false`. + +
+ + + +
+ +## Examples + + + +```javascript +var isWhitespace = require( '@stdlib/assert/is-whitespace' ); + +var out = isWhitespace( ' ' ); +// returns true + +out = isWhitespace( '' ); +// returns false + +out = isWhitespace( '\\r\\n' ); +// returns false + +out = isWhitespace( 123 ); +// returns false +``` + +
+ + + + + + diff --git a/is-whitespace/benchmark/benchmark.js b/is-whitespace/benchmark/benchmark.js new file mode 100644 index 00000000..a47b4fa4 --- /dev/null +++ b/is-whitespace/benchmark/benchmark.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var isWhitespace = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::single-character', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = fromCodePoint( i%200 ); + bool = isWhitespace( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::string', function benchmark( b ) { + var bool; + var str; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = ' \n\r\t\v ' + fromCodePoint( i%200 ); + bool = isWhitespace( str ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-whitespace/bin/cli b/is-whitespace/bin/cli new file mode 100644 index 00000000..76b1d6e6 --- /dev/null +++ b/is-whitespace/bin/cli @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/tools/cli' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var reEOL = require( '@stdlib/regexp/eol' ); +var isWhitespace = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + console.log( isWhitespace( String( args[ 0 ] ) ) ); // eslint-disable-line no-console + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( reEOL.REGEXP ); + for ( i = 0; i < lines.length; i++ ) { + console.log( isWhitespace( lines[ i ] ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/is-whitespace/docs/repl.txt b/is-whitespace/docs/repl.txt new file mode 100644 index 00000000..2fb24b91 --- /dev/null +++ b/is-whitespace/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( str ) + Tests whether a string contains only white space characters. + + A white space character is defined as one of the 25 characters defined as a + white space ("WSpace=Y","WS") character in the Unicode 9.0 character + database, as well as one related white space character without the Unicode + character property "WSpace=Y" (zero width non-breaking space which was + deprecated as of Unicode 3.2). + + Parameters + ---------- + str: string + Input string. + + Returns + ------- + bool: boolean + Boolean indicating whether a string contains only white space + characters. + + Examples + -------- + > var bool = {{alias}}( ' ' ) + true + > bool = {{alias}}( 'abcdef' ) + false + > bool = {{alias}}( '' ) + false + + See Also + -------- + diff --git a/is-whitespace/docs/types/index.d.ts b/is-whitespace/docs/types/index.d.ts new file mode 100644 index 00000000..324cb694 --- /dev/null +++ b/is-whitespace/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests whether a string contains only white space characters. +* +* @param x - value to test +* @returns boolean indicating if a string contains only white space characters +* +* @example +* var out = isWhitespace( ' ' ); +* // returns true +* +* @example +* var out = isWhitespace( 'beep boop' ); +* // returns false +* +* @example +* var out = isWhitespace( '' ); +* // returns false +* +* @example +* var out = isWhitespace( 123 ); +* // returns false +*/ +declare function isWhitespace( x: any ): boolean; + + +// EXPORTS // + +export = isWhitespace; diff --git a/is-whitespace/docs/types/test.ts b/is-whitespace/docs/types/test.ts new file mode 100644 index 00000000..305f6f5b --- /dev/null +++ b/is-whitespace/docs/types/test.ts @@ -0,0 +1,34 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isWhitespace = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + isWhitespace( 'abd' ); // $ExpectType boolean + isWhitespace( 123 ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isWhitespace(); // $ExpectError + isWhitespace( 'abc', 123 ); // $ExpectError +} diff --git a/is-whitespace/docs/usage.txt b/is-whitespace/docs/usage.txt new file mode 100644 index 00000000..eb0d8c7a --- /dev/null +++ b/is-whitespace/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: is-whitespace [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + diff --git a/is-whitespace/etc/cli_opts.json b/is-whitespace/etc/cli_opts.json new file mode 100644 index 00000000..f245a17e --- /dev/null +++ b/is-whitespace/etc/cli_opts.json @@ -0,0 +1,14 @@ +{ + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/is-whitespace/examples/index.js b/is-whitespace/examples/index.js new file mode 100644 index 00000000..62b0d934 --- /dev/null +++ b/is-whitespace/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isWhitespace = require( './../lib' ); + +console.log( isWhitespace( ' ' ) ); +// => true + +console.log( isWhitespace( '' ) ); +// => false + +console.log( isWhitespace( '\\r\\n' ) ); +// => false + +console.log( isWhitespace( 123 ) ); +// => false diff --git a/is-whitespace/lib/index.js b/is-whitespace/lib/index.js new file mode 100644 index 00000000..beab8595 --- /dev/null +++ b/is-whitespace/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether a string contains only white space characters. +* +* @module @stdlib/assert/is-whitespace +* +* @example +* var isWhitespace = require( '@stdlib/assert/is-whitespace' ); +* +* var out = isWhitespace( ' ' ); +* // returns true +* +* out = isWhitespace( 'beep boop' ); +* // returns false +* +* out = isWhitespace( '' ); +* // returns false +*/ + +// MODULES // + +var isWhitespace = require( './main.js' ); + + +// EXPORTS // + +module.exports = isWhitespace; diff --git a/is-whitespace/lib/main.js b/is-whitespace/lib/main.js new file mode 100644 index 00000000..35ede236 --- /dev/null +++ b/is-whitespace/lib/main.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var reWhitespace = require( '@stdlib/regexp/whitespace' ); + + +// VARIABLES // + +var RE = new RegExp( '^'+reWhitespace.REGEXP.source+'+$' ); + + +// MAIN // + +/** +* Tests whether a string contains only white space characters. +* +* @param {*} x - value to test +* @returns {boolean} boolean indicating if a string contains only white space characters +* +* @example +* var out = isWhitespace( ' ' ); +* // returns true +* +* @example +* var out = isWhitespace( 'beep boop' ); +* // returns false +* +* @example +* var out = isWhitespace( '' ); +* // returns false +* +* @example +* var out = isWhitespace( 123 ); +* // returns false +*/ +function isWhitespace( x ) { + if ( !isString( x ) ) { + return false; + } + return RE.test( x ); +} + + +// EXPORTS // + +module.exports = isWhitespace; diff --git a/is-whitespace/package.json b/is-whitespace/package.json new file mode 100644 index 00000000..8f4a1ef8 --- /dev/null +++ b/is-whitespace/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/assert/is-whitespace", + "version": "0.0.0", + "description": "Test whether a string contains only white space characters.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": { + "is-whitespace": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "string", + "str", + "whitespace", + "space", + "is", + "isstring", + "iswhitespace", + "isspace", + "validate", + "valid", + "isvalid", + "test", + "check" + ] +} diff --git a/is-whitespace/test/fixtures/stdin_error.js.txt b/is-whitespace/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..30db78fd --- /dev/null +++ b/is-whitespace/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/is-whitespace/test/test.cli.js b/is-whitespace/test/test.cli.js new file mode 100644 index 00000000..90105323 --- /dev/null +++ b/is-whitespace/test/test.cli.js @@ -0,0 +1,219 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface tests if a string argument consists only of white space characters', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \' \'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf " \nbeep"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'true\nfalse\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/is-whitespace/test/test.js b/is-whitespace/test/test.js new file mode 100644 index 00000000..8353a7e3 --- /dev/null +++ b/is-whitespace/test/test.js @@ -0,0 +1,102 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isWhitespace = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof isWhitespace, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if provided a string containing all white space characters', function test( t ) { + var values; + var i; + + values = [ + '\r', + '\n', + ' ', + '\t', + '\v', + '\u0009', + '\u000A', + '\u000B', + '\u000C', + '\u000D', + '\u0020', + '\u0085', + '\u00A0', + '\u1680', + '\u2000', + '\u2001', + '\u2002', + '\u2003', + '\u2004', + '\u2005', + '\u2006', + '\u2007', + '\u2008', + '\u2009', + '\u200A', + '\u2028', + '\u2029', + '\u202F', + '\u205F', + '\u3000', + '\uFEFF', + ' ' + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isWhitespace( values[i] ), true, 'returns true when provided ' + values[i] ); + } + t.end(); +}); + +tape( 'the function returns `false` if not provided a string containing all white space characters', function test( t ) { + var values; + var i; + + values = [ + '', + 'beep boop', + new String( ' ' ), // eslint-disable-line no-new-wrappers + 5, + null, + NaN, + undefined, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( isWhitespace( values[i] ), false, 'returns false when provided ' + values[i] ); + } + t.end(); +}); diff --git a/is-windows/README.md b/is-windows/README.md new file mode 100644 index 00000000..54e6f1f6 --- /dev/null +++ b/is-windows/README.md @@ -0,0 +1,71 @@ + + +# isWindows + +> Boolean indicating if the current process is running on Windows. + +
+ +## Usage + +```javascript +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +``` + +#### IS_WINDOWS + +`Boolean` indicating if the current process is running on Windows. + +```javascript +console.log( IS_WINDOWS ); +// => +``` + +
+ + + +
+ +## Examples + + + +```javascript +var PLATFORM = require( '@stdlib/os/platform' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); + +if ( IS_WINDOWS ) { + console.log( 'Running on Windows...' ); +} else { + console.log( 'Running on %s...', PLATFORM ); +} +``` + +
+ + + + + + diff --git a/is-windows/docs/repl.txt b/is-windows/docs/repl.txt new file mode 100644 index 00000000..5ace8332 --- /dev/null +++ b/is-windows/docs/repl.txt @@ -0,0 +1,12 @@ + +{{alias}} + Boolean indicating if the current process is running on Windows. + + Examples + -------- + > {{alias}} + + + See Also + -------- + diff --git a/is-windows/docs/types/index.d.ts b/is-windows/docs/types/index.d.ts new file mode 100644 index 00000000..9249b798 --- /dev/null +++ b/is-windows/docs/types/index.d.ts @@ -0,0 +1,38 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Boolean indicating if the current process is running on Windows. +* +* @example +* var PLATFORM = require( `@stdlib/os/platform` ); +* +* if ( IS_WINDOWS ) { +* console.log( 'Running on Windows...' ); +* } else { +* console.log( 'Running on %s...', PLATFORM ); +* } +*/ +declare const IS_WINDOWS: boolean; + + +// EXPORTS // + +export = IS_WINDOWS; diff --git a/is-windows/docs/types/test.ts b/is-windows/docs/types/test.ts new file mode 100644 index 00000000..92027ac9 --- /dev/null +++ b/is-windows/docs/types/test.ts @@ -0,0 +1,28 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import IS_WINDOWS = require( './index' ); + + +// TESTS // + +// The variable is a boolean... +{ + // tslint:disable-next-line:no-unused-expression + IS_WINDOWS; // $ExpectType boolean +} diff --git a/is-windows/examples/index.js b/is-windows/examples/index.js new file mode 100644 index 00000000..57c85b14 --- /dev/null +++ b/is-windows/examples/index.js @@ -0,0 +1,28 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var PLATFORM = require( '@stdlib/os/platform' ); +var IS_WINDOWS = require( './../lib' ); + +if ( IS_WINDOWS ) { + console.log( 'Running on Windows...' ); +} else { + console.log( 'Running on %s...', PLATFORM ); +} diff --git a/is-windows/lib/index.js b/is-windows/lib/index.js new file mode 100644 index 00000000..4a0ba137 --- /dev/null +++ b/is-windows/lib/index.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Boolean indicating if the current process is running on Windows. +* +* @module @stdlib/assert/is-windows +* @type {boolean} +* +* @example +* var PLATFORM = require( '@stdlib/os/platform' ); +* var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +* +* if ( IS_WINDOWS ) { +* console.log( 'Running on Windows...' ); +* } else { +* console.log( 'Running on %s...', PLATFORM ); +* } +*/ + +// MODULES // + +var PLATFORM = require( '@stdlib/os/platform' ); + + +// MAIN // + +/** +* Boolean indicating if the current process is running on Windows. +* +* @constant +* @type {boolean} +*/ +var IS_WINDOWS = ( PLATFORM === 'win32' ); + + +// EXPORTS // + +module.exports = IS_WINDOWS; diff --git a/is-windows/package.json b/is-windows/package.json new file mode 100644 index 00000000..ac595b21 --- /dev/null +++ b/is-windows/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/is-windows", + "version": "0.0.0", + "description": "Boolean indicating if the current process is running on Windows.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utils", + "util", + "utilities", + "utility", + "process", + "platform", + "os", + "check", + "windows", + "posix", + "linux", + "darwin", + "is", + "iswindows", + "win32" + ] +} diff --git a/is-windows/test/test.js b/is-windows/test/test.js new file mode 100644 index 00000000..93f6a15f --- /dev/null +++ b/is-windows/test/test.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var IS_WINDOWS = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a boolean', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof IS_WINDOWS, 'boolean', 'main export is a boolean' ); + t.end(); +}); + +tape( 'the main export is `true` if the current process is running on Windows', function test( t ) { + var IS_WINDOWS; + + IS_WINDOWS = proxyquire( './../lib', { + '@stdlib/os/platform': 'win32' + }); + t.equal( IS_WINDOWS, true, 'is true' ); + t.end(); +}); + +tape( 'the main export is `false` if the current process is not running on Windows', function test( t ) { + var IS_WINDOWS; + + IS_WINDOWS = proxyquire( './../lib', { + '@stdlib/os/platform': 'darwin' + }); + t.equal( IS_WINDOWS, false, 'is false' ); + t.end(); +}); diff --git a/is-writable-property-in/README.md b/is-writable-property-in/README.md new file mode 100644 index 00000000..68ddac04 --- /dev/null +++ b/is-writable-property-in/README.md @@ -0,0 +1,148 @@ + + +# isWritablePropertyIn + +> Test if an object's own or inherited property is writable. + +
+ +## Usage + +```javascript +var isWritablePropertyIn = require( '@stdlib/assert/is-writable-property-in' ); +``` + +#### isWritablePropertyIn( value, property ) + +Returns a `boolean` indicating if a `value` has a writable `property`. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var obj = { + 'foo': 'bar' +}; + +defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'boop' +}); + +defineProperty( obj, 'setter', { + 'configurable': false, + 'enumerable': false, + 'set': function setter( v ) { + obj.foo = v; + } +}); + +var bool = isWritablePropertyIn( obj, 'foo' ); +// returns true + +bool = isWritablePropertyIn( obj, 'setter' ); +// returns true + +bool = isWritablePropertyIn( obj, 'beep' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isWritablePropertyIn( 'beep', 'toString' ); + // returns true + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var obj = { + 'null': 'foo' + }; + + var bool = isWritablePropertyIn( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isWritablePropertyIn = require( '@stdlib/assert/is-writable-property-in' ); + +var bool = isWritablePropertyIn( [ 'a' ], 'length' ); +// returns true + +bool = isWritablePropertyIn( { 'a': 'b' }, 'a' ); +// returns true + +bool = isWritablePropertyIn( [ 'a' ], 0 ); +// returns true + +bool = isWritablePropertyIn( { 'null': false }, null ); +// returns true + +bool = isWritablePropertyIn( { '[object Object]': false }, {} ); +// returns true + +bool = isWritablePropertyIn( {}, 'toString' ); +// returns true + +bool = isWritablePropertyIn( {}, 'hasOwnProperty' ); +// returns true + +bool = isWritablePropertyIn( null, 'a' ); +// returns false + +bool = isWritablePropertyIn( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-writable-property-in/benchmark/benchmark.js b/is-writable-property-in/benchmark/benchmark.js new file mode 100644 index 00000000..ef0ef807 --- /dev/null +++ b/is-writable-property-in/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isWritablePropertyIn = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isWritablePropertyIn( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-writable-property-in/docs/repl.txt b/is-writable-property-in/docs/repl.txt new file mode 100644 index 00000000..0f326466 --- /dev/null +++ b/is-writable-property-in/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, property ) + Tests if an object's own or inherited property is writable. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own or inherited property is writable. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = false; + > desc.writable = false; + > desc.value = 'beep'; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + true + > bool = {{alias}}( obj, 'beep' ) + false + + See Also + -------- + diff --git a/is-writable-property-in/docs/types/index.d.ts b/is-writable-property-in/docs/types/index.d.ts new file mode 100644 index 00000000..43d8eaab --- /dev/null +++ b/is-writable-property-in/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own or inherited property is writable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is writable +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'writable': false, +* 'value': 'beep' +* }); +* +* var bool = isWritablePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isWritablePropertyIn( obj, 'beep' ); +* // returns false +*/ +declare function isWritablePropertyIn( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isWritablePropertyIn; diff --git a/is-writable-property-in/docs/types/test.ts b/is-writable-property-in/docs/types/test.ts new file mode 100644 index 00000000..9cd4194e --- /dev/null +++ b/is-writable-property-in/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isWritablePropertyIn = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + Object.defineProperty( obj, 'beep', { + 'configurable': true, + 'enumerable': true, + 'writable': false, + 'value': 'beep' + } ); + isWritablePropertyIn( obj, 'beep' ); // $ExpectType boolean + isWritablePropertyIn( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isWritablePropertyIn(); // $ExpectError + isWritablePropertyIn( {} ); // $ExpectError + isWritablePropertyIn( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-writable-property-in/examples/index.js b/is-writable-property-in/examples/index.js new file mode 100644 index 00000000..b56bcaaf --- /dev/null +++ b/is-writable-property-in/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isWritablePropertyIn = require( './../lib' ); + +var bool = isWritablePropertyIn( [ 'a' ], 'length' ); +console.log( bool ); +// => true + +bool = isWritablePropertyIn( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = isWritablePropertyIn( [ 'a' ], 0 ); +console.log( bool ); +// => true + +bool = isWritablePropertyIn( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = isWritablePropertyIn( { '[object Object]': false }, {} ); +console.log( bool ); +// => true + +bool = isWritablePropertyIn( {}, 'toString' ); +console.log( bool ); +// => true + +bool = isWritablePropertyIn( {}, 'hasOwnProperty' ); +console.log( bool ); +// => true + +bool = isWritablePropertyIn( null, 'a' ); +console.log( bool ); +// => false + +bool = isWritablePropertyIn( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-writable-property-in/lib/index.js b/is-writable-property-in/lib/index.js new file mode 100644 index 00000000..d2ca8011 --- /dev/null +++ b/is-writable-property-in/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own or inherited property is writable. +* +* @module @stdlib/assert/is-writable-property-in +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isWritablePropertyIn = require( '@stdlib/assert/is-writable-property-in' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'writable': false, +* 'value': 'beep' +* }); +* +* var bool = isWritablePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isWritablePropertyIn( obj, 'beep' ); +* // returns false +*/ + +// MODULES // + +var isWritablePropertyIn = require( './main.js' ); + + +// EXPORTS // + +module.exports = isWritablePropertyIn; diff --git a/is-writable-property-in/lib/main.js b/is-writable-property-in/lib/main.js new file mode 100644 index 00000000..718908e5 --- /dev/null +++ b/is-writable-property-in/lib/main.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptorIn = require( '@stdlib/utils/property-descriptor-in' ); + + +// MAIN // + +/** +* Tests if an object's own or inherited property is writable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is writable +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'writable': false, +* 'value': 'beep' +* }); +* +* var bool = isWritablePropertyIn( obj, 'boop' ); +* // returns true +* +* bool = isWritablePropertyIn( obj, 'beep' ); +* // returns false +*/ +function isWritablePropertyIn( value, property ) { + var desc = propertyDescriptorIn( value, property ); + return ( + desc !== null && + ( + desc.writable === true || + typeof desc.set === 'function' + ) + ); +} + + +// EXPORTS // + +module.exports = isWritablePropertyIn; diff --git a/is-writable-property-in/package.json b/is-writable-property-in/package.json new file mode 100644 index 00000000..f90a6de9 --- /dev/null +++ b/is-writable-property-in/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/assert/is-writable-property-in", + "version": "0.0.0", + "description": "Test if an object's own or inherited property is writable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "in", + "inherited", + "prototype", + "proto", + "writable", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-writable-property-in/test/test.js b/is-writable-property-in/test/test.js new file mode 100644 index 00000000..f9161015 --- /dev/null +++ b/is-writable-property-in/test/test.js @@ -0,0 +1,139 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isWritablePropertyIn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isWritablePropertyIn, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property is writable', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isWritablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isWritablePropertyIn( [ 1, 2, 3 ], '1' ); + t.equal( bool, true, 'returns true' ); + + bool = isWritablePropertyIn( [ 1, 2, 3 ], 1 ); + t.equal( bool, true, 'returns true' ); + + bool = isWritablePropertyIn( new Foo(), 'bar' ); + t.equal( bool, true, 'returns true' ); + + bool = isWritablePropertyIn( [ 'a' ], 'length' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'b' + }); + + bool = isWritablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'set': setter + }); + + bool = isWritablePropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `true` if provided a writable inherited property', function test( t ) { + var bool; + + bool = isWritablePropertyIn( {}, 'hasOwnProperty' ); + t.equal( bool, true, 'returns true' ); + + bool = isWritablePropertyIn( {}, 'constructor' ); + t.equal( bool, true, 'returns true' ); + + bool = isWritablePropertyIn( {}, 'toString' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isWritablePropertyIn( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isWritablePropertyIn( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a writable property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + bool = isWritablePropertyIn( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isWritablePropertyIn( 'beep', 'toString' ); + t.equal( bool, true, 'returns true' ); + + t.end(); +}); diff --git a/is-writable-property/README.md b/is-writable-property/README.md new file mode 100644 index 00000000..8a3d544a --- /dev/null +++ b/is-writable-property/README.md @@ -0,0 +1,148 @@ + + +# isWritableProperty + +> Test if an object's own property is writable. + +
+ +## Usage + +```javascript +var isWritableProperty = require( '@stdlib/assert/is-writable-property' ); +``` + +#### isWritableProperty( value, property ) + +Returns a `boolean` indicating if a `value` has a writable `property`. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var obj = { + 'foo': 'bar' +}; + +defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'boop' +}); + +defineProperty( obj, 'setter', { + 'configurable': false, + 'enumerable': false, + 'set': function setter( v ) { + obj.foo = v; + } +}); + +var bool = isWritableProperty( obj, 'foo' ); +// returns true + +bool = isWritableProperty( obj, 'setter' ); +// returns true + +bool = isWritableProperty( obj, 'beep' ); +// returns false +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isWritableProperty( 'beep', 'length' ); + // returns false + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var obj = { + 'null': 'foo' + }; + + var bool = isWritableProperty( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isWritableProperty = require( '@stdlib/assert/is-writable-property' ); + +var bool = isWritableProperty( [ 'a' ], 'length' ); +// returns true + +bool = isWritableProperty( { 'a': 'b' }, 'a' ); +// returns true + +bool = isWritableProperty( [ 'a' ], 0 ); +// returns true + +bool = isWritableProperty( { 'null': false }, null ); +// returns true + +bool = isWritableProperty( { '[object Object]': false }, {} ); +// returns true + +bool = isWritableProperty( {}, 'toString' ); +// returns false + +bool = isWritableProperty( {}, 'hasOwnProperty' ); +// returns false + +bool = isWritableProperty( null, 'a' ); +// returns false + +bool = isWritableProperty( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-writable-property/benchmark/benchmark.js b/is-writable-property/benchmark/benchmark.js new file mode 100644 index 00000000..3fb20800 --- /dev/null +++ b/is-writable-property/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isWritableProperty = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isWritableProperty( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-writable-property/docs/repl.txt b/is-writable-property/docs/repl.txt new file mode 100644 index 00000000..c4960946 --- /dev/null +++ b/is-writable-property/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, property ) + Tests if an object's own property is writable. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own property is writable. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = false; + > desc.writable = false; + > desc.value = 'beep'; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + true + > bool = {{alias}}( obj, 'beep' ) + false + + See Also + -------- + diff --git a/is-writable-property/docs/types/index.d.ts b/is-writable-property/docs/types/index.d.ts new file mode 100644 index 00000000..96f0eb29 --- /dev/null +++ b/is-writable-property/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own property is writable. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is writable +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'writable': false, +* 'value': 'beep' +* }); +* +* var bool = isWritableProperty( obj, 'boop' ); +* // returns true +* +* bool = isWritableProperty( obj, 'beep' ); +* // returns false +*/ +declare function isWritableProperty( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isWritableProperty; diff --git a/is-writable-property/docs/types/test.ts b/is-writable-property/docs/types/test.ts new file mode 100644 index 00000000..360baaab --- /dev/null +++ b/is-writable-property/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isWritableProperty = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + Object.defineProperty( obj, 'beep', { + 'configurable': true, + 'enumerable': true, + 'writable': false, + 'value': 'beep' + } ); + isWritableProperty( obj, 'beep' ); // $ExpectType boolean + isWritableProperty( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isWritableProperty(); // $ExpectError + isWritableProperty( {} ); // $ExpectError + isWritableProperty( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-writable-property/examples/index.js b/is-writable-property/examples/index.js new file mode 100644 index 00000000..1e5a444f --- /dev/null +++ b/is-writable-property/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isWritableProperty = require( './../lib' ); + +var bool = isWritableProperty( [ 'a' ], 'length' ); +console.log( bool ); +// => true + +bool = isWritableProperty( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => true + +bool = isWritableProperty( [ 'a' ], 0 ); +console.log( bool ); +// => true + +bool = isWritableProperty( { 'null': false }, null ); +console.log( bool ); +// => true + +bool = isWritableProperty( { '[object Object]': false }, {} ); +console.log( bool ); +// => true + +bool = isWritableProperty( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isWritableProperty( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isWritableProperty( null, 'a' ); +console.log( bool ); +// => false + +bool = isWritableProperty( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-writable-property/lib/index.js b/is-writable-property/lib/index.js new file mode 100644 index 00000000..f25bbc53 --- /dev/null +++ b/is-writable-property/lib/index.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own property is writable. +* +* @module @stdlib/assert/is-writable-property +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isWritableProperty = require( '@stdlib/assert/is-writable-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'writable': false, +* 'value': 'beep' +* }); +* +* var bool = isWritableProperty( obj, 'boop' ); +* // returns true +* +* bool = isWritableProperty( obj, 'beep' ); +* // returns false +*/ + +// MODULES // + +var isWritableProperty = require( './main.js' ); + + +// EXPORTS // + +module.exports = isWritableProperty; diff --git a/is-writable-property/lib/main.js b/is-writable-property/lib/main.js new file mode 100644 index 00000000..d8b450df --- /dev/null +++ b/is-writable-property/lib/main.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptor = require( '@stdlib/utils/property-descriptor' ); + + +// MAIN // + +/** +* Tests if an object's own property is writable. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is writable +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': false, +* 'writable': false, +* 'value': 'beep' +* }); +* +* var bool = isWritableProperty( obj, 'boop' ); +* // returns true +* +* bool = isWritableProperty( obj, 'beep' ); +* // returns false +*/ +function isWritableProperty( value, property ) { + var desc = propertyDescriptor( value, property ); + return ( + desc !== null && + ( + desc.writable === true || + typeof desc.set === 'function' + ) + ); +} + + +// EXPORTS // + +module.exports = isWritableProperty; diff --git a/is-writable-property/package.json b/is-writable-property/package.json new file mode 100644 index 00000000..55cc5e58 --- /dev/null +++ b/is-writable-property/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/assert/is-writable-property", + "version": "0.0.0", + "description": "Test if an object's own property is writable.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "writable", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-writable-property/test/test.js b/is-writable-property/test/test.js new file mode 100644 index 00000000..b593e1dd --- /dev/null +++ b/is-writable-property/test/test.js @@ -0,0 +1,139 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isWritableProperty = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isWritableProperty, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property is writable', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isWritableProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isWritableProperty( [ 1, 2, 3 ], '1' ); + t.equal( bool, true, 'returns true' ); + + bool = isWritableProperty( [ 1, 2, 3 ], 1 ); + t.equal( bool, true, 'returns true' ); + + bool = isWritableProperty( new Foo(), 'bar' ); + t.equal( bool, true, 'returns true' ); + + bool = isWritableProperty( [ 'a' ], 'length' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'b' + }); + + bool = isWritableProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'set': setter + }); + + bool = isWritableProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isWritableProperty( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isWritableProperty( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a writable property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + bool = isWritableProperty( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided an inherited property', function test( t ) { + var bool; + + bool = isWritableProperty( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isWritableProperty( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isWritableProperty( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isWritableProperty( 'beep', 'length' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); diff --git a/is-write-only-property-in/README.md b/is-write-only-property-in/README.md new file mode 100644 index 00000000..ca437a6a --- /dev/null +++ b/is-write-only-property-in/README.md @@ -0,0 +1,166 @@ + + +# isWriteOnlyPropertyIn + +> Test if an object's own or inherited property is [write-only][@stdlib/utils/define-write-only-accessor]. + +
+ +## Usage + +```javascript +var isWriteOnlyPropertyIn = require( '@stdlib/assert/is-write-only-property-in' ); +``` + +#### isWriteOnlyPropertyIn( value, property ) + +Returns a `boolean` indicating if a `value` has a [write-only][@stdlib/utils/define-write-only-accessor] `property`. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var bool; +var obj; + +function Foo() { + this.foo = 'bar'; + return this; +} + +defineProperty( Foo.prototype, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'boop' +}); + +defineProperty( Foo.prototype, 'accessor', { + 'configurable': false, + 'enumerable': true, + 'set': function setter( v ) { + obj.foo = v; + } +}); + +obj = new Foo(); + +bool = isWriteOnlyPropertyIn( obj, 'foo' ); +// returns false + +bool = isWriteOnlyPropertyIn( obj, 'beep' ); +// returns false + +bool = isWriteOnlyPropertyIn( obj, 'accessor' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isWriteOnlyPropertyIn( 'beep', 'length' ); + // returns false + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var defineProperty = require( '@stdlib/utils/define-property' ); + + var obj = {}; + + function setter( v ) { + obj.null = v; + } + + defineProperty( obj, 'null', { + 'configurable': false, + 'enumerable': true, + 'set': setter + }); + + var bool = isWriteOnlyPropertyIn( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isWriteOnlyPropertyIn = require( '@stdlib/assert/is-write-only-property-in' ); + +var bool = isWriteOnlyPropertyIn( 'a', 'length' ); +// returns false + +bool = isWriteOnlyPropertyIn( { 'a': 'b' }, 'a' ); +// returns false + +bool = isWriteOnlyPropertyIn( [ 'a' ], 0 ); +// returns false + +bool = isWriteOnlyPropertyIn( { 'null': false }, null ); +// returns false + +bool = isWriteOnlyPropertyIn( { '[object Object]': false }, {} ); +// returns false + +bool = isWriteOnlyPropertyIn( {}, 'toString' ); +// returns false + +bool = isWriteOnlyPropertyIn( {}, 'hasOwnProperty' ); +// returns false + +bool = isWriteOnlyPropertyIn( null, 'a' ); +// returns false + +bool = isWriteOnlyPropertyIn( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-write-only-property-in/benchmark/benchmark.js b/is-write-only-property-in/benchmark/benchmark.js new file mode 100644 index 00000000..14e76637 --- /dev/null +++ b/is-write-only-property-in/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isWriteOnlyPropertyIn = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isWriteOnlyPropertyIn( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-write-only-property-in/docs/repl.txt b/is-write-only-property-in/docs/repl.txt new file mode 100644 index 00000000..aa8777c1 --- /dev/null +++ b/is-write-only-property-in/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( value, property ) + Tests if an object's own or inherited property is write-only. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own or inherited property is write- + only. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = true; + > desc.set = function setter( v ) { obj.boop = v; }; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + false + > bool = {{alias}}( obj, 'beep' ) + true + + See Also + -------- + diff --git a/is-write-only-property-in/docs/types/index.d.ts b/is-write-only-property-in/docs/types/index.d.ts new file mode 100644 index 00000000..e6d34391 --- /dev/null +++ b/is-write-only-property-in/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own or inherited property is write-only. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is write-only +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'set': setter +* }); +* +* var bool = isWriteOnlyPropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isWriteOnlyPropertyIn( obj, 'beep' ); +* // returns true +*/ +declare function isWriteOnlyPropertyIn( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isWriteOnlyPropertyIn; diff --git a/is-write-only-property-in/docs/types/test.ts b/is-write-only-property-in/docs/types/test.ts new file mode 100644 index 00000000..b6008c8d --- /dev/null +++ b/is-write-only-property-in/docs/types/test.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isWriteOnlyPropertyIn = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + + const setter = ( v: boolean ) => { + obj.boop = v; + }; + + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': true, + 'set': setter + } ); + isWriteOnlyPropertyIn( obj, 'beep' ); // $ExpectType boolean + isWriteOnlyPropertyIn( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isWriteOnlyPropertyIn(); // $ExpectError + isWriteOnlyPropertyIn( {} ); // $ExpectError + isWriteOnlyPropertyIn( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-write-only-property-in/examples/index.js b/is-write-only-property-in/examples/index.js new file mode 100644 index 00000000..c09c6c32 --- /dev/null +++ b/is-write-only-property-in/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isWriteOnlyPropertyIn = require( './../lib' ); + +var bool = isWriteOnlyPropertyIn( 'a', 'length' ); +console.log( bool ); +// => false + +bool = isWriteOnlyPropertyIn( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => false + +bool = isWriteOnlyPropertyIn( [ 'a' ], 0 ); +console.log( bool ); +// => false + +bool = isWriteOnlyPropertyIn( { 'null': false }, null ); +console.log( bool ); +// => false + +bool = isWriteOnlyPropertyIn( { '[object Object]': false }, {} ); +console.log( bool ); +// => false + +bool = isWriteOnlyPropertyIn( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isWriteOnlyPropertyIn( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isWriteOnlyPropertyIn( null, 'a' ); +console.log( bool ); +// => false + +bool = isWriteOnlyPropertyIn( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-write-only-property-in/lib/index.js b/is-write-only-property-in/lib/index.js new file mode 100644 index 00000000..bc19131f --- /dev/null +++ b/is-write-only-property-in/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own or inherited property is write-only. +* +* @module @stdlib/assert/is-write-only-property-in +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isWriteOnlyPropertyIn = require( '@stdlib/assert/is-write-only-property-in' ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'set': setter +* }); +* +* var bool = isWriteOnlyPropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isWriteOnlyPropertyIn( obj, 'beep' ); +* // returns true +*/ + +// MODULES // + +var isWriteOnlyPropertyIn = require( './main.js' ); + + +// EXPORTS // + +module.exports = isWriteOnlyPropertyIn; diff --git a/is-write-only-property-in/lib/main.js b/is-write-only-property-in/lib/main.js new file mode 100644 index 00000000..4409387a --- /dev/null +++ b/is-write-only-property-in/lib/main.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptorIn = require( '@stdlib/utils/property-descriptor-in' ); + + +// MAIN // + +/** +* Tests if an object's own or inherited property is write-only. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is write-only +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'set': setter +* }); +* +* var bool = isWriteOnlyPropertyIn( obj, 'boop' ); +* // returns false +* +* bool = isWriteOnlyPropertyIn( obj, 'beep' ); +* // returns true +*/ +function isWriteOnlyPropertyIn( value, property ) { + var desc = propertyDescriptorIn( value, property ); + return ( + desc !== null && + desc.configurable === false && + ( + typeof desc.set === 'function' && + desc.get === void 0 + ) + ); +} + + +// EXPORTS // + +module.exports = isWriteOnlyPropertyIn; diff --git a/is-write-only-property-in/package.json b/is-write-only-property-in/package.json new file mode 100644 index 00000000..65dd706e --- /dev/null +++ b/is-write-only-property-in/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/assert/is-write-only-property-in", + "version": "0.0.0", + "description": "Test if an object's own or inherited property is write-only.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "inherited", + "in", + "prototype", + "proto", + "readable", + "writable", + "write-only", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-write-only-property-in/test/test.js b/is-write-only-property-in/test/test.js new file mode 100644 index 00000000..04ef67ce --- /dev/null +++ b/is-write-only-property-in/test/test.js @@ -0,0 +1,257 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isWriteOnlyPropertyIn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isWriteOnlyPropertyIn, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property is write-only', function test( t ) { + var bool; + var obj; + + obj = {}; + + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': true, + 'set': setter + }); + + defineProperty( obj, 'b', { + 'configurable': false, + 'enumerable': false, + 'set': setter + }); + + bool = isWriteOnlyPropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isWriteOnlyPropertyIn( obj, 'b' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `true` if an inherited object property is write-only', function test( t ) { + var bool; + var obj; + + function Foo() { + return this; + } + + defineProperty( Foo.prototype, 'a', { + 'configurable': false, + 'enumerable': true, + 'set': setter + }); + + defineProperty( Foo.prototype, 'b', { + 'configurable': false, + 'enumerable': false, + 'set': setter + }); + + obj = new Foo(); + + bool = isWriteOnlyPropertyIn( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isWriteOnlyPropertyIn( obj, 'b' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `false` if an object property is not write-only', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isWriteOnlyPropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyPropertyIn( [ 1, 2, 3 ], '1' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyPropertyIn( [ 1, 2, 3 ], 1 ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyPropertyIn( new Foo(), 'bar' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyPropertyIn( [ 'a' ], 'length' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'b' + }); + + bool = isWriteOnlyPropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'get': getter, + 'set': setter + }); + + bool = isWriteOnlyPropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'get': getter + }); + + bool = isWriteOnlyPropertyIn( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function getter() { + // No-op... + } + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isWriteOnlyPropertyIn( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isWriteOnlyPropertyIn( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a write-only property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + defineProperty( obj, 'd', { + 'configurable': true, + 'enumerable': true, + 'get': getter + }); + + defineProperty( obj, 'e', { + 'configurable': true, + 'enumerable': true, + 'set': setter, + 'get': getter + }); + + defineProperty( obj, 'f', { + 'configurable': true, + 'enumerable': false, + 'writable': false, + 'value': 'g' + }); + + bool = isWriteOnlyPropertyIn( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyPropertyIn( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyPropertyIn( obj, 'e' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyPropertyIn( obj, 'f' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function setter() { + // No-op... + } + + function getter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided an inherited property which is not write-only', function test( t ) { + var bool; + + bool = isWriteOnlyPropertyIn( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyPropertyIn( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyPropertyIn( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isWriteOnlyPropertyIn( 'beep', 'length' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); diff --git a/is-write-only-property/README.md b/is-write-only-property/README.md new file mode 100644 index 00000000..71b0e996 --- /dev/null +++ b/is-write-only-property/README.md @@ -0,0 +1,160 @@ + + +# isWriteOnlyProperty + +> Test if an object's own property is [write-only][@stdlib/utils/define-write-only-accessor]. + +
+ +## Usage + +```javascript +var isWriteOnlyProperty = require( '@stdlib/assert/is-write-only-property' ); +``` + +#### isWriteOnlyProperty( value, property ) + +Returns a `boolean` indicating if a `value` has a [write-only][@stdlib/utils/define-write-only-accessor] `property`. + + + +```javascript +var defineProperty = require( '@stdlib/utils/define-property' ); + +var obj = { + 'foo': 'bar' +}; + +defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': false, + 'writable': false, + 'value': 'boop' +}); + +defineProperty( obj, 'accessor', { + 'configurable': false, + 'enumerable': true, + 'set': function setter( v ) { + obj.foo = v; + } +}); + +var bool = isWriteOnlyProperty( obj, 'foo' ); +// returns false + +bool = isWriteOnlyProperty( obj, 'beep' ); +// returns false + +bool = isWriteOnlyProperty( obj, 'accessor' ); +// returns true +``` + +
+ + + +
+ +## Notes + +- Value arguments other than `null` or `undefined` are coerced to `objects`. + + ```javascript + var bool = isWriteOnlyProperty( 'beep', 'length' ); + // returns false + ``` + +- Property arguments are coerced to `strings`. + + ```javascript + var defineProperty = require( '@stdlib/utils/define-property' ); + + var obj = {}; + + function setter( v ) { + obj.null = v; + } + + defineProperty( obj, 'null', { + 'configurable': false, + 'enumerable': true, + 'set': setter + }); + + var bool = isWriteOnlyProperty( obj, null ); + // returns true + ``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var isWriteOnlyProperty = require( '@stdlib/assert/is-write-only-property' ); + +var bool = isWriteOnlyProperty( 'a', 'length' ); +// returns false + +bool = isWriteOnlyProperty( { 'a': 'b' }, 'a' ); +// returns false + +bool = isWriteOnlyProperty( [ 'a' ], 0 ); +// returns false + +bool = isWriteOnlyProperty( { 'null': false }, null ); +// returns false + +bool = isWriteOnlyProperty( { '[object Object]': false }, {} ); +// returns false + +bool = isWriteOnlyProperty( {}, 'toString' ); +// returns false + +bool = isWriteOnlyProperty( {}, 'hasOwnProperty' ); +// returns false + +bool = isWriteOnlyProperty( null, 'a' ); +// returns false + +bool = isWriteOnlyProperty( void 0, 'a' ); +// returns false +``` + +
+ + + + + + diff --git a/is-write-only-property/benchmark/benchmark.js b/is-write-only-property/benchmark/benchmark.js new file mode 100644 index 00000000..d3f2daf0 --- /dev/null +++ b/is-write-only-property/benchmark/benchmark.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var isWriteOnlyProperty = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var bool; + var arr; + var i; + + arr = new Array( 100 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + bool = isWriteOnlyProperty( arr, i ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/is-write-only-property/docs/repl.txt b/is-write-only-property/docs/repl.txt new file mode 100644 index 00000000..f2a82047 --- /dev/null +++ b/is-write-only-property/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( value, property ) + Tests if an object's own property is write-only. + + Parameters + ---------- + value: any + Value to test. + + property: any + Property to test. + + Returns + ------- + bool: boolean + Boolean indicating if an object's own property is write-only. + + Examples + -------- + > var obj = { 'boop': true }; + > var desc = {}; + > desc.configurable = false; + > desc.enumerable = true; + > desc.set = function setter( v ) { obj.boop = v; }; + > {{alias:@stdlib/utils/define-property}}( obj, 'beep', desc ); + > var bool = {{alias}}( obj, 'boop' ) + false + > bool = {{alias}}( obj, 'beep' ) + true + + See Also + -------- + diff --git a/is-write-only-property/docs/types/index.d.ts b/is-write-only-property/docs/types/index.d.ts new file mode 100644 index 00000000..22a5f223 --- /dev/null +++ b/is-write-only-property/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Tests if an object's own property is write-only. +* +* @param value - value to test +* @param property - property to test +* @returns boolean indicating if an object property is write-only +* +* @example +* var defineProperty = require( `@stdlib/utils/define-property` ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'set': setter +* }); +* +* var bool = isWriteOnlyProperty( obj, 'boop' ); +* // returns false +* +* bool = isWriteOnlyProperty( obj, 'beep' ); +* // returns true +*/ +declare function isWriteOnlyProperty( value: any, property: any ): boolean; + + +// EXPORTS // + +export = isWriteOnlyProperty; diff --git a/is-write-only-property/docs/types/test.ts b/is-write-only-property/docs/types/test.ts new file mode 100644 index 00000000..c8e7cdfa --- /dev/null +++ b/is-write-only-property/docs/types/test.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import isWriteOnlyProperty = require( './index' ); + + +// TESTS // + +// The function returns a boolean... +{ + const obj = { + 'boop': true + }; + + const setter = ( v: boolean ) => { + obj.boop = v; + }; + + Object.defineProperty( obj, 'beep', { + 'configurable': false, + 'enumerable': true, + 'set': setter + } ); + isWriteOnlyProperty( obj, 'beep' ); // $ExpectType boolean + isWriteOnlyProperty( obj, 'boop' ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + isWriteOnlyProperty(); // $ExpectError + isWriteOnlyProperty( {} ); // $ExpectError + isWriteOnlyProperty( {}, 'beep', 123 ); // $ExpectError +} diff --git a/is-write-only-property/examples/index.js b/is-write-only-property/examples/index.js new file mode 100644 index 00000000..329d6fd2 --- /dev/null +++ b/is-write-only-property/examples/index.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable object-curly-newline */ + +'use strict'; + +var isWriteOnlyProperty = require( './../lib' ); + +var bool = isWriteOnlyProperty( 'a', 'length' ); +console.log( bool ); +// => false + +bool = isWriteOnlyProperty( { 'a': 'b' }, 'a' ); +console.log( bool ); +// => false + +bool = isWriteOnlyProperty( [ 'a' ], 0 ); +console.log( bool ); +// => false + +bool = isWriteOnlyProperty( { 'null': false }, null ); +console.log( bool ); +// => false + +bool = isWriteOnlyProperty( { '[object Object]': false }, {} ); +console.log( bool ); +// => false + +bool = isWriteOnlyProperty( {}, 'toString' ); +console.log( bool ); +// => false + +bool = isWriteOnlyProperty( {}, 'hasOwnProperty' ); +console.log( bool ); +// => false + +bool = isWriteOnlyProperty( null, 'a' ); +console.log( bool ); +// => false + +bool = isWriteOnlyProperty( void 0, 'a' ); +console.log( bool ); +// => false diff --git a/is-write-only-property/lib/index.js b/is-write-only-property/lib/index.js new file mode 100644 index 00000000..188106d7 --- /dev/null +++ b/is-write-only-property/lib/index.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Test whether an object's own property is write-only. +* +* @module @stdlib/assert/is-write-only-property +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* var isWriteOnlyProperty = require( '@stdlib/assert/is-write-only-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'set': setter +* }); +* +* var bool = isWriteOnlyProperty( obj, 'boop' ); +* // returns false +* +* bool = isWriteOnlyProperty( obj, 'beep' ); +* // returns true +*/ + +// MODULES // + +var isWriteOnlyProperty = require( './main.js' ); + + +// EXPORTS // + +module.exports = isWriteOnlyProperty; diff --git a/is-write-only-property/lib/main.js b/is-write-only-property/lib/main.js new file mode 100644 index 00000000..585089e1 --- /dev/null +++ b/is-write-only-property/lib/main.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertyDescriptor = require( '@stdlib/utils/property-descriptor' ); + + +// MAIN // + +/** +* Tests if an object's own property is write-only. +* +* @param {*} value - value to test +* @param {*} property - property to test +* @returns {boolean} boolean indicating if an object property is write-only +* +* @example +* var defineProperty = require( '@stdlib/utils/define-property' ); +* +* var obj = { +* 'boop': true +* }; +* +* function setter( v ) { +* obj.boop = v; +* } +* +* defineProperty( obj, 'beep', { +* 'configurable': false, +* 'enumerable': true, +* 'set': setter +* }); +* +* var bool = isWriteOnlyProperty( obj, 'boop' ); +* // returns false +* +* bool = isWriteOnlyProperty( obj, 'beep' ); +* // returns true +*/ +function isWriteOnlyProperty( value, property ) { + var desc = propertyDescriptor( value, property ); + return ( + desc !== null && + desc.configurable === false && + ( + typeof desc.set === 'function' && + desc.get === void 0 + ) + ); +} + + +// EXPORTS // + +module.exports = isWriteOnlyProperty; diff --git a/is-write-only-property/package.json b/is-write-only-property/package.json new file mode 100644 index 00000000..bdda37f8 --- /dev/null +++ b/is-write-only-property/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/assert/is-write-only-property", + "version": "0.0.0", + "description": "Test if an object's own property is write-only.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "utilities", + "utility", + "utils", + "util", + "object", + "obj", + "is", + "has", + "own", + "readable", + "writable", + "write-only", + "property", + "prop", + "test", + "check", + "validate" + ] +} diff --git a/is-write-only-property/test/test.js b/is-write-only-property/test/test.js new file mode 100644 index 00000000..bf44632a --- /dev/null +++ b/is-write-only-property/test/test.js @@ -0,0 +1,222 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var defineProperty = require( '@stdlib/utils/define-property' ); +var isWriteOnlyProperty = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof isWriteOnlyProperty, 'function', 'export is a function' ); + t.end(); +}); + +tape( 'the function returns `true` if an object property is write-only', function test( t ) { + var bool; + var obj; + + obj = {}; + + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': true, + 'set': setter + }); + + defineProperty( obj, 'b', { + 'configurable': false, + 'enumerable': false, + 'set': setter + }); + + bool = isWriteOnlyProperty( obj, 'a' ); + t.equal( bool, true, 'returns true' ); + + bool = isWriteOnlyProperty( obj, 'b' ); + t.equal( bool, true, 'returns true' ); + + t.end(); + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `false` if an object property is not write-only', function test( t ) { + var bool; + var obj; + + function Foo() { + this.bar = 'beep'; + return this; + } + + obj = { + 'a': 'b' + }; + bool = isWriteOnlyProperty( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyProperty( [ 1, 2, 3 ], '1' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyProperty( [ 1, 2, 3 ], 1 ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyProperty( new Foo(), 'bar' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyProperty( [ 'a' ], 'length' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'writable': true, + 'value': 'b' + }); + + bool = isWriteOnlyProperty( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'get': getter, + 'set': setter + }); + + bool = isWriteOnlyProperty( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + obj = {}; + defineProperty( obj, 'a', { + 'configurable': false, + 'enumerable': false, + 'get': getter + }); + + bool = isWriteOnlyProperty( obj, 'a' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function getter() { + // No-op... + } + + function setter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided `null` or `undefined` for the first argument', function test( t ) { + var bool; + + bool = isWriteOnlyProperty( null, 'beep' ); + t.equal( bool, false, 'returns false when provided null' ); + + bool = isWriteOnlyProperty( void 0, 'beep' ); + t.equal( bool, false, 'returns false when provided undefined' ); + + t.end(); +}); + +tape( 'the function returns `false` if provided a property argument which does not correspond to a write-only property', function test( t ) { + var bool; + var obj; + + obj = { + 'a': 'b' + }; + + defineProperty( obj, 'd', { + 'configurable': true, + 'enumerable': true, + 'get': getter + }); + + defineProperty( obj, 'e', { + 'configurable': true, + 'enumerable': true, + 'set': setter, + 'get': getter + }); + + defineProperty( obj, 'f', { + 'configurable': true, + 'enumerable': false, + 'writable': false, + 'value': 'g' + }); + + bool = isWriteOnlyProperty( obj, 'c' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyProperty( obj, 'd' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyProperty( obj, 'e' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyProperty( obj, 'f' ); + t.equal( bool, false, 'returns false' ); + + t.end(); + + function setter() { + // No-op... + } + + function getter() { + // No-op... + } +}); + +tape( 'the function returns `false` if provided an inherited property', function test( t ) { + var bool; + + bool = isWriteOnlyProperty( {}, 'hasOwnProperty' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyProperty( {}, 'toString' ); + t.equal( bool, false, 'returns false' ); + + bool = isWriteOnlyProperty( {}, 'constructor' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); + +tape( 'values are coerced to objects', function test( t ) { + var bool; + + bool = isWriteOnlyProperty( 'beep', 'length' ); + t.equal( bool, false, 'returns false' ); + + t.end(); +}); diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 00000000..98439708 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,2229 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/* +* When adding modules to the namespace, ensure that they are added in alphabetical order according to module name. +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-read-only-property' ); + + +// MAIN // + +/** +* Top-level namespace. +* +* @namespace ns +*/ +var ns = {}; + +/** +* @name contains +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/contains} +*/ +setReadOnly( ns, 'contains', require( '@stdlib/assert/contains' ) ); + +/** +* @name deepEqual +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/deep-equal} +*/ +setReadOnly( ns, 'deepEqual', require( '@stdlib/assert/deep-equal' ) ); + +/** +* @name deepHasOwnProp +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/deep-has-own-property} +*/ +setReadOnly( ns, 'deepHasOwnProp', require( '@stdlib/assert/deep-has-own-property' ) ); + +/** +* @name deepHasProp +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/deep-has-property} +*/ +setReadOnly( ns, 'deepHasProp', require( '@stdlib/assert/deep-has-property' ) ); + +/** +* @name hasArrayBufferSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-arraybuffer-support} +*/ +setReadOnly( ns, 'hasArrayBufferSupport', require( '@stdlib/assert/has-arraybuffer-support' ) ); + +/** +* @name hasAsyncAwaitSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-async-await-support} +*/ +setReadOnly( ns, 'hasAsyncAwaitSupport', require( '@stdlib/assert/has-async-await-support' ) ); + +/** +* @name hasAsyncIteratorSymbolSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-async-iterator-symbol-support} +*/ +setReadOnly( ns, 'hasAsyncIteratorSymbolSupport', require( '@stdlib/assert/has-async-iterator-symbol-support' ) ); + +/** +* @name hasBigIntSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-bigint-support} +*/ +setReadOnly( ns, 'hasBigIntSupport', require( '@stdlib/assert/has-bigint-support' ) ); + +/** +* @name hasClassSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-class-support} +*/ +setReadOnly( ns, 'hasClassSupport', require( '@stdlib/assert/has-class-support' ) ); + +/** +* @name hasDataViewSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-dataview-support} +*/ +setReadOnly( ns, 'hasDataViewSupport', require( '@stdlib/assert/has-dataview-support' ) ); + +/** +* @name hasDefinePropertiesSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-define-properties-support} +*/ +setReadOnly( ns, 'hasDefinePropertiesSupport', require( '@stdlib/assert/has-define-properties-support' ) ); + +/** +* @name hasDefinePropertySupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-define-property-support} +*/ +setReadOnly( ns, 'hasDefinePropertySupport', require( '@stdlib/assert/has-define-property-support' ) ); + +/** +* @name hasFloat32ArraySupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-float32array-support} +*/ +setReadOnly( ns, 'hasFloat32ArraySupport', require( '@stdlib/assert/has-float32array-support' ) ); + +/** +* @name hasFloat64ArraySupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-float64array-support} +*/ +setReadOnly( ns, 'hasFloat64ArraySupport', require( '@stdlib/assert/has-float64array-support' ) ); + +/** +* @name hasFunctionNameSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-function-name-support} +*/ +setReadOnly( ns, 'hasFunctionNameSupport', require( '@stdlib/assert/has-function-name-support' ) ); + +/** +* @name hasGeneratorSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-generator-support} +*/ +setReadOnly( ns, 'hasGeneratorSupport', require( '@stdlib/assert/has-generator-support' ) ); + +/** +* @name hasGlobalThisSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-globalthis-support} +*/ +setReadOnly( ns, 'hasGlobalThisSupport', require( '@stdlib/assert/has-globalthis-support' ) ); + +/** +* @name hasInt8ArraySupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-int8array-support} +*/ +setReadOnly( ns, 'hasInt8ArraySupport', require( '@stdlib/assert/has-int8array-support' ) ); + +/** +* @name hasInt16ArraySupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-int16array-support} +*/ +setReadOnly( ns, 'hasInt16ArraySupport', require( '@stdlib/assert/has-int16array-support' ) ); + +/** +* @name hasInt32ArraySupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-int32array-support} +*/ +setReadOnly( ns, 'hasInt32ArraySupport', require( '@stdlib/assert/has-int32array-support' ) ); + +/** +* @name hasIteratorSymbolSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-iterator-symbol-support} +*/ +setReadOnly( ns, 'hasIteratorSymbolSupport', require( '@stdlib/assert/has-iterator-symbol-support' ) ); + +/** +* @name hasMapSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-map-support} +*/ +setReadOnly( ns, 'hasMapSupport', require( '@stdlib/assert/has-map-support' ) ); + +/** +* @name hasNodeBufferSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-node-buffer-support} +*/ +setReadOnly( ns, 'hasNodeBufferSupport', require( '@stdlib/assert/has-node-buffer-support' ) ); + +/** +* @name hasOwnProp +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-own-property} +*/ +setReadOnly( ns, 'hasOwnProp', require( '@stdlib/assert/has-own-property' ) ); + +/** +* @name hasProp +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-property} +*/ +setReadOnly( ns, 'hasProp', require( '@stdlib/assert/has-property' ) ); + +/** +* @name hasProxySupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-proxy-support} +*/ +setReadOnly( ns, 'hasProxySupport', require( '@stdlib/assert/has-proxy-support' ) ); + +/** +* @name hasSetSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-set-support} +*/ +setReadOnly( ns, 'hasSetSupport', require( '@stdlib/assert/has-set-support' ) ); + +/** +* @name hasSharedArrayBufferSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-sharedarraybuffer-support} +*/ +setReadOnly( ns, 'hasSharedArrayBufferSupport', require( '@stdlib/assert/has-sharedarraybuffer-support' ) ); + +/** +* @name hasSymbolSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-symbol-support} +*/ +setReadOnly( ns, 'hasSymbolSupport', require( '@stdlib/assert/has-symbol-support' ) ); + +/** +* @name hasToStringTagSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-tostringtag-support} +*/ +setReadOnly( ns, 'hasToStringTagSupport', require( '@stdlib/assert/has-tostringtag-support' ) ); + +/** +* @name hasUint8ArraySupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-uint8array-support} +*/ +setReadOnly( ns, 'hasUint8ArraySupport', require( '@stdlib/assert/has-uint8array-support' ) ); + +/** +* @name hasUint8ClampedArraySupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-uint8clampedarray-support} +*/ +setReadOnly( ns, 'hasUint8ClampedArraySupport', require( '@stdlib/assert/has-uint8clampedarray-support' ) ); + +/** +* @name hasUint16ArraySupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-uint16array-support} +*/ +setReadOnly( ns, 'hasUint16ArraySupport', require( '@stdlib/assert/has-uint16array-support' ) ); + +/** +* @name hasUint32ArraySupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-uint32array-support} +*/ +setReadOnly( ns, 'hasUint32ArraySupport', require( '@stdlib/assert/has-uint32array-support' ) ); + +/** +* @name hasUTF16SurrogatePairAt +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-utf16-surrogate-pair-at} +*/ +setReadOnly( ns, 'hasUTF16SurrogatePairAt', require( '@stdlib/assert/has-utf16-surrogate-pair-at' ) ); + +/** +* @name hasWebAssemblySupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-wasm-support} +*/ +setReadOnly( ns, 'hasWebAssemblySupport', require( '@stdlib/assert/has-wasm-support' ) ); + +/** +* @name hasWeakMapSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-weakmap-support} +*/ +setReadOnly( ns, 'hasWeakMapSupport', require( '@stdlib/assert/has-weakmap-support' ) ); + +/** +* @name hasWeakSetSupport +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/has-weakset-support} +*/ +setReadOnly( ns, 'hasWeakSetSupport', require( '@stdlib/assert/has-weakset-support' ) ); + +/** +* @name instanceOf +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/instance-of} +*/ +setReadOnly( ns, 'instanceOf', require( '@stdlib/assert/instance-of' ) ); + +/** +* @name isAbsolutePath +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-absolute-path} +*/ +setReadOnly( ns, 'isAbsolutePath', require( '@stdlib/assert/is-absolute-path' ) ); + +/** +* @name isAccessorProperty +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-accessor-property} +*/ +setReadOnly( ns, 'isAccessorProperty', require( '@stdlib/assert/is-accessor-property' ) ); + +/** +* @name isAccessorPropertyIn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-accessor-property-in} +*/ +setReadOnly( ns, 'isAccessorPropertyIn', require( '@stdlib/assert/is-accessor-property-in' ) ); + +/** +* @name isAlphagram +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-alphagram} +*/ +setReadOnly( ns, 'isAlphagram', require( '@stdlib/assert/is-alphagram' ) ); + +/** +* @name isAlphaNumeric +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-alphanumeric} +*/ +setReadOnly( ns, 'isAlphaNumeric', require( '@stdlib/assert/is-alphanumeric' ) ); + +/** +* @name isAnagram +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-anagram} +*/ +setReadOnly( ns, 'isAnagram', require( '@stdlib/assert/is-anagram' ) ); + +/** +* @name isArguments +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-arguments} +*/ +setReadOnly( ns, 'isArguments', require( '@stdlib/assert/is-arguments' ) ); + +/** +* @name isArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-array} +*/ +setReadOnly( ns, 'isArray', require( '@stdlib/assert/is-array' ) ); + +/** +* @name isArrayArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-array-array} +*/ +setReadOnly( ns, 'isArrayArray', require( '@stdlib/assert/is-array-array' ) ); + +/** +* @name isArrayLength +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-array-length} +*/ +setReadOnly( ns, 'isArrayLength', require( '@stdlib/assert/is-array-length' ) ); + +/** +* @name isArrayLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-array-like} +*/ +setReadOnly( ns, 'isArrayLike', require( '@stdlib/assert/is-array-like' ) ); + +/** +* @name isArrayLikeObject +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-array-like-object} +*/ +setReadOnly( ns, 'isArrayLikeObject', require( '@stdlib/assert/is-array-like-object' ) ); + +/** +* @name isArrayBuffer +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-arraybuffer} +*/ +setReadOnly( ns, 'isArrayBuffer', require( '@stdlib/assert/is-arraybuffer' ) ); + +/** +* @name isASCII +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-ascii} +*/ +setReadOnly( ns, 'isASCII', require( '@stdlib/assert/is-ascii' ) ); + +/** +* @name isBetween +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-between} +*/ +setReadOnly( ns, 'isBetween', require( '@stdlib/assert/is-between' ) ); + +/** +* @name isBetweenArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-between-array} +*/ +setReadOnly( ns, 'isBetweenArray', require( '@stdlib/assert/is-between-array' ) ); + +/** +* @name IS_BIG_ENDIAN +* @memberof ns +* @readonly +* @type {boolean} +* @see {@link module:@stdlib/assert/is-big-endian} +*/ +setReadOnly( ns, 'IS_BIG_ENDIAN', require( '@stdlib/assert/is-big-endian' ) ); + +/** +* @name isBigInt +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-bigint} +*/ +setReadOnly( ns, 'isBigInt', require( '@stdlib/assert/is-bigint' ) ); + +/** +* @name isBinaryString +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-binary-string} +*/ +setReadOnly( ns, 'isBinaryString', require( '@stdlib/assert/is-binary-string' ) ); + +/** +* @name isBoolean +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-boolean} +*/ +setReadOnly( ns, 'isBoolean', require( '@stdlib/assert/is-boolean' ) ); + +/** +* @name isBooleanArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-boolean-array} +*/ +setReadOnly( ns, 'isBooleanArray', require( '@stdlib/assert/is-boolean-array' ) ); + +/** +* @name isBoxedPrimitive +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-boxed-primitive} +*/ +setReadOnly( ns, 'isBoxedPrimitive', require( '@stdlib/assert/is-boxed-primitive' ) ); + +/** +* @name IS_BROWSER +* @memberof ns +* @readonly +* @type {boolean} +* @see {@link module:@stdlib/assert/is-browser} +*/ +setReadOnly( ns, 'IS_BROWSER', require( '@stdlib/assert/is-browser' ) ); + +/** +* @name isBuffer +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-buffer} +*/ +setReadOnly( ns, 'isBuffer', require( '@stdlib/assert/is-buffer' ) ); + +/** +* @name isCapitalized +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-capitalized} +*/ +setReadOnly( ns, 'isCapitalized', require( '@stdlib/assert/is-capitalized' ) ); + +/** +* @name isCentrosymmetricMatrix +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-centrosymmetric-matrix} +*/ +setReadOnly( ns, 'isCentrosymmetricMatrix', require( '@stdlib/assert/is-centrosymmetric-matrix' ) ); + +/** +* @name isCircular +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-circular} +*/ +setReadOnly( ns, 'isCircular', require( '@stdlib/assert/is-circular' ) ); + +/** +* @name isCircularArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-circular-array} +*/ +setReadOnly( ns, 'isCircularArray', require( '@stdlib/assert/is-circular-array' ) ); + +/** +* @name isCircularPlainObject +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-circular-plain-object} +*/ +setReadOnly( ns, 'isCircularPlainObject', require( '@stdlib/assert/is-circular-plain-object' ) ); + +/** +* @name isCollection +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-collection} +*/ +setReadOnly( ns, 'isCollection', require( '@stdlib/assert/is-collection' ) ); + +/** +* @name isComplex +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-complex} +*/ +setReadOnly( ns, 'isComplex', require( '@stdlib/assert/is-complex' ) ); + +/** +* @name isComplexLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-complex-like} +*/ +setReadOnly( ns, 'isComplexLike', require( '@stdlib/assert/is-complex-like' ) ); + +/** +* @name isComplexTypedArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-complex-typed-array} +*/ +setReadOnly( ns, 'isComplexTypedArray', require( '@stdlib/assert/is-complex-typed-array' ) ); + +/** +* @name isComplexTypedArrayLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-complex-typed-array-like} +*/ +setReadOnly( ns, 'isComplexTypedArrayLike', require( '@stdlib/assert/is-complex-typed-array-like' ) ); + +/** +* @name isComplex64 +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-complex64} +*/ +setReadOnly( ns, 'isComplex64', require( '@stdlib/assert/is-complex64' ) ); + +/** +* @name isComplex64Array +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-complex64array} +*/ +setReadOnly( ns, 'isComplex64Array', require( '@stdlib/assert/is-complex64array' ) ); + +/** +* @name isComplex128 +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-complex128} +*/ +setReadOnly( ns, 'isComplex128', require( '@stdlib/assert/is-complex128' ) ); + +/** +* @name isComplex128Array +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-complex128array} +*/ +setReadOnly( ns, 'isComplex128Array', require( '@stdlib/assert/is-complex128array' ) ); + +/** +* @name isComposite +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-composite} +*/ +setReadOnly( ns, 'isComposite', require( '@stdlib/assert/is-composite' ) ); + +/** +* @name isConfigurableProperty +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-configurable-property} +*/ +setReadOnly( ns, 'isConfigurableProperty', require( '@stdlib/assert/is-configurable-property' ) ); + +/** +* @name isConfigurablePropertyIn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-configurable-property-in} +*/ +setReadOnly( ns, 'isConfigurablePropertyIn', require( '@stdlib/assert/is-configurable-property-in' ) ); + +/** +* @name isCubeNumber +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-cube-number} +*/ +setReadOnly( ns, 'isCubeNumber', require( '@stdlib/assert/is-cube-number' ) ); + +/** +* @name IS_DARWIN +* @memberof ns +* @readonly +* @type {boolean} +* @see {@link module:@stdlib/assert/is-darwin} +*/ +setReadOnly( ns, 'IS_DARWIN', require( '@stdlib/assert/is-darwin' ) ); + +/** +* @name isDataProperty +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-data-property} +*/ +setReadOnly( ns, 'isDataProperty', require( '@stdlib/assert/is-data-property' ) ); + +/** +* @name isDataPropertyIn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-data-property-in} +*/ +setReadOnly( ns, 'isDataPropertyIn', require( '@stdlib/assert/is-data-property-in' ) ); + +/** +* @name isDataView +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-dataview} +*/ +setReadOnly( ns, 'isDataView', require( '@stdlib/assert/is-dataview' ) ); + +/** +* @name isDateObject +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-date-object} +*/ +setReadOnly( ns, 'isDateObject', require( '@stdlib/assert/is-date-object' ) ); + +/** +* @name isDigitString +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-digit-string} +*/ +setReadOnly( ns, 'isDigitString', require( '@stdlib/assert/is-digit-string' ) ); + +/** +* @name IS_ELECTRON +* @memberof ns +* @readonly +* @type {boolean} +* @see {@link module:@stdlib/assert/is-electron} +*/ +setReadOnly( ns, 'IS_ELECTRON', require( '@stdlib/assert/is-electron' ) ); + +/** +* @name IS_ELECTRON_MAIN +* @memberof ns +* @readonly +* @type {boolean} +* @see {@link module:@stdlib/assert/is-electron-main} +*/ +setReadOnly( ns, 'IS_ELECTRON_MAIN', require( '@stdlib/assert/is-electron-main' ) ); + +/** +* @name IS_ELECTRON_RENDERER +* @memberof ns +* @readonly +* @type {boolean} +* @see {@link module:@stdlib/assert/is-electron-renderer} +*/ +setReadOnly( ns, 'IS_ELECTRON_RENDERER', require( '@stdlib/assert/is-electron-renderer' ) ); + +/** +* @name isEmailAddress +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-email-address} +*/ +setReadOnly( ns, 'isEmailAddress', require( '@stdlib/assert/is-email-address' ) ); + +/** +* @name isEmptyArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-empty-array} +*/ +setReadOnly( ns, 'isEmptyArray', require( '@stdlib/assert/is-empty-array' ) ); + +/** +* @name isEmptyObject +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-empty-object} +*/ +setReadOnly( ns, 'isEmptyObject', require( '@stdlib/assert/is-empty-object' ) ); + +/** +* @name isEmptyString +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-empty-string} +*/ +setReadOnly( ns, 'isEmptyString', require( '@stdlib/assert/is-empty-string' ) ); + +/** +* @name isEnumerableProperty +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-enumerable-property} +*/ +setReadOnly( ns, 'isEnumerableProperty', require( '@stdlib/assert/is-enumerable-property' ) ); + +/** +* @name isEnumerablePropertyIn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-enumerable-property-in} +*/ +setReadOnly( ns, 'isEnumerablePropertyIn', require( '@stdlib/assert/is-enumerable-property-in' ) ); + +/** +* @name isError +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-error} +*/ +setReadOnly( ns, 'isError', require( '@stdlib/assert/is-error' ) ); + +/** +* @name isEvalError +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-eval-error} +*/ +setReadOnly( ns, 'isEvalError', require( '@stdlib/assert/is-eval-error' ) ); + +/** +* @name isEven +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-even} +*/ +setReadOnly( ns, 'isEven', require( '@stdlib/assert/is-even' ) ); + +/** +* @name isFalsy +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-falsy} +*/ +setReadOnly( ns, 'isFalsy', require( '@stdlib/assert/is-falsy' ) ); + +/** +* @name isFalsyArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-falsy-array} +*/ +setReadOnly( ns, 'isFalsyArray', require( '@stdlib/assert/is-falsy-array' ) ); + +/** +* @name isFinite +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-finite} +*/ +setReadOnly( ns, 'isFinite', require( '@stdlib/assert/is-finite' ) ); + +/** +* @name isFiniteArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-finite-array} +*/ +setReadOnly( ns, 'isFiniteArray', require( '@stdlib/assert/is-finite-array' ) ); + +/** +* @name isFloat32Array +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-float32array} +*/ +setReadOnly( ns, 'isFloat32Array', require( '@stdlib/assert/is-float32array' ) ); + +/** +* @name isFloat32MatrixLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-float32matrix-like} +*/ +setReadOnly( ns, 'isFloat32MatrixLike', require( '@stdlib/assert/is-float32matrix-like' ) ); + +/** +* @name isFloat32ndarrayLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-float32ndarray-like} +*/ +setReadOnly( ns, 'isFloat32ndarrayLike', require( '@stdlib/assert/is-float32ndarray-like' ) ); + +/** +* @name isFloat32VectorLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-float32vector-like} +*/ +setReadOnly( ns, 'isFloat32VectorLike', require( '@stdlib/assert/is-float32vector-like' ) ); + +/** +* @name isFloat64Array +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-float64array} +*/ +setReadOnly( ns, 'isFloat64Array', require( '@stdlib/assert/is-float64array' ) ); + +/** +* @name isFloat64MatrixLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-float64matrix-like} +*/ +setReadOnly( ns, 'isFloat64MatrixLike', require( '@stdlib/assert/is-float64matrix-like' ) ); + +/** +* @name isFloat64ndarrayLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-float64ndarray-like} +*/ +setReadOnly( ns, 'isFloat64ndarrayLike', require( '@stdlib/assert/is-float64ndarray-like' ) ); + +/** +* @name isFloat64VectorLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-float64vector-like} +*/ +setReadOnly( ns, 'isFloat64VectorLike', require( '@stdlib/assert/is-float64vector-like' ) ); + +/** +* @name isFunction +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-function} +*/ +setReadOnly( ns, 'isFunction', require( '@stdlib/assert/is-function' ) ); + +/** +* @name isFunctionArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-function-array} +*/ +setReadOnly( ns, 'isFunctionArray', require( '@stdlib/assert/is-function-array' ) ); + +/** +* @name isGeneratorObject +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-generator-object} +*/ +setReadOnly( ns, 'isGeneratorObject', require( '@stdlib/assert/is-generator-object' ) ); + +/** +* @name isGeneratorObjectLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-generator-object-like} +*/ +setReadOnly( ns, 'isGeneratorObjectLike', require( '@stdlib/assert/is-generator-object-like' ) ); + +/** +* @name isgzipBuffer +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-gzip-buffer} +*/ +setReadOnly( ns, 'isgzipBuffer', require( '@stdlib/assert/is-gzip-buffer' ) ); + +/** +* @name isHexString +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-hex-string} +*/ +setReadOnly( ns, 'isHexString', require( '@stdlib/assert/is-hex-string' ) ); + +/** +* @name isInfinite +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-infinite} +*/ +setReadOnly( ns, 'isInfinite', require( '@stdlib/assert/is-infinite' ) ); + +/** +* @name isInheritedProperty +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-inherited-property} +*/ +setReadOnly( ns, 'isInheritedProperty', require( '@stdlib/assert/is-inherited-property' ) ); + +/** +* @name isInt8Array +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-int8array} +*/ +setReadOnly( ns, 'isInt8Array', require( '@stdlib/assert/is-int8array' ) ); + +/** +* @name isInt16Array +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-int16array} +*/ +setReadOnly( ns, 'isInt16Array', require( '@stdlib/assert/is-int16array' ) ); + +/** +* @name isInt32Array +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-int32array} +*/ +setReadOnly( ns, 'isInt32Array', require( '@stdlib/assert/is-int32array' ) ); + +/** +* @name isInteger +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-integer} +*/ +setReadOnly( ns, 'isInteger', require( '@stdlib/assert/is-integer' ) ); + +/** +* @name isIntegerArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-integer-array} +*/ +setReadOnly( ns, 'isIntegerArray', require( '@stdlib/assert/is-integer-array' ) ); + +/** +* @name isIterableLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-iterable-like} +*/ +setReadOnly( ns, 'isIterableLike', require( '@stdlib/assert/is-iterable-like' ) ); + +/** +* @name isIteratorLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-iterator-like} +*/ +setReadOnly( ns, 'isIteratorLike', require( '@stdlib/assert/is-iterator-like' ) ); + +/** +* @name isJSON +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-json} +*/ +setReadOnly( ns, 'isJSON', require( '@stdlib/assert/is-json' ) ); + +/** +* @name isLeapYear +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-leap-year} +*/ +setReadOnly( ns, 'isLeapYear', require( '@stdlib/assert/is-leap-year' ) ); + +/** +* @name IS_LITTLE_ENDIAN +* @memberof ns +* @readonly +* @type {boolean} +* @see {@link module:@stdlib/assert/is-little-endian} +*/ +setReadOnly( ns, 'IS_LITTLE_ENDIAN', require( '@stdlib/assert/is-little-endian' ) ); + +/** +* @name isLowercase +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-lowercase} +*/ +setReadOnly( ns, 'isLowercase', require( '@stdlib/assert/is-lowercase' ) ); + +/** +* @name isMatrixLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-matrix-like} +*/ +setReadOnly( ns, 'isMatrixLike', require( '@stdlib/assert/is-matrix-like' ) ); + +/** +* @name isMethod +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-method} +*/ +setReadOnly( ns, 'isMethod', require( '@stdlib/assert/is-method' ) ); + +/** +* @name isMethodIn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-method-in} +*/ +setReadOnly( ns, 'isMethodIn', require( '@stdlib/assert/is-method-in' ) ); + +/** +* @name isNamedTypedTupleLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-named-typed-tuple-like} +*/ +setReadOnly( ns, 'isNamedTypedTupleLike', require( '@stdlib/assert/is-named-typed-tuple-like' ) ); + +/** +* @name isnan +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nan} +*/ +setReadOnly( ns, 'isnan', require( '@stdlib/assert/is-nan' ) ); + +/** +* @name isNaNArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nan-array} +*/ +setReadOnly( ns, 'isNaNArray', require( '@stdlib/assert/is-nan-array' ) ); + +/** +* @name isNativeFunction +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-native-function} +*/ +setReadOnly( ns, 'isNativeFunction', require( '@stdlib/assert/is-native-function' ) ); + +/** +* @name isndarrayLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-ndarray-like} +*/ +setReadOnly( ns, 'isndarrayLike', require( '@stdlib/assert/is-ndarray-like' ) ); + +/** +* @name isNegativeInteger +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-negative-integer} +*/ +setReadOnly( ns, 'isNegativeInteger', require( '@stdlib/assert/is-negative-integer' ) ); + +/** +* @name isNegativeIntegerArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-negative-integer-array} +*/ +setReadOnly( ns, 'isNegativeIntegerArray', require( '@stdlib/assert/is-negative-integer-array' ) ); + +/** +* @name isNegativeNumber +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-negative-number} +*/ +setReadOnly( ns, 'isNegativeNumber', require( '@stdlib/assert/is-negative-number' ) ); + +/** +* @name isNegativeNumberArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-negative-number-array} +*/ +setReadOnly( ns, 'isNegativeNumberArray', require( '@stdlib/assert/is-negative-number-array' ) ); + +/** +* @name isNegativeZero +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-negative-zero} +*/ +setReadOnly( ns, 'isNegativeZero', require( '@stdlib/assert/is-negative-zero' ) ); + +/** +* @name IS_NODE +* @memberof ns +* @readonly +* @type {boolean} +* @see {@link module:@stdlib/assert/is-node} +*/ +setReadOnly( ns, 'IS_NODE', require( '@stdlib/assert/is-node' ) ); + +/** +* @name isNodeBuiltin +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-node-builtin} +*/ +setReadOnly( ns, 'isNodeBuiltin', require( '@stdlib/assert/is-node-builtin' ) ); + +/** +* @name isNodeDuplexStreamLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-node-duplex-stream-like} +*/ +setReadOnly( ns, 'isNodeDuplexStreamLike', require( '@stdlib/assert/is-node-duplex-stream-like' ) ); + +/** +* @name isNodeReadableStreamLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-node-readable-stream-like} +*/ +setReadOnly( ns, 'isNodeReadableStreamLike', require( '@stdlib/assert/is-node-readable-stream-like' ) ); + +/** +* @name isNodeREPL +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-node-repl} +*/ +setReadOnly( ns, 'isNodeREPL', require( '@stdlib/assert/is-node-repl' ) ); + +/** +* @name isNodeStreamLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-node-stream-like} +*/ +setReadOnly( ns, 'isNodeStreamLike', require( '@stdlib/assert/is-node-stream-like' ) ); + +/** +* @name isNodeTransformStreamLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-node-transform-stream-like} +*/ +setReadOnly( ns, 'isNodeTransformStreamLike', require( '@stdlib/assert/is-node-transform-stream-like' ) ); + +/** +* @name isNodeWritableStreamLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-node-writable-stream-like} +*/ +setReadOnly( ns, 'isNodeWritableStreamLike', require( '@stdlib/assert/is-node-writable-stream-like' ) ); + +/** +* @name isNonConfigurableProperty +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nonconfigurable-property} +*/ +setReadOnly( ns, 'isNonConfigurableProperty', require( '@stdlib/assert/is-nonconfigurable-property' ) ); + +/** +* @name isNonConfigurablePropertyIn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nonconfigurable-property-in} +*/ +setReadOnly( ns, 'isNonConfigurablePropertyIn', require( '@stdlib/assert/is-nonconfigurable-property-in' ) ); + +/** +* @name isNonEnumerableProperty +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nonenumerable-property} +*/ +setReadOnly( ns, 'isNonEnumerableProperty', require( '@stdlib/assert/is-nonenumerable-property' ) ); + +/** +* @name isNonEnumerablePropertyIn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nonenumerable-property-in} +*/ +setReadOnly( ns, 'isNonEnumerablePropertyIn', require( '@stdlib/assert/is-nonenumerable-property-in' ) ); + +/** +* @name isNonNegativeInteger +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nonnegative-integer} +*/ +setReadOnly( ns, 'isNonNegativeInteger', require( '@stdlib/assert/is-nonnegative-integer' ) ); + +/** +* @name isNonNegativeIntegerArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nonnegative-integer-array} +*/ +setReadOnly( ns, 'isNonNegativeIntegerArray', require( '@stdlib/assert/is-nonnegative-integer-array' ) ); + +/** +* @name isNonNegativeNumber +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nonnegative-number} +*/ +setReadOnly( ns, 'isNonNegativeNumber', require( '@stdlib/assert/is-nonnegative-number' ) ); + +/** +* @name isNonNegativeNumberArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nonnegative-number-array} +*/ +setReadOnly( ns, 'isNonNegativeNumberArray', require( '@stdlib/assert/is-nonnegative-number-array' ) ); + +/** +* @name isNonPositiveInteger +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nonpositive-integer} +*/ +setReadOnly( ns, 'isNonPositiveInteger', require( '@stdlib/assert/is-nonpositive-integer' ) ); + +/** +* @name isNonPositiveIntegerArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nonpositive-integer-array} +*/ +setReadOnly( ns, 'isNonPositiveIntegerArray', require( '@stdlib/assert/is-nonpositive-integer-array' ) ); + +/** +* @name isNonPositiveNumber +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nonpositive-number} +*/ +setReadOnly( ns, 'isNonPositiveNumber', require( '@stdlib/assert/is-nonpositive-number' ) ); + +/** +* @name isNonPositiveNumberArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nonpositive-number-array} +*/ +setReadOnly( ns, 'isNonPositiveNumberArray', require( '@stdlib/assert/is-nonpositive-number-array' ) ); + +/** +* @name isNonSymmetricMatrix +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-nonsymmetric-matrix} +*/ +setReadOnly( ns, 'isNonSymmetricMatrix', require( '@stdlib/assert/is-nonsymmetric-matrix' ) ); + +/** +* @name isNull +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-null} +*/ +setReadOnly( ns, 'isNull', require( '@stdlib/assert/is-null' ) ); + +/** +* @name isNullArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-null-array} +*/ +setReadOnly( ns, 'isNullArray', require( '@stdlib/assert/is-null-array' ) ); + +/** +* @name isNumber +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-number} +*/ +setReadOnly( ns, 'isNumber', require( '@stdlib/assert/is-number' ) ); + +/** +* @name isNumberArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-number-array} +*/ +setReadOnly( ns, 'isNumberArray', require( '@stdlib/assert/is-number-array' ) ); + +/** +* @name isNumericArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-numeric-array} +*/ +setReadOnly( ns, 'isNumericArray', require( '@stdlib/assert/is-numeric-array' ) ); + +/** +* @name isObject +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-object} +*/ +setReadOnly( ns, 'isObject', require( '@stdlib/assert/is-object' ) ); + +/** +* @name isObjectArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-object-array} +*/ +setReadOnly( ns, 'isObjectArray', require( '@stdlib/assert/is-object-array' ) ); + +/** +* @name isObjectLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-object-like} +*/ +setReadOnly( ns, 'isObjectLike', require( '@stdlib/assert/is-object-like' ) ); + +/** +* @name isOdd +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-odd} +*/ +setReadOnly( ns, 'isOdd', require( '@stdlib/assert/is-odd' ) ); + +/** +* @name isPersymmetricMatrix +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-persymmetric-matrix} +*/ +setReadOnly( ns, 'isPersymmetricMatrix', require( '@stdlib/assert/is-persymmetric-matrix' ) ); + +/** +* @name isPlainObject +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-plain-object} +*/ +setReadOnly( ns, 'isPlainObject', require( '@stdlib/assert/is-plain-object' ) ); + +/** +* @name isPlainObjectArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-plain-object-array} +*/ +setReadOnly( ns, 'isPlainObjectArray', require( '@stdlib/assert/is-plain-object-array' ) ); + +/** +* @name isPositiveInteger +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-positive-integer} +*/ +setReadOnly( ns, 'isPositiveInteger', require( '@stdlib/assert/is-positive-integer' ) ); + +/** +* @name isPositiveIntegerArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-positive-integer-array} +*/ +setReadOnly( ns, 'isPositiveIntegerArray', require( '@stdlib/assert/is-positive-integer-array' ) ); + +/** +* @name isPositiveNumber +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-positive-number} +*/ +setReadOnly( ns, 'isPositiveNumber', require( '@stdlib/assert/is-positive-number' ) ); + +/** +* @name isPositiveNumberArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-positive-number-array} +*/ +setReadOnly( ns, 'isPositiveNumberArray', require( '@stdlib/assert/is-positive-number-array' ) ); + +/** +* @name isPositiveZero +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-positive-zero} +*/ +setReadOnly( ns, 'isPositiveZero', require( '@stdlib/assert/is-positive-zero' ) ); + +/** +* @name isPrime +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-prime} +*/ +setReadOnly( ns, 'isPrime', require( '@stdlib/assert/is-prime' ) ); + +/** +* @name isPrimitive +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-primitive} +*/ +setReadOnly( ns, 'isPrimitive', require( '@stdlib/assert/is-primitive' ) ); + +/** +* @name isPrimitiveArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-primitive-array} +*/ +setReadOnly( ns, 'isPrimitiveArray', require( '@stdlib/assert/is-primitive-array' ) ); + +/** +* @name isPRNGLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-prng-like} +*/ +setReadOnly( ns, 'isPRNGLike', require( '@stdlib/assert/is-prng-like' ) ); + +/** +* @name isProbability +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-probability} +*/ +setReadOnly( ns, 'isProbability', require( '@stdlib/assert/is-probability' ) ); + +/** +* @name isProbabilityArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-probability-array} +*/ +setReadOnly( ns, 'isProbabilityArray', require( '@stdlib/assert/is-probability-array' ) ); + +/** +* @name isPrototypeOf +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-prototype-of} +*/ +setReadOnly( ns, 'isPrototypeOf', require( '@stdlib/assert/is-prototype-of' ) ); + +/** +* @name isRangeError +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-range-error} +*/ +setReadOnly( ns, 'isRangeError', require( '@stdlib/assert/is-range-error' ) ); + +/** +* @name isReadOnlyProperty +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-read-only-property} +*/ +setReadOnly( ns, 'isReadOnlyProperty', require( '@stdlib/assert/is-read-only-property' ) ); + +/** +* @name isReadOnlyPropertyIn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-read-only-property-in} +*/ +setReadOnly( ns, 'isReadOnlyPropertyIn', require( '@stdlib/assert/is-read-only-property-in' ) ); + +/** +* @name isReadWriteProperty +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-read-write-property} +*/ +setReadOnly( ns, 'isReadWriteProperty', require( '@stdlib/assert/is-read-write-property' ) ); + +/** +* @name isReadWritePropertyIn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-read-write-property-in} +*/ +setReadOnly( ns, 'isReadWritePropertyIn', require( '@stdlib/assert/is-read-write-property-in' ) ); + +/** +* @name isReadableProperty +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-readable-property} +*/ +setReadOnly( ns, 'isReadableProperty', require( '@stdlib/assert/is-readable-property' ) ); + +/** +* @name isReadablePropertyIn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-readable-property-in} +*/ +setReadOnly( ns, 'isReadablePropertyIn', require( '@stdlib/assert/is-readable-property-in' ) ); + +/** +* @name isReferenceError +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-reference-error} +*/ +setReadOnly( ns, 'isReferenceError', require( '@stdlib/assert/is-reference-error' ) ); + +/** +* @name isRegExp +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-regexp} +*/ +setReadOnly( ns, 'isRegExp', require( '@stdlib/assert/is-regexp' ) ); + +/** +* @name isRegExpString +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-regexp-string} +*/ +setReadOnly( ns, 'isRegExpString', require( '@stdlib/assert/is-regexp-string' ) ); + +/** +* @name isRelativePath +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-relative-path} +*/ +setReadOnly( ns, 'isRelativePath', require( '@stdlib/assert/is-relative-path' ) ); + +/** +* @name isSafeInteger +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-safe-integer} +*/ +setReadOnly( ns, 'isSafeInteger', require( '@stdlib/assert/is-safe-integer' ) ); + +/** +* @name isSafeIntegerArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-safe-integer-array} +*/ +setReadOnly( ns, 'isSafeIntegerArray', require( '@stdlib/assert/is-safe-integer-array' ) ); + +/** +* @name isSameValue +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-same-value} +*/ +setReadOnly( ns, 'isSameValue', require( '@stdlib/assert/is-same-value' ) ); + +/** +* @name isSameValueZero +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-same-value-zero} +*/ +setReadOnly( ns, 'isSameValueZero', require( '@stdlib/assert/is-same-value-zero' ) ); + +/** +* @name isSharedArrayBuffer +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-sharedarraybuffer} +*/ +setReadOnly( ns, 'isSharedArrayBuffer', require( '@stdlib/assert/is-sharedarraybuffer' ) ); + +/** +* @name isSkewCentrosymmetricMatrix +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-skew-centrosymmetric-matrix} +*/ +setReadOnly( ns, 'isSkewCentrosymmetricMatrix', require( '@stdlib/assert/is-skew-centrosymmetric-matrix' ) ); + +/** +* @name isSkewPersymmetricMatrix +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-skew-persymmetric-matrix} +*/ +setReadOnly( ns, 'isSkewPersymmetricMatrix', require( '@stdlib/assert/is-skew-persymmetric-matrix' ) ); + +/** +* @name isSkewSymmetricMatrix +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-skew-symmetric-matrix} +*/ +setReadOnly( ns, 'isSkewSymmetricMatrix', require( '@stdlib/assert/is-skew-symmetric-matrix' ) ); + +/** +* @name isSquareMatrix +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-square-matrix} +*/ +setReadOnly( ns, 'isSquareMatrix', require( '@stdlib/assert/is-square-matrix' ) ); + +/** +* @name isSquareNumber +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-square-number} +*/ +setReadOnly( ns, 'isSquareNumber', require( '@stdlib/assert/is-square-number' ) ); + +/** +* @name isSquareTriangularNumber +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-square-triangular-number} +*/ +setReadOnly( ns, 'isSquareTriangularNumber', require( '@stdlib/assert/is-square-triangular-number' ) ); + +/** +* @name isStrictEqual +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-strict-equal} +*/ +setReadOnly( ns, 'isStrictEqual', require( '@stdlib/assert/is-strict-equal' ) ); + +/** +* @name isString +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-string} +*/ +setReadOnly( ns, 'isString', require( '@stdlib/assert/is-string' ) ); + +/** +* @name isStringArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-string-array} +*/ +setReadOnly( ns, 'isStringArray', require( '@stdlib/assert/is-string-array' ) ); + +/** +* @name isSymbol +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-symbol} +*/ +setReadOnly( ns, 'isSymbol', require( '@stdlib/assert/is-symbol' ) ); + +/** +* @name isSymbolArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-symbol-array} +*/ +setReadOnly( ns, 'isSymbolArray', require( '@stdlib/assert/is-symbol-array' ) ); + +/** +* @name isSymmetricMatrix +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-symmetric-matrix} +*/ +setReadOnly( ns, 'isSymmetricMatrix', require( '@stdlib/assert/is-symmetric-matrix' ) ); + +/** +* @name isSyntaxError +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-syntax-error} +*/ +setReadOnly( ns, 'isSyntaxError', require( '@stdlib/assert/is-syntax-error' ) ); + +/** +* @name isTriangularNumber +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-triangular-number} +*/ +setReadOnly( ns, 'isTriangularNumber', require( '@stdlib/assert/is-triangular-number' ) ); + +/** +* @name isTruthy +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-truthy} +*/ +setReadOnly( ns, 'isTruthy', require( '@stdlib/assert/is-truthy' ) ); + +/** +* @name isTruthyArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-truthy-array} +*/ +setReadOnly( ns, 'isTruthyArray', require( '@stdlib/assert/is-truthy-array' ) ); + +/** +* @name isTypeError +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-type-error} +*/ +setReadOnly( ns, 'isTypeError', require( '@stdlib/assert/is-type-error' ) ); + +/** +* @name isTypedArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-typed-array} +*/ +setReadOnly( ns, 'isTypedArray', require( '@stdlib/assert/is-typed-array' ) ); + +/** +* @name isTypedArrayLength +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-typed-array-length} +*/ +setReadOnly( ns, 'isTypedArrayLength', require( '@stdlib/assert/is-typed-array-length' ) ); + +/** +* @name isTypedArrayLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-typed-array-like} +*/ +setReadOnly( ns, 'isTypedArrayLike', require( '@stdlib/assert/is-typed-array-like' ) ); + +/** +* @name isUint8Array +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-uint8array} +*/ +setReadOnly( ns, 'isUint8Array', require( '@stdlib/assert/is-uint8array' ) ); + +/** +* @name isUint8ClampedArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-uint8clampedarray} +*/ +setReadOnly( ns, 'isUint8ClampedArray', require( '@stdlib/assert/is-uint8clampedarray' ) ); + +/** +* @name isUint16Array +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-uint16array} +*/ +setReadOnly( ns, 'isUint16Array', require( '@stdlib/assert/is-uint16array' ) ); + +/** +* @name isUint32Array +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-uint32array} +*/ +setReadOnly( ns, 'isUint32Array', require( '@stdlib/assert/is-uint32array' ) ); + +/** +* @name isUNCPath +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-unc-path} +*/ +setReadOnly( ns, 'isUNCPath', require( '@stdlib/assert/is-unc-path' ) ); + +/** +* @name isUndefined +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-undefined} +*/ +setReadOnly( ns, 'isUndefined', require( '@stdlib/assert/is-undefined' ) ); + +/** +* @name isUndefinedOrNull +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-undefined-or-null} +*/ +setReadOnly( ns, 'isUndefinedOrNull', require( '@stdlib/assert/is-undefined-or-null' ) ); + +/** +* @name isUnityProbabilityArray +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-unity-probability-array} +*/ +setReadOnly( ns, 'isUnityProbabilityArray', require( '@stdlib/assert/is-unity-probability-array' ) ); + +/** +* @name isUppercase +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-uppercase} +*/ +setReadOnly( ns, 'isUppercase', require( '@stdlib/assert/is-uppercase' ) ); + +/** +* @name isURI +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-uri} +*/ +setReadOnly( ns, 'isURI', require( '@stdlib/assert/is-uri' ) ); + +/** +* @name isURIError +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-uri-error} +*/ +setReadOnly( ns, 'isURIError', require( '@stdlib/assert/is-uri-error' ) ); + +/** +* @name isVectorLike +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-vector-like} +*/ +setReadOnly( ns, 'isVectorLike', require( '@stdlib/assert/is-vector-like' ) ); + +/** +* @name IS_WEB_WORKER +* @memberof ns +* @readonly +* @type {boolean} +* @see {@link module:@stdlib/assert/is-web-worker} +*/ +setReadOnly( ns, 'IS_WEB_WORKER', require( '@stdlib/assert/is-web-worker' ) ); + +/** +* @name isWhitespace +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-whitespace} +*/ +setReadOnly( ns, 'isWhitespace', require( '@stdlib/assert/is-whitespace' ) ); + +/** +* @name IS_WINDOWS +* @memberof ns +* @readonly +* @type {boolean} +* @see {@link module:@stdlib/assert/is-windows} +*/ +setReadOnly( ns, 'IS_WINDOWS', require( '@stdlib/assert/is-windows' ) ); + +/** +* @name isWritableProperty +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-writable-property} +*/ +setReadOnly( ns, 'isWritableProperty', require( '@stdlib/assert/is-writable-property' ) ); + +/** +* @name isWritablePropertyIn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-writable-property-in} +*/ +setReadOnly( ns, 'isWritablePropertyIn', require( '@stdlib/assert/is-writable-property-in' ) ); + +/** +* @name isWriteOnlyProperty +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-write-only-property} +*/ +setReadOnly( ns, 'isWriteOnlyProperty', require( '@stdlib/assert/is-write-only-property' ) ); + +/** +* @name isWriteOnlyPropertyIn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/is-write-only-property-in} +*/ +setReadOnly( ns, 'isWriteOnlyPropertyIn', require( '@stdlib/assert/is-write-only-property-in' ) ); + + +// EXPORTS // + +module.exports = ns; diff --git a/package.json b/package.json new file mode 100644 index 00000000..87d2a36e --- /dev/null +++ b/package.json @@ -0,0 +1,94 @@ +{ + "name": "@stdlib/assert", + "version": "0.0.0", + "description": "Standard assertion utilities.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "lib/index.js", + "directories": { + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/assert.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": { + "@stdlib/array": "^0.0.x", + "@stdlib/complex": "^0.0.x", + "@stdlib/constants": "^0.0.x", + "@stdlib/fs": "^0.0.x", + "@stdlib/math": "^0.0.x", + "@stdlib/ndarray": "^0.0.x", + "@stdlib/number": "^0.0.x", + "@stdlib/os": "^0.0.x", + "@stdlib/process": "^0.0.x", + "@stdlib/regexp": "^0.0.x", + "@stdlib/streams": "^0.0.x", + "@stdlib/string": "^0.0.x", + "@stdlib/symbol": "^0.0.x", + "@stdlib/tools": "^0.0.x", + "@stdlib/types": "^0.0.x", + "@stdlib/utils": "^0.0.x" + }, + "devDependencies": { + "@stdlib/bench": "^0.0.x", + "@stdlib/bigint": "^0.0.x", + "@stdlib/buffer": "^0.0.x", + "@stdlib/datasets": "^0.0.x", + "@stdlib/random": "^0.0.x", + "proxyquire": "^2.0.0", + "tape": "git+https://github.com/kgryte/tape.git#fix/globby" + }, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "expectation", + "expect", + "utilities", + "utility", + "utils", + "util", + "test", + "check", + "validate", + "validation", + "valid", + "is", + "isvalid", + "type" + ] +} diff --git a/test/test.js b/test/test.js new file mode 100644 index 00000000..f735ff12 --- /dev/null +++ b/test/test.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var objectKeys = require( '@stdlib/utils/keys' ); +var ns = require( './../lib' ); + + +// TESTS // + +tape( 'main export is an object', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ns, 'object', 'main export is an object' ); + t.end(); +}); + +tape( 'the exported object contains assertion utilities', function test( t ) { + var keys = objectKeys( ns ); + t.strictEqual( keys.length > 0, true, 'has keys' ); + t.end(); +}); diff --git a/tools/README.md b/tools/README.md new file mode 100644 index 00000000..8aabb967 --- /dev/null +++ b/tools/README.md @@ -0,0 +1,69 @@ + + +# Assert + +> Standard library assertion utility tools. + +
+ +## Usage + +```javascript +var tools = require( '@stdlib/assert/tools' ); +``` + +#### tools + +Standard library assertion utility tools. + +```javascript +var o = tools; +// returns {...} +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var objectKeys = require( '@stdlib/utils/keys' ); +var tools = require( '@stdlib/assert/tools' ); + +console.log( objectKeys( tools ) ); +``` + +
+ + + + + + diff --git a/tools/array-function/README.md b/tools/array-function/README.md new file mode 100644 index 00000000..1e9c3f3b --- /dev/null +++ b/tools/array-function/README.md @@ -0,0 +1,115 @@ + + +# Array Function + +> Return a function which tests if every element in an array passes a test condition. + +
+ +## Usage + +```javascript +var arrayfcn = require( '@stdlib/assert/tools/array-function' ); +``` + + + +#### arrayfcn( predicate ) + +Returns a function which tests if every element in an [`array`][mdn-array] passes a test condition. Given an input [`array`][mdn-array], the function returns `true` if all elements pass the test and `false` otherwise. + +```javascript +var isOdd = require( '@stdlib/assert/is-odd' ); + +var arr1 = [ 1, 3, 5, 7 ]; +var arr2 = [ 3, 5, 8 ]; + +var f = arrayfcn( isOdd ); + +var bool = f( arr1 ); +// returns true + +bool = f( arr2 ); +// returns false +``` + +
+ + + +
+ +## Notes + +- The returned function will return `false` if **not** provided an [`array`][mdn-array]. +- The returned function will return `false` if provided an empty [`array`][mdn-array]. +- A `predicate` function should accept a single argument: an [`array`][mdn-array] element. If the [`array`][mdn-array] element satisfies a test condition, the `predicate` function should return `true`; otherwise, the `predicate` function should return `false`. + +
+ + + +
+ +## Examples + + + +```javascript +var isEven = require( '@stdlib/assert/is-even' ); +var arrayfcn = require( '@stdlib/assert/tools/array-function' ); + +var arr1; +var arr2; +var bool; +var f; +var i; + +arr1 = new Array( 25 ); +for ( i = 0; i < arr1.length; i++ ) { + arr1[ i ] = i; +} + +arr2 = new Array( 25 ); +for ( i = 0; i < arr2.length; i++ ) { + arr2[ i ] = 2 * i; +} + +f = arrayfcn( isEven ); + +bool = f( arr1 ); +// returns false + +bool = f( arr2 ); +// returns true +``` + +
+ + + + + + diff --git a/tools/array-function/benchmark/benchmark.js b/tools/array-function/benchmark/benchmark.js new file mode 100644 index 00000000..7c8bf816 --- /dev/null +++ b/tools/array-function/benchmark/benchmark.js @@ -0,0 +1,115 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var arrayfcn = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Tests if a value is a number. +* +* @private +* @param {*} v - value to test +* @returns {boolean} boolean indicating whether a value is a number +*/ +function isNumber( v ) { + return ( typeof v === 'number' ); +} + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( i ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x[ len-1 ] += 1; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var fcn; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + fcn = arrayfcn( isNumber ); + f = createBenchmark( fcn, len, false ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/tools/array-function/docs/repl.txt b/tools/array-function/docs/repl.txt new file mode 100644 index 00000000..9f360327 --- /dev/null +++ b/tools/array-function/docs/repl.txt @@ -0,0 +1,40 @@ + +{{alias}}( predicate ) + Returns a function which tests if every element in an array passes a test + condition. + + The predicate function should accept a single argument: an array element. If + the array element satisfies a test condition, the function should return + `true`; otherwise, the function should return `false`. + + Given an input array, the returned function returns `true` if all elements + pass the test and `false` otherwise. + + The returned function returns `false` if provided an empty array. + + The returned function returns `false` is not provided an array. + + Parameters + ---------- + predicate: Function + Function to apply. + + Returns + ------- + fcn: Function + Function which tests if every element in an array passes a test + condition. + + Examples + -------- + > var fcn = {{alias}}( {{alias:@stdlib/assert/is-odd}} ); + > var arr = [ 1, 3, 5, 7 ]; + > var bool = fcn( arr ) + true + > arr = [ 3, 5, 8 ]; + > bool = fcn( arr ) + false + + See Also + -------- + diff --git a/tools/array-function/docs/types/index.d.ts b/tools/array-function/docs/types/index.d.ts new file mode 100644 index 00000000..54f99158 --- /dev/null +++ b/tools/array-function/docs/types/index.d.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Predicate function. +* +* @param elem - array element +* @returns test result +*/ +type PredicateFunction = ( elem: any ) => boolean; + +/** +* Returns a function which tests if every element in an array passes a test condition. +* +* ## Notes +* +* - The predicate function should accept a single argument: an array element. If the array element satisfies a test condition, the function should return `true`; otherwise, the function should return `false`. +* - Given an input array, the returned function returns `true` if all elements pass the test and `false` otherwise. +* - The returned function returns `false` if provided an empty array. +* - The returned function returns `false` is not provided an array. +* +* @param predicate - function to apply +* @returns an array function +* +* @example +* var isOdd = require( `@stdlib/assert/is-odd` ); +* +* var arr1 = [ 1, 3, 5, 7 ]; +* var arr2 = [ 3, 5, 8 ]; +* +* var validate = arrayfcn( isOdd ); +* +* var bool = validate( arr1 ); +* // returns true +* +* bool = validate( arr2 ); +* // returns false +*/ +declare function arrayfcn( predicate: PredicateFunction ): Function; + + +// EXPORTS // + +export = arrayfcn; diff --git a/tools/array-function/docs/types/test.ts b/tools/array-function/docs/types/test.ts new file mode 100644 index 00000000..0280644b --- /dev/null +++ b/tools/array-function/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import arrayfcn = require( './index' ); + + +// TESTS // + +// The function returns a function... +{ + arrayfcn( ( x: number ): boolean => x % 2 === 0 ); // $ExpectType Function + arrayfcn( ( x: number ): boolean => x % 2 !== 0 ); // $ExpectType Function +} + +// The compiler throws an error if the function is provided an argument that is not a function... +{ + arrayfcn( true ); // $ExpectError + arrayfcn( false ); // $ExpectError + arrayfcn( 3 ); // $ExpectError + arrayfcn( [] ); // $ExpectError + arrayfcn( {} ); // $ExpectError + arrayfcn( 'abc' ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + arrayfcn(); // $ExpectError + arrayfcn( ( x: number ): number => x, ( x: number ): number => x ); // $ExpectError +} diff --git a/tools/array-function/examples/index.js b/tools/array-function/examples/index.js new file mode 100644 index 00000000..df20183b --- /dev/null +++ b/tools/array-function/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isEven = require( '@stdlib/assert/is-even' ); +var arrayfcn = require( './../lib' ); + +var arr1; +var arr2; +var bool; +var f; +var i; + +arr1 = new Array( 25 ); +for ( i = 0; i < arr1.length; i++ ) { + arr1[ i ] = i; +} + +arr2 = new Array( 25 ); +for ( i = 0; i < arr2.length; i++ ) { + arr2[ i ] = 2 * i; +} + +f = arrayfcn( isEven ); + +bool = f( arr1 ); +console.log( bool ); +// => false + +bool = f( arr2 ); +console.log( bool ); +// => true diff --git a/tools/array-function/lib/arrayfcn.js b/tools/array-function/lib/arrayfcn.js new file mode 100644 index 00000000..c770a826 --- /dev/null +++ b/tools/array-function/lib/arrayfcn.js @@ -0,0 +1,84 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isArray = require( '@stdlib/assert/is-array' ); + + +// MAIN // + +/** +* Returns a function which tests if every element in an array passes a test condition. +* +* @param {Function} predicate - function to apply +* @throws {TypeError} must provide a function +* @returns {Function} an array function +* +* @example +* var isOdd = require( '@stdlib/assert/is-odd' ); +* +* var arr1 = [ 1, 3, 5, 7 ]; +* var arr2 = [ 3, 5, 8 ]; +* +* var validate = arrayfcn( isOdd ); +* +* var bool = validate( arr1 ); +* // returns true +* +* bool = validate( arr2 ); +* // returns false +*/ +function arrayfcn( predicate ) { + if ( typeof predicate !== 'function' ) { + throw new TypeError( 'invalid argument. Must provide a function. Value: `' + predicate + '`.' ); + } + return every; + + /** + * Tests if every element in an array passes a test condition. + * + * @private + * @param {*} value - value to test + * @returns {boolean} boolean indicating whether a value is an array for which all elements pass a test condition + */ + function every( value ) { + var len; + var i; + if ( !isArray( value ) ) { + return false; + } + len = value.length; + if ( len === 0 ) { + return false; + } + for ( i = 0; i < len; i++ ) { + if ( predicate( value[ i ] ) === false ) { + return false; + } + } + return true; + } +} + + +// EXPORTS // + +module.exports = arrayfcn; diff --git a/tools/array-function/lib/index.js b/tools/array-function/lib/index.js new file mode 100644 index 00000000..e6f7f985 --- /dev/null +++ b/tools/array-function/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return a function which tests if every element in an array passes a test condition. +* +* @module @stdlib/assert/tools/array-function +* +* @example +* var isOdd = require( '@stdlib/assert/is-odd' ); +* var arrayfcn = require( '@stdlib/assert/tools/array-function' ); +* +* var arr1 = [ 1, 3, 5, 7 ]; +* var arr2 = [ 3, 5, 8 ]; +* +* var validate = arrayfcn( isOdd ); +* +* var bool = validate( arr1 ); +* // returns true +* +* bool = validate( arr2 ); +* // returns false +*/ + +// MODULES // + +var arrayfcn = require( './arrayfcn.js' ); + + +// EXPORTS // + +module.exports = arrayfcn; diff --git a/tools/array-function/package.json b/tools/array-function/package.json new file mode 100644 index 00000000..661c2ddd --- /dev/null +++ b/tools/array-function/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/assert/tools/array-function", + "version": "0.0.0", + "description": "Return a function which tests if every element in an array passes a test condition.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "tools", + "tool", + "valid", + "validate", + "assert", + "is", + "apply", + "array", + "test", + "check", + "function" + ] +} diff --git a/tools/array-function/test/test.js b/tools/array-function/test/test.js new file mode 100644 index 00000000..c0098860 --- /dev/null +++ b/tools/array-function/test/test.js @@ -0,0 +1,128 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isEven = require( '@stdlib/math/base/assert/is-even' ); +var noop = require( '@stdlib/utils/noop' ); +var arrayfcn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof arrayfcn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a predicate function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws TypeError' ); + } + function badValue( value ) { + return function badValue() { + arrayfcn( value ); + }; + } + t.end(); +}); + +tape( 'the function returns a function', function test( t ) { + var f = arrayfcn( isEven ); + t.strictEqual( typeof f, 'function' ); + + f = arrayfcn( noop ); + t.strictEqual( typeof f, 'function' ); + t.end(); +}); + +tape( 'the function returns a function that tests an array', function test( t ) { + var bool; + var arr; + var f; + + f = arrayfcn( isEven ); + + arr = [ 2, 2, 2, 2 ]; + bool = f( arr ); + + t.strictEqual( bool, true ); + + arr = [ 2, 2, 2, 3 ]; + bool = f( arr ); + + t.strictEqual( bool, false ); + + t.end(); +}); + +tape( 'the function returns a function which returns `false` if not supplied an array', function test( t ) { + var values; + var f; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + function noop() {} + ]; + + f = arrayfcn( isEven ); + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( f( values[ i ] ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns a function which returns `false` if provided an empty array', function test( t ) { + var bool; + var f; + + f = arrayfcn( isEven ); + bool = f( [] ); + + t.strictEqual( bool, false ); + + t.end(); +}); diff --git a/tools/array-like-function/README.md b/tools/array-like-function/README.md new file mode 100644 index 00000000..8c6f8c2c --- /dev/null +++ b/tools/array-like-function/README.md @@ -0,0 +1,115 @@ + + +# Array-Like Function + +> Return a function which tests if every element in an array-like object passes a test condition. + +
+ +## Usage + +```javascript +var arraylikefcn = require( '@stdlib/assert/tools/array-like-function' ); +``` + + + +#### arraylikefcn( predicate ) + +Returns a function which tests if every element in an [array-like object][array-like] passes a test condition. Given an input [array-like object][array-like], the function returns `true` if all elements pass the test and `false` otherwise. + +```javascript +var isOdd = require( '@stdlib/assert/is-odd' ); + +var arr1 = [ 1, 3, 5, 7 ]; +var arr2 = [ 3, 5, 8 ]; + +var f = arraylikefcn( isOdd ); + +var bool = f( arr1 ); +// returns true + +bool = f( arr2 ); +// returns false +``` + +
+ + + +
+ +## Notes + +- The returned function will return `false` if **not** provided an [array-like object][array-like]. +- The returned function will return `false` if provided an empty [array-like object][array-like]. +- A `predicate` function should accept a single argument: an element from an [array-like object][array-like]. If the element satisfies a test condition, the `predicate` function should return `true`; otherwise, the `predicate` function should return `false`. + +
+ + + +
+ +## Examples + + + +```javascript +var isEven = require( '@stdlib/assert/is-even' ); +var arraylikefcn = require( '@stdlib/assert/tools/array-like-function' ); + +var arr1; +var arr2; +var bool; +var f; +var i; + +arr1 = new Array( 25 ); +for ( i = 0; i < arr1.length; i++ ) { + arr1[ i ] = i; +} + +arr2 = new Array( 25 ); +for ( i = 0; i < arr2.length; i++ ) { + arr2[ i ] = 2 * i; +} + +f = arraylikefcn( isEven ); + +bool = f( arr1 ); +// returns false + +bool = f( arr2 ); +// returns true +``` + +
+ + + + + + diff --git a/tools/array-like-function/benchmark/benchmark.js b/tools/array-like-function/benchmark/benchmark.js new file mode 100644 index 00000000..6e54127c --- /dev/null +++ b/tools/array-like-function/benchmark/benchmark.js @@ -0,0 +1,115 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var arraylikefcn = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Tests if a value is a number. +* +* @private +* @param {*} v - value to test +* @returns {boolean} boolean indicating whether a value is a number +*/ +function isNumber( v ) { + return ( typeof v === 'number' ); +} + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len ) { + var x; + var i; + + x = []; + for ( i = 0; i < len; i++ ) { + x.push( i ); + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x[ len-1 ] += 1; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var fcn; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + fcn = arraylikefcn( isNumber ); + f = createBenchmark( fcn, len, false ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/tools/array-like-function/docs/repl.txt b/tools/array-like-function/docs/repl.txt new file mode 100644 index 00000000..3cc500ee --- /dev/null +++ b/tools/array-like-function/docs/repl.txt @@ -0,0 +1,41 @@ + +{{alias}}( predicate ) + Returns a function which tests if every element in an array-like object + passes a test condition. + + The predicate function should accept a single argument: an element from an + array-like object. If the element satisfies a test condition, the function + should return `true`; otherwise, the function should return `false`. + + Given an input array-like object, the returned function returns `true` if + all elements pass the test and `false` otherwise. + + The returned function returns `false` if provided an empty array-like + object. + + The returned function returns `false` is not provided an array-like object. + + Parameters + ---------- + predicate: Function + Function to apply. + + Returns + ------- + fcn: Function + Function which tests if every element in an array-like object passes a + test condition. + + Examples + -------- + > var fcn = {{alias}}( {{alias:@stdlib/assert/is-odd}} ); + > var arr = [ 1, 3, 5, 7 ]; + > var bool = fcn( arr ) + true + > arr = [ 3, 5, 8 ]; + > bool = fcn( arr ) + false + + See Also + -------- + diff --git a/tools/array-like-function/docs/types/index.d.ts b/tools/array-like-function/docs/types/index.d.ts new file mode 100644 index 00000000..4c13e4a4 --- /dev/null +++ b/tools/array-like-function/docs/types/index.d.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Predicate function. +* +* @param elem - array element +* @returns test result +*/ +type PredicateFunction = ( elem: any ) => boolean; + +/** +* Returns a function which tests if every element in an array-like object passes a test condition. +* +* ## Notes +* +* - The predicate function should accept a single argument: an element from an array-like object. If the element satisfies a test condition, the function should return `true`; otherwise, the function should return `false`. +* - Given an input array-like object, the returned function returns `true` if all elements pass the test and `false` otherwise. +* - The returned function returns `false` if provided an empty array-like object. +* - The returned function returns `false` is not provided an array-like object. +* +* @param predicate - function to apply +* @returns an array-like object function +* +* @example +* var isOdd = require( `@stdlib/assert/is-odd` ); +* +* var arr1 = [ 1, 3, 5, 7 ]; +* var arr2 = [ 3, 5, 8 ]; +* +* var validate = arraylikefcn( isOdd ); +* +* var bool = validate( arr1 ); +* // returns true +* +* bool = validate( arr2 ); +* // returns false +*/ +declare function arraylikefcn( predicate: PredicateFunction ): Function; + + +// EXPORTS // + +export = arraylikefcn; diff --git a/tools/array-like-function/docs/types/test.ts b/tools/array-like-function/docs/types/test.ts new file mode 100644 index 00000000..0dfe09c3 --- /dev/null +++ b/tools/array-like-function/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import arraylikefcn = require( './index' ); + + +// TESTS // + +// The function returns a function... +{ + arraylikefcn( ( x: number ): boolean => x % 2 === 0 ); // $ExpectType Function + arraylikefcn( ( x: number ): boolean => x % 2 !== 0 ); // $ExpectType Function +} + +// The compiler throws an error if the function is provided an argument that is not a function... +{ + arraylikefcn( true ); // $ExpectError + arraylikefcn( false ); // $ExpectError + arraylikefcn( 3 ); // $ExpectError + arraylikefcn( [] ); // $ExpectError + arraylikefcn( {} ); // $ExpectError + arraylikefcn( 'abc' ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + arraylikefcn(); // $ExpectError + arraylikefcn( ( x: number ): number => x, ( x: number ): number => x ); // $ExpectError +} diff --git a/tools/array-like-function/examples/index.js b/tools/array-like-function/examples/index.js new file mode 100644 index 00000000..b3bc2d89 --- /dev/null +++ b/tools/array-like-function/examples/index.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isEven = require( '@stdlib/assert/is-even' ); +var arraylikefcn = require( './../lib' ); + +var arr1; +var arr2; +var bool; +var f; +var i; + +arr1 = new Array( 25 ); +for ( i = 0; i < arr1.length; i++ ) { + arr1[ i ] = i; +} + +arr2 = new Array( 25 ); +for ( i = 0; i < arr2.length; i++ ) { + arr2[ i ] = 2 * i; +} + +f = arraylikefcn( isEven ); + +bool = f( arr1 ); +console.log( bool ); +// => false + +bool = f( arr2 ); +console.log( bool ); +// => true diff --git a/tools/array-like-function/lib/arraylikefcn.js b/tools/array-like-function/lib/arraylikefcn.js new file mode 100644 index 00000000..3234c409 --- /dev/null +++ b/tools/array-like-function/lib/arraylikefcn.js @@ -0,0 +1,84 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isArrayLike = require( '@stdlib/assert/is-array-like' ); + + +// MAIN // + +/** +* Returns a function which tests if every element in an array-like object passes a test condition. +* +* @param {Function} predicate - function to apply +* @throws {TypeError} must provide a function +* @returns {Function} an array-like object function +* +* @example +* var isOdd = require( '@stdlib/assert/is-odd' ); +* +* var arr1 = [ 1, 3, 5, 7 ]; +* var arr2 = [ 3, 5, 8 ]; +* +* var validate = arraylikefcn( isOdd ); +* +* var bool = validate( arr1 ); +* // returns true +* +* bool = validate( arr2 ); +* // returns false +*/ +function arraylikefcn( predicate ) { + if ( typeof predicate !== 'function' ) { + throw new TypeError( 'invalid argument. Must provide a function. Value: `' + predicate + '`.' ); + } + return every; + + /** + * Tests if every element in an array-like object passes a test condition. + * + * @private + * @param {*} value - value to test + * @returns {boolean} boolean indicating whether a value is an array-like object for which all elements pass a test condition + */ + function every( value ) { + var len; + var i; + if ( !isArrayLike( value ) ) { + return false; + } + len = value.length; + if ( len === 0 ) { + return false; + } + for ( i = 0; i < len; i++ ) { + if ( predicate( value[ i ] ) === false ) { + return false; + } + } + return true; + } +} + + +// EXPORTS // + +module.exports = arraylikefcn; diff --git a/tools/array-like-function/lib/index.js b/tools/array-like-function/lib/index.js new file mode 100644 index 00000000..2108e35e --- /dev/null +++ b/tools/array-like-function/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return a function which tests if every element in an array-like object passes a test condition. +* +* @module @stdlib/assert/tools/array-like-function +* +* @example +* var isOdd = require( '@stdlib/assert/is-odd' ); +* var arraylikefcn = require( '@stdlib/assert/tools/array-like-function' ); +* +* var arr1 = [ 1, 3, 5, 7 ]; +* var arr2 = [ 3, 5, 8 ]; +* +* var validate = arraylikefcn( isOdd ); +* +* var bool = validate( arr1 ); +* // returns true +* +* bool = validate( arr2 ); +* // returns false +*/ + +// MODULES // + +var arraylikefcn = require( './arraylikefcn.js' ); + + +// EXPORTS // + +module.exports = arraylikefcn; diff --git a/tools/array-like-function/package.json b/tools/array-like-function/package.json new file mode 100644 index 00000000..b42b2a44 --- /dev/null +++ b/tools/array-like-function/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/assert/tools/array-like-function", + "version": "0.0.0", + "description": "Return a function which tests if every element in an array-like object passes a test condition.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "tools", + "tool", + "valid", + "validate", + "assert", + "is", + "apply", + "array", + "array-like", + "typed array", + "object", + "test", + "check", + "function" + ] +} diff --git a/tools/array-like-function/test/test.js b/tools/array-like-function/test/test.js new file mode 100644 index 00000000..94b4b596 --- /dev/null +++ b/tools/array-like-function/test/test.js @@ -0,0 +1,143 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isEven = require( '@stdlib/math/base/assert/is-even' ); +var noop = require( '@stdlib/utils/noop' ); +var arraylikefcn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof arraylikefcn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a predicate function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws TypeError' ); + } + function badValue( value ) { + return function badValue() { + arraylikefcn( value ); + }; + } + t.end(); +}); + +tape( 'the function returns a function', function test( t ) { + var f = arraylikefcn( isEven ); + t.strictEqual( typeof f, 'function' ); + + f = arraylikefcn( noop ); + t.strictEqual( typeof f, 'function' ); + t.end(); +}); + +tape( 'the function returns a function that tests an array-like object', function test( t ) { + var bool; + var arr; + var f; + + f = arraylikefcn( isEven ); + + arr = [ 2, 2, 2, 2 ]; + bool = f( arr ); + + t.strictEqual( bool, true ); + + arr = [ 2, 2, 2, 3 ]; + bool = f( arr ); + + t.strictEqual( bool, false ); + + arr = { + 'length': 1, + '0': 2 + }; + bool = f( arr ); + + t.strictEqual( bool, true ); + + t.end(); +}); + +tape( 'the function returns a function which returns `false` if not supplied an array-like object', function test( t ) { + var values; + var f; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {} + ]; + + f = arraylikefcn( isEven ); + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( f( values[ i ] ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns a function which returns `false` if provided an empty array-like object', function test( t ) { + var bool; + var o; + var f; + + f = arraylikefcn( isEven ); + bool = f( [] ); + + t.strictEqual( bool, false ); + + o = { + 'length': 0 + }; + bool = f( o ); + + t.strictEqual( bool, false ); + + t.end(); +}); diff --git a/tools/docs/types/index.d.ts b/tools/docs/types/index.d.ts new file mode 100644 index 00000000..35844a60 --- /dev/null +++ b/tools/docs/types/index.d.ts @@ -0,0 +1,128 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/* tslint:disable:max-line-length */ +/* tslint:disable:max-file-line-count */ + +import arrayfcn = require( '@stdlib/assert/tools/array-function' ); +import arraylikefcn = require( '@stdlib/assert/tools/array-like-function' ); +import typedarrayfcn = require( '@stdlib/assert/tools/typed-array-function' ); + +/** +* Interface describing the `tools` namespace. +*/ +interface Namespace { + /** + * Returns a function which tests if every element in an array passes a test condition. + * + * ## Notes + * + * - The predicate function should accept a single argument: an array element. If the array element satisfies a test condition, the function should return `true`; otherwise, the function should return `false`. + * - Given an input array, the returned function returns `true` if all elements pass the test and `false` otherwise. + * - The returned function returns `false` if provided an empty array. + * - The returned function returns `false` is not provided an array. + * + * @param predicate - function to apply + * @returns an array function + * + * @example + * var isOdd = require( `@stdlib/assert/is-odd` ); + * + * var arr1 = [ 1, 3, 5, 7 ]; + * var arr2 = [ 3, 5, 8 ]; + * + * var validate = ns.arrayfcn( isOdd ); + * + * var bool = validate( arr1 ); + * // returns true + * + * bool = validate( arr2 ); + * // returns false + */ + arrayfcn: typeof arrayfcn; + + /** + * Returns a function which tests if every element in an array-like object passes a test condition. + * + * ## Notes + * + * - The predicate function should accept a single argument: an element from an array-like object. If the element satisfies a test condition, the function should return `true`; otherwise, the function should return `false`. + * - Given an input array-like object, the returned function returns `true` if all elements pass the test and `false` otherwise. + * - The returned function returns `false` if provided an empty array-like object. + * - The returned function returns `false` is not provided an array-like object. + * + * @param predicate - function to apply + * @returns an array-like object function + * + * @example + * var isOdd = require( `@stdlib/assert/is-odd` ); + * + * var arr1 = [ 1, 3, 5, 7 ]; + * var arr2 = [ 3, 5, 8 ]; + * + * var validate = ns.arraylikefcn( isOdd ); + * + * var bool = validate( arr1 ); + * // returns true + * + * bool = validate( arr2 ); + * // returns false + */ + arraylikefcn: typeof arraylikefcn; + + /** + * Returns a function which tests if every element in a typed array passes a test condition. + * + * ## Notes + * + * - The predicate function should accept a single argument: a typed array element. If the element satisfies a test condition, the function should return `true`; otherwise, the function should return `false`. + * - Given an input typed array, the returned function returns `true` if all elements pass the test and `false` otherwise. + * - The returned function returns `false` if provided an empty typed array. + * - The returned function returns `false` is not provided a typed array. + * + * @param predicate - function to apply + * @returns a typed array function + * + * @example + * var isOdd = require( `@stdlib/assert/is-odd` ); + * + * var arr1 = new Int32Array( [ 1, 3, 5, 7 ] ); + * var arr2 = new Int32Array( [ 1, 3, 5, 8 ] ); + * + * var validate = ns.typedarrayfcn( isOdd ); + * + * var bool = validate( arr1 ); + * // returns true + * + * bool = validate( arr2 ); + * // returns false + */ + typedarrayfcn: typeof typedarrayfcn; +} + +/** +* Standard library assertion utility tools. +*/ +declare var ns: Namespace; + + +// EXPORTS // + +export = ns; diff --git a/tools/docs/types/test.ts b/tools/docs/types/test.ts new file mode 100644 index 00000000..e9b94b1e --- /dev/null +++ b/tools/docs/types/test.ts @@ -0,0 +1,29 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* tslint:disable:no-unused-expression */ + +import tools = require( './index' ); + + +// TESTS // + +// The exported value is the expected interface... +{ + tools; // $ExpectType Namespace +} diff --git a/tools/lib/index.js b/tools/lib/index.js new file mode 100644 index 00000000..e68f2f95 --- /dev/null +++ b/tools/lib/index.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/* +* When adding modules to the namespace, ensure that they are added in alphabetical order according to module name. +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-read-only-property' ); + + +// MAIN // + +/** +* Top-level namespace. +* +* @namespace ns +*/ +var ns = {}; + +/** +* @name arrayfcn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/tools/array-function} +*/ +setReadOnly( ns, 'arrayfcn', require( '@stdlib/assert/tools/array-function' ) ); + +/** +* @name arraylikefcn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/tools/array-like-function} +*/ +setReadOnly( ns, 'arraylikefcn', require( '@stdlib/assert/tools/array-like-function' ) ); + +/** +* @name typedarrayfcn +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/assert/tools/typed-array-function} +*/ +setReadOnly( ns, 'typedarrayfcn', require( '@stdlib/assert/tools/typed-array-function' ) ); + + +// EXPORTS // + +module.exports = ns; diff --git a/tools/package.json b/tools/package.json new file mode 100644 index 00000000..d6458f89 --- /dev/null +++ b/tools/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/assert/tools", + "version": "0.0.0", + "description": "Standard library assertion utility tools.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "assertion", + "assert", + "expectation", + "expect", + "utilities", + "utility", + "utils", + "util", + "test", + "check", + "validate", + "validation", + "valid", + "is", + "isvalid", + "type", + "tool", + "tools" + ] +} diff --git a/tools/test/test.js b/tools/test/test.js new file mode 100644 index 00000000..aee67294 --- /dev/null +++ b/tools/test/test.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var objectKeys = require( '@stdlib/utils/keys' ); +var ns = require( './../lib' ); + + +// TESTS // + +tape( 'main export is an object', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof ns, 'object', 'main export is an object' ); + t.end(); +}); + +tape( 'the exported object contains assertion utility tools', function test( t ) { + var keys = objectKeys( ns ); + t.equal( keys.length > 0, true, 'has keys' ); + t.end(); +}); diff --git a/tools/typed-array-function/README.md b/tools/typed-array-function/README.md new file mode 100644 index 00000000..27e42fbb --- /dev/null +++ b/tools/typed-array-function/README.md @@ -0,0 +1,117 @@ + + +# Typed Array Function + +> Return a function which tests if every element in a [typed array][mdn-typed-array] passes a test condition. + +
+ +## Usage + +```javascript +var typedarrayfcn = require( '@stdlib/assert/tools/typed-array-function' ); +``` + + + +#### typedarrayfcn( predicate ) + +Returns a function which tests if every element in a [`typed array`][mdn-typed-array] passes a test condition. Given an input [`typed array`][mdn-typed-array], the function returns `true` if all elements pass the test and `false` otherwise. + +```javascript +var isOdd = require( '@stdlib/assert/is-odd' ); +var Int32Array = require( '@stdlib/array/int32' ); + +var arr1 = new Int32Array( [ 1, 3, 5, 7 ] ); +var arr2 = new Int32Array( [ 3, 5, 8 ] ); + +var f = typedarrayfcn( isOdd ); + +var bool = f( arr1 ); +// returns true + +bool = f( arr2 ); +// returns false +``` + +
+ + + +
+ +## Notes + +- The returned function will return `false` if **not** provided a [`typed array`][mdn-typed-array]. +- The returned function will return `false` if provided an empty [`typed array`][mdn-typed-array]. +- A `predicate` function should accept a single argument: a [`typed array`][mdn-typed-array] element. If the element satisfies a test condition, the `predicate` function should return `true`; otherwise, the `predicate` function should return `false`. + +
+ + + +
+ +## Examples + + + +```javascript +var isEven = require( '@stdlib/assert/is-even' ); +var Int32Array = require( '@stdlib/array/int32' ); +var typedarrayfcn = require( '@stdlib/assert/tools/typed-array-function' ); + +var arr1; +var arr2; +var bool; +var f; +var i; + +arr1 = new Int32Array( 25 ); +for ( i = 0; i < arr1.length; i++ ) { + arr1[ i ] = i; +} + +arr2 = new Int32Array( 25 ); +for ( i = 0; i < arr2.length; i++ ) { + arr2[ i ] = 2 * i; +} + +f = typedarrayfcn( isEven ); + +bool = f( arr1 ); +// returns false + +bool = f( arr2 ); +// returns true +``` + +
+ + + + + + diff --git a/tools/typed-array-function/benchmark/benchmark.js b/tools/typed-array-function/benchmark/benchmark.js new file mode 100644 index 00000000..f3d2b039 --- /dev/null +++ b/tools/typed-array-function/benchmark/benchmark.js @@ -0,0 +1,116 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Int32Array = require( '@stdlib/array/int32' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var pow = require( '@stdlib/math/base/special/pow' ); +var pkg = require( './../package.json' ).name; +var typedarrayfcn = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Tests if a value is a number. +* +* @private +* @param {*} v - value to test +* @returns {boolean} boolean indicating whether a value is a number +*/ +function isNumber( v ) { + return ( typeof v === 'number' ); +} + +/** +* Creates a benchmark function. +* +* @private +* @param {Function} fcn - function to benchmark +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( fcn, len ) { + var x; + var i; + + x = new Int32Array( len ); + for ( i = 0; i < len; i++ ) { + x[ i ] = i; + } + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var bool; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x[ len-1 ] += 1; + bool = fcn( x ); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + } + b.toc(); + if ( !isBoolean( bool ) ) { + b.fail( 'should return a boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var fcn; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + fcn = typedarrayfcn( isNumber ); + f = createBenchmark( fcn, len, false ); + bench( pkg+':len='+len, f ); + } +} + +main(); diff --git a/tools/typed-array-function/docs/repl.txt b/tools/typed-array-function/docs/repl.txt new file mode 100644 index 00000000..6ff0c73a --- /dev/null +++ b/tools/typed-array-function/docs/repl.txt @@ -0,0 +1,40 @@ + +{{alias}}( predicate ) + Returns a function which tests if every element in a typed array passes a + test condition. + + The predicate function should accept a single argument: a typed array + element. If the element satisfies a test condition, the function should + return `true`; otherwise, the function should return `false`. + + Given an input typed array, the returned function returns `true` if all + elements pass the test and `false` otherwise. + + The returned function returns `false` if provided an empty typed array. + + The returned function returns `false` is not provided a typed array. + + Parameters + ---------- + predicate: Function + Function to apply. + + Returns + ------- + fcn: Function + Function which tests if every element in a typed array passes a test + condition. + + Examples + -------- + > var fcn = {{alias}}( {{alias:@stdlib/assert/is-odd}} ); + > var arr = new {{alias:@stdlib/array/int32}}( [ 1, 3, 5, 7 ] ); + > var bool = fcn( arr ) + true + > arr = new {{alias:@stdlib/array/int32}}( [ 1, 3, 5, 8 ] ); + > bool = fcn( arr ) + false + + See Also + -------- + diff --git a/tools/typed-array-function/docs/types/index.d.ts b/tools/typed-array-function/docs/types/index.d.ts new file mode 100644 index 00000000..c3cee8ce --- /dev/null +++ b/tools/typed-array-function/docs/types/index.d.ts @@ -0,0 +1,61 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Predicate function. +* +* @param elem - array element +* @returns test result +*/ +type PredicateFunction = ( elem: any ) => boolean; + +/** +* Returns a function which tests if every element in a typed array passes a test condition. +* +* ## Notes +* +* - The predicate function should accept a single argument: a typed array element. If the element satisfies a test condition, the function should return `true`; otherwise, the function should return `false`. +* - Given an input typed array, the returned function returns `true` if all elements pass the test and `false` otherwise. +* - The returned function returns `false` if provided an empty typed array. +* - The returned function returns `false` is not provided a typed array. +* +* @param predicate - function to apply +* @returns a typed array function +* +* @example +* var isOdd = require( `@stdlib/assert/is-odd` ); +* +* var arr1 = new Int32Array( [ 1, 3, 5, 7 ] ); +* var arr2 = new Int32Array( [ 1, 3, 5, 8 ] ); +* +* var validate = typedarrayfcn( isOdd ); +* +* var bool = validate( arr1 ); +* // returns true +* +* bool = validate( arr2 ); +* // returns false +*/ +declare function typedarrayfcn( predicate: PredicateFunction ): Function; + + +// EXPORTS // + +export = typedarrayfcn; diff --git a/tools/typed-array-function/docs/types/test.ts b/tools/typed-array-function/docs/types/test.ts new file mode 100644 index 00000000..63108b16 --- /dev/null +++ b/tools/typed-array-function/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import typedarrayfcn = require( './index' ); + + +// TESTS // + +// The function returns a function... +{ + typedarrayfcn( ( x: number ): boolean => x % 2 === 0 ); // $ExpectType Function + typedarrayfcn( ( x: number ): boolean => x % 2 !== 0 ); // $ExpectType Function +} + +// The compiler throws an error if the function is provided an argument that is not a function... +{ + typedarrayfcn( true ); // $ExpectError + typedarrayfcn( false ); // $ExpectError + typedarrayfcn( 3 ); // $ExpectError + typedarrayfcn( [] ); // $ExpectError + typedarrayfcn( {} ); // $ExpectError + typedarrayfcn( 'abc' ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + typedarrayfcn(); // $ExpectError + typedarrayfcn( ( x: number ): number => x, ( x: number ): number => x ); // $ExpectError +} diff --git a/tools/typed-array-function/examples/index.js b/tools/typed-array-function/examples/index.js new file mode 100644 index 00000000..b7258f41 --- /dev/null +++ b/tools/typed-array-function/examples/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var isEven = require( '@stdlib/math/base/assert/is-even' ); +var Int32Array = require( '@stdlib/array/int32' ); +var typedarrayfcn = require( './../lib' ); + +var arr1; +var arr2; +var bool; +var f; +var i; + +arr1 = new Int32Array( 25 ); +for ( i = 0; i < arr1.length; i++ ) { + arr1[ i ] = i; +} + +arr2 = new Int32Array( 25 ); +for ( i = 0; i < arr2.length; i++ ) { + arr2[ i ] = 2 * i; +} + +f = typedarrayfcn( isEven ); + +bool = f( arr1 ); +console.log( bool ); +// => false + +bool = f( arr2 ); +console.log( bool ); +// => true diff --git a/tools/typed-array-function/lib/index.js b/tools/typed-array-function/lib/index.js new file mode 100644 index 00000000..4a7945c4 --- /dev/null +++ b/tools/typed-array-function/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return a function which tests if every element in a typed array passes a test condition. +* +* @module @stdlib/assert/tools/typed-array-function +* +* @example +* var isOdd = require( '@stdlib/math/base/assert/is-odd' ); +* var typedarrayfcn = require( '@stdlib/assert/tools/typed-array-function' ); +* +* var arr1 = new Int32Array( [ 1, 3, 5, 7 ] ); +* var arr2 = new Int32Array( [ 3, 5, 6, 8 ] ); +* +* var f = typedarrayfcn( isOdd ); +* +* var bool = f( arr1 ); +* // returns true +* +* bool = f( arr2 ); +* // returns false +*/ + +// MODULES // + +var typedarrayfcn = require( './typedarrayfcn.js' ); + + +// EXPORTS // + +module.exports = typedarrayfcn; diff --git a/tools/typed-array-function/lib/typedarrayfcn.js b/tools/typed-array-function/lib/typedarrayfcn.js new file mode 100644 index 00000000..deaafabc --- /dev/null +++ b/tools/typed-array-function/lib/typedarrayfcn.js @@ -0,0 +1,84 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isTypedArray = require( '@stdlib/assert/is-typed-array' ); + + +// MAIN // + +/** +* Returns a function which tests if every element in a typed array passes a test condition. +* +* @param {Function} predicate - function to apply +* @throws {TypeError} must provide a function +* @returns {Function} a typed array function +* +* @example +* var isOdd = require( '@stdlib/assert/is-odd' ); +* +* var arr1 = new Int32Array( [ 1, 3, 5, 7 ] ); +* var arr2 = new Int32Array( [ 1, 3, 5, 8 ] ); +* +* var validate = typedarrayfcn( isOdd ); +* +* var bool = validate( arr1 ); +* // returns true +* +* bool = validate( arr2 ); +* // returns false +*/ +function typedarrayfcn( predicate ) { + if ( typeof predicate !== 'function' ) { + throw new TypeError( 'invalid argument. Must provide a function. Value: `' + predicate + '`.' ); + } + return every; + + /** + * Tests if every element in a typed array passes a test condition. + * + * @private + * @param {*} value - value to test + * @returns {boolean} boolean indicating whether a value is a typed array for which all elements pass a test condition + */ + function every( value ) { + var len; + var i; + if ( !isTypedArray( value ) ) { + return false; + } + len = value.length; + if ( len === 0 ) { + return false; + } + for ( i = 0; i < len; i++ ) { + if ( predicate( value[ i ] ) === false ) { + return false; + } + } + return true; + } +} + + +// EXPORTS // + +module.exports = typedarrayfcn; diff --git a/tools/typed-array-function/package.json b/tools/typed-array-function/package.json new file mode 100644 index 00000000..1fcc670b --- /dev/null +++ b/tools/typed-array-function/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/assert/tools/typed-array-function", + "version": "0.0.0", + "description": "Return a function which tests if every element in a typed array passes a test condition.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdassert", + "tools", + "tool", + "valid", + "validate", + "assert", + "is", + "apply", + "typed array", + "test", + "check", + "function" + ] +} diff --git a/tools/typed-array-function/test/test.js b/tools/typed-array-function/test/test.js new file mode 100644 index 00000000..b7111ae9 --- /dev/null +++ b/tools/typed-array-function/test/test.js @@ -0,0 +1,131 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Int32Array = require( '@stdlib/array/int32' ); +var isEven = require( '@stdlib/math/base/assert/is-even' ); +var noop = require( '@stdlib/utils/noop' ); +var typedarrayfcn = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof typedarrayfcn, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a predicate function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws TypeError' ); + } + function badValue( value ) { + return function badValue() { + typedarrayfcn( value ); + }; + } + t.end(); +}); + +tape( 'the function returns a function', function test( t ) { + var f = typedarrayfcn( isEven ); + t.strictEqual( typeof f, 'function' ); + + f = typedarrayfcn( noop ); + t.strictEqual( typeof f, 'function' ); + t.end(); +}); + +tape( 'the function returns a function that tests a typed array', function test( t ) { + var bool; + var arr; + var f; + + f = typedarrayfcn( isEven ); + + arr = new Int32Array( [ 2, 2, 2, 2 ] ); + bool = f( arr ); + + t.strictEqual( bool, true ); + + arr = new Int32Array( [ 2, 2, 2, 3 ] ); + bool = f( arr ); + + t.strictEqual( bool, false ); + + t.end(); +}); + +tape( 'the function returns a function which returns `false` if not supplied a typed array', function test( t ) { + var values; + var f; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + [ 3, 3, 3, 3 ], + function noop() {} + ]; + + f = typedarrayfcn( isEven ); + + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( f( values[ i ] ), false, 'returns false' ); + } + t.end(); +}); + +tape( 'the function returns a function which returns `false` if provided an empty typed array', function test( t ) { + var bool; + var f; + + f = typedarrayfcn( isEven ); + bool = f( new Int32Array() ); + + t.strictEqual( bool, false ); + + t.end(); +});