最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Javascript: Convert string path to a formatted array - Stack Overflow

programmeradmin0浏览0评论

So I am trying to use the github api and convert it to use for dynatree.

I am looking to use javascript. But if it's easier done in Ruby, ClojureScript etc that could work as well.

So Github api returns something that looks like:

 {
 "sha": "a1f28431d5ee1a00361ff7e1e2dc91172d5bbabd",
 "url": "",
"tree": [
{
  "mode": "100644",
  "type": "blob",
  "sha": "b81f704ee24f08f5f3e00675a9912da79274c3bc",
  "path": ".gitignore",
  "size": 114,
  "url":  ""
  },
   {
  "mode": "100644",
  "type": "blob",
  "sha": "c6231c2f161f50bededaaac624a7a96db6179e9a",
  "path": "Makefile.am",
  "size": 40,
  "url": ""
   },
  {
  "mode": "100644",
  "type": "blob",
  "sha": "ca41e5b8fb710b4ae7a2cdb35cdbafb143382dc2",
  "path": "README.md",
  "size": 2194,
  "url": ""
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "dd3b02c5fcf3312ef7a83f4b6370a8b546739bc1",
  "path": "config.h.in",
  "size": 625,
  "url": ""
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "e242df0dc8be1fec7b2da2091ab8161aaee1b7a2",
  "path": "configure.ac",
  "size": 156,
  "url": ""
},
{
  "mode": "100755",
  "type": "blob",
  "sha": "df8eea7e4ce8862105fcd7929b20bdb45488048b",
  "path": "depp",
  "size": 18615,
  "url": ""
},
{
  "mode": "100755",
  "type": "blob",
  "sha": "6781b987bdbcbc23efe6bbe1654a1e3637b9af07",
  "path": "install-sh",
  "size": 13663,
  "url": ""
},
{
  "mode": "100755",
  "type": "blob",
  "sha": "28055d2ae6f2a2c584afcd769d7881e11f62ecd9",
  "path": "missing",
  "size": 11419,
  "url": ""
},
{
  "mode": "040000",
  "type": "tree",
  "sha": "57bb4a62d2de582c74b2e6de71e3024db7905cac",
  "path": "src",
  "url": ""
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "daaed524883bd510502834d472e9be6cff4e3a69",
  "path": "src/Makefile.am",
  "size": 59,
  "url": ""
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "cf3f8f549fe870315eb4417c9f84dec587c8286b",
  "path": "src/c.c",
  "size": 9480,
  "url": ""
}
 ]
}

So basically I want to get all of the paths that are in the array like "Makefile.am", "src" (recognize that is a folder), "src/c.c". This would work for recursive folders as well.

The format that dynatree looks like:

  var obj = [
    { title: 'Lazy node 1', isLazy: true },
    { title: 'Lazy node 2', isLazy: true },
    { title: 'Folder node 3', isFolder: true,
      children: [
        { title: 'node 3.1' },
        { title: 'node 3.2',
          children: [
            { title: 'node 3.2.1' },
            { title: 'node 3.2.2',
              children: [
                { title: 'node 3.2.2.1' }
              ]
            }
          ]
        }
      ]
    }
  ];

OR

  var rootNode = $("#tree").dynatree("getRoot");
  var childNode = rootNode.addChild({
    title: "Programatically addded nodes",
    tooltip: "This folder and all child nodes were added programmatically.",
    isFolder: true
  });
  childNode.addChild({
    title: "Document using a custom icon",
    icon: "customdoc1.gif"
  });

I am not sure which one would be easier to get to. I am thinking I could use some type of hash table with but I am just not sure how to get it working recursively for files like: "src/a/b/c/index.text"

So I am trying to use the github api and convert it to use for dynatree.

I am looking to use javascript. But if it's easier done in Ruby, ClojureScript etc that could work as well.

