- Saved searches
- Use saved searches to filter your results more quickly
- SitePen/js-doc-parse
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- How to Read a Local/Remote JSON File in JavaScript [Examples]
- What is JSON?
- Parsing JSON in JavaScript
- Reading a File at URL from the Browser
- Reading a Local File from the Browser
- Reading a Local File from Node.js
- Reading a Remote file from Node.js
- 8 examples of ‘parse file javascript’ in JavaScript
- All examples are scanned by Snyk Code
- Related snippets
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
An experimental library for parsing JavaScript files and extracting inline documentation.
SitePen/js-doc-parse
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
A library for parsing JavaScript files and extracting inline documentation. Designed primarily for use with the Dojo Toolkit, but extensible enough to work with any application or documentation format.
New BSD License © 2011–2012 Colin Snover http://zetafleet.com. Released under Dojo Foundation CLA.
Why is this library special?
- Works anywhere, not only on Rhino. (n.b. by “anywhere” I mean “node.js”, but there’s only a handful of code that needs to be abstracted for it to work on Rhino and in the browser, too, and I plan on doing just that.)
- It isn’t lazy! js-doc-parse completely parses the actual JavaScript source code to extract API details instead of just running some crappy regular expressions over the source text to pluck out comment blocks.
- Highly extensible, with initially planned support for two code commenting styles (dojodoc and jsdoc).
Wait, highly extensible? Tell me more!
Oh, alright! This documentation parser is designed to allow you, a JavaScript developer, to very easily extend four key areas of operation:
- Environments Your code might run in a variety of environments and exploit natively available functionality of each environment. If the documentation parser doesn’t know about all those delicious native objects, it can’t help you document anything you borrow from them. Pluggable environments let you define all the built-ins of the environment your code runs in so it doesn’t fall over when you try to var slice = [].slice; .
- Call handlers Chances are good that whatever library you’re using has at least one function that retrieves, decorates, or iterates over objects that you need to document. Adding custom call handlers makes it possible for the documentation parser to understand and evaluate these special calls so that the correct properties exist on the correct objects and that any special magic that these functions might represent can be annotated.
- Documentation processors Some people don’t like the standard jsdoc syntax, and that’s cool. If you’ve extended jsdoc with some new tags, or use some completely different format like dojodocs, Natural Docs, or docco – or, after an evening of heavy drinking, created your own custom documentation format from scratch – writing a new documentation processor will enable you to process any format your heart desires. It will not, however, cure your cirrhosis.
- Exporters Once the code has been parsed and the documentation parsed, you’ll need to output the result to a format that you (or some tool other than you) can actually use! Exporters are completely isolated from the documentation processing workflow, so you can pick and choose one (or lots of) exporters for your final output.
dojo – AMD loader & helper library
esprima – ECMAScript parser
marked – Markdown parser
- git clone —recursive https://github.com/csnover/js-doc-parse.git
- Edit config.js as appropriate for your environment. For parsing Dojo Toolkit source, you should only need to set environmentConfig.basePath to the path to your Dojo 1.8 checkout.
- Run ./parse.sh file-or-directory [file-or-directory…] to generate documentation for the files/directories you’ve specified. By default, this documentation will be exported to details.xml ; update the exporter configuration if you want it to go elsewhere.
About
An experimental library for parsing JavaScript files and extracting inline documentation.
How to Read a Local/Remote JSON File in JavaScript [Examples]
This article will show you how to read a JSON file into JavaScript as a JSON object – both local and remote files.
What is JSON?
JSON (JavaScript Object Notation) is a file format that stores objects, and arrays of objects, as human-readable text. It’s become the most popular method of storing and transmitting structured data on the internet.
Thousands of APIs (used for mapping, communication, authentication, and many other purposes) use it as the format for submitting and receiving data. Desktop applications also use it to store user data, and databases have adopted it for storing structured data.
It’s everywhere, so you’ll be getting pretty familiar with it no matter what you’re working on. Here’s how it looks:
Above, a list of pets is defined in JSON syntax. If you’ve been working with JavaScript objects, you’ll recognize that it is very similar to the syntax used to define objects and arrays in JavaScript. You can find out more about the JSON syntax itself here.
Parsing JSON in JavaScript
JSON is just text – it needs to be interpreted and converted into objects to be useful in JavaScript.
All of the below methods will do this and return usable objects and arrays as JavaScript objects.
Reading a File at URL from the Browser
If you are building a website and wish to read a JSON file using JavaScript being executed in the browser, it must be read from a URL – even if it’s stored on the same disk, in the same folder, as the JavaScript file being executed.
The fetch function in JavaScript will read the contents of a file at a given URL and has built-in functionality for parsing the JSON into usable JavaScript objects.
var url = 'https://example.com/test.json'; fetch(url) .then(response => response.json()) .then(json => < console.log(json); // Do stuff with the contents of the JSON file here >);
Above, fetch is used to retrieve the file at the given URL. The first then statement retrieves the JSON parsed version of the response.
The second then statement then contains the parsed JSON file ready for use – in this case, it’s simply logged to the console for inspection.
Reading a Local File from the Browser
The FileReader object, a relatively recent addition to HTML5 and JavaScript, allows you to read files stored locally on your computer directly into JavaScript running in a browser, without the need to first upload it to a server.
Note, the below example relies heavily on JavaScript Promises – so it’s worth getting familiar with them!
Reading a Local File from Node.js
The fs library in Node.js handles all local file read/write operations. Once it’s been used to read a file, the contents can simply be parsed into JSON:
const fs = require('fs'); let fileText = fs.readFileSync('data.json'); let jsonParsed = JSON.parse(fileText); console.log(jsonParsed);
Reading a Remote file from Node.js
The fetch method outlined above is also the best way to accomplish this from a Node.js environment – give it a shot!
8 examples of ‘parse file javascript’ in JavaScript
Every line of ‘parse file javascript’ code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.
All examples are scanned by Snyk Code
81 function parseJavaScriptFiles (fileStructure) 82 _(fileStructure.allFiles) 83 .filter(function (file) 84 return COMPONENT_EXTENSION_REGEX.test(file.path) || STEP_DEFINITION_EXTENSION_REGEX.test(file.path); 85 >) 86 .each(javaScriptParser.parse) 87 .value(); 88 >
126 function evalFileScript(path) 127 return 'var scriptFile = new File(app.path + \'/Presets/Scripts/' + path + '\');' 128 + '$.evalFile(scriptFile, 30000);'; 129 >
39 function parseJS(code) 40 var indirect = eval 41 code = code.trim() 42 if (code) 43 if (code.indexOf("use strict") === 1) 44 var script = document.createElement("script") 45 script.text = code; 46 head.appendChild(script).parentNode.removeChild(script) 47 > else 48 indirect(code) 49 > 50 > 51 >
10 function parse(inputFile, moduleId) 11 var ast = getAst(inputFile); 12 var deps = parseStatic(ast, moduleId); 13 14 if (deps === undefined) 15 deps = parseDynamic(ast); 16 > 17 return deps; 18 >
55 parse(scriptText) 56 let ast = parse(scriptText, 57 sourceType: 'module', 58 // Note that even when this option is enabled, @babel/parser could throw for unrecoverable errors. 59 // errorRecovery: true, //没啥用,碰到let和var对同一变量进行声明时,当场报错!还会中断转换进程 60 plugins: [ 61 "asyncGenerators", 62 "classProperties", 63 "decorators-legacy", //"decorators", 64 "doExpressions", 65 "dynamicImport", 66 "exportExtensions", 67 "flow", 68 "functionBind", 69 "functionSent", 70 "jsx", 71 "objectRestSpread", 72 ] 73 >); 74 // resolve(ast); 75 76 //使用下面的代码,在遇到解构语法(. )时,会报错,改用babel-parser方案 77 // const scriptParsed = babylon.parse(scriptText, 78 // sourceType: 'module', 79 // plugins: [ 80 // // "estree", //这个插件会导致解析的结果发生变化,因此去除,这本来是acron的插件 81 // "jsx", 82 // "flow", 83 // "doExpressions", 84 // "objectRestSpread", 85 // "exportExtensions", 86 // "classProperties", 87 // "decorators", 88 // "asyncGenerators", 89 // "functionBind", 90 // "functionSent", 91 // "throwExpressions", 92 // "templateInvalidEscapes", 93 // ] 94 // >) 95 // resolve(scriptParsed); 96 97 return ast; 98 >
264 parseJavascript(filename, content) 265 const jsContent = flowRemoveTypes(content).toString(); 266 267 const extractedStringsFromScript = jsExtractor.extractStringsFromJavascript(filename, jsContent); 268 269 this.processStrings(extractedStringsFromScript); 270 >
75 function parse(file) 76 file = file || exports.spmrcfile; 77 if (!fs.existsSync(file)) 78 return <>; 79 > 80 var data; 81 if (_cache.hasOwnProperty(file)) 82 data = _cache[file]; 83 > else 84 data = grunt.file.read(file); 85 _cache[file] = data; 86 > 87 var value = <>; 88 var lines = data.split(/\r\n|\r|\n/); 89 var section = null; 90 var match; 91 lines.forEach(function(line) 92 if (regex.comment.test(line)) 93 return; 94 > 95 if (regex.param.test(line)) 96 match = line.match(regex.param); 97 if (section) 98 value[section][match[1]] = match[2]; 99 >else 100 value[match[1]] = match[2]; 101 > 102 > else if (regex.section.test(line)) 103 match = line.match(regex.section); 104 value[match[1]] = <>; 105 section = match[1]; 106 > else if (line.length === 0 && section) 107 section = null; 108 > 109 >); 110 return value; 111 >
37 function js(file) 38 var opts = 39 fromString: true, 40 sourceMapUrl: false 41 >; 42 43 if (file.map) 44 opts.inSourceMap = _.isObject(file.map) ? file.map : JSON.parse(file.map); 45 opts.outSourceMap = file.destFilename + '.map'; 46 > 47 48 var result = uglifyjs.minify(file.content, opts); 49 50 return Promise.resolve(< content: result.code, map: result.map >); 51 >
Related snippets
- create file javascript
- while else javascript
- javascript padleft
- do while javascript
- javascript rename file
- javascript pause
- javascript readfile
- writefile in javascript
- javascript parse text file to array
- edit json file javascript
- parsedecimal javascript
- parse decimal javascript
- clear cache javascript
- javascript save to file
- javascript ucfirst
- javascript array select
- javascript leapyear
- javascript until
- javascript isset
- javascript read file from url
- javascript write json to file
© 2023 Snyk Limited
Registered in England and Wales
Company number: 09677925
Registered address: Highlands House, Basingstoke Road, Spencers Wood, Reading, Berkshire, RG7 1NT.