So Github api returns something that looks like:

 {
 "sha": "a1f28431d5ee1a00361ff7e1e2dc91172d5bbabd",
 "url": "https://api.github./repos/Jonovono/c/git/trees/a1f28431d5ee1a00361ff7e1e2dc91172d5bbabd",
"tree": [
{
  "mode": "100644",
  "type": "blob",
  "sha": "b81f704ee24f08f5f3e00675a9912da79274c3bc",
  "path": ".gitignore",
  "size": 114,
  "url":  "https://api.github./repos/Jonovono/c/git/blobs/b81f704ee24f08f5f3e00675a9912da79274c3bc"
  },
   {
  "mode": "100644",
  "type": "blob",
  "sha": "c6231c2f161f50bededaaac624a7a96db6179e9a",
  "path": "Makefile.am",
  "size": 40,
  "url": "https://api.github./repos/Jonovono/c/git/blobs/c6231c2f161f50bededaaac624a7a96db6179e9a"
   },
  {
  "mode": "100644",
  "type": "blob",
  "sha": "ca41e5b8fb710b4ae7a2cdb35cdbafb143382dc2",
  "path": "README.md",
  "size": 2194,
  "url": "https://api.github./repos/Jonovono/c/git/blobs/ca41e5b8fb710b4ae7a2cdb35cdbafb143382dc2"
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "dd3b02c5fcf3312ef7a83f4b6370a8b546739bc1",
  "path": "config.h.in",
  "size": 625,
  "url": "https://api.github./repos/Jonovono/c/git/blobs/dd3b02c5fcf3312ef7a83f4b6370a8b546739bc1"
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "e242df0dc8be1fec7b2da2091ab8161aaee1b7a2",
  "path": "configure.ac",
  "size": 156,
  "url": "https://api.github./repos/Jonovono/c/git/blobs/e242df0dc8be1fec7b2da2091ab8161aaee1b7a2"
},
{
  "mode": "100755",
  "type": "blob",
  "sha": "df8eea7e4ce8862105fcd7929b20bdb45488048b",
  "path": "depp",
  "size": 18615,
  "url": "https://api.github./repos/Jonovono/c/git/blobs/df8eea7e4ce8862105fcd7929b20bdb45488048b"
},
{
  "mode": "100755",
  "type": "blob",
  "sha": "6781b987bdbcbc23efe6bbe1654a1e3637b9af07",
  "path": "install-sh",
  "size": 13663,
  "url": "https://api.github./repos/Jonovono/c/git/blobs/6781b987bdbcbc23efe6bbe1654a1e3637b9af07"
},
{
  "mode": "100755",
  "type": "blob",
  "sha": "28055d2ae6f2a2c584afcd769d7881e11f62ecd9",
  "path": "missing",
  "size": 11419,
  "url": "https://api.github./repos/Jonovono/c/git/blobs/28055d2ae6f2a2c584afcd769d7881e11f62ecd9"
},
{
  "mode": "040000",
  "type": "tree",
  "sha": "57bb4a62d2de582c74b2e6de71e3024db7905cac",
  "path": "src",
  "url": "https://api.github./repos/Jonovono/c/git/trees/57bb4a62d2de582c74b2e6de71e3024db7905cac"
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "daaed524883bd510502834d472e9be6cff4e3a69",
  "path": "src/Makefile.am",
  "size": 59,
  "url": "https://api.github./repos/Jonovono/c/git/blobs/daaed524883bd510502834d472e9be6cff4e3a69"
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "cf3f8f549fe870315eb4417c9f84dec587c8286b",
  "path": "src/c.c",
  "size": 9480,
  "url": "https://api.github./repos/Jonovono/c/git/blobs/cf3f8f549fe870315eb4417c9f84dec587c8286b"
}
 ]
}

So basically I want to get all of the paths that are in the array like "Makefile.am", "src" (recognize that is a folder), "src/c.c". This would work for recursive folders as well.

The format that dynatree looks like:

  var obj = [
    { title: 'Lazy node 1', isLazy: true },
    { title: 'Lazy node 2', isLazy: true },
    { title: 'Folder node 3', isFolder: true,
      children: [
        { title: 'node 3.1' },
        { title: 'node 3.2',
          children: [
            { title: 'node 3.2.1' },
            { title: 'node 3.2.2',
              children: [
                { title: 'node 3.2.2.1' }
              ]
            }
          ]
        }
      ]
    }
  ];

OR

  var rootNode = $("#tree").dynatree("getRoot");
  var childNode = rootNode.addChild({
    title: "Programatically addded nodes",
    tooltip: "This folder and all child nodes were added programmatically.",
    isFolder: true
  });
  childNode.addChild({
    title: "Document using a custom icon",
    icon: "customdoc1.gif"
  });

I am not sure which one would be easier to get to. I am thinking I could use some type of hash table with but I am just not sure how to get it working recursively for files like: "src/a/b/c/index.text"

Share Improve this question edited May 23, 2013 at 3:08 Jonovono asked May 23, 2013 at 2:59 JonovonoJonovono 3,4255 gold badges46 silver badges65 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Here is a simple solution that takes a string like "/src/hello/world" and adds its paths to an object

files = {};
function parseFolder(path){
    var cur = files;
    path.split("/").slice(1).forEach(function(elem){
        cur[elem] = cur[elem] || {}; 
        cur = cur[elem];
    });
}

Usage would be something like

parseFolder("/src/stuff/hello");
parseFolder("/src/stuff/world");
parseFolder("/bin/stuff/world");

Here is a working jsfiddle

This is not the format Dynatree recognizes but you get the idea, instead of adding a property that is named like a path, you should push an object to an array.

发布评论

评论列表(0)

  1. 暂无评论